|
@ -4,13 +4,16 @@ import uvicorn |
|
|
import traceback |
|
|
import traceback |
|
|
|
|
|
|
|
|
from config_parser import Config as ConfigParser |
|
|
from config_parser import Config as ConfigParser |
|
|
from config_parser import TranscodeStatus, TranscodeTools |
|
|
from config_parser import TranscodeStatus, TranscodeTools, Go2Rtc |
|
|
from nvr_core import NVR |
|
|
from nvr_core import NVR |
|
|
from nvr_types import File |
|
|
from nvr_types import File |
|
|
|
|
|
|
|
|
class Server: |
|
|
class Server: |
|
|
app: FastAPI = FastAPI() |
|
|
app: FastAPI = FastAPI() |
|
|
config: ConfigParser = ConfigParser() |
|
|
config: ConfigParser = ConfigParser() |
|
|
|
|
|
go2rtc: Go2Rtc = Go2Rtc() |
|
|
|
|
|
|
|
|
|
|
|
API_BASE_REF = "/api/dvrip" |
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
def __init__(self): |
|
|
self.setup_events() |
|
|
self.setup_events() |
|
@ -19,10 +22,17 @@ class Server: |
|
|
def setup_events(self): |
|
|
def setup_events(self): |
|
|
@self.app.on_event('startup') |
|
|
@self.app.on_event('startup') |
|
|
async def on_startup(): |
|
|
async def on_startup(): |
|
|
print("i am alive") |
|
|
for i in range(0, len(self.config.recorders)): |
|
|
|
|
|
nvr:NVR = self.config.getRecorder(i).nvr |
|
|
|
|
|
await nvr.login() |
|
|
|
|
|
channels = await nvr.channels() |
|
|
|
|
|
nvr.logout() |
|
|
|
|
|
self.config.recorders[i].channels = len(channels) |
|
|
|
|
|
print(f"{self.config.recorders[i]} channels count: {self.config.recorders[i].channels}") |
|
|
|
|
|
await self.go2rtc.start_go2rtc(self.config.recorders) |
|
|
|
|
|
|
|
|
def setup_routers(self): |
|
|
def setup_routers(self): |
|
|
@self.app.get("/api", status_code=200) |
|
|
@self.app.get(self.API_BASE_REF, status_code=200) |
|
|
async def getRecorders(response: Response): |
|
|
async def getRecorders(response: Response): |
|
|
try: |
|
|
try: |
|
|
return {"ok":True, "data":self.config.getRecorders()} |
|
|
return {"ok":True, "data":self.config.getRecorders()} |
|
@ -31,7 +41,7 @@ class Server: |
|
|
response.status_code = 400 |
|
|
response.status_code = 400 |
|
|
return {"ok":False, "error":e} |
|
|
return {"ok":False, "error":e} |
|
|
|
|
|
|
|
|
@self.app.get("/api/channels/{recorder_index}", status_code=200) |
|
|
@self.app.get(self.API_BASE_REF + "/channels/{recorder_index}", status_code=200) |
|
|
async def getRecorder(response: Response, recorder_index:int): |
|
|
async def getRecorder(response: Response, recorder_index:int): |
|
|
try: |
|
|
try: |
|
|
nvr:NVR = self.config.getRecorder(recorder_index).nvr |
|
|
nvr:NVR = self.config.getRecorder(recorder_index).nvr |
|
@ -45,7 +55,7 @@ class Server: |
|
|
finally: |
|
|
finally: |
|
|
nvr.logout() |
|
|
nvr.logout() |
|
|
|
|
|
|
|
|
@self.app.get("/api/history/{recorder_index}/{channel}/{stream}") |
|
|
@self.app.get(self.API_BASE_REF + "/history/{recorder_index}/{channel}/{stream}") |
|
|
async def getHistory(response: Response, recorder_index:int, channel: int, stream: int, start_date:str = None, end_date:str = None): |
|
|
async def getHistory(response: Response, recorder_index:int, channel: int, stream: int, start_date:str = None, end_date:str = None): |
|
|
try: |
|
|
try: |
|
|
nvr:NVR = self.config.getRecorder(recorder_index).nvr |
|
|
nvr:NVR = self.config.getRecorder(recorder_index).nvr |
|
@ -58,7 +68,7 @@ class Server: |
|
|
finally: |
|
|
finally: |
|
|
nvr.logout() |
|
|
nvr.logout() |
|
|
|
|
|
|
|
|
@self.app.get("/api/snapshot/{recorder_index}/{channel}") |
|
|
@self.app.get(self.API_BASE_REF + "/snapshot/{recorder_index}/{channel}") |
|
|
async def getSnapshot(response: Response, recorder_index:int, channel: int): |
|
|
async def getSnapshot(response: Response, recorder_index:int, channel: int): |
|
|
try: |
|
|
try: |
|
|
nvr:NVR = self.config.getRecorder(recorder_index).nvr |
|
|
nvr:NVR = self.config.getRecorder(recorder_index).nvr |
|
@ -73,7 +83,7 @@ class Server: |
|
|
finally: |
|
|
finally: |
|
|
nvr.logout() |
|
|
nvr.logout() |
|
|
|
|
|
|
|
|
@self.app.get("/api/file/{recorder_index}") |
|
|
@self.app.get(self.API_BASE_REF + "/file/{recorder_index}") |
|
|
async def getFile(response: Response, recorder_index:int, b64:str, background_tasks: BackgroundTasks): |
|
|
async def getFile(response: Response, recorder_index:int, b64:str, background_tasks: BackgroundTasks): |
|
|
try: |
|
|
try: |
|
|
if len(b64) == 0: |
|
|
if len(b64) == 0: |
|
@ -102,7 +112,7 @@ class Server: |
|
|
response.status_code = 400 |
|
|
response.status_code = 400 |
|
|
return {"ok":False, "error":e} |
|
|
return {"ok":False, "error":e} |
|
|
|
|
|
|
|
|
@self.app.get("/api/transcode/status/{recorder_index}") |
|
|
@self.app.get(self.API_BASE_REF + "/transcode/status/{recorder_index}") |
|
|
async def getTranscodeStatus(response: Response, recorder_index:int, b64:str, background_tasks: BackgroundTasks): |
|
|
async def getTranscodeStatus(response: Response, recorder_index:int, b64:str, background_tasks: BackgroundTasks): |
|
|
try: |
|
|
try: |
|
|
if len(b64) == 0: |
|
|
if len(b64) == 0: |
|
@ -125,7 +135,7 @@ class Server: |
|
|
response.status_code = 400 |
|
|
response.status_code = 400 |
|
|
return {"ok":False, "error":e} |
|
|
return {"ok":False, "error":e} |
|
|
|
|
|
|
|
|
@self.app.get("/api/transcode/download") |
|
|
@self.app.get(self.API_BASE_REF + "/transcode/download") |
|
|
async def getTranscodeDownload(response: Response, b64:str): |
|
|
async def getTranscodeDownload(response: Response, b64:str): |
|
|
try: |
|
|
try: |
|
|
if len(b64) == 0: |
|
|
if len(b64) == 0: |
|
|