From d844e8ccb50d6feeab7e8056c5b3884c6d24749c Mon Sep 17 00:00:00 2001 From: Jarno Rankinen Date: Sun, 23 Oct 2022 19:48:48 +0000 Subject: [PATCH] First implementation of cambot, missing configuration --- Dockerfile | 2 +- modules/cam.py | 49 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6257824..d6d778e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,11 +12,11 @@ RUN pip install -r requirements.txt COPY bot.py *.json *.pickle /bot/ COPY config config -COPY modules modules VOLUME /bot/config RUN useradd -m HomeBot && chown HomeBot -R /bot && apt install curl jq -y USER HomeBot WORKDIR /bot +COPY modules modules CMD [ "python", "-u", "./bot.py" ] diff --git a/modules/cam.py b/modules/cam.py index 53a5c01..75972c5 100644 --- a/modules/cam.py +++ b/modules/cam.py @@ -1,3 +1,4 @@ +import re from modules.common.module import BotModule import requests @@ -6,19 +7,49 @@ class MatrixModule(BotModule): def __init__(self,name): super().__init__(name) self.motionurl = 'http://192.168.1.220:8080' + self.allowed_cmds = { + 'config': ['list','set','get','write'], + 'detection': ['status','connection','start','pause'], + 'action': ['eventstart','eventend','snapshot','restart','quit','end'] + } + self.helptext = """Control the motion daemon. + Available commands: + - config list|set|get|write + - detection status|connection|start|pause + - action eventstart|eventend|snapshot|restart|quit|end + + Usage: '!cam category command' + + is the numerical id of the camera. Use 0 for all cameras. + If is omitted, 0 is assumed.""" async def matrix_message(self, bot, room, event): args = event.body.split() args.pop(0) + if args[0] == 'help': + await bot.send_text(room, self.helptext, event) + return - if args[0] == 'config': - if args[1] == 'list': - req_url = self.motionurl - for arg in args: - req_url = f'{req_url}/{arg}' - - resp = requests.get(req_url) - await bot.send_text(resp.content) + recvd_camid = 1 + try: + # Check if first argument is numeric (camera id) + camid = int(args[0]) + camid = str(camid) + except ValueError: + recvd_camid = 0 + camid = '0' + if args[recvd_camid] not in self.allowed_cmds: + await bot.send_text(room, f'Unknown category: "{args[1]}"', event) + return + category = args[recvd_camid] + recvd_camid = recvd_camid + 1 + if args[recvd_camid] not in self.allowed_cmds[category]: + await bot.send_text(room, f'Unknown command: "{args[recvd_camid]}"', event) + return + command = args[recvd_camid] + req_url = f'{self.motionurl}/{camid}/{category}/{command}' + resp = requests.get(req_url).text + await bot.send_text(room, resp, event) def help(self): - return 'Echoes back what user has said' + return self.helptext.splitlines()[0]