From 839d42549e26afc1529c8d11a9f80fc59a2bb799 Mon Sep 17 00:00:00 2001 From: xyj <10908227994@qq.com> Date: Fri, 1 Dec 2023 09:21:23 +0800 Subject: [PATCH] first --- .idea/.gitignore | 3 + .idea/encodings.xml | 4 + .idea/inspectionProfiles/Project_Default.xml | 26 +++++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/lot_manager.iml | 12 +++ .idea/misc.xml | 7 ++ .idea/modules.xml | 8 ++ MQTT.py | 45 ++++++++ __pycache__/api.cpython-310-pytest-7.4.3.pyc | Bin 0 -> 2076 bytes __pycache__/tool.cpython-310.pyc | Bin 0 -> 786 bytes api.py | 99 ++++++++++++++++++ conf/mqtt.conf | 10 ++ conf/push_stream.conf | 9 ++ db/__init__.py | 0 db/__pycache__/__init__.cpython-310.pyc | Bin 0 -> 162 bytes db/__pycache__/base.cpython-310.pyc | Bin 0 -> 793 bytes db/__pycache__/session.cpython-310.pyc | Bin 0 -> 1323 bytes db/base.py | 19 ++++ db/models/__init__.py | 0 .../__pycache__/__init__.cpython-310.pyc | Bin 0 -> 169 bytes db/models/__pycache__/base.cpython-310.pyc | Bin 0 -> 1241 bytes .../log_data_model.cpython-310.pyc | Bin 0 -> 3266 bytes db/models/base.py | 25 +++++ db/models/log_data_model.py | 49 +++++++++ db/repository/__init__.py | 1 + .../__pycache__/__init__.cpython-310.pyc | Bin 0 -> 209 bytes .../lot_data_repository.cpython-310.pyc | Bin 0 -> 1913 bytes db/repository/lot_data_repository.py | 51 +++++++++ db/session.py | 46 ++++++++ exec_sh.sh | 1 + lot_data.db | Bin 0 -> 8192 bytes reload_conf.sh | 7 ++ requirements.txt | 6 ++ start_push.sh | 2 + stop_push.sh | 2 + test.py | 26 +++++ tool.py | 18 ++++ update.sh | 4 + 38 files changed, 486 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/lot_manager.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 MQTT.py create mode 100644 __pycache__/api.cpython-310-pytest-7.4.3.pyc create mode 100644 __pycache__/tool.cpython-310.pyc create mode 100644 api.py create mode 100644 conf/mqtt.conf create mode 100644 conf/push_stream.conf create mode 100755 db/__init__.py create mode 100644 db/__pycache__/__init__.cpython-310.pyc create mode 100644 db/__pycache__/base.cpython-310.pyc create mode 100644 db/__pycache__/session.cpython-310.pyc create mode 100755 db/base.py create mode 100755 db/models/__init__.py create mode 100644 db/models/__pycache__/__init__.cpython-310.pyc create mode 100644 db/models/__pycache__/base.cpython-310.pyc create mode 100644 db/models/__pycache__/log_data_model.cpython-310.pyc create mode 100755 db/models/base.py create mode 100755 db/models/log_data_model.py create mode 100755 db/repository/__init__.py create mode 100644 db/repository/__pycache__/__init__.cpython-310.pyc create mode 100644 db/repository/__pycache__/lot_data_repository.cpython-310.pyc create mode 100755 db/repository/lot_data_repository.py create mode 100755 db/session.py create mode 100644 exec_sh.sh create mode 100644 lot_data.db create mode 100644 reload_conf.sh create mode 100644 requirements.txt create mode 100644 start_push.sh create mode 100644 stop_push.sh create mode 100644 test.py create mode 100644 tool.py create mode 100644 update.sh diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..da0415a --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..50b418a --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,26 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/lot_manager.iml b/.idea/lot_manager.iml new file mode 100644 index 0000000..f0c20fd --- /dev/null +++ b/.idea/lot_manager.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..419a75c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..890d108 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/MQTT.py b/MQTT.py new file mode 100644 index 0000000..55a29ed --- /dev/null +++ b/MQTT.py @@ -0,0 +1,45 @@ +import json + +import paho.mqtt.client as mqtt + +from tool import push_stream, close_stream, update, exec_sh + + +# Connection success callback +def on_connect(client, userdata, flags, rc): + print(rc) + if rc == 0: + client.subscribe('lot_data') + else: + client.publish('conn_error', payload=rc, qos=0) + + +# Message receiving callback +def on_message(client, userdata, msg): + data = json.loads(msg.payload.decode('utf-8'))["msg"] + if data == "push_stream": + # TODO 启动推流视频 + push_stream() + elif data == "close_stream": + # TODO 关闭推流视频 + close_stream() + elif data == "exec": + # TODO 执行命令 更新配置 + exec_sh() + elif data == "update": + # TODO git更新项目 + update() + else: + client.publish('conn_error', payload='No Such Type', qos=0) + + +if __name__ == '__main__': + client = mqtt.Client() + client.username_pw_set("ceshi", "123456") + # Specify callback function + client.on_connect = on_connect + client.on_message = on_message + # Establish a connection + client.connect('ceshi-mqtt.lihaink.cn', 1883) + # Publish a message + client.loop_forever() diff --git a/__pycache__/api.cpython-310-pytest-7.4.3.pyc b/__pycache__/api.cpython-310-pytest-7.4.3.pyc new file mode 100644 index 0000000000000000000000000000000000000000..44b7cbc526b8a4eb9966a9263e6c0c52714f5948 GIT binary patch liter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)Q3#-+X=k$~JiqyhN&keoyexc>r z@3>lD1si@2WUfOO;{X2$e*YK$;m@k#B#~gIBpL2h@pvvWFpsG^H;P4a&RtHR9)S<{ zKwqw%qxLj^s)glM>l~`0CDw+`PCQ99_+Cg(qxrFzD4lDU>~BJx_8{sd)XL1%!SAXC z-F^dRYFbx!FQl7px2xB*3o@D)$_T6UqOP&N-nd++(GmOrpib%);Gj|#MHPaq^~JFY zAk}?+@d@qoDxaU01Cy;U)8oUw1idK85ZYibXiT literal 0 HcmV?d00001 diff --git a/__pycache__/tool.cpython-310.pyc b/__pycache__/tool.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a2d715a05f3f74304faa6d6992ccc4a1949ccd77 GIT binary patch literal 786 zcmah`v2NQi5G5tbmL0okvt;qu#Xu4LKoJxuI&?7vq*E3{(Xo*Y$rO$hP-iQWDL{jK zMm%N8&uA?YAak}%xswbdLyQo(cSqil@9xnIhkZe7|97T^C&Yi=yk2xBzLD841Ry}> z;#hzO{)IS}pdcXcK?l0zeF&jPUV%Ik+wmX<6Cqz-GTzejV6M=Xu?+4ou}5 zuMx+p4oaLYT-~?TNr@)a*49C6O1-F6{4@Ts637xX@O&6$rqEG7%aU0XFVif+B1SXU zsk2eJbeUPKw-F>RnHI?+In@|BWAcM?S?_Q|Q|nMCh2kL@LOGJi6KvJuX+&8k<`N`n z;8D~V_!|w>OqjT?46Jc8 eR6~oLdb7>&Yhj?u^`WE+%aVm6^ur)j1N8#e=AEzr literal 0 HcmV?d00001 diff --git a/api.py b/api.py new file mode 100644 index 0000000..700092a --- /dev/null +++ b/api.py @@ -0,0 +1,99 @@ +import os +import subprocess + +import uvicorn +from db.models.base import BaseResponse +from db.models.log_data_model import LOT_DATA +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 + +app = FastAPI() + + +@app.post("/add/lot_data/{device_name}") +async def add(data: LOT_DATA, device_name: str): + try: + add_kb_to_db(data, device_name) + return BaseResponse() + except Exception as e: + return BaseResponse(code=500, msg=e) + + +@app.get("/get/all/lot_data") +async def get_data(): + try: + data = get_kb_detail() + return BaseResponse(data=data) + except Exception as e: + return BaseResponse(code=404, msg=e) + + +@app.get("/get/data/by/time") +async def get_data(start_time, end_time): + try: + data = get_kb_detail_by_time(start_time, end_time) + return BaseResponse(data=data) + except Exception as e: + return BaseResponse(code=404, msg=e) + + +@app.get("/delete/data/by/time") +async def delete_data(start_time, end_time): + try: + data = delete_kb_detail_by_time(start_time, end_time) + return BaseResponse(data=data) + except Exception as e: + return BaseResponse(code=404, msg=e) + + +@app.get("/create/db") +async def create(): + from db.base import Base, engine + try: + Base.metadata.create_all(bind=engine) + return BaseResponse() + except Exception as e: + return BaseResponse(code=404, msg=e) + + +@app.get("/start") +def device_test(): + ## 不管有没有都进行一个杀死 + # os.popen() + # stop_cmd = r'kill -9 `lsof -t -i:554`' + # os.system(stop_cmd) + start_cmd = r'nohup ffmpeg -rtsp_transport tcp -re -i rtsp://admin:123456@192.168.0.226:554/mpeg4 -c:v copy -c:a aac -preset ultrafast -r 20 -flvflags no_duration_filesize -f rtsp -rtsp_transport tcp rtsp://127.0.0.1/live/test > app.out 2>&1 & echo $! > app.pid' + p = subprocess.Popen(start_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = p.communicate() + return_code = p.returncode + if return_code == 0: + return BaseResponse(code=return_code, msg=out, + data="http://192.168.1.27/live/test.live.mp4?secret=gqig2yFKkDpIMic1uWZY1L5MsIo0eflm") + else: + return BaseResponse(code=return_code, msg=err) + + +@app.get("/stop") +async def device_test2(): + ## 不管有没有都进行一个杀死 + cmd = 'lsof -t -i:554' + p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + # 获取执行结果 + out, err = p.communicate() + # 获取返回值 + return_code = p.returncode + if out == b'': + print("失败") + else: + pid = out + cmd = f'kill -9 {pid}' + print(pid) + os.system(cmd) + if return_code == 0: + return BaseResponse(code=return_code, msg=out) + return BaseResponse(code=return_code, msg=err) + + +if __name__ == '__main__': + uvicorn.run(app, host="127.0.0.1", port=8000) diff --git a/conf/mqtt.conf b/conf/mqtt.conf new file mode 100644 index 0000000..e992409 --- /dev/null +++ b/conf/mqtt.conf @@ -0,0 +1,10 @@ +[program:mqtt] +directory=/home/lihai/PycharmProjects/pythonProject/lot_manager +command=/home/lihai/anaconda3/envs/chatchat/bin/python MQTT.py +user=lihai +autostart=true +autorestart=true +redirect_stderr=true +stopsignal=TERM +stopasgroup=True +priority=1 \ No newline at end of file diff --git a/conf/push_stream.conf b/conf/push_stream.conf new file mode 100644 index 0000000..f2311da --- /dev/null +++ b/conf/push_stream.conf @@ -0,0 +1,9 @@ +[program:push_stream] +directory=/home/lihai/PycharmProjects/pythonProject/lot_manager +command=/usr/bin/ffmpeg -rtsp_transport tcp -re -i rtsp://admin:123456@192.168.0.123:554/mpeg4 -c:v copy -c:a aac -preset ultrafast -r 20 -flvflags no_duration_filesize -f rtsp -rtsp_transport tcp rstp://47.108.186.87:554/live/test9 +user=lihai +autostart=true +autorestart=true +redirect_stderr=true +stopsignal=TERM +stopasgroup=True \ No newline at end of file diff --git a/db/__init__.py b/db/__init__.py new file mode 100755 index 0000000..e69de29 diff --git a/db/__pycache__/__init__.cpython-310.pyc b/db/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..69f4cfe85a0c2b9125d99c4f35e829b7c27b79cc GIT binary patch literal 162 zcmd1j<>g`kg0h;>R1p0bL?8o3AjbiSi&=m~3PUi1CZpd8B*=$H!;pWtPOp Z>lIYq;;_lhPbtkwwF4Pn%mgG@7yt|0Cl~+# literal 0 HcmV?d00001 diff --git a/db/__pycache__/base.cpython-310.pyc b/db/__pycache__/base.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f5a57d1bc38e4197c74872762a31310923c4a6b3 GIT binary patch literal 793 zcmYjPOK%e~5VpO$d2Dt!r57Z`9p#d}qp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-(5Y^J-MPX^Y(yN>7bElqxxZj%!1_{Xjvz-MGWi(at4(gA)9lUL2PuG`p+~7 zqv?NE&ks6Ddua^sTu7W$S7ytzlJIi5`~a6cP!XT-E-rCS9w#qFq%ffldR2ai;wcbM zk@<7neM9DILFJsTIhhH9F|h|sPN6gALBZt&PFO4#ts)!ZR*{Xl5+qBa^%)uz;f0*Q zDs7nI@*Jw~MrHgcucwr)>@?#X{T_>w7_q%=) z$DxuMnzCy7{=-g7l5=K>)@;jDL0(L=uI_|h=(mGtI2+4I5}%8%6MA{tdFT^Kysg)s di(=6agDX^sI)jtL9DD%|aBT|gT7B}2{XZ~VBIN)8 literal 0 HcmV?d00001 diff --git a/db/base.py b/db/base.py new file mode 100755 index 0000000..f7e4b6c --- /dev/null +++ b/db/base.py @@ -0,0 +1,19 @@ +import os + +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta +from sqlalchemy.orm import sessionmaker + +import json +KB_ROOT_PATH = "./" +DB_ROOT_PATH = os.path.join(KB_ROOT_PATH, "lot_data.db") +SQLALCHEMY_DATABASE_URI = f"sqlite:///{DB_ROOT_PATH}" + +engine = create_engine( + SQLALCHEMY_DATABASE_URI, + json_serializer=lambda obj: json.dumps(obj, ensure_ascii=False), +) + +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +Base: DeclarativeMeta = declarative_base() diff --git a/db/models/__init__.py b/db/models/__init__.py new file mode 100755 index 0000000..e69de29 diff --git a/db/models/__pycache__/__init__.cpython-310.pyc b/db/models/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..17ff0d96c6d72cf461ee968f9dfc3e568b686196 GIT binary patch literal 169 zcmd1j<>g`kg0h;>R1p0bL?8o3AjbiSi&=m~3PUi1CZpd8B*==jNxR<`nD4 f$7kkcmc+;F6;$5humK8|=A_zzY$;{}5-bb=MWQLv literal 0 HcmV?d00001 diff --git a/db/models/__pycache__/base.cpython-310.pyc b/db/models/__pycache__/base.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..94acc2b406c3cfc2c3fb0e0acca73e5ea438d19c GIT binary patch literal 1241 zcmZux&yUNe_-F6XF+HNblTztUF#AYWYPNq&G2&YN~ z2?_O9wTDWe-r#~bR0y^I0Dpt8unYSyaN<2@N-KdSTkm`C`T6sg=QPu4N?>f7ToFph z?+9KU0f5Ue+b>~}M0yp`gcA>Kzw)%teN8!am{tJ~z#mkB4teN&VHN3^$C`1b6Q1al zr_LW$6P@u)Px;g%9}pQ!_7#y#h4(0*0ZwEJIKAidP|{muF}Z;A$-=Wt=AG(Ftrote z2lZfsqq6OeA(C9lTXnsWs#*j#y4qB|t}Q!+^b<(8(XGyu^{I`o)tx$32KLX6Zk&k2 zgOdYs{l?+@N0z<71s?Y50BJ@U-UUMu?n%P^k3kMjRiHxYUm}w7NX1|WV27Nk1ndZH zpdZ@|*?pwiO;fk(;x{N`g96C%xb2HVwXLPPJw3Jo+%I3GmI+bkS_xrOA#@}A3hhjY zJAGb_5i`Row5&V3BSc=;O_z5i)EC0+xKP^}GY88c{~w8Y6K!1n{&2Z&v|3i>Ixm+u zhhm)@ebY3zRnfJ}&CsozdJC6GNa(!ILA0e@ErE=x+T{xFv%eWk2IVF&TLw!?GB5My zp7Z`k#RxJkW9lccyioC_hSHZ*2KNKdF#sjm?L>lt_bE>i%hAJR0vQwVWY9FJi%+*` z;?PXSpm=6NIHZ3Amh>GMXWu{j^U2-wAD*0j_pNiB^ho%RU!Pw*`03i=g4)T(lsY$q z_)HBpENiL0uyN67t?I7FTb_OM`0V+!^GCm2JbDV<6HE1J-dCM@3u4DEu|7D6@!d`?u(;8yFG(!_{- zL{Z-rb@Mr&6h^@j0v`sSjORIE@RB^)hF1emw!zi#vKvNg538bRNTS8wVkGe9bnfkX zGng(09LIyZVFRzpHI~7jgk_sS=5<#VF3+azohq-2wbH}L-jPN&<30A|YJcpc{i-?qen|Hsm@OXG`p&x5oUJ3J$EkNKIm=gs{&-HrYMoZdc- literal 0 HcmV?d00001 diff --git a/db/models/__pycache__/log_data_model.cpython-310.pyc b/db/models/__pycache__/log_data_model.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..df6fc00e53e26459c86afaffee040aa2a1bc647b GIT binary patch literal 3266 zcmb_eTXP)66`t9E8ZMd%0Xzfy+^9{5O4y@-J*`y%NxP9BzLWfGJG16-TY8POK7Bu^qGHr}2N> zPB_U*QdNGbumnq9P*{?y=i`+Wv{Nh%?KDq9JIykulw!{sWK)VU+4FPL=g)YI+cNo- z#cftprTQa)1U{%@)YfZbG)GlM;YvkiN+tGdg~yr7Vk~|sR!Q(APqDLGLnc+7$XY2V+ zr|`|Cqx#*i-aC9m|HrcIqX1?JUd0aUgAmld{Au{n`PExD!rOm_{QtagUJjYgyv5zX z2)NVW!VH>%Z;k!i-#f7fo|~2VZfw2jSj-9**2f{~H@9D3UH&9|{jK&#e}Sa>xfxy8 z8x2SHRZTJPxdyYmmn_ED$2Lc>d?md68U;^H>%W>a<#0E(X$hktxbNdQH2kou*Vh)` zfLOXXy@H@6m{u{mnns?`I_ zt9@B9`^1f%;+hzmD9ffyiL!X?|b)1qMMLVA5)5P7s5!$}; zdHCr{`|=`t0J$l33?9s#LemXZR(-bu~yRL6M zYhU|f?c%MqKYk4&B;H|l1<4c7{USL5yudEp)3%RBQE{8makAs^f^Z|4o3X!<*3hH-x{^OSyMa%!fN zT0XFI=I&P%M`Xq@T+`u(A+v_zc&uq7?=_4Unx@@(BH}P%q6a`G&)A+B$bQ2xT^F+8 zKJX1g;Ll3r5c&}Y5QY$N_X%8Xu?t}j!af9CSFs=AL4*SUHx%(>$S~OEDaj@!+ImghdS>jTng2tR-$@40f2$s1N zuXHT9hh@Ra(Rwwe5l4H~STAw3RgL9{qn&E3k2u<>#`=k)eQGRE9Bor$1H{oTH8w~b zZBk=H#L*r#HcWhk_z3Y);-kdJh>sB;Cq7Pm7x7)hceCAW&jqEjhwTNM-6#9HHfucd z?D1cmXyT$~3vjhxz47huue85fTK&`V>ZO~j*FOy3dySr)ma(Ug7voZE2+J|Wg7G|G zkU6s%c$Qlg+yUd1>8j@-S(}D@&0c_SZ+;zqyx3lOD}3V%sp`d;m;zO%89!q-?SNv! zSFVO%-d=tCuWN6AR#YiKq#;V+5kO!TfvzCX+XZ^J7)BUHK-U$!5%wb7hfqLx0O28k zDwO1n3pp0+C*k(*05J6fr3S}^CG~g8S@o*2q_Q~i81V%0IPoO$1o0H{B=I!y6!8r4 zH1Qtd8RA*uJ;XKQS>nCKHR3tqy~O*7=ZN=L4Mfe2npkd)P87O@XG{Gnw}v+LZmpyK+bdeRHMSYIwQ!YN+C-sS ztF~q9)*uqe^0vJ!B#(UT$A*J$#R-P46zZ z4%{oQ>%`^n)M9Z!CeFe(kTHvq-TpAPipNc#+h;b-RZPH77M)-4hA<4Qhkgv;j@laA zzUYU#F(-?A1)dPZBM1ipWZE#8S2c`E&M*RV-sVvqNj>S=O~;k#r`>>qC5y)}I&~@# za8ke{*NavaswNa2slkrN5*k?0NIr$N(t$!p{@AnR(T}@L&?X72mb>v1=;}s literal 0 HcmV?d00001 diff --git a/db/models/base.py b/db/models/base.py new file mode 100755 index 0000000..43c821d --- /dev/null +++ b/db/models/base.py @@ -0,0 +1,25 @@ +from datetime import datetime +from typing import Any, List + +import pydantic +from pydantic import BaseModel +from sqlalchemy import Column, DateTime, String, Integer + +from db.models.log_data_model import LOT_DATA_MODEL, LOT_DATA + + +class BaseResponse(BaseModel): + code: int = 200 + msg: str = "success" + data: Any = None + + +class BaseModel: + """ + 基础模型 + """ + id = Column(Integer, primary_key=True, index=True, comment="主键ID") + create_time = Column(DateTime, default=datetime.utcnow, comment="创建时间") + update_time = Column(DateTime, default=None, onupdate=datetime.utcnow, comment="更新时间") + create_by = Column(String, default=None, comment="创建者") + update_by = Column(String, default=None, comment="更新者") diff --git a/db/models/log_data_model.py b/db/models/log_data_model.py new file mode 100755 index 0000000..4d1c177 --- /dev/null +++ b/db/models/log_data_model.py @@ -0,0 +1,49 @@ +from pydantic import BaseModel, Field +from sqlalchemy import * +from db.base import Base + + +class LOT_DATA(BaseModel): + wind_speed: float = Field(None, description='风速:(0到30)m/s ') + wind_direction: float = Field(None, description='风向:0~360°') + ambient_temperature: float = Field(None, description='环境温度:℃') + ambient_humidity: float = Field(None, description='环境湿度:%RH') + carbon_dioxide: float = Field(None, description='二氧化碳:0~5000ppm') + ambient_air_pressure: float = Field(None, description='环境气压:0~120KPa') + rainfall: float = Field(None, description='雨量:mm') + ambient_lighting: float = Field(None, description='环境光照:0-65535Lux;0-20万Lux') + soil_temperature: float = Field(None, description='土壤温度:-40~80℃') + soil_moisture: float = Field(None, description='土壤湿度:0-100%RH') + soil_conductivity: float = Field(None, description='土壤电导率:0-20000μS/cm') + soil_PH: float = Field(None, description='土壤PH:3~9PH') + soil_potassium_phosphate_nitrogen: float = Field(None, description='土壤磷酸钾:氮的标准值在140-225mg/kg') + soil_potassium_phosphate_phosphorus: float = Field(None, description='土壤磷酸钾:磷的标准值在57-100mg/kg,') + soil_potassium_phosphate_potassium: float = Field(None, description='土壤磷酸钾:钾的标准值在106-150mg/kg') + + +class LOT_DATA_MODEL(Base): + """ + 物联网数据模型 + """ + __tablename__ = 'LOT_DATA_MODEL' + id = Column(Integer, primary_key=True, autoincrement=True, comment='ID') + device_name = Column(String(50), comment='设备编号', default="0") + create_time = Column(Integer, comment="创建时间") + wind_speed = Column(Float, comment='风速:(0到30)m/s ') + wind_direction = Column(Float, comment='风向:0~360°') + ambient_temperature = Column(Float, comment='环境温度:℃') + ambient_humidity = Column(Float, comment='环境湿度:%RH') + carbon_dioxide = Column(Float, comment='二氧化碳:0~5000ppm') + ambient_air_pressure = Column(Float, comment='环境气压:0~120KPa') + rainfall = Column(Float, comment='雨量:mm') + ambient_lighting = Column(Float, comment='环境光照:0-65535Lux;0-20万Lux') + soil_temperature = Column(Float, comment='土壤温度:-40~80℃') + soil_moisture = Column(Float, comment='土壤湿度:0-100%RH') + soil_conductivity = Column(Float, comment='土壤电导率:0-20000μS/cm') + soil_PH = Column(Float, comment='土壤PH:3~9PH') + soil_potassium_phosphate_nitrogen = Column(Float, comment='土壤磷酸钾:氮的标准值在140-225mg/kg') + soil_potassium_phosphate_phosphorus = Column(Float, comment='土壤磷酸钾:磷的标准值在57-100mg/kg,') + soil_potassium_phosphate_potassium = Column(Float, comment='土壤磷酸钾:钾的标准值在106-150mg/kg') + + def __repr__(self): + return f"LOT_DATA_MODEL(id={self.id}, wind_speed={self.wind_speed}, wind_direction={self.wind_direction}, ambient_temperature={self.ambient_temperature}, ambient_humidity={self.ambient_humidity}, carbon_dioxide={self.carbon_dioxide}, ambient_air_pressure={self.ambient_air_pressure}, rainfall={self.rainfall}, ambient_lighting={self.ambient_lighting}, soil_temperature={self.soil_temperature}, soil_moisture={self.soil_moisture}, soil_conductivity={self.soil_conductivity}, Soil_PH={self.soil_PH}, soil_potassium_phosphate_nitrogen={self.soil_potassium_phosphate_nitrogen}, soil_potassium_phosphate_phosphorus={self.soil_potassium_phosphate_phosphorus}, soil_potassium_phosphate_potassium={self.soil_potassium_phosphate_potassium})" diff --git a/db/repository/__init__.py b/db/repository/__init__.py new file mode 100755 index 0000000..747de98 --- /dev/null +++ b/db/repository/__init__.py @@ -0,0 +1 @@ +from .lot_data_repository import * diff --git a/db/repository/__pycache__/__init__.cpython-310.pyc b/db/repository/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..099b64951ea90562b893bd952a2d65f6cfe8812c GIT binary patch literal 209 zcmd1j<>g`kg6!^?RAnIj7{oyaOhAqU5Et_Ri4=wu#vFzah7_h?22JLdj6fkx##@Y9 zewvI?!a4aR@hOQViSb3L1^LC9CHX~_D;bKIfoj0SuK@jw{M=OioXm{GO#Oh$C|tkmR^V*P^3l8pR3C|4hBOm1RcVtQ(keoB%)!bJV}_{_Y_lK6PNg34PQHo5sJ Qr8%i~Acqu#T*|`$01Tx!#sB~S literal 0 HcmV?d00001 diff --git a/db/repository/__pycache__/lot_data_repository.cpython-310.pyc b/db/repository/__pycache__/lot_data_repository.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..19f5d93382d623062f98608a9772e6d4c798d709 GIT binary patch literal 1913 zcmah}OK;pZ5Ee;kSJpma8%^p4X`7^vL_n~cLyv~hpzbYA0>q~+3PCHC?7 z?zc~ExUgel+dt-JoM@qSUQVsgQhob=iNjLorc|@OEKTBmU#;~HXSNL>lF2YJWx|I@f<`v( zvo2BVpkxStF_1Hyj;Dv901KWW=j4D^z|BttSvsK=Rhy0<>iD5GKg80U@5O-)UL6TF zt9e=5&!rJ68P065pDS%_g9|B)h?(lZ1!@CD7sVwX>IRs0quz(m_f=T{c-*CN*DdOk zi1#B3=vRZKW5H{D{kB}dLwFDXA{tINo45`Q)XqgH0JiLy~)qu;o!sz}1+BSi| z)EKpt19$_3Z5Nr4mUfWSEbh5>V zy`qG;bx)R~`kb>}+qm`|M6hS^5@U8O(|``&K=+Qgo^W||%sBVgK)LF04^y}eo{yy1 c!G}jW8+3@3rT_o{ literal 0 HcmV?d00001 diff --git a/db/repository/lot_data_repository.py b/db/repository/lot_data_repository.py new file mode 100755 index 0000000..926ef86 --- /dev/null +++ b/db/repository/lot_data_repository.py @@ -0,0 +1,51 @@ +import time + +from db.models.log_data_model import LOT_DATA_MODEL, LOT_DATA +from db.session import with_session + + +@with_session +def add_kb_to_db(session, data: LOT_DATA, device_name: str): + # 创建知识库实例 + kb = LOT_DATA_MODEL(**data.__dict__, device_name=device_name, create_time=int(time.time())) + session.add(kb) + + +@with_session +def delete_kb_from_db(session, id): + kb = session.query(LOT_DATA_MODEL).filter_by(id=id).first() + if kb: + session.delete(kb) + return True + + +# 直接查询所有数据 +@with_session +def get_kb_detail(session): + all_data = session.query(LOT_DATA_MODEL).all() + data = [LOT_DATA(**d.__dict__) for d in all_data] + return data + + +# 根据时间查询数据 +@with_session +def get_kb_detail_by_time(session, start_time, end_time): + all_data = session.query(LOT_DATA_MODEL).filter( + start_time <= LOT_DATA_MODEL.create_time, LOT_DATA_MODEL.create_time <= end_time).all() + data = [LOT_DATA(**d.__dict__) for d in all_data] + return data + + +# 删除数据 +@with_session +def delete_kb_detail_by_time(session, start_time, end_time): + d = session.query(LOT_DATA_MODEL).filter(start_time <= LOT_DATA_MODEL.create_time, LOT_DATA_MODEL.create_time <= end_time).delete() + return d + + +# 查询数据 +@with_session +def get_data_by_field(session, field, value): + query = session.query(LOT_DATA_MODEL).filter(getattr(LOT_DATA_MODEL, field) == value) + result = query.all() + return result diff --git a/db/session.py b/db/session.py new file mode 100755 index 0000000..47cb59f --- /dev/null +++ b/db/session.py @@ -0,0 +1,46 @@ +from functools import wraps +from contextlib import contextmanager +from db.base import SessionLocal +from sqlalchemy.orm import Session + + +@contextmanager +def session_scope() -> Session: + """上下文管理器用于自动获取 Session, 避免错误""" + session = SessionLocal() + try: + yield session + session.commit() + except: + session.rollback() + raise + finally: + session.close() + + +def with_session(f): + @wraps(f) + def wrapper(*args, **kwargs): + with session_scope() as session: + try: + result = f(session, *args, **kwargs) + session.commit() + return result + except: + session.rollback() + raise + + return wrapper + + +def get_db() -> SessionLocal: + db = SessionLocal() + try: + yield db + finally: + db.close() + + +def get_db0() -> SessionLocal: + db = SessionLocal() + return db diff --git a/exec_sh.sh b/exec_sh.sh new file mode 100644 index 0000000..a9bf588 --- /dev/null +++ b/exec_sh.sh @@ -0,0 +1 @@ +#!/bin/bash diff --git a/lot_data.db b/lot_data.db new file mode 100644 index 0000000000000000000000000000000000000000..c438be439156e816fa3cee3018496cd3122fcb27 GIT binary patch literal 8192 zcmeI0UvASt5XSA0s&Y^i1P?)ihb0maQ&RqvpbCV9(-=~4lH$6R%8RwJH}OcZ*IKU& zycYr2-~?QSYj6sjl+?IHp$C|eSKgWLoALVhX79hVKr%AsBBYXRDOVI#RbCRJD9T#4 zO4(Y3nr+I9B)iY@|HYcJIsR46Tx#`)l3f@`00|%gB!C2v01`j~NB{{S0VHri0w;ZS zy{_x(*8@pMQ)Uevw`(|t+aGpKtC*E`Y}0T|;usyvB#oljAkB&fKG_{O=4;a?gP}tP z2bOi8R5YI*gU8&6hKw8<_D;{Ro9&1CO!5SylDQHx=}+sd`~f20O=8A;^2!<-&RNX| z!8{2(%Ih>70gI$7Sr{`xWh&UeVvvU5gZz{$9u*@VxjyiZ;4kT*Alz86Bq_`j6rwSm zPV+g7$4_ArNQfr6l5m(VKh<0bIV8oy&dVN;{4{&T_7|dg^D^sc6BlW6fw^Y`a_^qK+c)eZ^42^eP4M-KKKnK=*SbpWXYG3} z$u<~B00|%gB!C2v01`j~NB{{S0VIF~kib#`t@1VHMp-S!=}(kLEO`2@eebT;tM^N9 z>d!kJ(r#T^Y5$^MeY|mKZ#DbXn-8|%-)fas+COhU+q_(PvT^;kc1K%(VYJkh_LFn= E-##|wLI3~& literal 0 HcmV?d00001 diff --git a/reload_conf.sh b/reload_conf.sh new file mode 100644 index 0000000..f59097e --- /dev/null +++ b/reload_conf.sh @@ -0,0 +1,7 @@ +#!/bin/bash +#复制配置文件 +cp -r conf/*.conf /etc/supervisor/conf.d/ +# 重启所有配置 +supervisorctl reload +# 推流不启动 +supervisorctl stop push_stream diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0bfef0a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +pydantic~=1.10.13 +sqlalchemy~=2.0.19 +uvicorn~=0.23.2 +fastapi~=0.95.1 +paho-mqtt~=1.6.1 +requests~=2.31.0 \ No newline at end of file diff --git a/start_push.sh b/start_push.sh new file mode 100644 index 0000000..ed60632 --- /dev/null +++ b/start_push.sh @@ -0,0 +1,2 @@ +#!/bin/bash +supervisorctl start push_stream \ No newline at end of file diff --git a/stop_push.sh b/stop_push.sh new file mode 100644 index 0000000..fc96a8f --- /dev/null +++ b/stop_push.sh @@ -0,0 +1,2 @@ +#!/bin/bash +supervisorctl stop push_stream \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..cd750d7 --- /dev/null +++ b/test.py @@ -0,0 +1,26 @@ +import datetime +import time + +import requests + +while (True): + print(time.time()) + time.sleep(10.0) +# data = { +# "wind_speed": 26, +# "wind_direction": 6, +# "ambient_temperature": 66, +# "ambient_humidity": 78, +# "carbon_dioxide": 94, +# "ambient_air_pressure": 48, +# "rainfall": 3, +# "ambient_lighting": 59, +# "soil_temperature": 71, +# "soil_moisture": 19, +# "soil_conductivity": 62, +# "soil_PH": 49, +# "soil_potassium_phosphate_nitrogen": 5, +# "soil_potassium_phosphate_phosphorus": 5, +# "soil_potassium_phosphate_potassium": 86 +# } +# requests.post("http://192.168.1.27:8000/add/lot_data", json=data) diff --git a/tool.py b/tool.py new file mode 100644 index 0000000..647c333 --- /dev/null +++ b/tool.py @@ -0,0 +1,18 @@ +import subprocess + + +def push_stream(): + print("启动推流") + subprocess.Popen(['start_push.sh'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + +def close_stream(): + subprocess.Popen(['stop_push.sh'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + +def exec_sh(): + subprocess.Popen(['exec_sh.sh'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + +def update(): + subprocess.Popen(['update.sh'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) diff --git a/update.sh b/update.sh new file mode 100644 index 0000000..35b0308 --- /dev/null +++ b/update.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +# TODO git更新 +git pull \ No newline at end of file