-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
200 lines (169 loc) · 5.81 KB
/
run.py
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
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, BackgroundTasks
from fastapi.staticfiles import StaticFiles
from fastapi.responses import StreamingResponse, HTMLResponse
from starlette.middleware.cors import CORSMiddleware
import signal
import logging
import base64
import asyncio
import redis.asyncio as aioredis
from db.controller import MysqlDB, RedisDB, RedisMQ, SocketManager
from services import cctv as Cctv
from services import event as Event
from services import location as Location
from services import rtsp as RTSP
from services import log as Log
from services import snap as Snap
from models.log import LogReadModel, LogCheckModel
from models.cctv import CctvCreateModel, CctvCheckModel, CctvUpdateModel, CctvDeleteModel
from models.location import LocationCreateModel, LocationCheckModel, LocationUpdateModel, LocationDeleteModel
from models.event import EventCreateModel, EventReadModel, EventUpdateModel, EventDeleteModel
from models.message import MessageSendModel, MessageRecvModel
from models.rtsp import RtspSnapModel
from models.snap import SnapReadModel, SnapUpdateModel
from utils.clip import clipGroup
app = FastAPI()
mysql = MysqlDB()
redis = RedisDB()
redis_client = aioredis.from_url("redis://localhost:50001")
mq = RedisMQ()
socketManager = SocketManager()
clipManagers = clipGroup()
origins = [
"*"
]
app.add_middleware(
CORSMiddleware,
# allow_origins=origins,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/file/snap", StaticFiles(directory="static/snap"), name="snap")
app.mount("/file/clip", StaticFiles(directory="static/clip"), name="clip")
# Root Services
@app.get("/")
def read_root():
return {"msg": "Server Is Working"}
# CCTV Services
@app.post('/cctv/create')
def create_cctv(option: CctvCreateModel):
response = Cctv.create(option.name, option.url)
return response
@app.post('/cctv/read')
def read_cctv():
response = Cctv.read()
return response
@app.post('/cctv/check')
def read_cctv(option: CctvCheckModel):
response = Cctv.check(option.name)
return response
@app.post('/cctv/update')
def update_cctv(option: CctvUpdateModel):
response = Cctv.update(option.target, option.name, option.url)
return response
@app.post('/cctv/delete')
def delete_cctv(option: CctvDeleteModel):
response = Cctv.delete(option.target)
return response
# Event Services
@app.post('/event/create')
def create_event(option: EventCreateModel):
response = Event.create(option.e_type, option.name)
return response
@app.post('/event/read')
def read_event(option: EventReadModel):
response = Event.read(option.target)
return response
@app.post('/event/update')
def update_event(option: EventUpdateModel):
response = Event.update(option.target, option.e_type, option.name)
return response
@app.post('/event/delete')
def delete_event(option: EventDeleteModel):
response = Event.delete(option.target)
return response
# Location Services
@app.post('/location/create')
def create_location(option: LocationCreateModel):
response = Location.create(option.name, option.cctv)
return response
@app.post('/location/read')
def read_location():
response = Location.read()
return response
@app.post('/location/check')
def read_location(option: LocationCheckModel):
response = Location.check(option.name)
return response
@app.post('/location/read/cctv')
def read_location_cctv():
response = Location.read_with_cctv()
return response
@app.post('/location/update')
def update_location(option: LocationUpdateModel):
response = Location.update(option.target, option.name, option.cctv)
return response
@app.post('/location/delete')
def delete_location(option: LocationDeleteModel):
response = Location.delete(option.target)
return response
# Log Services
@app.post('/log/read')
def read_log(option: LogReadModel):
response = Log.read(option.datetime, option.locations, option.types)
return response
@app.post('/log/read/limit')
def read_log(option: LogReadModel):
response = Log.readLimit(option.datetime, option.locations, option.types)
return response
@app.post('/log/check')
def read_log(option: LogCheckModel):
response = Log.check(option.target)
return response
# Message Services
@app.post('/message/send')
def message_send(option: MessageSendModel, background_tasks: BackgroundTasks):
response = mq.send(option.key, option.message)
background_tasks.add_task(clipManagers[option.message["location"]].start_recording, option.message)
return response
@app.post('/message/recv')
def message_send(option: MessageRecvModel):
response = mq.recv(option.key)
return response
@app.post('/message/live')
def message_live():
response = mq.live()
return response
# RTSP Services
@app.get("/rtsp")
def stream_rtsp(url: str):
url_bytes = url.encode('ascii')
decoded = base64.b64decode(url_bytes)
str_url = decoded.decode('UTF-8')
return StreamingResponse(RTSP.getRtspStream(str_url), media_type="multipart/x-mixed-replace; boundary=PNPframe")
@app.post("/rtsp/snap")
def stream_rtsp(option: RtspSnapModel):
response = RTSP.snapshot(option.location, option.url, option.target)
return response
# Snap Services
@app.post("/snap/read")
def stream_rtsp(option: SnapReadModel):
response = Snap.read(option.target)
return response
@app.post("/snap/update")
def stream_rtsp(option: SnapUpdateModel):
response = Snap.update(option.target, option.data)
return response
@app.websocket("/message")
async def websocket_endpoint(websocket: WebSocket):
await socketManager.connect(websocket)
try:
while True:
await asyncio.sleep(1)
data = mq.live()
if data:
await socketManager.broadcast(data)
except WebSocketDisconnect:
socketManager.disconnect(websocket)