Adding support for PP output message format (#59)

* To support IoT Edge Asa, the original PP output format is added and selectable using module twin property "Version":"1"

* Removed the SqlCommand message from GA output

* Original repository and version restored

* removed unneeded .vs files

* Added GA Payload

Message is ommitted for now

* Added GA example message

The readme now has an example message from a recorded session.

* GA Message Payload format

Typo fix

* Fixed GA to PP for the new version switch

PP comes before GA. Code was already OK but I now reference the right version.
This commit is contained in:
Sander van de Velde 2018-09-18 09:23:51 +02:00 коммит произвёл Stephen
Родитель 582bf225a8
Коммит 6355e834c9
3 изменённых файлов: 122 добавлений и 27 удалений

Просмотреть файл

@ -30,7 +30,6 @@ Azure IoT Edge is designed to be used with a broad range of operating system pla
- [Windows 10 IoT Core](https://docs.microsoft.com/en-us/azure/iot-edge/how-to-install-iot-core)
- [Linux](https://docs.microsoft.com/en-us/azure/iot-edge/quickstart-linux)
## Build Environment Setup ##
Modbus module is a .NET Core 2.1 application, which is developed and built based on the guidelines in Azure IoT Edge document.
Please follow [this link](https://docs.microsoft.com/en-us/azure/iot-edge/tutorial-csharp-module) to setup the build environment.
@ -56,6 +55,7 @@ Before running the module, proper configuration is required. Here is a sample co
```json
{
"PublishInterval": "2000",
"Version":"2",
"SlaveConfigs": {
"Slave01": {
"SlaveConnection": "192.168.0.1",
@ -112,6 +112,7 @@ Before running the module, proper configuration is required. Here is a sample co
Meaning of each field:
* "PublishInterval" - Interval between each push to IoT Hub in millisecond
* "Version" - Switch between the PP (Public Preview) and the latest Message Payload format. (valid value for PP: "1", all other values will switch to the latest format)
* "SlaveConfigs" - Contains one or more Modbus slaves' configuration. In this sample, we have "Slave01" and "Slave02" two devices:
* "Slave01", "Slave02" - User defined names for each Modbus slave, cannot have duplicates under "SlaveConfigs".
* "SlaveConnection" - Ipv4 address or the serial port name of the Modbus slave.
@ -148,7 +149,7 @@ Message Properties:
```json
"content-type": "application/edge-modbus-json"
```
Message Payload:
Latest Message Payload:
```json
[
{
@ -215,6 +216,32 @@ Message Payload:
}
]
```
PP (Public Preview) Message Payload:
```json
[
{
"DisplayName":"RotaryOne",
"HwId":"Wise4012E",
"Address":"40001",
"Value":"0",
"SourceTimestamp":"2018-09-18 04:14:32"
},
{
"DisplayName":"SwitchOne",
"HwId":"Wise4012E",
"Address":"00001",
"Value":"1",
"SourceTimestamp":"2018-09-18 04:14:33"
},
{
"DisplayName":"RelayOne",
"HwId":"Wise4012E",
"Address":"00017",
"Value":"0",
"SourceTimestamp":"2018-09-18 04:14:33"
}
]
```
#### Route to IoT Hub ####
```json

Просмотреть файл

@ -105,6 +105,43 @@
}
return obj_list;
}
public List<object> CollectAndResetOutMessageFromSessionsV1()
{
List<object> obj_list = new List<object>();
foreach (ModbusSlaveSession session in ModbusSessionList)
{
var obj = session.GetOutMessage();
if (obj != null)
{
var content = (obj as ModbusOutContent);
string hwId = content.HwId;
foreach (var data in content.Data)
{
var sourceTimestamp = data.SourceTimestamp;
foreach (var value in data.Values)
{
obj_list.Add(new ModbusOutMessageV1
{
HwId = hwId,
SourceTimestamp = sourceTimestamp,
Address = value.Address,
DisplayName = value.DisplayName,
Value = value.Value,
});
}
}
session.ClearOutMessage();
}
}
return obj_list;
}
}
/// <summary>
@ -1079,6 +1116,25 @@
}
public int PublishInterval { get; set; }
}
class ModbusVersion
{
public ModbusVersion(string version)
{
Version = version;
}
public string Version { get; set; }
}
class ModbusOutMessageV1
{
public string DisplayName { get; set; }
public string HwId { get; set; }
public string Address { get; set; }
public string Value { get; set; }
public string SourceTimestamp { get; set; }
}
class ModbusOutMessage
{
public string PublishTimestamp { get; set; }

Просмотреть файл

@ -19,10 +19,12 @@ namespace Modbus.Containers
{
const string ModbusSlaves = "SlaveConfigs";
const int DefaultPushInterval = 5000;
const string DefaultVersion = "2";
static int m_counter = 0;
static List<Task> m_task_list = new List<Task>();
static bool m_run = true;
static ModbusPushInterval m_interval = null;
static ModbusVersion m_version = null;
static ModuleConfig m_existingConfig = null;
static object message_lock = new object();
static List<ModbusOutMessage> result = new List<ModbusOutMessage>();
@ -236,12 +238,19 @@ namespace Modbus.Containers
Console.WriteLine("Attempt to load configuration: " + jsonStr);
config = JsonConvert.DeserializeObject<ModuleConfig>(jsonStr);
m_interval = JsonConvert.DeserializeObject<ModbusPushInterval>(jsonStr);
if (m_interval == null)
{
m_interval = new ModbusPushInterval(DefaultPushInterval);
}
m_version = JsonConvert.DeserializeObject<ModbusVersion>(jsonStr);
if (m_version == null)
{
m_version = new ModbusVersion(DefaultVersion);
}
config.Validate();
moduleHandle = await Slaves.ModuleHandle.CreateHandleFromConfiguration(config);
@ -304,40 +313,43 @@ namespace Modbus.Containers
while (m_run)
{
Message message = null;
Message sqliteMessage = null;
List<object> result = moduleHandle.CollectAndResetOutMessageFromSessions();
if (result.Count > 0)
switch (m_version.Version)
{
ModbusOutMessage out_message = new ModbusOutMessage
{
PublishTimestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
Content = result
};
SQLiteCommandMessage sqlite_out_message = new SQLiteCommandMessage
{
RequestId = 0,
RequestModule = "modbus";
DbName = "/app/db/test.db",
Command = "select * from test;"
};
case "1":
List<object> resultV1 = moduleHandle.CollectAndResetOutMessageFromSessionsV1();
message = new Message(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(out_message)));
message.Properties.Add("content-type", "application/edge-modbus-json");
if (resultV1.Count > 0)
{
message = new Message(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(resultV1)));
message.Properties.Add("content-type", "application/edge-modbus-json");
}
sqliteMessage = new Message(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(sqlite_out_message)));
sqliteMessage.Properties.Add("command-type", "SQLiteCmd");
break;
default:
List<object> result = moduleHandle.CollectAndResetOutMessageFromSessions();
if (result.Count > 0)
{
ModbusOutMessage out_message = new ModbusOutMessage
{
PublishTimestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
Content = result
};
message = new Message(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(out_message)));
message.Properties.Add("content-type", "application/edge-modbus-json");
}
break;
}
if (message != null)
{
await ioTHubModuleClient.SendEventAsync("modbusOutput", message);
}
if (sqliteMessage != null)
{
await ioTHubModuleClient.SendEventAsync("modbusOutput", sqliteMessage);
}
if (!m_run)
{
break;