def plugin_poll(handle): _LOGGER.debug("coral-enviro plugin_poll") global _enviro try: time_stamp = utils.local_timestamp() readings = {} if handle['temperatureSensor']['value'] == 'true': readings['temperature'] = _enviro.temperature if handle['pressureSensor']['value'] == 'true': readings['pressure'] = _enviro.pressure if handle['humiditySensor']['value'] == 'true': readings['humidity'] = _enviro.humidity if handle['ambientLightSensor']['value'] == 'true': readings['ambient_light'] = _enviro.ambient_light if handle['groveAnalogSensor']['value'] == 'true': readings['grove_analog'] = _enviro.grove_analog wrapper = { 'asset': handle['assetName']['value'], 'timestamp': time_stamp, 'readings': readings } return wrapper except Exception as ex: _LOGGER.exception("coral-enviro exception: {}".format(str(ex))) raise exceptions.DataRetrievalError(ex)
def plugin_poll(handle): """ Extracts data from the sensor and returns it in a JSON document as a Python dict. Available for poll mode only. Args: handle: handle returned by the plugin initialisation call Returns: returns a sensor reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: Exception """ try: time_stamp = utils.local_timestamp() mean_value, max_value = generate_data(ATTRIBUTE_NAME) data = { 'asset': handle['assetName']['value'], 'timestamp': time_stamp, 'key': str(uuid.uuid4()), 'readings': { "mean_value": mean_value, "max_value": max_value } } except (Exception, RuntimeError) as ex: _LOGGER.exception("Pandas CSV Reader exception: {}".format(str(ex))) raise ex else: return data
def plugin_poll(handle): """ Poll readings from the modbus device and returns it in a JSON document as a Python dict. Available for poll mode only. Args: handle: handle returned by the plugin initialisation call Returns: returns a reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: DataRetrievalError """ try: readings = get_readings(handle) wrapper = { 'asset': handle['assetName']['value'], 'timestamp': utils.local_timestamp(), 'key': str(uuid.uuid4()), 'readings': readings } except Exception as ex: raise exceptions.DataRetrievalError(ex) else: return wrapper
def plugin_poll(handle): """ Extracts data from the sensor and returns it in a JSON document as a Python dict. Available for poll mode only. Args: handle: handle returned by the plugin initialisation call Returns: returns a sensor reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: Exception """ try: time_stamp = utils.local_timestamp() data = { 'asset': handle['assetName']['value'], 'timestamp': time_stamp, 'readings': { "sinusoid": next(generate_data()) } } except (Exception, RuntimeError) as ex: _LOGGER.exception("Sinusoid exception: {}".format(str(ex))) raise ex else: return data
def fetch(self): try: conn = http.client.HTTPConnection(self.url) conn.request('GET', '/data/2.5/weather?q={}&APPID={}'.format(self.city, self.appid)) r = conn.getresponse() res = r.read().decode() conn.close() if r.status != 200: raise ValueError(res) jdoc = json.loads(res) reads = { 'city': jdoc['name'], 'wind_speed': jdoc['wind']['speed'], 'clouds': jdoc['clouds']['all'], 'temperature': jdoc['main']['temp'], 'pressure': jdoc['main']['pressure'], 'humidity': jdoc['main']['humidity'], 'visibility': jdoc['visibility'] } data = { 'asset': self.asset_name, 'timestamp': utils.local_timestamp(), 'readings': reads } async_ingest.ingest_callback(c_callback, c_ingest_ref, data) except ValueError as ex: err = "Unable to fetch information from api.openweathermap: {}".format(str(ex)) _LOGGER.error(err)
def plugin_poll(handle): """ Extracts data from the sensor and returns it in a JSON document as a Python dict. Available for poll mode only. Args: handle: handle returned by the plugin initialisation call Returns: returns a sensor reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: Exception """ probes = handle['probes'] data = list() try: for probe in probes: temperature = probe.readTemp() data.append({ 'asset': '{}temperature{}'.format(handle['assetNamePrefix']['value'], probe.csPin), 'timestamp': utils.local_timestamp(), 'readings': { "temperature": temperature } }) except (Exception, RuntimeError) as ex: _LOGGER.exception("PT100 exception: {}".format(str(ex))) raise ex else: _LOGGER.debug("PT100 reading: {}".format(json.dumps(data))) return data
def plugin_poll(handle): """ Extracts data from the sensor and returns it in a JSON document as a Python dict. Available for poll mode only. Args: handle: handle returned by the plugin initialisation call Returns: returns a sensor reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: DataRetrievalError """ try: humidity, temperature = Adafruit_DHT.read_retry( Adafruit_DHT.DHT11, handle['gpioPin']['value']) if humidity is not None and temperature is not None: time_stamp = utils.local_timestamp() readings = {'temperature': temperature, 'humidity': humidity} wrapper = { 'asset': handle['assetName']['value'], 'timestamp': time_stamp, 'readings': readings } else: raise exceptions.DataRetrievalError except Exception: raise else: return wrapper
async def save(self, msg): """Store msg content to Fledge """ # TODO: string and other types? payload_json = json.loads(msg.payload.decode('utf-8')) _LOGGER.debug("Ingesting %s on topic %s", payload_json, str(msg.topic)) data = { 'asset': self.asset, 'timestamp': utils.local_timestamp(), 'readings': payload_json } async_ingest.ingest_callback(c_callback, c_ingest_ref, data)
def construct_readings(objs): """ Takes the detection results from the model and convert into readings suitable to insert into database. For Example Lets say a single person is detected then there will be a single element in the array whose contents will be {'label': 'person', 'score': 64, # probability of prediction 'bounding_box': [xmin, ymin, xmax, ymax] # bounding box coordinates } A reading will be constructed in the form given below : reads = { 'person_' + '1' + '_' + 'label': 'person' 'person_' + '1' + '_' + 'score': 64 'person_' + '1' + '_' + 'x1': xmin 'person_' + '1' + '_' + 'y1': ymin 'person_' + '1' + '_' + 'x2': xmax 'person_' + '1' + '_' + 'y2': ymax 'count': 1 } Args: x -> an array of detection results Returns: Readings to be inserted into database. Raises: None """ global asset_name reads = {} for r_index in range(len(objs)): reads['person_' + str(r_index + 1) + '_' + 'label'] = objs[r_index]['label'] reads['person_' + str(r_index + 1) + '_' + 'score'] = objs[r_index]['score'] reads['person_' + str(r_index + 1) + '_' + 'x1'] = objs[r_index]['bounding_box'][0] reads['person_' + str(r_index + 1) + '_' + 'y1'] = objs[r_index]['bounding_box'][1] reads['person_' + str(r_index + 1) + '_' + 'x2'] = objs[r_index]['bounding_box'][2] reads['person_' + str(r_index + 1) + '_' + 'y2'] = objs[r_index]['bounding_box'][3] reads['count'] = len(objs) data = { 'asset': asset_name, 'timestamp': utils.local_timestamp(), 'readings': reads } return data
def plugin_poll(handle): """ Extracts data from the sensor and returns it in a JSON document as a Python dict. Available for poll mode only. Args: handle: handle returned by the plugin initialisation call Returns: returns a sensor reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: Exception """ try: if handle['lastValue'] is None: new = randint(int(handle['minValue']['value']), int(handle['maxValue']['value'])) else: new = handle['lastValue'] + randint(-1, 1) if new > int(handle['maxValue']['value']): new = int(handle['maxValue']['value']) elif new < int(handle['minValue']['value']): new = int(handle['minValue']['value']) time_stamp = utils.local_timestamp() data = { 'asset': handle['assetName']['value'], 'timestamp': time_stamp, 'readings': { "randomwalk": new } } handle['lastValue'] = new except (Exception, RuntimeError) as ex: _LOGGER.exception("RandomWalk exception: {}".format(str(ex))) raise ex else: return data
def on_message(client, userdata, msg): global _callback_event_loop try: inbound_payload = sparkplug_b_pb2.Payload() inbound_payload.ParseFromString(msg.payload) time_stamp = utils.local_timestamp() if _callback_event_loop is None: _LOGGER.debug("Message processing event doesn't yet exist - creating new event loop.") asyncio.set_event_loop(asyncio.new_event_loop()) _callback_event_loop = asyncio.get_event_loop() for metric in inbound_payload.metrics: data = { 'asset': metric.name, 'timestamp': time_stamp, # metric.timestamp 'readings': { "value": metric.float_value, } } _callback_event_loop.run_until_complete(save_data(data)) except Exception as e: _LOGGER.error(e)
def plugin_poll(handle): """ Poll readings from the modbus device and returns it in a JSON document as a Python dict. Available for poll mode only. Args: handle: handle returned by the plugin initialisation call Returns: returns a reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: """ try: global mbus_client if mbus_client is None: try: source_address = handle['address']['value'] source_port = int(handle['port']['value']) except Exception as ex: e_msg = 'Failed to parse Modbus TCP address and / or port configuration.' _LOGGER.error('%s %s', e_msg, str(ex)) raise ValueError(e_msg) try: mbus_client = ModbusTcpClient(host=source_address, port=source_port) mbus_client_connected = mbus_client.connect() if mbus_client_connected: _LOGGER.info('Modbus TCP Client is connected. %s:%d', source_address, source_port) else: raise RuntimeError("Modbus TCP Connection failed!") except: mbus_client = None _LOGGER.warn( 'Failed to connect! Modbus TCP host %s on port %d', source_address, source_port) return """ read_coils(self, address, count=1, **kwargs) read_discrete_inputs(self, address, count=1, **kwargs) read_holding_registers(self, address, count=1, **kwargs) read_input_registers(self, address, count=1, **kwargs) - address: The starting address to read from - count: The number of coils / discrete or registers to read - unit: The slave unit this request is targeting On TCP/IP, the MODBUS server is addressed using its IP address; therefore, the MODBUS Unit Identifier is useless. Remark : The value 0 is also accepted to communicate directly to a MODBUS TCP device. """ unit_id = UNIT modbus_map = json.loads(handle['map']['value']) readings = {} # Read coils coils_address_info = modbus_map['coils'] if len(coils_address_info) > 0: for k, address in coils_address_info.items(): coil_bit_values = mbus_client.read_coils(int(address), 1, unit=unit_id) if coil_bit_values is None: _LOGGER.error('Failed to read coil %d', address) else: readings.update({k: coil_bit_values.bits[0]}) # Discrete input discrete_input_info = modbus_map['inputs'] if len(discrete_input_info) > 0: for k, address in discrete_input_info.items(): read_discrete_inputs = mbus_client.read_discrete_inputs( int(address), 1, unit=unit_id) if read_discrete_inputs is None: _LOGGER.error('Failed to read input %d', address) else: readings.update({k: read_discrete_inputs.bits[0]}) # Holding registers holding_registers_info = modbus_map['registers'] if len(holding_registers_info) > 0: for k, address in holding_registers_info.items(): register_values = mbus_client.read_holding_registers( int(address), 1, unit=unit_id) if register_values is None: _LOGGER.error('Failed to read holding register %d', address) else: readings.update({k: register_values.registers[0]}) # Read input registers input_registers_info = modbus_map['inputRegisters'] if len(input_registers_info) > 0: for k, address in input_registers_info.items(): read_input_reg = mbus_client.read_input_registers(int(address), 1, unit=unit_id) if read_input_reg is None: _LOGGER.error('Failed to read input register %d', address) else: readings.update({k: read_input_reg.registers[0]}) wrapper = { 'asset': handle['assetName']['value'], 'timestamp': utils.local_timestamp(), 'readings': readings } except Exception as ex: _LOGGER.error('Failed to read data from modbus device. Got error %s', str(ex)) raise ex else: return wrapper
async def render_post(request): """Store sensor readings from SensorPhone to Fledge Args: request: The payload decodes to JSON similar to the following: .. code-block:: python { "mode" : "sync", "messages" : [ { "audio": 0.0005980864, "device": "iOS Device", "altitude": 30.71550178527832, "latitude": 51.55842144498452, "longitude": -0.8672407515484514, "timestamp": 1514597142, "gyroscopex": -0.005210537929087877, "gyroscopey": -0.02013654075562954, "gyroscopez": -0.005803442094475031, "accelerometerx": 0.0119628909160383, "accelerometery": -0.7869872764804313, "accelerometerz": -0.6097259288653731} ] } """ # TODO: Decide upon the correct format of message message = {'result': 'success'} code = web.HTTPOk.status_code try: payload = await request.json() asset = 'SensorPhone' timestamp = utils.local_timestamp() messages = payload.get('messages') if not isinstance(messages, list): raise ValueError('messages must be a list') for readings in messages: data = { 'asset': asset, 'timestamp': timestamp, 'readings': readings } async_ingest.ingest_callback(c_callback, c_ingest_ref, data) except (ValueError, TypeError) as e: code = web.HTTPBadRequest.status_code message = {'error': str(e)} _LOGGER.exception(str(e)) except Exception as e: code = web.HTTPInternalServerError.status_code message = {'error': str(e)} _LOGGER.exception(str(e)) # expect keys in response: # (code = 2xx) result Or busy # (code = 4xx, 5xx) error message['status'] = code return web.json_response(message)
def plugin_poll(handle): """ Poll readings from the s7 device and returns it in a JSON document as a Python dict. Available for poll mode only. Args: handle: handle returned by the plugin initialisation call Returns: returns a reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: """ try: global client if client is None: try: host = handle['host']['value'] port = int(handle['port']['value']) rack = int(handle['rack']['value']) slot = int(handle['slot']['value']) except Exception as ex: e_msg = 'Failed to parse S7 TCP host address and / or port configuration.' _LOGGER.error('%s %s', e_msg, str(ex)) raise ValueError(e_msg) try: client = snap7.client.Client() client.connect(host, rack, slot, port) client_connected = client.get_connected() if client_connected: _LOGGER.info('S7 TCP Client is connected. %s:%d', host, port) else: raise RuntimeError("S7 TCP Connection failed!") except: client = None _LOGGER.warn( 'Failed to connect! S7 TCP host %s on port %d, rack %d and slot %d ', host, port, rack, slot) return unit_id = UNIT s7_map = json.loads(handle['map']['value']) db = s7_map['DB'] readings = {} if len(db.keys()) > 0: for dbnumber, variable in db.items(): if len(variable.keys()) > 0: a = [] for index, item in variable.items(): byte_index = int(index.split('.')[0]) a.append( [byte_index, byte_index + get_type_size(item) - 1]) _LOGGER.debug("union_range(a): %s", str(union_range(a))) for start, end in union_range(a): size = end - start + 1 _LOGGER.debug( "DEBUG: dbnumber: %s start: %s, end: %s, size: %s", str(dbnumber), str(start), str(end), str(size)) try: buffer_ = client.read_area(snap7.types.Areas.DB, int(dbnumber), start, size) for index, item in variable.items(): byte_index, bool_index = get_byte_and_bool_index( index) if start <= byte_index and byte_index <= end: _LOGGER.debug( "DEBUG: byte_index - start: %d, byte_index: %d, start: %d, bool_index: %d, type: %s", byte_index - start, byte_index, start, bool_index, item['type']) data = get_value(buffer_, byte_index - start, item, bool_index) if data is None: _LOGGER.error( 'Failed to read DB: %s index: %s name: %s', str(dbnumber), str(index), str(item['name'])) else: if handle["saveAs"]["value"] == "flat": for element in list( walk( data, "DB" + dbnumber + "_" + item['name'])): readings.update(element) elif handle["saveAs"][ "value"] == "escaped": #_LOGGER.warn('No support for escaped JSON currently') # _LOGGER.debug( # 'json.dumps(data)=' + json.dumps(data)) # _LOGGER.debug( # 'json.dumps(json.dumps(data))=' + json.dumps(json.dumps(data))) # readings.update({"DB" + dbnumber + "_" + item['name']: # "[{\\\"Produktionsauftrag\\\": \\\"P12346789\\\", \\\"ProductionId\\\": 6636321}]"}) readings.update({ "DB" + dbnumber + "_" + item['name']: escape_json(json.dumps(data)) }) else: readings.update({ "DB" + dbnumber + "_" + item['name']: data }) except Exception as ex: _LOGGER.error( 'Failed to read area from s7 device: dbnumber: %s start: %s, end: %s, size: %s Got error %s', str(dbnumber), str(start), str(end), str(size), str(ex)) raise ex _LOGGER.debug('DEBUG OUT=' + str(readings)) wrapper = { 'asset': handle['assetName']['value'], 'timestamp': utils.local_timestamp(), 'readings': readings } except Exception as ex: _LOGGER.error('Failed to read data from s7 device. Got error %s', str(ex)) client.disconnect() client = None raise ex else: return wrapper
def plugin_poll(handle): """ Extracts data from the sensor and returns it in a JSON document as a Python dict. Available for poll mode only. Args: handle: handle returned by the plugin initialisation call Returns: returns a sensor reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: Exception """ global _handle, _restart_config bluetooth_adr = _handle['bluetoothAddress']['value'] tag = _handle['tag'] asset_prefix = '{}'.format(_handle['assetNamePrefix']['value']).replace( '%M', bluetooth_adr) try: if not tag.is_connected: raise RuntimeError( "SensorTagCC2650 {} not connected".format(bluetooth_adr)) time_stamp = utils.local_timestamp() data = list() # In this method, cannot use "handle" as it might have changed due to restart. Hence use "_handle". if _handle['temperatureSensor']['value'] == 'true': count = 0 while count < SensorTagCC2650.reading_iterations: object_temp_celsius, ambient_temp_celsius = tag.hex_temp_to_celsius( tag.char_read_hnd( _handle['characteristics']['temperature']['data'] ['handle'], "temperature")) time.sleep(0.5) # wait for a while count = count + 1 data.append({ 'asset': '{}{}'.format(asset_prefix, _handle['temperatureSensorName']['value']), 'timestamp': time_stamp, 'readings': { "object": object_temp_celsius, 'ambient': ambient_temp_celsius } }) if _handle['luminanceSensor']['value'] == 'true': lux_luminance = tag.hex_lux_to_lux( tag.char_read_hnd( _handle['characteristics']['luminance']['data']['handle'], "luminance")) data.append({ 'asset': '{}{}'.format(asset_prefix, _handle['luminanceSensorName']['value']), 'timestamp': time_stamp, 'readings': { "lux": lux_luminance } }) if _handle['humiditySensor']['value'] == 'true': rel_humidity, rel_temperature = tag.hex_humidity_to_rel_humidity( tag.char_read_hnd( _handle['characteristics']['humidity']['data']['handle'], "humidity")) data.append({ 'asset': '{}{}'.format(asset_prefix, _handle['humiditySensorName']['value']), 'timestamp': time_stamp, 'readings': { "humidity": rel_humidity, "temperature": rel_temperature } }) if _handle['pressureSensor']['value'] == 'true': bar_pressure = tag.hex_pressure_to_pressure( tag.char_read_hnd( _handle['characteristics']['pressure']['data']['handle'], "pressure")) data.append({ 'asset': '{}{}'.format(asset_prefix, _handle['pressureSensorName']['value']), 'timestamp': time_stamp, 'readings': { "pressure": bar_pressure } }) if _handle['movementSensor']['value'] == 'true': gyro_x, gyro_y, gyro_z, acc_x, acc_y, acc_z, mag_x, mag_y, mag_z, acc_range = tag.hex_movement_to_movement( tag.char_read_hnd( _handle['characteristics']['movement']['data']['handle'], "movement")) data.append({ 'asset': '{}{}'.format(asset_prefix, _handle['gyroscopeSensorName']['value']), 'timestamp': time_stamp, 'readings': { "x": gyro_x, "y": gyro_y, "z": gyro_z } }) data.append({ 'asset': '{}{}'.format(asset_prefix, _handle['accelerometerSensorName']['value']), 'timestamp': time_stamp, 'readings': { "x": acc_x, "y": acc_y, "z": acc_z } }) data.append({ 'asset': '{}{}'.format(asset_prefix, _handle['magnetometerSensorName']['value']), 'timestamp': time_stamp, 'readings': { "x": mag_x, "y": mag_y, "z": mag_z } }) if _handle['batteryData']['value'] == 'true': battery_level = tag.get_battery_level( tag.char_read_hnd( _handle['characteristics']['battery']['data']['handle'], "battery")) data.append({ 'asset': '{}{}'.format(asset_prefix, _handle['batterySensorName']['value']), 'timestamp': time_stamp, 'readings': { "percentage": battery_level } }) except (Exception, RuntimeError, pexpect.exceptions.TIMEOUT) as ex: _plugin_restart(bluetooth_adr) raise ex return data
def plugin_poll(handle): """ Extracts data from the system info and returns it in a JSON document as a Python dict. Available for async mode only. Args: handle: handle returned by the plugin initialisation call Returns: a system info reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: TimeoutError """ readings = [] def get_subprocess_result(cmd): a = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) outs, errs = a.communicate() if a.returncode != 0: raise OSError('Error in executing command "{}". Error: {}'.format( cmd, errs.decode('utf-8').replace('\n', ''))) d = [b for b in outs.decode('utf-8').split('\n') if b != ''] return d def get_system_info(time_stamp): data = {} # Get hostname hostname = get_subprocess_result(cmd='hostname')[0] insert_reading("hostName", time_stamp, {"hostName": hostname}) # Get platform info platform = get_subprocess_result(cmd='cat /proc/version')[0] insert_reading("platform", time_stamp, {"platform": platform}) # Get uptime uptime_secs = get_subprocess_result(cmd='cat /proc/uptime')[0].split() uptime = { "system_seconds": float(uptime_secs[0].strip()), "idle_processes_seconds": float(uptime_secs[1].strip()), } insert_reading("uptime", time_stamp, uptime) # Get load average line_load = get_subprocess_result(cmd='cat /proc/loadavg')[0].split() load_average = { "overLast1min": float(line_load[0].strip()), "overLast5mins": float(line_load[1].strip()), "overLast15mins": float(line_load[2].strip()) } insert_reading("loadAverage", time_stamp, load_average) # Get processes count tasks_states = get_subprocess_result(cmd="ps -e -o state") processes = { "running": tasks_states.count("R"), "sleeping": tasks_states.count("S") + tasks_states.count("D"), "stopped": tasks_states.count("T") + tasks_states.count("t"), "paging": tasks_states.count("W"), "dead": tasks_states.count("X"), "zombie": tasks_states.count("Z") } insert_reading("processes", time_stamp, processes) # Get CPU usage c3_mpstat = get_subprocess_result(cmd='mpstat') cpu_usage = {} col_heads = c3_mpstat[1].split() # first line is the header row start_index = col_heads.index("CPU") + 1 for line in c3_mpstat[2:]: # second line onwards are value rows col_vals = line.split() for i in range(start_index, len(col_vals)): cpu_usage[col_heads[i].replace("%", "prcntg_")] = float( col_vals[i].strip()) insert_reading("cpuUsage_" + col_vals[start_index - 1], time_stamp, cpu_usage) # Get memory info c3_mem = get_subprocess_result(cmd='cat /proc/meminfo') mem_info = {} for line in c3_mem: line_a = line.split(':') line_vals = line_a[1].split() k = "{}{}".format(line_a[0], '_KB' if len(line_vals) > 1 else '').replace( "(", "").replace(")", "").strip() v = int(line_vals[0].strip()) mem_info.update({k: v}) insert_reading("memInfo", time_stamp, mem_info) # Get disk usage c3_all = get_subprocess_result(cmd='df -l') # On some systems, errors are reported by df command, hence we need to filter those lines first c3_temp1 = get_subprocess_result(cmd='df -l | grep -n Filesystem') c3_temp2 = c3_temp1[0].split("Filesystem") c3_start = int(c3_temp2[0].strip().replace(":", "")) - 1 c3 = c3_all[c3_start:] col_heads = c3[0].split() # first line is the header row for line in c3[1:]: # second line onwards are value rows col_vals = line.split() disk_usage = {} for i in range(1, len(col_vals)): disk_usage[col_heads[i].replace( "%", "_prcntg")] = int(col_vals[i].replace( "%", "").strip()) if i < len(col_vals) - 1 else col_vals[i] dev_key = (col_vals[0])[1:] if col_vals[0].startswith( '/') else col_vals[0] # remove starting / from /dev/sda5 etc insert_reading("diskUsage_" + dev_key, time_stamp, disk_usage) # Get Network and other info c3_net = get_subprocess_result(cmd='cat /proc/net/dev') col_heads = c3_net[1].replace("|", " ").split() for i in range(len(col_heads)): col_heads[i] = "{}_{}".format("Receive" if i <= 8 else "Transmit", col_heads[i].strip()) col_heads[0] = "Interface" for line in c3_net[2:]: line_a = line.replace(":", " ").split() interface_name = line_a[0].strip() net_info = {} for i in range(1, len(line_a)): net_info.update({col_heads[i]: line_a[i]}) insert_reading("networkTraffic_" + interface_name, time_stamp, net_info) # Paging and Swapping c6 = get_subprocess_result(cmd='vmstat -s') paging_swapping = {} for line in c6: if 'page' in line: a_line = line.strip().split("pages") paging_swapping.update( {a_line[1].replace(' ', ''): int(a_line[0].strip())}) insert_reading("pagingAndSwappingEvents", time_stamp, paging_swapping) # Disk Traffic c4 = get_subprocess_result(cmd='iostat -xd 2 1') c5 = [i for i in c4[1:] if i.strip() != ''] # Remove all empty lines col_heads = c5[0].split() # first line is header row for line in c5[1:]: # second line onwards are value rows col_vals = line.split() disk_traffic = {} for i in range(1, len(col_vals)): disk_traffic[col_heads[i].replace("%", "prcntg_").replace( "/s", "_per_sec")] = float(col_vals[i].strip()) insert_reading("diskTraffic_" + col_vals[0], time_stamp, disk_traffic) return data def insert_reading(asset, time_stamp, data): data = { 'asset': "{}{}".format(handle['assetNamePrefix']['value'], asset), 'timestamp': time_stamp, 'readings': data } readings.append(data) try: time_stamp = utils.local_timestamp() get_system_info(time_stamp) except (OSError, Exception, RuntimeError) as ex: _LOGGER.exception("System Info exception: {}".format(str(ex))) raise ex return readings
def plugin_poll(handle): """ Extracts data from the sensor and returns it in a JSON document as a Python dict. Available for poll mode only. Args: handle: handle returned by the plugin initialisation call Returns: returns a sensor reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: TimeoutError """ # air quality is voltage reading between 0 and 5.1 # we scale is to a value between 0 and 1023 try: time_stamp = utils.local_timestamp() data = list() if handle['tempHumEnable']['value'] == 'true' and handle[ 'tempHumCount'] == 0: data.append({ 'asset': '{}{}'.format(handle['assetPrefix']['value'], handle['tempHumAssetName']['value']), 'timestamp': time_stamp, 'readings': { "temperature": handle['temperature'].getTemperature(), "humidity": handle['humidity'].getHumidity() } }) if handle['currentEnable']['value'] == 'true' and handle[ 'currentCount'] == 0: data.append({ 'asset': '{}{}'.format(handle['assetPrefix']['value'], handle['currentAssetName']['value']), 'timestamp': time_stamp, 'readings': { "current": handle['current'].getCurrent() } }) if handle['encoderEnable']['value'] == 'true' and handle[ 'encoderCount'] == 0: value = handle['encoder'].getPosition() # convert time_stamp to be usable if ":" == time_stamp[-3:-2]: timestamp_new = datetime.datetime.strptime( time_stamp[:-3] + time_stamp[-2:], '%Y-%m-%d %H:%M:%S.%f%z') else: timestamp_new = datetime.datetime.strptime( time_stamp, '%Y-%m-%d %H:%M:%S.%f%z') if handle['encoderPreviousValue'] > 0: # omit first one # calculate elapse time in milliseconds elapse_time = timestamp_new - handle['encoderPreviousTime'] elapse_time = elapse_time.total_seconds() data.append({ 'asset': '{}{}'.format(handle['assetPrefix']['value'], handle['encoderAssetName']['value']), 'timestamp': time_stamp, 'readings': { # (current_total_iterations - previous_total_iterations) / (elapsed time in seconds) "rotation-per-second": ((value - handle['encoderPreviousValue']) / 1200) / elapse_time } }) # update old values handle['encoderPreviousValue'] = value handle['encoderPreviousTime'] = timestamp_new if handle['accelerometerEnable']['value'] == 'true' and handle[ 'accelerometerCount'] == 0: x, y, z = handle['accelerometer'].getAcceleration() data.append({ 'asset': '{}{}'.format(handle['assetPrefix']['value'], handle['accelerometerAssetName']['value']), 'timestamp': time_stamp, 'readings': { "accelerometer-x": x, "accelerometer-y": y, "accelerometer-z": z } }) if handle['gyroscopeEnable']['value'] == 'true' and handle[ 'gyroscopeCount'] == 0: x, y, z = handle['gyroscope'].getAngularRate() data.append({ 'asset': '{}{}'.format(handle['assetPrefix']['value'], handle['gyroscopeAssetName']['value']), 'timestamp': time_stamp, 'readings': { "gyroscope-x": x, "gyroscope-y": y, "gyroscope-z": z } }) if handle['magnetometerEnable']['value'] == 'true' and handle[ 'magnetometerCount'] == 0: x, y, z = handle['magnetometer'].getMagneticField() data.append({ 'asset': '{}{}'.format(handle['assetPrefix']['value'], handle['magnetometerAssetName']['value']), 'timestamp': time_stamp, 'readings': { "magnetometer-x": x, "magnetometer-y": y, "magnetometer-z": z } }) handle['tempHumCount'] = (handle['tempHumCount'] + 1) % int( handle['tempHumPoll']['value']) handle['currentCount'] = (handle['currentCount'] + 1) % int( handle['currentPoll']['value']) handle['encoderCount'] = (handle['encoderCount'] + 1) % int( handle['encoderPoll']['value']) handle['accelerometerCount'] = ( handle['accelerometerCount'] + 1) % int( handle['accelerometerPoll']['value']) handle['gyroscopeCount'] = (handle['gyroscopeCount'] + 1) % int( handle['gyroscopePoll']['value']) handle['magnetometerCount'] = (handle['magnetometerCount'] + 1) % int( handle['magnetometerPoll']['value']) except (Exception, RuntimeError) as ex: _LOGGER.exception("wind_turbine exception: {}".format(str(ex))) raise exceptions.DataRetrievalError(ex) else: return data
def run(self): eof_reached = False while True: time_start = time.time() sensor_data = {} try: if self.handle['ingestMode']['value'] == 'burst': # Support for burst of data. Allow a burst size to be defined in the configuration, default 1. # If a size of greater than 1 is set then that number of input values should be sent as an # array of value. E.g. with a burst size of 10, which data point in the reading will be an # array of 10 elements. burst_data_points = [] for i in range(int(self.handle['burstSize']['value'])): readings = next(self.iter_sensor_data) # If we need to cherry pick cols, and possibly with a different name if len(self.reading_cols) > 0: new_dict = {} for k, v in self.reading_cols.items(): if k in readings: new_dict.update({v: readings[k]}) burst_data_points.append(new_dict) else: burst_data_points.append(readings) sensor_data.update({"data": burst_data_points}) next_iteration_secs = self.period else: readings = next(self.iter_sensor_data) # If we need to cherry pick cols, and possibly with a different name if len(self.reading_cols) > 0: for k, v in self.reading_cols.items(): if k in readings: sensor_data.update({v: readings[k]}) else: sensor_data.update(readings) if self.handle['historicTimestamps']['value'] == 'true': next_iteration_secs = self.period elif self.handle['timestampFromFile']['value'] == 'true': next_iteration_secs = self.get_time_stamp_diff( readings) else: next_iteration_secs = self.period except StopIteration as ex: _LOGGER.warning("playback - EOF reached: {}".format(str(ex))) eof_reached = True if self.handle['ingestMode']['value'] == 'burst': if len(burst_data_points) > 0: sensor_data.update({"data": burst_data_points}) except Exception as ex: _LOGGER.warning("playback producer exception: {}".format( str(ex))) wait_event.wait(timeout=next_iteration_secs - (time.time() - time_start)) time_stamp = utils.local_timestamp() if not self.condition._is_owned(): self.condition.acquire() if len(sensor_data) > 0: value = {'data': sensor_data, 'ts': time_stamp} self.queue.put(value) if self.queue.full() or eof_reached: self.condition.notify() self.condition.release() if eof_reached: # Rewind CSV file if it is to be read in an infinite loop if self.handle['repeatLoop']['value'] == 'true': # repeatLoop should not continue if thread has been signalled to stop if self._tstate_lock is None: return self.iter_sensor_data = iter(self.get_data()) eof_reached = False else: return
def plugin_poll(handle): """ Extracts data from the sensor and returns it in a JSON document as a Python dict. Available for poll mode only. Args: handle: handle returned by the plugin initialisation call Returns: returns a sensor reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: DataRetrievalError """ try: bus = handle["bus"] i2c_address = handle['i2cAddress']['value'] sensor_add = hex(int(i2c_address, 16)) start_add = 0x00 function_code = 0x03 register_number = 0x04 response_bytes = 8 attempt_threshold = 50 asset_name = '{}'.format(handle['assetName']['value']).replace('%M', i2c_address) try: # wake up call bus.write_i2c_block_data(sensor_add, function_code, [start_add, register_number]) except Exception as e: # expected exception as sensor is sleeping pass # request data bus.write_i2c_block_data(sensor_add, function_code, [start_add, register_number]) # read data sensor_response = bytearray(bus.read_i2c_block_data(sensor_add, function_code, response_bytes)) # temperature temperature= (sensor_response[4] * 256 + sensor_response[5])/10 # humidity humidity= (sensor_response[2] * 256 + sensor_response[3])/10 # crc crc = sensor_response[7] * 256 + sensor_response[6] # calc crc to verify calc_crc = 0xFFFF for byte in sensor_response[0:6]: calc_crc = calc_crc ^ byte for i in range(1,9): if(calc_crc & 0x01): calc_crc = calc_crc >> 1 calc_crc = calc_crc ^ 0xA001 else: calc_crc = calc_crc >> 1 if calc_crc != crc: pass time_stamp = utils.local_timestamp() data = { 'asset': asset_name, 'timestamp': time_stamp, 'readings': { "temperature": temperature, "humidity": humidity } } except (Exception, RuntimeError) as ex: _LOGGER.exception("AM2315 exception: {}".format(str(ex))) raise exceptions.DataRetrievalError(ex) return data
def plugin_poll(handle): """ Extracts data from the sensor and returns it in a JSON document as a Python dict. Available for poll mode only. Args: handle: handle returned by the plugin initialisation call Returns: returns a sensor reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: Exception """ def _str_to_bool(s): return True if s == 'true' else False # The IMU (inertial measurement unit) sensor is a combination of three sensors, each with an x, y and z axis # Enables and disables the magnetometer, accelerometer, gyroscope sense.set_imu_config(_str_to_bool(handle['magnetometerSensor']['value']), _str_to_bool(handle['gyroscopeSensor']['value']), _str_to_bool(handle['accelerometerSensor']['value'])) time_stamp = utils.local_timestamp() data = list() asset_prefix = handle['assetNamePrefix']['value'] try: if handle['pressureSensor']['value'] == 'true': pressure = sense.get_pressure() data.append({ 'asset': '{}{}'.format(asset_prefix, handle['pressureSensorName']['value']), 'timestamp': time_stamp, 'readings': { "pressure": pressure } }) if handle['temperatureSensor']['value'] == 'true': temperature = sense.get_temperature() data.append({ 'asset': '{}{}'.format(asset_prefix, handle['temperatureSensorName']['value']), 'timestamp': time_stamp, 'readings': { "temperature": temperature } }) if handle['humiditySensor']['value'] == 'true': humidity = sense.get_humidity() data.append({ 'asset': '{}{}'.format(asset_prefix, handle['humiditySensorName']['value']), 'timestamp': time_stamp, 'readings': { "humidity": humidity } }) if handle['magnetometerSensor']['value'] == 'true': magnetometer = sense.get_compass_raw() data.append({ 'asset': '{}{}'.format(asset_prefix, handle['magnetometerSensorName']['value']), 'timestamp': time_stamp, 'readings': magnetometer }) if handle['gyroscopeSensor']['value'] == 'true': gyroscope = sense.get_gyroscope_raw() data.append({ 'asset': '{}{}'.format(asset_prefix, handle['gyroscopeSensorName']['value']), 'timestamp': time_stamp, 'readings': gyroscope }) if handle['accelerometerSensor']['value'] == 'true': accelerometer = sense.get_accelerometer_raw() data.append({ 'asset': '{}{}'.format(asset_prefix, handle['accelerometerSensorName']['value']), 'timestamp': time_stamp, 'readings': accelerometer }) if handle['joystickSensor']['value'] == 'true': for event in sense.stick.get_events(): data.append({ 'asset': '{}{}'.format(asset_prefix, handle['joystickSensorName']['value']), 'timestamp': time_stamp, 'readings': { "direction": event.direction, "action": event.action } }) except RuntimeError as e: _LOGGER.exception("Sense HAT runtime error: %s", e) raise e except Exception as ex: _LOGGER.exception("Sense HAT exception: %s", ex) raise ex else: return data
def plugin_poll(handle): """ Extracts data from the sensor and returns it in a JSON document as a Python dict. Available for poll mode only. Args: handle: handle returned by the plugin initialisation call Returns: returns a sensor reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: Exception """ unit = 'hPa' # Pressure unit, can be either hPa (hectopascals) or Pa (pascals) time_stamp = utils.local_timestamp() data = list() asset_prefix = handle['assetNamePrefix']['value'] try: """ if handle['rgbSensor']['value'] == 'true': rgb = light.rgb() data.append({ 'asset': '{}{}'.format(asset_prefix, handle['rgbSensorName']['value']), 'timestamp': time_stamp, 'readings': { "r": rgb[0], "g": rgb[1], "b": rgb[2] } }) if handle['magnetometerSensor']['value'] == 'true': magnetometer = motion.magnetometer() data.append({ 'asset': '{}{}'.format(asset_prefix, handle['magnetometerSensorName']['value']), 'timestamp': time_stamp, 'readings': { "x": magnetometer[0], "y": magnetometer[1], "z": magnetometer[2] } }) if handle['accelerometerSensor']['value'] == 'true': accelerometer = [round(x, 2) for x in motion.accelerometer()] data.append({ 'asset': '{}{}'.format(asset_prefix, handle['accelerometerSensorName']['value']), 'timestamp': time_stamp, 'readings': { "x": accelerometer[0], "y": accelerometer[1], "z": accelerometer[2] } }) if handle['weatherSensor']['value'] == 'true': altitude = weather.altitude() temperature = weather.temperature() pressure = weather.pressure(unit=unit) data.append({ 'asset': '{}{}'.format(asset_prefix, handle['weatherSensorName']['value']), 'timestamp': time_stamp, 'readings': { "altitude": altitude, "temperature": temperature, "pressure": pressure, } }) """ ser.write("Q\r\n") resp = ser.readline() fltHum = float(resp[2:6]) fltTemp = float(resp[10:14]) fltCo2 = float(resp[18:22]) data.append({ 'asset': '{}{}'.format(asset_prefix, handle['CozirOutside']['value']), 'timestamp': time_stamp, 'readings': { "humidity": fltHum, "temperature": fltTemp, "co2": fltCo2 } }) except Exception as ex: _LOGGER.exception("Enviro pHAT exception: {}".format(str(ex))) raise ex return data
def plugin_poll(handle): """ Extracts data from the sensor and returns it in a JSON document as a Python dict. Available for poll mode only. Args: handle: handle returned by the plugin initialisation call Returns: returns a sensor reading in a JSON document, as a Python dict, if it is available None - If no reading is available Raises: Exception """ time_stamp = utils.local_timestamp() data = list() try: moving = False rgb = light.rgb() magnetometer = motion.magnetometer() if abs(magnetometer[0] - state["magx"]) > _MAGNETOMETER_THRESHOLD: moving = True state["magx"] = magnetometer[0] state["magy"] = magnetometer[1] state["magz"] = magnetometer[2] accelerometer = [round(x, 1) for x in motion.accelerometer()] if moving and state["light"] != "red" and rgb[0] > rgb[ 1] + _LIGHT_THRESHOLD and rgb[0] > rgb[2] + _LIGHT_THRESHOLD: data.append({ 'asset': 'game/points', 'timestamp': time_stamp, 'readings': { "red": _RED_POINTS, "green": 0.0, "blue": 0.0, "accelerometer": 0.0, "lateral": 0.0, "flip": 0.0 } }) state["light"] = "red" leds.on() elif moving and state["light"] != "green" and rgb[1] > rgb[ 0] + _LIGHT_THRESHOLD and rgb[1] > rgb[2] + _LIGHT_THRESHOLD: data.append({ 'asset': 'game/points', 'timestamp': time_stamp, 'readings': { "red": 0.0, "green": _GREEN_POINTS, "blue": 0.0, "accelerometer": 0.0, "lateral": 0.0, "flip": 0.0 } }) state["light"] = "green" leds.on() elif moving and state["light"] != "blue" and rgb[2] > rgb[ 0] + _LIGHT_THRESHOLD and rgb[2] > rgb[1] + _LIGHT_THRESHOLD: data.append({ 'asset': 'game/points', 'timestamp': time_stamp, 'readings': { "red": 0.0, "green": 0.0, "blue": _BLUE_POINTS, "accelerometer": 0.0, "lateral": 0.0, "flip": 0.0 } }) state["light"] = "blue" leds.on() elif moving: state["light"] = "white" leds.off() if abs(accelerometer[0]) > 0.1: data.append({ 'asset': 'game/points', 'timestamp': time_stamp, 'readings': { "red": 0.0, "green": 0.0, "blue": 0.0, "accelerometer": abs(accelerometer[0] * _LINEAR_FACTOR), "lateral": 0.0, "flip": 0.0 } }) if abs(accelerometer[1]) > 0.1: data.append({ 'asset': 'game/points', 'timestamp': time_stamp, 'readings': { "red": 0.0, "green": 0.0, "blue": 0.0, "accelerometer": 0.0, "lateral": abs(accelerometer[1] * _LATERAL_FACTOR), "flip": 0.0 } }) if state["inverted"] == "No" and accelerometer[2] < -0.2: data.append({ 'asset': 'game/points', 'timestamp': time_stamp, 'readings': { "red": 0.0, "green": 0.0, "blue": 0.0, "accelerometer": 0.0, "lateral": 0.0, "flip": _FLIP_PENALTY } }) state["inverted"] = "Yes" elif accelerometer[2] > 0.2: state["inverted"] = "No" except (Exception, RuntimeError) as ex: _LOGGER.exception("IoT Lab Game exception: {}".format(str(ex))) raise ex else: _LOGGER.debug("IoT Lab Game reading: {}".format(json.dumps(data))) return data