diff --git a/MQTT.py b/MQTT.py index e56ab4d..e5b9a40 100644 --- a/MQTT.py +++ b/MQTT.py @@ -9,21 +9,26 @@ class MQTT: def on_connect(self, client, userdata, flags, rc): if rc == 0: client.subscribe('lot_stream') + # Message receiving callback def on_message(self, client, userdata, msg): data = json.loads(msg.payload.decode('utf-8'))["msg"] if data == "push_stream": # 启动推流视频 push_stream() + client.publish('success', payload='push_stream success', qos=0) elif data == "close_stream": # 关闭推流视频 close_stream() + client.publish('success', payload='close_stream success', qos=0) elif data == "exec": # 执行命令 exec_sh(msg) + client.publish('success', payload='exec_sh success', qos=0) elif data == "update": # git更新项目和配置文件 update() + client.publish('success', payload='update success', qos=0) else: client.publish('error', payload='No Such Type', qos=0) @@ -38,5 +43,6 @@ class MQTT: # Publish a message self.client.loop_forever(retry_first_connection=True) + if __name__ == '__main__': mq = MQTT() diff --git a/api.py b/api.py index fa4818c..9d42af0 100644 --- a/api.py +++ b/api.py @@ -3,7 +3,7 @@ import subprocess import uvicorn from db.models.base import BaseResponse -from db.models.log_data_model import LOT_DATA +from db.models.log_data_model import LOT_DATA, LOT_DATA_MODEL from db.repository import add_kb_to_db, get_kb_detail, get_kb_detail_by_time, delete_kb_detail_by_time from fastapi import FastAPI @@ -17,6 +17,14 @@ def add(data: LOT_DATA): return BaseResponse(code=500, msg=e) +def delete(start_time, end_time): + try: + delete_kb_detail_by_time(start_time, end_time) + return BaseResponse() + except Exception as e: + return BaseResponse(code=500, msg=e) + + def get_data(): try: data = get_kb_detail() diff --git a/ceshi.py b/ceshi.py index 5333fac..99a1e41 100644 --- a/ceshi.py +++ b/ceshi.py @@ -202,13 +202,14 @@ def run_with_client(client): 'soil_potassium_phosphate_potassium': jia_data, 'rainfall': rainfall_data, 'wind_speed': speedwind_data, - 'wind_direction': winddirection_data} - - t = LOT_DATA(**data, create_time=int(time.time())) + 'wind_direction': winddirection_data, + 'create_time': int(time.time()) + } + t = LOT_DATA(**data) # TODO 判断数据是否正常 - add(t) # 发送给服务器 - client.publish('lot_data', payload=data, qos=0) + client.publish('lot_data', payload=json.dumps(data, ensure_ascii=False), qos=0) + r = add(t) def run_no_client(): @@ -375,13 +376,12 @@ def run_no_client(): 'wind_direction': winddirection_data} data = LOT_DATA(**data, create_time=int(time.time())) # TODO 判断数据是否正常 - add(data) - + r = add(data) def t1(): import time - for i in range(100): + while (1): time.sleep(2) data = { "wind_speed": 1, @@ -411,6 +411,8 @@ def t2(): time = 3 from threading import Thread + + def on_connect(client, userdata, flags, rc): global time if rc == 0: diff --git a/tool.py b/tool.py index e7e61a0..28242ff 100644 --- a/tool.py +++ b/tool.py @@ -11,7 +11,7 @@ def close_stream(): def exec_sh(msg): - cmd = json.loads(msg.payload.decode('utf-8'))["cmd"] + cmd = json.loads(msg.payload.decode('utf-8'))["data"] subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)