def get(self, serial):
        """REST GET implementation for the URI:

        http://<server>:<port>/analytics/temperature/sensors/<string:serial>?
            duration=<duration>

        Parameters
        ----------
        serial:  str (required)
            sensor serial number

        It is assumed that this method is called within the context of an HTTP
        request.  And that the HTTP request contains query parameters
        with the request.args as containing the following:

        duration: str (required)
            A valid duration as defined by the DURATION_ENUM

        Raises:
        ------
        BadRequest if sensor with serial is not registered or if sensor is
        not enabled.
        """
        params = request.args
        if not params:
            raise BadRequest("Parameter duration=<duration> is missing")

        if not isinstance(params, dict):
            raise BadRequest("params must be an instance of dict")

        if "duration" not in params:
            raise BadRequest("Missing required duration parameter")

        duration = params.get("duration")

        is_valid = DURATION_ENUM.is_valid(duration)
        if not is_valid:
            raise BadRequest("duration=[{0}] is not valid".format(duration))

        # retrieve sensor info from DB
        sensor = SensorDAO.get_sensor(serial)

        if sensor is None:
            raise NotFound("No sensor registered for serial [{0}]".format(serial))

        readings = SensorDAO.get_readings(serial, duration)

        sensor_data = dict()
        sensor_data["serial"] = sensor["serial"]

        response = OrderedDict()
        response[STATUS_KEY] = STATUS_SUCCESS
        response[SENSOR_KEY] = sensor_data
        response[DATA_KEY] = readings

        json_response = jsonify(response)
        return json_response
Beispiel #2
0
 def test_duration_enum(self):
     status = DURATION_ENUM.is_valid("vasco")
     self.assertFalse(status)
     return