Esempio n. 1
0
    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)
Esempio n. 2
0
 def callback(msg):
     if device.state is device.UNSYNCED:
         return Message.reply(dev_name + "-" + req_name, "fail",
                              "Device not synced")
     d = device.send_request(req_name, *msg.arguments)
     d.addCallbacks(request_returned, request_failed)
     raise AsyncReply()
Esempio n. 3
0
 def request_one(self, req, i, d, b):
     if i == 3:
         return ("fail", "I failed!")
     if i == 5:
         return ("bananas", "This should never be sent")
     if i == 6:
         return ("ok", i, d, b, "extra parameter")
     if i == 9:
         self.finish_request_one(req, i, d, b)
         raise AsyncReply()
     return ("ok", i, d, b)
Esempio n. 4
0
 def request_one(self, sock, i, d, b):
     if i == 3:
         return ("fail", "I failed!")
     if i == 5:
         return ("bananas", "This should never be sent")
     if i == 6:
         return ("ok", i, d, b, "extra parameter")
     if i == 9:
         # This actually gets put in the callback params automatically
         orig_msg = Message.request("one", "foo", "bar")
         self.finish_request_one(orig_msg, sock, i, d, b)
         raise AsyncReply()
     return ("ok", i, d, b)
Esempio n. 5
0
    def request_halt(self, msg):
        """Halt the device server.

        Returns
        -------
        success : {'ok', 'fail'}
            Whether scheduling the halt succeeded.

        Examples
        --------
        ::

            ?halt
            !halt ok
        """
        self.send_message(Message.reply("halt", "ok"))
        self.factory.stop()
        raise AsyncReply()
Esempio n. 6
0
    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()
Esempio n. 7
0
 def request_deprovision(self, req):
     """Return ok."""
     print "{} received deprovision request on port :{}".format(Time.now(), server_port)
     req.reply("ok")
     raise AsyncReply()
Esempio n. 8
0
    def request_sensor_value(self, msg):
        """Request the value of a sensor or sensors.

        A list of sensor values as a sequence of #sensor-value informs.

        Parameters
        ----------
        name : str, optional
            Name of the sensor to poll (the default is to send values for all sensors).
            If name starts and ends with '/' it is treated as a regular expression and
            all sensors whose names contain the regular expression are returned.

        Informs
        -------
        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 cpu.power.on
            #sensor-value 1244631611415.231 1 cpu.power.on 0
            !sensor-value ok 1
        """
        exact, name_filter = construct_name_filter(msg.arguments[0]
                    if msg.arguments else None)
        sensors = [(name, sensor) for name, sensor in
                    sorted(self.factory.sensors.iteritems()) if name_filter(name)]

        if exact and not sensors:
            return Message.reply(msg.name, "fail", "Unknown sensor name.")

        def one_ok(sensor, timestamp_ms, status, value):
            self.send_message(Message.inform(msg.name, timestamp_ms, "1",
                                             sensor.name, status, value))

        def one_fail(failure, sensor):
            self.send_message(Message.inform(msg.name, sensor.name,
                                             "Sensor reading failed."))

        def all_finished(lst):
            # XXX write a test where lst is not-empty so we can fail
            self.send_message(Message.reply(msg.name, "ok",
                                            len(sensors)))

        lst = []
        for name, sensor in sensors:
            self.read_formatted_from_sensor(sensor, one_ok, one_fail, lst)
        DeferredList(lst).addCallback(all_finished)
        raise AsyncReply()