#!/usr/bin/env python3 """Websocket events.""" import re import types import models from models import network async def toggle_outlet(request, ws, data): """Toggles the state of a RelayDevice.""" device_id = data.get('device_id') sub_device_id = data.get('sub_device_id') data = {} data['device_id'] = device_id data['sub_device_id'] = sub_device_id device = network.find(device_id) if not device: data['ok'] = False data['error'] = "device_id not found" await ws.send_json(data) return state = device.toggle(sub_device_id) if state is None: data['ok'] = False data['error'] = "sub_device_id not found" await ws.send_json(data) return models.save_network(network) data['ok'] = True data['state'] = state res = {'event': 'toggle_outlet', 'data': data} await request.app.send_json_all(res) async def edit_field(request, 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') data = {} data['device_id'] = device_id if sub_device_id: data['sub_device_id'] = sub_device_id data['field'] = field data['value'] = value device = network.find(device_id) if not device: data['ok'] = False data['error'] = "device_id not found" await ws.send_json(data) return if device.locked: data['ok'] = False data['error'] = "device is locked for editing" await ws.send_json(data) return if sub_device_id: sub_device = device.find(sub_device_id) if not sub_device: data['ok'] = False data['error'] = "sub_device_id not found" await ws.send_json(data) return if hasattr(sub_device, field): setattr(sub_device, field, value) else: data['ok'] = False data['error'] = "sub_device field not found" await ws.send_json(data) return else: if hasattr(device, field): setattr(device, field, value) else: data['ok'] = False data['error'] = "device field not found" await ws.send_json(data) return models.save_network(network) data['ok'] = True res = {'event': 'edit_field', 'data': data} await request.app.send_json_all(res) async def new_device(request, ws, data): """ Allows adding a new device. Accepts device_type parameter, returns the device_id. """ device_type = data.get('device_type') data = {} data['device_type'] = device_type if device_type == 'RelayDevice': device = models.RelayDevice() else: data['ok'] = False data['error'] = "unknown device type" await ws.send_json(data) 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) data['ok'] = True data['device_id'] = device.id res = {'event': 'new_device', 'data': data} await request.app.send_json_all(res) async def lock_device(request, ws, data): """Locks or unlocks a device to prevent or allow editing it's fields.""" device_id = data.get('device_id') locked = bool(data.get('locked')) data = {} data['device_id'] = device_id data['locked'] = locked device = network.find(device_id) if not device: data['ok'] = False data['error'] = "device_id not found" await ws.send_json(data) return device.locked = locked models.save_network(network) data['ok'] = True res = {'event': 'lock_device', 'data': data} await request.app.send_json_all(res) async def delete_device(request, ws, data): """Deletes a device.""" device_id = data.get('device_id') data = {} data['device_id'] = device_id device = network.find(device_id) if not device: data['ok'] = False data['error'] = "device_id not found" await ws.send_json(data) return network.remove(device) models.save_network(network) data['ok'] = True res = {'event': 'delete_device', 'data': data} await request.app.send_json_all(res) events = {} for obj in dir(): if type(locals()[obj]) == types.FunctionType: events[locals()[obj].__name__] = locals()[obj]