hemppa/modules/cam.py

100 lines
3.9 KiB
Python
Raw Normal View History

import re
2022-10-23 21:17:02 +03:00
from modules.common.module import BotModule
import requests
class MatrixModule(BotModule):
def __init__(self,name):
super().__init__(name)
self.motionurl = 'http://localhost:8080'
self.allowed_cmds = {
'config': ['list','set','get','write'],
'detection': ['status','connection','start','pause'],
'action': ['eventstart','eventend','snapshot','restart','quit','end']
}
self.restricted_cmds = ['list','set','get','write','start','pause','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
2022-10-26 21:28:55 +03:00
- url get|set <motionurl>
Usage: '!cam <id> category command'
<id> is the numerical id of the camera. Use 0 for all cameras.
If <id> is omitted, 0 is assumed."""
2022-10-23 21:17:02 +03:00
2022-10-26 21:28:55 +03:00
def get_settings(self):
data = super().get_settings()
data['motionurl'] = self.motionurl
return data
def set_settings(self, data):
super().set_settings(data)
if data.get('motionurl'):
self.motionurl = data['motionurl']
2022-10-23 21:17:02 +03:00
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
2022-10-23 21:17:02 +03:00
2022-10-26 21:28:55 +03:00
elif args[0] == 'url':
if args[1] == 'set':
newurl = args[2]
bot.must_be_owner(event)
self.motionurl = newurl
bot.save_settings()
await bot.send_text(room, f"Motion API URL set to {self.motionurl}")
elif args[1] == 'get':
await bot.send_text(room, f"Motion URL is currently {self.motionurl}")
else:
2022-10-26 21:49:55 +03:00
cmdindex = 1
2022-10-26 21:28:55 +03:00
try:
# Check if first argument is numeric (camera id)
camid = int(args[0])
camid = str(camid)
except ValueError:
2022-10-26 21:49:55 +03:00
cmdindex = 0
2022-10-26 21:28:55 +03:00
camid = '0'
category = args[cmdindex]
## Quick commands start
if category == 'now':
await self.get_snapshot(camid, bot, room, event)
return
## Quick commands end
if category not in self.allowed_cmds:
2022-10-26 21:28:55 +03:00
await bot.send_text(room, f'Unknown category: "{args[1]}"', event)
return
2022-10-26 21:49:55 +03:00
cmdindex = cmdindex + 1
if args[cmdindex] not in self.allowed_cmds[category]:
await bot.send_text(room, f'Unknown command: "{args[cmdindex]}"', event)
2022-10-26 21:28:55 +03:00
return
2022-10-26 21:49:55 +03:00
command = args[cmdindex]
2022-10-26 21:28:55 +03:00
req_url = f'{self.motionurl}/{camid}/{category}/{command}'
if command in self.restricted_cmds:
bot.must_be_owner(event)
2022-10-26 21:49:55 +03:00
if category == 'config' and command == 'get':
queryparam = args[cmdindex + 1]
req_url = f'{req_url}?query={queryparam}'
elif category == 'config' and command == 'set':
param = args[cmdindex + 1]
value = args[cmdindex + 2]
req_url = f'{req_url}?{param}={value}'
if camid != 0 and command == 'snapshot':
await self.get_snapshot(camid, bot, room, event)
2022-10-26 21:28:55 +03:00
resp = requests.get(req_url).text
await bot.send_text(room, resp, event)
2022-10-23 21:17:02 +03:00
async def get_snapshot(self, camid, bot, room, event):
imgurl = f"{self.motionurl.replace(':8080',':8081')}/{camid}/current"
self.logger.info(f"Fetching image from {imgurl}")
2022-11-01 22:25:54 +02:00
await bot.upload_and_send_image(room, imgurl, event, no_cache=True)
2022-10-23 21:17:02 +03:00
def help(self):
return self.helptext.splitlines()[0]