Exemplo n.º 1
0
    def __init__(self, value, unit, utc):
        """constructor.

        Parameters
        ----------
        value:  float (required)
            the measurement reading numeric value
        value:  string (required)
            the measurement reading unit
        utc: string (required)
            the UTC timestamp
        """
        if not is_number(value):
            raise IllegalArgumentException("value is invalid.")

        if is_blank(unit):
            raise IllegalArgumentException("unit is required.")

        if is_blank(utc):
            raise IllegalArgumentException("utc is required.")

        self.value = value
        self.unit = unit
        self.utc = utc
        return
Exemplo n.º 2
0
    def convert(from_value, from_unit, to_unit):
        """convert length units.

        Parameters
        ----------
        from_value:  str (required)
            numeric value
        from_unit:  str (required)
            length unit
        to_unit:  str (required)
            length unit

        Returns
        -------
        float:
            the converted value
        """
        if not is_number(from_value):
            raise IllegalArgumentException(
                ("Parameter from_value=[{0}] not valid. "
                 "A numeric value must be provided.").format(from_value))

        if is_blank(from_unit):
            raise IllegalArgumentException(
                ("Parameter from_unit=[{0}] not valid. A unit be provided.")
                .format(from_unit))

        if is_blank(to_unit):
            raise IllegalArgumentException(
                ("Parameter to_unit=[{0}] not valid. A unit be provided.")
                .format(to_unit))

        if from_unit == to_unit:
            raise IllegalArgumentException(
                ("from_unit=[{0}] and to_unit=[{1}] units cannot be equal")
                .format(from_unit, to_unit))

        # pint temperature units need to be lower-cased or degC, degF, degK
        from_unit = from_unit.lower().strip()
        if from_unit == "degc":
            from_unit = "degC"
        elif from_unit == "degf":
            from_unit = "degF"
        elif from_unit == "degk":
            from_unit = "degK"

        # pint temperature units need to be lower-cased or degC, degF, degK
        to_unit = to_unit.lower().strip()
        if to_unit == "degc":
            to_unit = "degC"
        elif to_unit == "degf":
            to_unit = "degF"
        elif to_unit == "degk":
            to_unit = "degK"

        result = convert_unit(UNIT_TYPES_ENUM.temperature, from_unit,
                              from_value, to_unit)
        return result
Exemplo n.º 3
0
    def send_text(phone_number, message):
        """Sends given SMS message using http://textbelt.com/

        Parameters
        ----------
        phone_number:  str (required)
            recipient numeric string for a valid phone number. It cannot be
            greater than 20 digits
        message: str (optional)
            message to send. It cannot be greater than 40 characters.

        Returns
        -------
            nothing.
        """

        if is_blank(phone_number):
            raise IllegalArgumentException("phone_number is required")

        if not is_number(phone_number):
            raise IllegalArgumentException(
                "phone_number [{0}] needs to be a number."
                .format(phone_number))

        if len(phone_number.strip()) > SMS.MAX_PHONE_LENGTH:
            raise IllegalArgumentException(
                 "phone_number [{0}] cannot be greater than [{1}]."
                .format(phone_number, SMS.MAX_PHONE_LENGTH))

        if is_blank(message) :
            raise IllegalArgumentException ("message cannot blank")


        if len(message.strip()) > SMS.MAX_MSG_LENGTH:
            raise IllegalArgumentException(
                "message [{0}] cannot be greater than [{1}]."
                .format(message, SMS.MAX_MSG_LENGTH))

        url = 'http://textbelt.com/text'
        payload = {'number': phone_number,
                   'message': "iotgw.rubens.home: " + message.strip()}

        logging.debug("Sending SMS to [{0}] using url [{1}]"
                      .format(phone_number, url))

        r = requests.post(url, data=payload)

        if (r.status_code != 200):
            logging.debug("Failed SMS to [{0}] using url [{1}]: status [{2}]"
                          .format(phone_number, url, r.status_code))
            r.raise_for_status()

        return
