def on_mqtt_message (client, userdata, message): try: jstatus = extract_status (message) if (jstatus is not None): # TODO: should you have a costly exception setup in mqtt thread? socketio.emit (SERVER_EVENT, jstatus) # move this to a worker thread except Exception as e: print ('* EXCEPTION 3: ', str(e))
def set_device_status(): try: if (request.method=='GET'): jcmd = { 'device_id' : request.args.get('device_id'), 'relsen_id': request.args.get('relsen_id'), 'action' : request.args.get('action') } else: # it can only be a 'POST' if (request.json is None): return ({'result' : False, 'error':'invalid device'}) jcmd = request.json print (jcmd) socketio.emit (ACK_EVENT, jcmd) # must be a json object, to avoid messy client side escape characters topic = '{}/{}/{}'.format (PUB_PREFIX, jcmd['device_id'], jcmd['relsen_id']) if SIMULATION_MODE: devid = jcmd['device_id'] if (devid not in simul_status): return ({'result' : False, 'error' : 'invalid or disabled device_id'}) operate_simul_device (devid, jcmd['relsen_id'], jcmd['action'].lower()) # this will return the new status retval = {devid : simul_status[devid] } else: mqtt.publish (topic, jcmd['action']) if in_mem_status is None: send_tracer_broadcast() # to build the status return {'result' : False, 'error' : 'in-memory status is not available; please try again'} if (devid not in in_mem_status): return ({'result' : False, 'error' : 'invalid or disabled device_id'}) retval = {devid : in_mem_status[devid]} # this may only return the last known status ** return retval except Exception as e: print ('* EXCEPTION 7: ', str(e)) return ({'result' : False, 'error' : 'could not operate device'})
def on_socket_message (message): print ('pass-through message: ', message) jcmd = json.loads (message) try: mqtt.publish (jcmd.get('topic'), jcmd.get('payload')) socketio.emit (ACK_EVENT, jcmd) # must be a json object, to avoid messy client side escape characters except Exception as e: print ('* EXCEPTION 6: ', str(e))
def update_sensor_reading (device_id, str_msg): # TODO: save it in in-memory status (and later, in the database) #try: dprint ('sensor reading for: ', device_id) dprint (str_msg) jsensor = json.loads(str_msg) in_mem_status[devid][SENSOR_RELSEN] = jsensor['StatusSNS'] # this is stored as an inner json jstatus = {'device_id' : device_id, 'relsen_id' : SENSOR_RELSEN, 'status' : jsensor['StatusSNS']} socketio.emit (SERVER_EVENT, jstatus)
def on_mqtt_message(client, userdata, message): #print ("MQTT msg: ", message.payload.decode()) jstatus = extract_status(message) if (jstatus is not None): try: socketio.emit(SERVER_EVENT, jstatus) except Exception as e: print('* EXCEPTION 3: ', str(e))
def operate_simul_device(devid, relsid, action): new_status = ON if (action.upper() == 'TOGGLE'): if (simul_status[devid][relsid] == ON): new_status = OFF else: new_status = action.upper() # it was 'on' or 'off' command simul_status[devid][relsid] = new_status jstatus = {'device_id': devid, 'relsen_id': relsid, 'status': new_status} socketio.emit(SERVER_EVENT, jstatus)
def send_simul_status(): # to start a new socket client in the correct status in simulation mode if not SIMULATION_MODE: print ('Not in simulation mode: cannot simulate status') return #dprint ('sending simulated initial status...') for devid in simul_status: jstatus = {'device_id': devid} for rsid in simul_status[devid]: jstatus['relsen_id'] = rsid jstatus['status'] = simul_status[devid][rsid] socketio.emit (SERVER_EVENT, jstatus)
def save_sensor_reading(device_id, str_msg): # TODO: save it in in-memory status #try: print('sensor reading: ', device_id, str_msg) jsensor = json.loads(str_msg) jstatus = { 'device_id': device_id, 'relsen_id': SENSOR_RELSEN, 'status': jsensor['StatusSNS'] } socketio.emit(SERVER_EVENT, jstatus)
def on_socket_event (payload): print ('command: ', payload) jcmd = json.loads (payload) try: socketio.emit (ACK_EVENT, jcmd) # must be a json object, to avoid messy client side escape characters topic = '{}/{}/{}'.format (PUB_PREFIX, jcmd['device_id'], jcmd['relsen_id']) if SIMULATION_MODE: operate_simul_device (jcmd['device_id'], jcmd['relsen_id'], jcmd['action'].lower()) else: mqtt.publish (topic, jcmd['action']) except Exception as e: print ('* EXCEPTION 5: ', str(e))
def send_offline_notification(devid): print('sending offline notification for: ', devid) for rs in in_mem_relsens[devid]: msg = {'device_id': devid, 'relsen_id': rs, 'status': OFFLINE} socketio.emit(SERVER_EVENT, msg)