|
|
@ -19,18 +19,22 @@ def date_today(begin = True): |
|
|
|
return datetime.now().strftime("%Y-%m-%d 23:59:59") |
|
|
|
|
|
|
|
class NVR: |
|
|
|
def __init__(self, client, loop) -> None: |
|
|
|
def __init__(self, client, loop, index = 0) -> None: |
|
|
|
self.logger = create_logger(NVR.__name__) |
|
|
|
self.client:DVRIPCam = client |
|
|
|
self.loop = loop |
|
|
|
self.index = index |
|
|
|
|
|
|
|
async def login(self): |
|
|
|
self.logger.debug(f"[{self.index}] Login to {self.client}") |
|
|
|
await self.client.login(self.loop) |
|
|
|
|
|
|
|
def logout(self): |
|
|
|
self.logger.debug(f"[{self.index}] Logout to {self.client}") |
|
|
|
self.client.close() |
|
|
|
|
|
|
|
async def channels(self): |
|
|
|
self.logger.debug(f"[{self.index}] Get channels") |
|
|
|
return await self.client.get_command("ChannelTitle", 1048) |
|
|
|
|
|
|
|
async def files(self, channel, start = None, end = None, ftype = H264, stype = SECONDARY_STREAM, json = False): |
|
|
@ -38,7 +42,7 @@ class NVR: |
|
|
|
start = date_today() |
|
|
|
if not end: |
|
|
|
end = date_today(False) |
|
|
|
self.logger.info(f"Search files from {start} to {end}") |
|
|
|
self.logger.info(f"[{self.index}] Search files from {start} to {end}") |
|
|
|
for raw_file in await list_local_files(self.client, startTime=start, endTime=end, filetype=ftype, channel=channel, streamType=stype): |
|
|
|
if json: |
|
|
|
yield NvrFile(raw_file, channel, stype).json |
|
|
@ -47,13 +51,13 @@ class NVR: |
|
|
|
|
|
|
|
async def stream_file(self, file: NvrFile) -> bytes: |
|
|
|
len_data = await file.generate_first_bytes(self.client) |
|
|
|
self.logger.debug(f"len data = {len_data}") |
|
|
|
self.logger.debug(f"[{self.index}] len data = {len_data}, streaming file content") |
|
|
|
if (len_data is None): |
|
|
|
yield b"" |
|
|
|
else: |
|
|
|
async for chunk in file.get_file_stream(self.client, len_data): |
|
|
|
if (chunk == None): |
|
|
|
self.logger.debug("end of file") |
|
|
|
self.logger.debug(f"[{self.index}] end of file") |
|
|
|
break |
|
|
|
yield chunk |
|
|
|
|
|
|
@ -63,4 +67,4 @@ class NVR: |
|
|
|
async for byte in file.generate_bytes(self.client): |
|
|
|
f.write(byte) |
|
|
|
downloaded_bytes += len(byte) |
|
|
|
self.logger.debug(f"\r{downloaded_bytes}/{file.size}") |
|
|
|
self.logger.debug(f"\r [{self.index}] Downloaded: {downloaded_bytes}/{file.size}") |