#!/usr/bin/env python3 """Websocket events.""" import types import models from models import network async def toggle_outlet(ws, data): """Toggles the state of a RelayDevice.""" device_id = data.get('device_id') sub_device_id = data.get('sub_device_id') device = network.find(device_id) if not device: await ws.send_json({'ok': False, 'message': "device_id not found"}) return state = device.toggle(sub_device_id) if state is None: await ws.send_json({'ok': False, 'message': "sub_device_id not found"}) return models.save_network(network) data = {} data['ok'] = True data['device_id'] = device_id data['sub_device_id'] = sub_device_id data['state'] = state res = {'event': 'toggle_outlet', 'data': data} await ws.send_json(res) async def edit(ws, data): """Edits the text of a particular field.""" device_id = data.get('device_id') sub_device_id = data.get('sub_device_id') field = data.get('field') value = data.get('value') device = network.find(device_id) if not device: await ws.send_json({'ok': False, 'message': "device_id not found"}) return if device.locked: await ws.send_json({'ok': False, 'message': "device is locked for editing"}) return if sub_device_id: sub_device = device.find(sub_device_id) if not sub_device: await ws.send_json({'ok': False, 'message': "sub_device_id not found"}) return if hasattr(sub_device, field): setattr(sub_device, field, value) else: await ws.send_json({'ok': False, 'message': "sub_device field not found"}) return else: if hasattr(device, field): setattr(device, field, value) else: await ws.send_json({'ok': False, 'message': "device field not found"}) return res = { 'ok': True, 'field': field, 'value': value } models.save_network(network) await ws.send_json(res) async def new_device(ws, data): """ Allows adding a new device. Accepts device_type parameter, returns the device_id. """ device_type = data.get('device_type') if device_type == 'RelayDevice': device = models.RelayDevice() else: await ws.send_json({'ok': False, 'message': "unknown device type"}) return devices = [dev for dev in network if dev.type == device_type] devices.sort(key=lambda dev: dev.id) if not devices: device.id = device_type + '01' else: num = re.search(r'(\d*)$', devices[-1].id).groups() if not num: device.id = device_type + '01' else: num = str(int(num[0]) + 1).zfill(2) device.id = device_type + num network.append(device) models.save_network(network) res = {'ok': True, 'device_id': device.id} await ws.send_json(res) async def lock_device(ws, data): """Locks or unlocks a device to prevent or allow editing it's fields.""" device_id = data.get('device_id') locked = data.get('locked') == 'true' device = network.find(device_id) if not device: await ws.send_json({'ok': False, 'message': "device_id not found"}) return if locked: device.locked = True else: device.locked = False models.save_network(network) await ws.send_json({'ok': True, 'device_id': device.id, 'locked': device.locked}) async def delete(ws, data): """Deletes a device.""" device_id = data.get('device_id') device = network.find(device_id) if not device: await ws.send_json({'ok': False, 'message': "device_id not found"}) return network.remove(device) models.save_network(network) await ws.send_json({'ok': True}) events = {} for obj in dir(): if type(locals()[obj]) == types.FunctionType: events[locals()[obj].__name__] = locals()[obj]