def get(self, serial): """REST GET implementation for the URI: http://<server>:<port>/information/sensors/<string:serial> Parameters ---------- serial: str (required) sensor serial number Raises: ------ NotFound if sensor with serial is not found. """ # retrieve sensor info from DB sensor = SensorDAO.get_sensor(serial) if sensor is None: raise NotFound("No sensor registered for serial [{0}]".format(serial)) data = OrderedDict() data["serial"] = sensor["serial"] data["type"] = sensor["type"] data["name"] = sensor["name"] data["state"] = sensor["state"] data["geolocation"] = sensor["geolocation"] data["address"] = sensor["address"] data["location"] = sensor["location"] response = OrderedDict() response[STATUS_KEY] = STATUS_SUCCESS response[SENSOR_KEY] = data json_response = jsonify(response) return json_response
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