Exemplo n.º 4
0
    def convert(from_value, from_unit, to_unit):
        """convert length units.

        Parameters
        ----------
        from_value:  float (required)
            numeric value
        from_unit:  str (required)
            length unit
        to_unit:  str (required)
            length unit

        Returns
        -------
        float:
            the converted value

        """
        if not is_number(from_value):
            raise IllegalArgumentException(
                ("Parameter from_value=[{0}] not valid. "
                 "A numeric value must be provided.").format(from_value))

        if is_blank(from_unit):
            raise IllegalArgumentException(
                ("Parameter from_unit=[{0}] not valid. A unit be provided.")
                .format(from_unit))

        if is_blank(to_unit):
            raise IllegalArgumentException(
                ("Parameter to_unit=[{0}] not valid. A unit be provided.")
                .format(to_unit))

        if from_unit == to_unit:
            raise IllegalArgumentException(
                ("from_unit=[{0}] and to_unit=[{1}] units cannot be equal")
                .format(from_unit, to_unit))

        # pint unit registry only accepts lower case letters for length units
        from_unit = from_unit.lower().strip()
        to_unit = to_unit.lower().strip()

        result = convert_unit(UNIT_TYPES_ENUM.length, from_unit,
                              from_value, to_unit)
        return result
Exemplo n.º 5
0
    def add_reading(unit, value, utc, serial):
        """
        Adds given measurement data to sensor database

        Parameters
        ----------
        unit:  str (required)
            degF, degC, degF.
        value:  float (required)
            a float number
        utc: str (required)
            UTC timestamp of the reading
        serial: str (required)
            sensor unique serial number

        Returns
        -------
            nothing.
        """

        if is_blank(unit):
            raise IllegalArgumentException("unit is required.")

        if not value:
            raise IllegalArgumentException("utc is required.")

        if not is_number(value):
            raise IllegalArgumentException("value is not a numeric value.")

        if is_blank(utc):
            raise IllegalArgumentException("utc is required.")

        if is_blank(serial):
            raise IllegalArgumentException("serial is required.")

        test_sensor = SensorDAO.get_sensor(serial)
        if test_sensor is None:
            raise IllegalArgumentException("sensor with serial [{0}] " 
                                            "is not registered in the system."
                                            .format(serial))

        SensorDAO.SENSOR_DB.add_reading(unit, value, utc, serial)

        return
Exemplo n.º 6
0
    def get(self, to_unit):
        """REST GET implementation for the URI:

        http://<server>:<port>/weight/<to_unit>?from_unit=<from_unit>&
            from_value=<from_value>

        Parameters
        ----------
        to_unit:  str (required)
            length unit (required)

        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:

        from_unit: str (required)
            mass unit
        from_value: str (required)
            numeric value

        Returns
        -------
        str:
            A JSON string containing the response

        Raises
        ------
        BadRequest if there is an error validating the input parameters or
        some other error processing this method.
        """
        params = request.args
        if not params:
            raise BadRequest("Parameters "
                             "from_unit=<from_unit>&from_value=<_from_value> "
                             "are missing")

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

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

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

        from_unit = params.get("from_unit")

        from_value = params.get("from_value")
        if not is_number(from_value):
            raise BadRequest(("Parameter from_value=[{0}] not valid. "
                              "A numeric value must be provided.")
                             .format(from_value))

        from_value = float(from_value)

        if from_unit == to_unit:
            raise BadRequest("from_unit=[{0}] and to_unit=[{1}] units "
                             "cannot be equal".format(from_unit, to_unit))

        # pint unit registry only accepts lower case letters for mass units
        from_unit = from_unit.lower().strip()
        to_unit = to_unit.lower().strip()

        result = Weight.convert(from_value, from_unit, to_unit)

        data = OrderedDict()
        data["from_unit"] = from_unit
        data["from_value"] = from_value
        data["to_unit"] = to_unit
        data["to_value"] = result

        response = OrderedDict()
        response[STATUS_KEY] = STATUS_SUCCESS
        response[DATA_KEY] = data

        json_result = jsonify(response)
        return json_result