Пример #1
0
    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
Пример #2
0
    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
Пример #3
0
def read_store_readings ():
    """ Function used to read and store sensor readings
    """
    logging.debug("inside read_store_readings...")

    if globalFlaskApp is None:
        logging.error("Flask has not been initialized!")
        raise EnvironmentError("Flask has not been initialized")

    # error flag used to send email only once if error occurs.
    error_flag = False

    headers = {'content-type': 'application/json'}

    sensor_url = ini_config.get("Sensor", "SENSOR_TEMPERATURE_URL")
    sensor_serial = ini_config.get("Sensor", "SENSOR_TEMPERATURE_SERIAL")

    rest_url = (sensor_url + "/" + sensor_serial)
    logging.debug("sensor REST URL is [{0}]".format(rest_url))

    rest_user = ini_config.get("Sensor", "SENSOR_REST_API_USERNAME")
    rest_pw = ini_config.get("Sensor", "SENSOR_REST_API_PASSWORD")
    req_timeout = ini_config.getint("Sensor", "SENSOR_REQUEST_TIMEOUT")
    sleep_timeout = ini_config.getint("Sensor", "SENSOR_SLEEP_TIME")
    recipient = ini_config.get("Email", "RECIPIENT_EMAIL")

    with globalFlaskApp.app_context():

        logging.debug("starting forever loop within Flask app context")

        # start daemon forever loop
        while True:

            try:
                # collect data from sensor using RESTFul API
                logging.debug("Sending request to [{0}] with user [{1}]"
                               .format(rest_url, rest_user))

                r = requests.get(rest_url,
                                 verify=False,
                                 auth=(rest_user, rest_pw),
                                 headers=headers,
                                 timeout=req_timeout)

                # if code gets here no exception has occurred. reset flag.
                error_flag = False

                if (r.status_code != 200):
                    logging.error("Response status code [{0}] : [{1}]"
                                   .format(r.status_code, r.text))
                else:
                    output = r.json()
                    sensor = output[SENSOR_KEY]
                    readings = output[DATA_KEY]

                    logging.debug("Adding sensor record with unit [{0}], "
                                   "value [{1}], utc [{2}], serial [{3}] to "
                                   "database."
                                   .format(readings["unit"],
                                           readings["value"],
                                           readings["utc"],
                                           sensor["serial"]))

                    SensorDAO.add_reading(readings["unit"],
                                          readings["value"],
                                          readings["utc"],
                                          sensor["serial"])

            # python 3.4: ConnectionRefusedError
            except (ConnectionError, Timeout) as err:  # e.g., server is down.
                sys.stderr.write(str(err))

                logging.exception("Connection Error with URL [{0}], "
                                   "user [{1}] in runserver daemon: [{2}]"
                                   .format(rest_url, rest_user, err))

                if not error_flag :  # only send email once.
                    subject = "sensorserver: Connection/Timeout Error"
                    logging.info("Sending email to [{0}] with subject [{1}]"
                                  .format(recipient, subject))
                    message = ("Connection/Timeout Error to URL [{0}]: [{1}]"
                               .format(rest_url, err))
                    try:
                        EMail.send_email(recipient, subject, message)
                        logging.info("email to [{0}] with subject [{1}] sent."
                                      .format(recipient, subject))
                    except Exception as mail_err:
                        logging.error("Error [{0}] sending email."
                                       .format(mail_err))

                error_flag = True

            except (HTTPError, RequestException) as err:
                sys.stderr.write(str(err))

                logging.exception("HTTP/Request Error to URL [{0}] in "
                                   "runserver daemon: [{1}]"
                                   .format(rest_url, err))

                if not error_flag :  # only send email once.
                    subject = "sensorserver: HTTP Error"
                    logging.info("Sending email to [{0}] with subject [{1}]"
                                  .format(recipient, subject))
                    message = ("HTTP/Request to URL [{0}] Error: [{1}]"
                               .format(rest_url, err))

                    try:
                        EMail.send_email(recipient, subject, message)
                        logging.info(
                            "email to [{0}] with subject [{1}] was sent."
                            .format(recipient, subject))
                    except Exception as mail_err:
                        logging.error("Error [{0}] sending email."
                                       .format(mail_err))

                error_flag = True

            except (EnvironmentError, Exception) as err:
                sys.stderr.write(str(err))

                logging.exception("Error in runserver daemon: [{0}]"
                                   .format(err))

                if not error_flag :  # only send email once.
                    subject = "sensorserver: Environment Error"
                    logging.info("Sending email to [{0}] with subject [{1}]"
                                  .format(recipient, subject))
                    message = "EXITING APPLICATION. Error: [{0}]".format(err)

                    try:
                        EMail.send_email(recipient, subject, message)
                        logging.info(
                            "email to [{0}] with subject [{1}] was sent."
                            .format(recipient, subject))
                    except Exception as mail_err:
                        logging.error("Error [{0}] sending email."
                                       .format(mail_err))

                error_flag = True

            except:  # catch *all* other exceptions
                err = sys.exc_info()[0]
                logging.exception("Error occurred in runserver daemon: [{0}]"
                                   .format(err))

                write_to_file("<p>Error in runsensor daemon: [{0}]</p>"
                              .format(err), sys.stderr)
                exit(1)


            time.sleep(sleep_timeout)

    return