Exemplo n.º 1
0
class TestSensorConnection(unittest.TestCase):

    def setUp(self):
        self.sensor1 = DataHandler(MOCK_MAC)
        self.mocked_data = MockedData()
        self.cache = DeviceCache(time_out = 30)        

    def tearDown(self):
        pass

    def test_deviceCache(self):
        self.assertEqual(0, self.cache.temperature)
        self.assertEqual(0, self.cache.pressure)
        self.assertEqual(0, self.cache.humidity_level)
        self.assertEqual(0, self.cache.co2_level)
        self.assertEqual(0, self.cache.battery_level)
    

    def test_updateAllData(self):
        with mock.patch.object(DataHandler, 'updateAllData', return_value = None) as mock_method:
            thing = DataHandler(MOCK_MAC)
            thing.updateAllData()

            mock_method.assert_called_with()

    @mock.patch('CO2.sensor_connection.DataHandler.updateAllData')     
    def test_getParameter(self, mock_update):
            updater = DataHandler(MOCK_MAC, update_cache = False)

            with self.assertRaises(ValueError): updater.getParameter('parameter')
            mock_update.assert_called_with()


            

    def test_timedif(self):
       t1 = datetime.now()
       time.sleep(3)
       t2 = datetime.now()
       self.assertEqual(3,self.sensor1._timedif(t1, t2))
class Poller(object):                                                          #poller has a functions to read or write sensors data. functions can call functions from DataHandler, getData, and setData

    def __init__(self, mac, adapter='hci0', cache_timeout=120, update_timer = True, sensor_sample_rate = 10):
        self.handler      = DataHandler(mac, adapter, cache_timeout, update_timer, sensor_sample_rate)
        self.updater      = Updater(mac, adapter)
    """
    Funcion Get Handle
    Returns wanted handle
    """
    def getHandle(self, handle):
        return self.handler.getParameter(handle)    

    """
    This function sets a parameter to sensor that is enabled to set like Sample Rate or Turn Off.
    """
    def setHandle(self, parameter, data = None):
        if parameter == 'turn_off':
            self.updater.shutDown()
            return True
        if parameter == 'sample_rate':
            self.updater.setSampleRate(data)
            return True
        else:
            return False
 def setUp(self):
     self.sensor1 = DataHandler(MOCK_MAC)
     self.updater = Updater(MOCK_MAC)
     self.poller  = Poller(MOCK_MAC, adapter='hci0', cache_timeout=10, update_timer = False)
     self.mocked_data = MockedData()
 def __init__(self, mac, adapter='hci0', cache_timeout=120, update_timer = True, sensor_sample_rate = 10):
     self.handler      = DataHandler(mac, adapter, cache_timeout, update_timer, sensor_sample_rate)
     self.updater      = Updater(mac, adapter)
 def __init__(self, mac, adapter = 'hci0'):
     self.data = DataHandler(mac, adapter)
class Updater(object):
    def __init__(self, mac, adapter = 'hci0'):
        self.data = DataHandler(mac, adapter)

    # Sensor settings functions

    def setSampleRate(self, sample_rate):
        data = int(str(0x000000) + str(sample_rate))
        byte = pack('i', data)
        self.data.setHandle(_SETTINGS_SERVICE, _SENSORS_SAMPLE_RATE, byte)

    def shutDown(self):
        byte = pack('i', _SHUT_DOWN_DATA)
        self.data.setHandle(_SETTINGS_SERVICE, _SHUT_DOWN, byte)

    def passkey(self, key):
        raise NotImplementedError

    #Device info returning functions
    def getSampleRate(self):
        return self.data.getData(_SETTINGS_SERVICE, _SENSORS_SAMPLE_RATE) 

    def getManufacturerName(self):
        return self.data.getData(_DEVICE_INFO_SERVICE, _MANUFACTURER_NAME)

    def getSerialNumber(self):
        return self.data.getData(_DEVICE_INFO_SERVICE, _SERIAL_NUMBER)

    def getHardwareRevision(self):
        return self.data.getData(_DEVICE_INFO_SERVICE, _HARDWARE_REVISION)

    def getFirmwareRevision(self):
        return self.data.getData(_DEVICE_INFO_SERVICE, _FIRMWARE_REVISION)

    #Sensors readings
    def getTemperature(self):
        return self.data.getData(_SENSOR_SERVICE, _TEMPERATURE)

    def getPressure(self):
        return self.data.getData(_SENSOR_SERVICE, _PRESSURE)

    def getHumidity(self):
        return self.data.getData(_SENSOR_SERVICE, _HUMIDITY_LEVEL)

    def getCO2(self):
        return self.data.getData(_SENSOR_SERVICE, _CO2_LEVEL)

    def getBatteryLevel(self):
        return self.data.getData(_BATTERY, _BATTERY_LEVEL)   
Exemplo n.º 7
0
    def test_getParameter(self, mock_update):
            updater = DataHandler(MOCK_MAC, update_cache = False)

            with self.assertRaises(ValueError): updater.getParameter('parameter')
            mock_update.assert_called_with()
Exemplo n.º 8
0
    def test_updateAllData(self):
        with mock.patch.object(DataHandler, 'updateAllData', return_value = None) as mock_method:
            thing = DataHandler(MOCK_MAC)
            thing.updateAllData()

            mock_method.assert_called_with()
Exemplo n.º 9
0
 def setUp(self):
     self.sensor1 = DataHandler(MOCK_MAC)
     self.mocked_data = MockedData()
     self.cache = DeviceCache(time_out = 30)