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.
73 lines
2.7 KiB
73 lines
2.7 KiB
import asyncio
|
|
from datetime import datetime
|
|
from asyncio_dvrip import DVRIPCam
|
|
|
|
from nvr_types import File as NvrFile
|
|
from nvr_types import list_local_files
|
|
from nvr_types import PRIMARY_STREAM, SECONDARY_STREAM
|
|
from nvr_types import H264
|
|
from global_funcs import create_logger
|
|
|
|
START = "2024-08-04 6:22:34"
|
|
END = "2024-08-04 23:23:09"
|
|
CHANNEL = 0
|
|
|
|
def date_today(begin = True):
|
|
if begin:
|
|
return datetime.now().strftime("%Y-%m-%d 00:00:00")
|
|
else:
|
|
return datetime.now().strftime("%Y-%m-%d 23:59:59")
|
|
|
|
class NVR:
|
|
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):
|
|
try:
|
|
self.logger.debug(f"[{self.index}] Logout to {self.client}")
|
|
self.client.close()
|
|
except:
|
|
pass
|
|
|
|
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):
|
|
if not start:
|
|
start = date_today()
|
|
if not end:
|
|
end = date_today(False)
|
|
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
|
|
else:
|
|
yield NvrFile(raw_file, channel, stype)
|
|
|
|
async def stream_file(self, file: NvrFile, max_file_size = None) -> bytes:
|
|
len_data = await file.get_file_stream_start(self.client)
|
|
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, max_file_size):
|
|
if (chunk == None):
|
|
self.logger.debug(f"[{self.index}] end of file")
|
|
break
|
|
yield chunk
|
|
|
|
async def save_file(self, file:NvrFile, savePath = "out.unknown"):
|
|
downloaded_bytes = 0
|
|
with open(savePath, "wb") as f:
|
|
async for byte in file.generate_bytes(self.client):
|
|
f.write(byte)
|
|
downloaded_bytes += len(byte)
|
|
self.logger.debug(f"\r [{self.index}] Downloaded: {downloaded_bytes}/{file.size}")
|