def ping_relsens(device_id): if SIMULATION_MODE: dprint('In simulation mode: not pinging the relays') return dprint('\nPinging relsens in the device: ', device_id) topic = '{}/{}/{}'.format(PUB_PREFIX, device_id, BROADCAST_RELSEN) mqtt.publish(topic, EMPTY_PAYLOAD)
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 send_timer_command(device_id, relay_num, timer_id, time_pair, repeat): suffix = 'Timer' + str(timer_id) # timer_id can be from 1 to 16 topic = '{}/{}/{}'.format(PUB_PREFIX, device_id, suffix) pl = { "Enable": 1, "Mode": 0, "Time": time_pair[0], "Window": 0, "Days": "1111111", "Repeat": repeat, "Output": relay_num, "Action": 1 } # ON payload = json.dumps(pl) dprint(topic, payload) mqtt.publish(topic, payload) # OFF timer for the same relay: suffix = 'Timer' + str(timer_id + 1) topic = '{}/{}/{}'.format(PUB_PREFIX, device_id, suffix) pl = { "Enable": 1, "Mode": 0, "Time": time_pair[1], "Window": 0, "Days": "1111111", "Repeat": repeat, "Output": relay_num, "Action": 0 } # OFF payload = json.dumps(pl) dprint(topic, payload) mqtt.publish(topic, payload)
def ping_device(device_id): if SIMULATION_MODE: dprint('In simulation mode: not pinging the device') return dprint('\nPinging the device: ', device_id) topic = '{}/{}/{}'.format(PUB_PREFIX, device_id, PUB_SUFFIX) # POWER dprint(topic, ' (blank)') mqtt.publish(topic, EMPTY_PAYLOAD)
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 ping_mqtt(): if SIMULATION_MODE: dprint ('In simulation mode: not pinging MQTT devices') return dprint ('\nPinging all devices...') topic = '{}/{}/{}'.format (PUB_PREFIX, BROADCAST_DEVICE, PUB_SUFFIX) # POWER #dprint (topic, ' (blank)') mqtt.publish (topic, EMPTY_PAYLOAD)
def send_tracer_broadcast(): if SIMULATION_MODE: dprint('In simulation mode: not sending tracer') return topic = '{}/{}/{}'.format(PUB_PREFIX, BROADCAST_DEVICE, BROADCAST_RELSEN) # POWER0 dprint('Sending probe to: ', topic) mqtt.publish(topic, EMPTY_PAYLOAD) # empty payload gets the relay status
def send_tracer_broadcast(): global sensor_count if SIMULATION_MODE: dprint ('In simulation mode: not sending tracer') return topic = '{}/{}/{}'.format (PUB_PREFIX, BROADCAST_DEVICE, BROADCAST_RELSEN) # POWER0 dprint ('Sending probe to: ',topic) mqtt.publish (topic, EMPTY_PAYLOAD) # empty payload gets the relay status sensor_count = (sensor_count+1) % SENSOR_INTERVAL if (sensor_count==0): request_sensor_reading(BROADCAST_DEVICE)
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 clear_timers (device_id, relsen_id): dprint ('Clearing all timers for: {}/{}'.format (device_id, relsen_id)) relay = 1 if (relsen_id == 'POWER'): # the device has a single relay relay = 1 # redundant, but for clarity else: relay = relsen_id[-1] # ASSUMPTION: relsen id is in the form POWER1, POWER2 etc. *** if relay < '0' or relay > '4': print ('relsen_id has to be from POWER1 to POWER4 only') return False relay_num = int(relay) # relay number 1 to 4 within the device starting_timer_id = (relay_num-1)*4 + 1 for i in range (0, MAX_TIMERS): relsen = 'Timer'+ str(starting_timer_id+i) topic = '{}/{}/{}'.format (PUB_PREFIX, device_id, relsen) payload = json.dumps({"Enable":0}) dprint (topic, payload) mqtt.publish (topic, payload) return True
def request_network_params(device_id): dprint('\nRequesting network params..') topic = '{}/{}/{}'.format(PUB_PREFIX, device_id, IPMAC_SUFFIX) # cmnd/device_id/status dprint(topic, IPMAC_PAYLOAD) # '5' mqtt.publish(topic, IPMAC_PAYLOAD)
def request_sensor_reading(device_id): dprint('\nTriggering sensor reading..') topic = '{}/{}/{}'.format(PUB_PREFIX, device_id, SENSOR_SUFFIX) # cmnd/device_id/status dprint(topic, SENSOR_PAYLOAD) # '10' mqtt.publish(topic, SENSOR_PAYLOAD)