物联网平台组件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.js
            string 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  
}

  • 15
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值