def onMessageEvent(self, message, topics, data): if topics[1] == "add": # Try grab the sensor from the DB try: sensor = Sensor.objects.get(sensor=topics[-1]) except Exception as e: sensor = None if sensor is None: # Create sensor in event db and add event sensor = Sensor() sensor.sensor = topics[-1] event = Event() event.event_type = data['event-type'] if data.get('target') is not None: event.target = data['target'] event.logic = data['logic'] event.logic_value = data['logic-value'] if data.get('warning-message') is not None: event.warning_message = data['warning-message'] if data.get('actuation-type') is not None: event.actuation_type = data['actuation-type'] if data.get('actuation-state') is not None: event.actuation_state = data['actuation-state'] if data.get('time') is not None: event.time = data['time'] sensor.events.append(event) try: sensor.save() except Exception as e: return print("Couldn't save to event db: " + str(e)) return print("Event saved to db") elif topics[1] == "delete": try: sensor = Sensor.objects.get(sensor=topics[-1]) except Exception as e: sensor = None if sensor is None: # Create sensor in event db and add event return print("Sensor not found") notfound = True # Index the wanted event for idx, eventidx in enumerate(sensor.events): if str(eventidx['uuid']) == data['uuid']: notfound = False sensor.events.pop(idx) if notfound is True: return print("Event couldn't be deleted, not found") try: sensor.save() except Exception as e: return print("Error saving deletion to db" + str(e)) return print("Event " + data['uuid'] + " deleted") else: return print("Channel " + message.topic + " not handled")