def request_sensor_value(self, msg): """Poll a sensor value or value(s). A list of sensor values as a sequence of #sensor-value informs. Parameters ---------- name : str or pattern, optional If the name is not a pattern, list just the values of sensors with the given name. A pattern starts and ends with a slash ('/') and uses the Python re module's regular expression syntax. The values of all sensors whose names contain the pattern are listed. The default is to list the values of all sensors. Inform Arguments ---------------- timestamp : float Timestamp of the sensor reading in milliseconds since the Unix epoch. count : {1} Number of sensors described in this #sensor-value inform. Will always be one. It exists to keep this inform compatible with #sensor-status. name : str Name of the sensor whose value is being reported. value : object Value of the named sensor. Type depends on the type of the sensor. Returns ------- success : {'ok', 'fail'} Whether sending the list of values succeeded. informs : int Number of #sensor-value inform messages sent. Examples -------- ?sensor-value #sensor-value 1244631611415.231 1 psu.voltage 4.5 #sensor-value 1244631611415.200 1 cpu.status off ... !sensor-value ok 5 ?sensor-value /voltage/ #sensor-value 1244631611415.231 1 psu.voltage 4.5 #sensor-value 1244631611415.100 1 cpu.voltage 4.5 !sensor-value ok 2 ?sensor-value cpu.power.on #sensor-value 1244631611415.231 1 cpu.power.on 0 !sensor-value ok 1 """ if not msg.arguments: self._send_all_sensors() raise AsyncReply() name = msg.arguments[0] if len(name) >= 2 and name.startswith("/") and name.endswith("/"): # regex case self._send_all_sensors(name[1:-1]) raise AsyncReply() else: return DeviceProtocol.request_sensor_value(self, msg)
def request_halt(self, msg): """ drops connection to specified device """ def got_halt((informs, reply)): self.send_message(Message.reply('halt', dev_name, 'ok')) if not msg.arguments: return DeviceProtocol.halt(self, msg) try: dev_name = msg.arguments[0] device = self.factory.devices[dev_name] except KeyError: return Message.reply('halt', 'fail', 'Unknown device %s' % dev_name) else: self.factory.unregister_device(dev_name) device.send_request('halt').addCallback(got_halt) raise AsyncReply()
def request_halt(self, msg): """ drops connection to specified device """ def got_halt((informs, reply)): self.send_reply(Message.reply('halt', dev_name, 'ok'), msg) if not msg.arguments: return DeviceProtocol.halt(self, msg) try: dev_name = msg.arguments[0] device = self.factory.devices[dev_name] except KeyError: return Message.reply('halt', 'fail', 'Unknown device %s' % dev_name) else: self.factory.unregister_device(dev_name) device.send_request('halt').addCallback(got_halt) raise AsyncReply()
def request_sensor_list(self, msg): """Request the list of sensors. The list of sensors is sent as a sequence of #sensor-list informs. Parameters ---------- name : str or pattern, optional If the name is not a pattern, list just the sensor with the given name. A pattern starts and ends with a slash ('/') and uses the Python re module's regular expression syntax. All sensors whose names contain the pattern are listed. The default is to list all sensors. Inform Arguments ---------------- name : str The name of the sensor being described. description : str Description of the named sensor. units : str Units for the value of the named sensor. type : str Type of the named sensor. params : list of str, optional Additional sensor parameters (type dependent). For integer and float sensors the additional parameters are the minimum and maximum sensor value. For discrete sensors the additional parameters are the allowed values. For all other types no additional parameters are sent. Returns ------- success : {'ok', 'fail'} Whether sending the sensor list succeeded. informs : int Number of #sensor-list inform messages sent. Examples -------- ?sensor-list #sensor-list psu.voltage PSU\_voltage. V float 0.0 5.0 #sensor-list cpu.status CPU\_status. \@ discrete on off error ... !sensor-list ok 5 ?sensor-list /voltage/ #sensor-list psu.voltage PSU\_voltage. V float 0.0 5.0 #sensor-list cpu.voltage CPU\_voltage. V float 0.0 3.0 !sensor-list ok 2 ?sensor-list cpu.power.on #sensor-list cpu.power.on Whether\_CPU\_hase\_power. \@ boolean !sensor-list ok 1 """ # handle non-regex cases if not msg.arguments or not (msg.arguments[0].startswith("/") and msg.arguments[0].endswith("/")): return DeviceProtocol.request_sensor_list(self, msg) # handle regex name_re = re.compile(msg.arguments[0][1:-1]) sensors = dict([(name, sensor) for name, sensor in self.factory.sensors.iteritems() if name_re.search(name)]) for name, sensor in sorted(sensors.items(), key=lambda x: x[0]): self.send_message( Message.inform("sensor-list", name, sensor.description, sensor.units, sensor.stype, *sensor.formatted_params)) return Message.reply(msg.name, "ok", len(sensors))
def request_sensor_list(self, msg): """Request the list of sensors. The list of sensors is sent as a sequence of #sensor-list informs. Parameters ---------- name : str or pattern, optional If the name is not a pattern, list just the sensor with the given name. A pattern starts and ends with a slash ('/') and uses the Python re module's regular expression syntax. All sensors whose names contain the pattern are listed. The default is to list all sensors. Inform Arguments ---------------- name : str The name of the sensor being described. description : str Description of the named sensor. units : str Units for the value of the named sensor. type : str Type of the named sensor. params : list of str, optional Additional sensor parameters (type dependent). For integer and float sensors the additional parameters are the minimum and maximum sensor value. For discrete sensors the additional parameters are the allowed values. For all other types no additional parameters are sent. Returns ------- success : {'ok', 'fail'} Whether sending the sensor list succeeded. informs : int Number of #sensor-list inform messages sent. Examples -------- ?sensor-list #sensor-list psu.voltage PSU\_voltage. V float 0.0 5.0 #sensor-list cpu.status CPU\_status. \@ discrete on off error ... !sensor-list ok 5 ?sensor-list /voltage/ #sensor-list psu.voltage PSU\_voltage. V float 0.0 5.0 #sensor-list cpu.voltage CPU\_voltage. V float 0.0 3.0 !sensor-list ok 2 ?sensor-list cpu.power.on #sensor-list cpu.power.on Whether\_CPU\_hase\_power. \@ boolean !sensor-list ok 1 """ # handle non-regex cases if not msg.arguments or not (msg.arguments[0].startswith("/") and msg.arguments[0].endswith("/")): return DeviceProtocol.request_sensor_list(self, msg) # handle regex name_re = re.compile(msg.arguments[0][1:-1]) sensors = dict([(name, sensor) for name, sensor in self.factory.sensors.iteritems() if name_re.search(name)]) for name, sensor in sorted(sensors.items(), key=lambda x: x[0]): self.send_reply_inform(Message.inform("sensor-list", name, sensor.description, sensor.units, sensor.stype, *sensor.formatted_params), msg) return Message.reply(msg.name, "ok", len(sensors))