Configurable URL for Motion API

This commit is contained in:
Jarno Rankinen 2022-10-26 21:28:55 +03:00
parent d844e8ccb5
commit e9a2f9dca0
1 changed files with 42 additions and 20 deletions

View File

@ -17,12 +17,23 @@ class MatrixModule(BotModule):
- config list|set|get|write - config list|set|get|write
- detection status|connection|start|pause - detection status|connection|start|pause
- action eventstart|eventend|snapshot|restart|quit|end - action eventstart|eventend|snapshot|restart|quit|end
- url get|set <motionurl>
Usage: '!cam <id> category command' Usage: '!cam <id> category command'
<id> is the numerical id of the camera. Use 0 for all cameras. <id> is the numerical id of the camera. Use 0 for all cameras.
If <id> is omitted, 0 is assumed.""" If <id> is omitted, 0 is assumed."""
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']
async def matrix_message(self, bot, room, event): async def matrix_message(self, bot, room, event):
args = event.body.split() args = event.body.split()
args.pop(0) args.pop(0)
@ -30,26 +41,37 @@ class MatrixModule(BotModule):
await bot.send_text(room, self.helptext, event) await bot.send_text(room, self.helptext, event)
return return
recvd_camid = 1 elif args[0] == 'url':
try: if args[1] == 'set':
# Check if first argument is numeric (camera id) newurl = args[2]
camid = int(args[0]) bot.must_be_owner(event)
camid = str(camid) self.motionurl = newurl
except ValueError: bot.save_settings()
recvd_camid = 0 await bot.send_text(room, f"Motion API URL set to {self.motionurl}")
camid = '0' elif args[1] == 'get':
if args[recvd_camid] not in self.allowed_cmds: await bot.send_text(room, f"Motion URL is currently {self.motionurl}")
await bot.send_text(room, f'Unknown category: "{args[1]}"', event)
return else:
category = args[recvd_camid] recvd_camid = 1
recvd_camid = recvd_camid + 1 try:
if args[recvd_camid] not in self.allowed_cmds[category]: # Check if first argument is numeric (camera id)
await bot.send_text(room, f'Unknown command: "{args[recvd_camid]}"', event) camid = int(args[0])
return camid = str(camid)
command = args[recvd_camid] except ValueError:
req_url = f'{self.motionurl}/{camid}/{category}/{command}' recvd_camid = 0
resp = requests.get(req_url).text camid = '0'
await bot.send_text(room, resp, event) 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): def help(self):
return self.helptext.splitlines()[0] return self.helptext.splitlines()[0]