本文主要是介绍物联网平台组件1: 边缘网关自定义上传报文,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
【技术实现】
Net6通过Jint读取JS模板文件,并调用createMqttPayload方法,传入网关数据(JSON),得到平台报文(JSON)
【应用场景】
用户可以灵活配置边缘网关的报文格式到平台
【测试范例】
脚本名称:script1.js
输入Json:
{"GateCode": "gw1","Devices": [{"DeviceCode": "JY355","Tags": [{"TagCode": "40105","TagValue": "5"}, {"TagCode": "40106","TagValue": "6"}]}, {"DeviceCode": "JY356","Tags": [{"TagCode": "40107","TagValue": "7"}, {"TagCode": "40108","TagValue": "8"}]}] }
输出Json:
{"ts": "2024-08-30 14:23:22","d": [{"tag": "40105","value": "5"}, {"tag": "40106","value": "6"}, {"tag": "40107","value": "7"}, {"tag": "40108","value": "8"}] }
脚本名称:script2.js
输入Json:
{"GateCode": "gw1","Devices": [{"DeviceCode": "JY355","Tags": [{"TagCode": "40105","TagValue": "5"}, {"TagCode": "40106","TagValue": "6"}]}, {"DeviceCode": "JY356","Tags": [{"TagCode": "40107","TagValue": "7"}, {"TagCode": "40108","TagValue": "8"}]}] }
输出Json:
{"clientid": "gw1","time": "2024-08-30 14:33:10","JY355": [{"tag": "40105","value": "5"}, {"tag": "40106","value": "6"}],"JY356": [{"tag": "40107","value": "7"}, {"tag": "40108","value": "8"}] }
【代码实现】
Model.cs
namespace JsTemp2Json.Util {public class Gate{public string GateCode { get; set; }public List<Device> Devices { get; set; }}public class Device{public string DeviceCode { get; set; }public List<Tag> Tags { get; set; }}public class Tag{public string TagCode { get; set; }public string TagValue { get; set; }} }
Program.cs
using Jint; using JsTemp2Json.Util; using System.Text.Json;namespace JsTemp2Json {internal class Program{public static void Main(string[] args){var engine = new Engine();// 读取JavaScript文件内容string scriptPath = "script2.js";// script1.js / script2.jsstring scriptContent = File.ReadAllText(scriptPath); engine.Execute(scriptContent);var Gate = new Gate(){GateCode = "gw1",Devices = new List<Device>(){new Device(){DeviceCode = "JY355",Tags = new List<Tag>(){new Tag() { TagCode = "40105", TagValue = "5"},new Tag() { TagCode = "40106", TagValue = "6"}}},new Device(){DeviceCode = "JY356",Tags = new List<Tag>(){new Tag() { TagCode = "40107", TagValue = "7"},new Tag() { TagCode = "40108", TagValue = "8"}}},}};string dataJson = JsonSerializer.Serialize(Gate);var resultJson = engine.Invoke("createMqttPayload", dataJson).AsString();Console.WriteLine($"{scriptPath}\r\n");Console.WriteLine($"{dataJson}\r\n");Console.WriteLine($"{resultJson}"); Console.ReadLine();} } }
script1.js
// ======================================================================================= // ** 脚本名称:script1.js // ** 输入Json:{"GateCode":"gw1","Devices":[{"DeviceCode":"JY355","Tags":[{"TagCode":"40105","TagValue":"5"},{"TagCode":"40106","TagValue":"6"}]},{"DeviceCode":"JY356","Tags":[{"TagCode":"40107","TagValue":"7"},{"TagCode":"40108","TagValue":"8"}]}]} // ** 输出Json:{"ts":"2024-08-30 11:30:30","d":[{"tag":"40105","value":"5"},{"tag":"40106","value":"6"},{"tag":"40107","value":"7"},{"tag":"40108","value":"8"}]} // ======================================================================================= function createMqttPayload(dataJson) { let gate = JSON.parse(dataJson); let device = gate.Devices;let result = {"ts": formatDateTime(new Date()),"d": []};device.forEach(function (d) { let tag = d.Tags;tag.forEach(function (t) { let data = {"tag": t.TagCode,"value": t.TagValue};result.d.push(data);});}); return JSON.stringify(result); }function formatDateTime(date) {const year = date.getFullYear().toString().padStart(4, '0');const month = (date.getMonth() + 1).toString().padStart(2, '0');const day = date.getDate().toString().padStart(2, '0');const hour = date.getHours().toString().padStart(2, '0');const minute = date.getMinutes().toString().padStart(2, '0');const second = date.getSeconds().toString().padStart(2, '0');return `${year}-${month}-${day} ${hour}:${minute}:${second}`; // 2023-02-18 21:49:05 }
script2.js
// ======================================================================================= // ** 脚本名称:script2.js // ** 输入Json:{"GateCode":"gw1","Devices":[{"DeviceCode":"JY355","Tags":[{"TagCode":"40105","TagValue":"5"},{"TagCode":"40106","TagValue":"6"}]},{"DeviceCode":"JY356","Tags":[{"TagCode":"40107","TagValue":"7"},{"TagCode":"40108","TagValue":"8"}]}]} // ** 输出Json:{"clientid":"gw1","time":"2024-08-30 11:34:35","JY355":[{"tag":"40105","value":"5"},{"tag":"40106","value":"6"}],"JY356":[{"tag":"40107","value":"7"},{"tag":"40108","value":"8"}]} // ======================================================================================= function createMqttPayload(dataJson) { let gate = JSON.parse(dataJson);let clientid = gate.GateCode;let device = gate.Devices;let result = {clientid: gate.GateCode, time: formatDateTime(new Date()),}; device.forEach(function (d) {let deviceCode = d.DeviceCode;let tag = d.Tags;if (!result[deviceCode]) {result[deviceCode] = [];} tag.forEach(function (t) { result[deviceCode].push({tag: t.TagCode,value: t.TagValue}); }); });return JSON.stringify(result); }function formatDateTime(date) {const year = date.getFullYear().toString().padStart(4, '0');const month = (date.getMonth() + 1).toString().padStart(2, '0');const day = date.getDate().toString().padStart(2, '0');const hour = date.getHours().toString().padStart(2, '0');const minute = date.getMinutes().toString().padStart(2, '0');const second = date.getSeconds().toString().padStart(2, '0');return `${year}-${month}-${day} ${hour}:${minute}:${second}`; // 2023-02-18 21:49:05 }
这篇关于物联网平台组件1: 边缘网关自定义上传报文的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!