def ask_all_sensors(remote_devices_id, status, url, filename, lock): lock.acquire() local_xbee = ZigBeeDevice(PORT, BAUD_RATE) message = {'act': 1} data_string = json.dumps(message) measures = defaultdict(list) try: local_xbee.open() xbee_network = local_xbee.get_network() for xbee_id in remote_devices_id: remote_xbee = xbee_network.discover_device(xbee_id) if remote_xbee is None: print('No se ha podido encontrar el dispositivo remoto') continue print('Enviando datos asincronamente %s >> %s' % (remote_xbee.get_64bit_addr(), data_string)) local_xbee.send_data(remote_xbee, data_string) xbee_message = local_xbee.read_data(3) measure = xbee_message.data.decode() print('Received message from %s: %s' % (xbee_message.remote_device.get_64bit_addr(), measure)) json_measure = json.loads(measure) json_measure['timestamp'] = str(datetime.utcnow()) measures[xbee_id].append(json_measure) #if connected sends the file, if not, keeps it write_to_file(filename, status, measures, url) finally: if local_xbee is not None and local_xbee.is_open(): local_xbee.close() lock.release()
def ask_sensor(data, message_callback, lock): message = data['message'] data_string = json.dumps(message) REMOTE_NODE_ID = data['sensor'] lock.acquire() local_xbee = ZigBeeDevice(PORT, BAUD_RATE) try: local_xbee.open() xbee_network = local_xbee.get_network() remote_xbee = xbee_network.discover_device(REMOTE_NODE_ID) if remote_xbee is None: print('No se ha podido encontrar el dispositivo remoto') exit(1) print('Enviando datos asincronamente %s >> %s' % (remote_xbee.get_64bit_addr(), data_string)) local_xbee.send_data(remote_xbee, data_string) xbee_message = local_xbee.read_data(3) print('Received message from %s: %s' % (xbee_message.remote_device.get_64bit_addr(), xbee_message.data.decode())) sensor_data = { 'sender': 'section 2', 'receiver': data['sender'], 'message': json.loads(xbee_message.data.decode()) } message_callback(sensor_data) finally: if local_xbee is not None and local_xbee.is_open(): local_xbee.close() lock.release()