Example #1
0
    def test_pdu_data_packet(self):
        pdu = CWOPPDU(provider_id, **init_kwargs)

        packet = pdu.pdu_data_packet
        self.assertIsInstance(packet, bytes)

        return
Example #2
0
    def test_pdu_login_line(self):
        pdu = CWOPPDU(provider_id, **init_kwargs)
        login_line = pdu.login_line
        self.assertIsInstance(login_line, bytes)
        test = b'user FW9999 pass -1 vers PythonCWOP 1.0'
        self.assertEqual(login_line, test)

        return
Example #3
0
    def test_humidity(self):

        # 0% relative humidity should be impossible?
        pdu = CWOPPDU(provider_id, **base_kwargs, humidity=0)
        test = '01'
        self.assertEqual(test, pdu.data['humidity'])

        # 100% relative humidity is represented as 00
        pdu = CWOPPDU(provider_id, **base_kwargs, humidity=0)
        test = '01'
        self.assertEqual(test, pdu.data['humidity'])

        # Negative number
        with self.assertRaises(ValueError):
            pdu = CWOPPDU(provider_id, **base_kwargs, humidity=-26)

        # 1 <= humidity <100
        pdu = CWOPPDU(provider_id, **base_kwargs, humidity=26)
        test = '26'
        self.assertEqual(test, pdu.data['humidity'])

        return
Example #4
0
    def setUp(self):
        global lat, long, provider_id, base_kwargs, pdu
        # Form a fake PDU for testing CWOP Client
        long = Longitude(-95.458310)
        lat = Latitude(29.752320)
        base_kwargs = {'time':datetime.now(),
               'longitude':long,
               'latitude':lat,
               'temperature':74,
               'humidity':73,
               'dewpoint':64,
               'barometric_pressure':1015,
               }
        provider_id = 'FW8400'
        pdu = CWOPPDU(provider_id, **base_kwargs)

        return
Example #5
0
    def test_init(self):
        pdu = CWOPPDU(provider_id, **init_kwargs)

        # Time
        test = datetime.now().strftime('%d%H%M')
        self.assertEqual(test, pdu.data['time'])

        # XXX
        test = init_kwargs['longitude']
        self.assertEqual(test, pdu.data['longitude'])
        # XXX
        test = init_kwargs['latitude']
        self.assertEqual(test, pdu.data['latitude'])
        # XXX
        test = str(init_kwargs['wind_direction'])
        self.assertEqual(test, pdu.data['wind_direction'])
        # XXX
        test = str(init_kwargs['wind_speed'])
        self.assertEqual(test, pdu.data['wind_speed'])
        # XXX
        test = str(init_kwargs['peak_instantaneous_wind_velocity'])
        self.assertEqual(test, pdu.data['peak_instantaneous_wind_velocity'])
        # XXX
        test = str(init_kwargs['temperature'])
        self.assertEqual(test, pdu.data['temperature'])
        # XXX
        test = str(init_kwargs['rainfall_rate_hour'])
        self.assertEqual(test, pdu.data['rainfall_rate_hour'])
        # XXX
        test = str(init_kwargs['rainfall_rate_day'])
        self.assertEqual(test, pdu.data['rainfall_rate_day'])
        # XXX
        test = str(init_kwargs['rainfall_rate_midnight'])
        self.assertEqual(test, pdu.data['rainfall_rate_midnight'])
        # XXX
        test = str(init_kwargs['barometric_pressure'])
        self.assertEqual(test, pdu.data['barometric_pressure'])
        # XXX
        test = str(init_kwargs['humidity'])
        self.assertEqual(test, pdu.data['humidity'])

        return
Example #6
0
async def main():
    """
    1. Read weather information from BACnet server through the HTTP API
    2. Error check HTTP Client/Server API response
    3. Form CWOP Protocol data unit
    4. Send data to FindU Server
    """

    # 1. Read weather information from BACnet server through the HTTP
    # API
    url = 'http://{}:{}/readpropertymultiple/'.format(BACHTTPServerHost,
                                                      BACHTTPPort)
    headers = {'Content-Type': 'application/json', 'X-bacnet-timeout': '3'}
    try:
        res = requests.post(url,
                            headers=headers,
                            data=json.dumps(body),
                            timeout=5)
    except Exception as e:
        msg = 'HTTP POST unsuccessful\n' + str(res.status_code) + str(
            res.request)
        logger.error(str(e) + '\n' + msg)

    # 2. Error check HTTP Client/Server API response
    if res.headers['Content-Type'] != 'application/json':
        # An error was reported by the BACnet/HTTP server
        # Errors are reported with Content-Type = text/plain
        msg = ('Error at BACnet HTTP Server. Content dump: {}'.format(
            res.content))
        logger.error(msg)
        return

    if res.status_code != 202:
        msg = ('Error at BACnet HTTP Server. Content dump: {}'.format(
            res.content))
        logger.warning(msg)
        return

    try:
        # Parse the resposne (Should be JSON)
        res_json = res.json()
        for bac_obj in body['bacnet_objects']:
            # Make sure the response includes all the requested bacnet objects
            identifier = ObjectIdentifier(bac_obj['object']).value
            if not str(identifier) in res_json.keys():
                msg=('The BACnet HTTP client did not respond correctly to '+
                     'the request.\nRequest : {}\nResponse : {}\n'\
                         .format(res.request, res.content))
                # TODO Log error
                raise RuntimeError(msg)
    except json.JSONDecodeError as e:
        # Log the error
        msg=('The BACnet HTTP client did not respond correctly to '+
             'the request.\nRequest : {}\nResponse : {}\n'\
                 .format(res.request, res.content))
        logger.error(e)
        return

    # 3. Form CWOP Protocol data unit
    # 4. Send data to FindU Server
    try:
        pdu_kwargs = {
            'time': datetime.now().strftime('%d%H%M'),
            'latitude': Latitude(latitude),
            'longitude': Longitude(longitude),
            'temperature': res_json["('analogInput', 1)"]['presentValue'],
            'humidity': res_json["('analogInput', 4)"]['presentValue'],
            'dewpoint': res_json["('analogInput', 8)"]['presentValue'],
            'co2': res_json["('analogInput', 12)"]['presentValue'],
        }
        pdu = CWOPPDU(provider_id=provider_id, **pdu_kwargs)
        await cwop_client(pdu, CWOP_SERVER_HOST, CWOP_SERVER_PORT)
    except Exception as e:
        logger.exception(str(e))

    logger.info('PDU Sent')

    return None
Example #7
0
 def test_temperature(self):
     pdu = CWOPPDU(provider_id, **base_kwargs, temperature=-31)
     test = '-31'
     self.assertEqual(test, pdu.data['temperature'])
     return