You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
2.0 KiB
59 lines
2.0 KiB
import os, sys
|
|
from json import loads
|
|
from asyncio_dvrip import DVRIPCam
|
|
import asyncio
|
|
from nvr_core import NVR
|
|
from nvr_types import File
|
|
|
|
def app_dir():
|
|
return os.path.dirname(os.path.abspath(__file__))
|
|
|
|
def load_config(config_name):
|
|
try:
|
|
path = os.path.join(app_dir(), config_name)
|
|
print("Looking config file", path)
|
|
with open(path, "r", encoding="utf8") as f:
|
|
return loads(f.read())
|
|
except Exception as e:
|
|
print("cannot find or parse config.json", e)
|
|
sys.exit(1)
|
|
|
|
class Recorder:
|
|
loop = asyncio.get_event_loop()
|
|
def __init__(self, address, port, username, password, name = ""):
|
|
self.address = address
|
|
self.port = int(port)
|
|
self.username = username
|
|
self.password = password
|
|
self.name = name
|
|
|
|
@property
|
|
def nvr(self):
|
|
client = DVRIPCam(self.address, port = self.port, user = self.username, password = self.password)
|
|
return NVR(client, self.loop)
|
|
|
|
def __str__(self) -> str:
|
|
if not self.name:
|
|
return f"{self.address}:{self.port}"
|
|
else:
|
|
return self.name
|
|
|
|
class Config:
|
|
def __init__(self, config_name = "config.json") -> None:
|
|
raw = load_config(config_name)
|
|
self.listen_address = raw.get("backend", {}).get("address", "0.0.0.0")
|
|
self.listen_port = int(raw.get("backend", {}).get("port", "8080"))
|
|
self.recorders = []
|
|
for raw_server in raw.get("recorders", []):
|
|
self.recorders.append(Recorder(raw_server.get("ip"), raw_server.get("port"), raw_server.get("user"), raw_server.get("password"), raw_server.get("name", "")))
|
|
if (self.recorders.__len__() == 0):
|
|
print("Recorders not find")
|
|
else:
|
|
for recorder in self.recorders:
|
|
print(recorder)
|
|
|
|
def getRecorder(self, index = 0) -> Recorder:
|
|
return self.recorders[index]
|
|
|
|
def getRecorders(self):
|
|
return [str(r) for r in self.recorders]
|