From 3d9b67a3bd95ccf455ac28989fbd282ea9b25223 Mon Sep 17 00:00:00 2001 From: KC-Yu IoT <944472692@qq.com> Date: Tue, 11 Jan 2022 15:57:20 +0800 Subject: [PATCH 1/4] update 1/11 --- .../Log_yjc.md" | 7 ++++++ ...21\351\227\256\351\233\206\345\220\210.md" | 16 ++++++++++++- .../index.py" | 23 +++++++++++++++---- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git "a/\345\233\275\346\260\221\345\267\245\344\275\234\350\277\233\345\261\225\345\221\250\346\212\245/Log_yjc.md" "b/\345\233\275\346\260\221\345\267\245\344\275\234\350\277\233\345\261\225\345\221\250\346\212\245/Log_yjc.md" index 46c05ac..a4a92d2 100644 --- "a/\345\233\275\346\260\221\345\267\245\344\275\234\350\277\233\345\261\225\345\221\250\346\212\245/Log_yjc.md" +++ "b/\345\233\275\346\260\221\345\267\245\344\275\234\350\277\233\345\261\225\345\221\250\346\212\245/Log_yjc.md" @@ -1,3 +1,10 @@ +[1月11日] + +早上: +规范补充了中转脚本的代码,优化了数据结构 +写了服务器的接受接口,简单返回了错误信息 +1. 完成了对数据结构的识别,如果结构有误在网关处拦截 + [1月10日] 1. 今天主要对齐了之后的计划和时间节点,对后续工作有合理性的安排 diff --git "a/\345\274\200\345\217\221\346\211\213\345\206\214/\345\274\200\345\217\221\346\211\213\345\206\214\345\217\212\347\226\221\351\227\256\351\233\206\345\220\210.md" "b/\345\274\200\345\217\221\346\211\213\345\206\214/\345\274\200\345\217\221\346\211\213\345\206\214\345\217\212\347\226\221\351\227\256\351\233\206\345\220\210.md" index 9cd58bc..aaa2a0b 100644 --- "a/\345\274\200\345\217\221\346\211\213\345\206\214/\345\274\200\345\217\221\346\211\213\345\206\214\345\217\212\347\226\221\351\227\256\351\233\206\345\220\210.md" +++ "b/\345\274\200\345\217\221\346\211\213\345\206\214/\345\274\200\345\217\221\346\211\213\345\206\214\345\217\212\347\226\221\351\227\256\351\233\206\345\220\210.md" @@ -42,7 +42,7 @@ d. 用于双方数字签名的公钥 | --- | --- | --- | | topic | str | 上传消息的主题 | | payload | str | 消息内容主体 | -| timestamp | int | 时间戳 | +| timestamp | int | 时间戳(秒级别的) | | SNNo | str | 设备唯一标识SN码 | 2. 利用对称密钥对消息体进行加密 @@ -82,3 +82,17 @@ d. 用于双方数字签名的公钥 | callback | 交互返回信息 | 是 | | system | 系统返回信息(包括第一次注册时,都是用这个主题) | 是 | +## 错误码映射表 +回调返回格式 +```json +{ + "code": 200, + "msg": "success" +} +``` + +| code | 解释 | 备注 | +| --- | :---: | --- | +| 200 | 成功 | | +| 300 | 传送数据结构不对,或者缺少某个字段 | | +| 400 | 传输到服务器时失败 | 有可能原因时服务器断开连接等问题,msg有具体报错信息 | diff --git "a/\346\240\221\350\216\223\346\264\276\344\270\255\350\275\254\350\204\232\346\234\254/index.py" "b/\346\240\221\350\216\223\346\264\276\344\270\255\350\275\254\350\204\232\346\234\254/index.py" index 73a8aba..ef7adbb 100644 --- "a/\346\240\221\350\216\223\346\264\276\344\270\255\350\275\254\350\204\232\346\234\254/index.py" +++ "b/\346\240\221\350\216\223\346\264\276\344\270\255\350\275\254\350\204\232\346\234\254/index.py" @@ -23,13 +23,13 @@ def on_message(client, userdata, msg): message_handle(message, deviceId) def validate_message(msg, deviceId): - keys = [ 'topic', 'payload', 'timestamp', 'SNNo'] + keys = ['topic', 'payload', 'timestamp', 'SNNo'] try: json_data = json.loads(msg) for key in keys: json_data[key] except Exception: - client.publish("/" + deviceId + "/callback/error","data format wrong!", qos=0, retain=False) + return_callback(300, "data format error!", deviceId) return False return True @@ -39,9 +39,22 @@ def message_handle(msg, deviceId): 'Content-Type': 'application/json' } callback = requests.post('http://localhost:8000/recieveMessage',data=msg,headers=headers).content.decode() - client.publish("/" + deviceId + "/callback", callback, qos=0, retain=False) - except Exception: - print(Exception) + if callback == '"suceess"': + return_callback(200, "success", deviceId) + else: + return_callback(500, callback, deviceId) + except Exception as e: + print(str(e)) + return_callback(400, str(e), deviceId) + +# 回调统一处理函数 +def return_callback(code, msg, deviceId): + body = { + "code": code, + "msg": msg + } + body = json.dumps(body) + client.publish("/" + deviceId + "/callback", body, qos=0, retain=False) client = mqtt.Client(client_id="server_script", protocol=3) client.on_connect = on_connect -- Gitee From 6a4da8eac5b00797514e341eec74f37d2446acee Mon Sep 17 00:00:00 2001 From: KC-Yu <7746123+kcyu@user.noreply.gitee.com> Date: Tue, 11 Jan 2022 20:44:49 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=88=B3=E8=8E=B7=E5=8F=96=20=E4=B8=BB=E9=A2=98=E4=B8=BAClock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2\347\226\221\351\227\256\351\233\206\345\220\210.md" | 2 +- .../index.py" | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git "a/\345\274\200\345\217\221\346\211\213\345\206\214/\345\274\200\345\217\221\346\211\213\345\206\214\345\217\212\347\226\221\351\227\256\351\233\206\345\220\210.md" "b/\345\274\200\345\217\221\346\211\213\345\206\214/\345\274\200\345\217\221\346\211\213\345\206\214\345\217\212\347\226\221\351\227\256\351\233\206\345\220\210.md" index aaa2a0b..8366820 100644 --- "a/\345\274\200\345\217\221\346\211\213\345\206\214/\345\274\200\345\217\221\346\211\213\345\206\214\345\217\212\347\226\221\351\227\256\351\233\206\345\220\210.md" +++ "b/\345\274\200\345\217\221\346\211\213\345\206\214/\345\274\200\345\217\221\346\211\213\345\206\214\345\217\212\347\226\221\351\227\256\351\233\206\345\220\210.md" @@ -34,7 +34,7 @@ d. 用于双方数字签名的公钥 "topic": "temperture", "payload": "35.8", "timestamp": 1641611982391, - "SNNo": "device000001" + "SNNo": "device001" } ``` diff --git "a/\346\240\221\350\216\223\346\264\276\344\270\255\350\275\254\350\204\232\346\234\254/index.py" "b/\346\240\221\350\216\223\346\264\276\344\270\255\350\275\254\350\204\232\346\234\254/index.py" index ef7adbb..64a812c 100644 --- "a/\346\240\221\350\216\223\346\264\276\344\270\255\350\275\254\350\204\232\346\234\254/index.py" +++ "b/\346\240\221\350\216\223\346\264\276\344\270\255\350\275\254\350\204\232\346\234\254/index.py" @@ -2,7 +2,8 @@ import time import paho.mqtt.client as mqtt import json,requests -HOST = '192.168.31.240' +HOST = '192.168.238.131' +# HOST = '192.168.31.240' # IoT 树莓派的地址 PORT = 1883 def on_connect(client, userdata, flags, rc): @@ -16,11 +17,13 @@ def on_message(client, userdata, msg): topic_split = msg.topic.split("/") deviceId = topic_split[1] message_type = topic_split[2] - if not (message_type == "callback"): # 判断消息不是回调否则进入死循环 + if message_type == "message": # 判断消息不是回调否则进入死循环 validate_result = validate_message(message, deviceId) print(validate_result) if validate_result == True: message_handle(message, deviceId) + elif message_type == "clock": + return_callback(200, int(time.time()), deviceId) def validate_message(msg, deviceId): keys = ['topic', 'payload', 'timestamp', 'SNNo'] @@ -39,7 +42,7 @@ def message_handle(msg, deviceId): 'Content-Type': 'application/json' } callback = requests.post('http://localhost:8000/recieveMessage',data=msg,headers=headers).content.decode() - if callback == '"suceess"': + if callback == '"success"': return_callback(200, "success", deviceId) else: return_callback(500, callback, deviceId) -- Gitee From 2010d8df4788b70660721052e8fb05be95bee449 Mon Sep 17 00:00:00 2001 From: KC-Yu <7746123+kcyu@user.noreply.gitee.com> Date: Tue, 11 Jan 2022 20:45:48 +0800 Subject: [PATCH 3/4] update --- ...5\217\212\347\226\221\351\227\256\351\233\206\345\220\210.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/\345\274\200\345\217\221\346\211\213\345\206\214/\345\274\200\345\217\221\346\211\213\345\206\214\345\217\212\347\226\221\351\227\256\351\233\206\345\220\210.md" "b/\345\274\200\345\217\221\346\211\213\345\206\214/\345\274\200\345\217\221\346\211\213\345\206\214\345\217\212\347\226\221\351\227\256\351\233\206\345\220\210.md" index 8366820..3cc40ee 100644 --- "a/\345\274\200\345\217\221\346\211\213\345\206\214/\345\274\200\345\217\221\346\211\213\345\206\214\345\217\212\347\226\221\351\227\256\351\233\206\345\220\210.md" +++ "b/\345\274\200\345\217\221\346\211\213\345\206\214/\345\274\200\345\217\221\346\211\213\345\206\214\345\217\212\347\226\221\351\227\256\351\233\206\345\220\210.md" @@ -96,3 +96,4 @@ d. 用于双方数字签名的公钥 | 200 | 成功 | | | 300 | 传送数据结构不对,或者缺少某个字段 | | | 400 | 传输到服务器时失败 | 有可能原因时服务器断开连接等问题,msg有具体报错信息 | +| 500 | 时间戳非法 | 时间戳已超时或者时间戳大于当前时间 | -- Gitee From 72ceae1ee9de684fed7ea66fe70b9cc4931303ff Mon Sep 17 00:00:00 2001 From: KC-Yu <7746123+kcyu@user.noreply.gitee.com> Date: Tue, 11 Jan 2022 20:49:49 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0gitignore=E8=AE=B0?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 74b59e4..8f7ce78 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ ~* +.idea -- Gitee