• 实现常规厂家&品牌&型号业务对接物联网平台(snack3加json赋能)


    前言

           之前介绍过通过snack3快速对接物模型,不知道大家还有没有影响。记得还留了一个作业给大家想想,就是这么兼容多型号、多版本,这次就来跟大家分享下这么集成多型号。


    一、物模型文件调整

           上次是利用snack3 + 物模型json文件,快速对接。这次要支持多型号,那肯定首先物模型json文件调整时少不了的。

    [
      {
        "profile": {
          "device_category": "温度检测器",
          "device_unit": "WD-470AA1-01-0114",
          "version": "1.0"
        },
        "properties": [
          {
            "dataType": {
              "specs": {
                "1": "成功"
              },
              "type": "enum"
            },
            "identifier": "ack_state",
            "name": "指令下发状态"
          },
          {
            "dataType": {
              "specs": {
                "unit": "V"
              },
              "type": "double"
            },
            "identifier": "battery_voltage",
            "name": "电池电压"
          },
          {
            "dataType": {
              "specs": {},
              "type": "string"
            },
            "identifier": "date",
            "name": "数据上报时间"
          },
          {
            "dataType": {
              "specs": {
                "unit": "℃"
              },
              "type": "double"
            },
            "name": "温度",
            "identifier": "temperature"
          }
        ],
        "services": [
          {
            "inputs": [
              {
                "dataType": {
                  "specs": {
                    "unit": "/"
                  },
                  "type": "int"
                },
                "identifier": "year",
                "name": "年"
              },
              {
                "dataType": {
                  "specs": {
                    "unit": "/"
                  },
                  "type": "int"
                },
                "name": "月",
                "identifier": "month"
              },
              {
                "dataType": {
                  "specs": {
                    "unit": "/"
                  },
                  "type": "int"
                },
                "name": "日",
                "identifier": "day"
              },
              {
                "dataType": {
                  "specs": {
                    "unit": "/"
                  },
                  "type": "int"
                },
                "name": "时",
                "identifier": "hour"
              },
              {
                "dataType": {
                  "specs": {
                    "unit": "/"
                  },
                  "type": "int"
                },
                "name": "分",
                "identifier": "minute"
              },
              {
                "dataType": {
                  "specs": {
                    "unit": "/"
                  },
                  "type": "int"
                },
                "name": "秒",
                "identifier": "second"
              }
            ],
            "identifier": "setTime",
            "name": "时间同步设置",
            "dataType": {
              "specs": {}
            }
          },
          {
            "inputs": [
              {
                "dataType": {
                  "specs": {
                    "unit": "s",
                    "min": "60",
                    "max": "86340"
                  },
                  "type": "int"
                },
                "identifier": "heart_beat",
                "name": "心跳时间"
              }
            ],
            "identifier": "heartbeat",
            "name": "心跳周期设置",
            "dataType": {
              "specs": {}
            }
          }
        ]
      },
      {
        "profile": {
          "device_category": "温度检测器",
          "device_unit": "WD-470AA1-01-0241",
          "version": "1.0"
        },
        "properties": [
          {
            "dataType": {
              "specs": {},
              "type": "string"
            },
            "name": "设备ID",
            "identifier": "device_id"
          },
          {
            "dataType": {
              "specs": {
                "0": "未校准",
                "1": "已校准"
              },
              "type": "bool"
            },
            "name": "温度校准状态",
            "identifier": "temp_calibration_state"
          },
          {
            "dataType": {
              "specs": {
                "0": "低于10%",
                "1": "10%~20%",
                "2": "20%~30%",
                "3": "30%~50%",
                "4": "50%以上"
              },
              "type": "enum"
            },
            "name": "电量等级",
            "identifier": "battery_level"
          },
          {
            "dataType": {
              "specs": {
                "unit": "min",
                "min": "1",
                "max": "1440"
              },
              "type": "int"
            },
            "name": "心跳周期",
            "identifier": "heartbeat_time"
          },
          {
            "identifier": "temperature",
            "name": "温度",
            "dataType": {
              "type": "double",
              "specs": {
                "unit": "℃",
                "unit_name": "摄氏度",
                "min": "-20",
                "max": "60"
              }
            }
          }
        ],
        "services": [
          {
            "name": "设置心跳周期",
            "identifier": "set_heartbeat_time",
            "inputs": [
              {
                "dataType": {
                  "specs": {
                    "unit": "min",
                    "min": "1",
                    "max": "1440"
                  },
                  "type": "int",
                  "default": 60
                },
                "name": "心跳周期",
                "identifier": "heartbeat_time"
              }
            ],
            "isPooling": 0,
            "ackTimeout": null,
            "ackType": 0,
            "dataType": {
              "specs": {}
            }
          },
          {
            "name": "校准温度",
            "identifier": "calibrate_temperature",
            "inputs": [
              {
                "dataType": {
                  "specs": {
                    "min": "-2",
                    "max": "2"
                  },
                  "type": "double",
                  "default": 1
                },
                "name": "系数",
                "identifier": "k"
              },
              {
                "dataType": {
                  "specs": {
                    "min": "-10",
                    "max": "10",
                    "unit": "℃"
                  },
                  "type": "double",
                  "default": 0
                },
                "name": "偏移量",
                "identifier": "b"
              }
            ],
            "isPooling": 0,
            "ackTimeout": null,
            "ackType": 0,
            "dataType": {
              "specs": {}
            }
          }
        ]
      }
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272

    这里实际就是将json文件格式调整问json集合数组,一个元素时一个型号,例如这里就是个型号:WD-470AA1-01-0114、WD-470AA1-01-0241

    二、使用步骤

    1.下行指令扩展

    原下行指令转换(核心):

    /**
         * 读取物模型并返回下行服务
         *
         * @param deviceTypeNo 设备类型编码
         * @param servicesId   下行标识
         * @return
         */
        public static cn.hutool.json.JSONObject getDownCommand(String deviceTypeNo, String servicesId) {
            cn.hutool.json.JSONObject json = new cn.hutool.json.JSONObject();
            //设备类型编码获取物模型
            String thingsJsonUrl = "/things/" + deviceTypeNo + ".json";
            try {
                String thingsJsonStr = ResourcesUtil.getResourceAsString(thingsJsonUrl);
                ONode jsonONode = ONode.loadStr(thingsJsonStr);
                ONode commandONode = jsonONode.select("$.services[?(identifier == '" + servicesId + "')]");
                if (!commandONode.isNull()) {
                    commandONode = commandONode.get(0);
                    json = JSONUtil.parseObj(ONode.serialize(commandONode));
                }
                return json;
            } catch (Exception e) {
                log.error("读取linkos物模型,获取下行service服务异常,原因:", e.getMessage());
            }
            return null;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    现扩展下行转换(核心):

    /**
         * 获取转化后的下行指令
         *
         * @param deviceTypeNo 设备类型编码
         * @param servicesId   服务id
         * @param unitCode     型号编码
         * @return 下行指令
         */
        public static cn.hutool.json.JSONObject getDownCommand(String deviceTypeNo, String servicesId, String unitCode) {
        	//这里根据型号编码是否空判定是否走多型号物模型
            if (StringUtils.isBlank(unitCode)) {
                return getDownCommand(deviceTypeNo, servicesId);
            }
            cn.hutool.json.JSONObject json = new cn.hutool.json.JSONObject();
            //设备类型编码获取物模型
            String thingsJsonUrl = "/things/" + deviceTypeNo + ".json";
            try {
                String thingsJsonStr = ResourcesUtil.getResourceAsString(thingsJsonUrl);
                ONode jsonONode = ONode.loadStr(thingsJsonStr);
                ONode commandONode = jsonONode.select("$.[?(@.profile.device_unit == '" + unitCode + "')]").get(0).select("$.services[?(identifier == '" + servicesId + "')]");
                if (!commandONode.isNull()) {
                    commandONode = commandONode.get(0);
                    json = JSONUtil.parseObj(ONode.serialize(commandONode));
                }
                return json;
            } catch (Exception e) {
                log.error("读取linkos物模型,获取下行service服务异常,原因:", e.getMessage());
            }
            return null;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    实际上就是根据型号编码判定是否走多型号支持。

    这里snack3功不可没,就是这:
           ONode commandONode = jsonONode.select(“ . [ ? ( @ . p r o f i l e . d e v i c e u n i t = = ′ " + u n i t C o d e + " ′ ) ] " ) . g e t ( 0 ) . s e l e c t ( " .[?(@.profile.device_unit == '" + unitCode + "')]").get(0).select(" .[?(@.profile.deviceunit=="+unitCode+")]").get(0).select(".services[?(identifier == '” + servicesId + “')]”);这一行连续过滤轻轻松松找到对应的物模型下行指令信息。

    2.业务模块支持

           业务模块有设置设备的厂家、品牌、型号信息:
    在这里插入图片描述
           那么业务系统只需要在定义型号时,设置为跟物模型文件里的型号一致就ok。


    总结

    • 设计思路还是很重要的,我这个支持多型号,实际在上次就计划了,早就留好了扩展的口子
    • snack3确实挺好用
    • 常规的设备厂家、品牌、型号 + snack3跟物模型json赋能,真的是对接物模型平台so easy。
      要支持多版本,相信大家也知道怎么处理了。好,就写到这里,UPing!
  • 相关阅读:
    winform语言切换C#设计笔记(八)
    【学习笔记之菜Dog学C】动态内存管理
    延伸产业链推进水稻循环经济 国稻种芯集团谋定益海嘉里合作
    解决Playwright无法登录Google账号的问题
    看卡塔尔世界杯,diff一下足球比赛与软件开发
    通通锁接口调用<Response [400]>报错及python示例代码
    两个栈实现一个队列,两个队列实现一个栈(C++实现)
    实操 | 制造一个OOM,生成jvm的dump文件,并通过jvisualvm工具解析
    VS + QT 封装带UI界面的DLL
    【陪学职言】如何让自己40岁以上还有竞争力?
  • 原文地址:https://blog.csdn.net/zwrlj527/article/details/127846730