def test_url_get(self): with self.assertRaises(requests.ConnectionError): deviceconfig = DeviceConfig.download( "http://does/not/exist") # The post_key supplied here is not valid for anything, but we pass the "must have post_key test". deviceconfig = DeviceConfig.download( "https://friskby.herokuapp.com/sensor/api/device/FriskPITest/" , post_key = "xxx") deviceconfig.save( filename = self.config_file )
def test_url_get(self): with self.assertRaises(requests.ConnectionError): deviceconfig = DeviceConfig.download("http://does/not/exist") # The post_key supplied here is not valid for anything, but we pass the "must have post_key test". deviceconfig = DeviceConfig.download( "https://friskby.herokuapp.com/sensor/api/device/FriskPITest/", post_key="xxx") deviceconfig.save(filename=self.config_file)
def devices(self): if self._args.file: self._devices = DeviceConfig.fom_file(self._args.file) if self._args.api_server: self._devices = DeviceConfig.fom_api_server(self._args.api_server, self._args.admin_user, self._args.admin_password, self._args.admin_tenant_name) for d in self._devices: yield d
def devices(self): if self._args.device_config_file: self._devices = DeviceConfig.fom_file( self._args.device_config_file) elif self._args.api_server_list: self._devices = DeviceConfig.fom_api_server( self._args.api_server_list, self._args.admin_user, self._args.admin_password, self._args.admin_tenant_name, self._args.auth_host, self._args.auth_port, self._args.auth_protocol, self._cb) return self._devices
def __init__(self): self.post_key = "6c918af4-7ab0-4812-a73e-80f7ed4f10f9" self.device_id = "FriskPITest" self.server_url = "https://friskby.herokuapp.com" self.post_path = "sensor/api/device/%s/" % self.device_id self.device_config = DeviceConfig.download( "%s/%s" % (self.server_url , self.post_path) , post_key = self.post_key) self.sensor_id = "FriskPITest_PM10" self.device_config_broken = DeviceConfig.download( "%s/%s" % (self.server_url , self.post_path) , post_key = self.post_key) self.device_config_broken.data["server_url"] = "http://invalid.no"
def devices(self): if self._args.device_config_file: self._devices = DeviceConfig.fom_file( self._args.device_config_file) elif self._args.api_server: self._devices = DeviceConfig.fom_api_server( [self._args.api_server], self._args.admin_user, self._args.admin_password, self._args.admin_tenant_name, self._args.auth_host, self._args.auth_port, self._args.auth_protocol, self._cb) #TODO SUNDAR_TO_ADD_API_SERVERS_LIST return self._devices
def devices(self): if self._args.device_config_file: self._devices = DeviceConfig.fom_file( self._args.device_config_file) elif self._args.api_server: self._devices = DeviceConfig.fom_api_server( self._args.api_server, self._args.admin_user, self._args.admin_password, self._args.admin_tenant_name) elif self._args.disc_server_port: try: self._devices = DeviceConfig.fom_api_server( self.get_api_svr(), self._args.admin_user, self._args.admin_password, self._args.admin_tenant_name) except Exception as e: self._devices = [] for d in self._devices: yield d
def devices(self): prouters = [] if self._args.api_server_list: prouters = DeviceConfig.get_prouters( self._args.api_server_list, self._args.admin_user, self._args.admin_password, self._args.admin_tenant_name, self._args.api_server_use_ssl, self._args.auth_host, self._args.auth_port, self._args.auth_protocol, self._cb) return prouters
def devices(self): if self._args.device_config_file: self._devices = DeviceConfig.fom_file( self._args.device_config_file) elif self._args.api_server: self._devices = DeviceConfig.fom_api_server( [self._args.api_server], self._args.admin_user, self._args.admin_password, self._args.admin_tenant_name, self._args.auth_host, self._args.auth_port, self._args.auth_protocol, self._cb) elif self._args.disc_server_port: apis = self.get_api_svrs() if apis: self._devices = DeviceConfig.fom_api_server( apis, self._args.admin_user, self._args.admin_password, self._args.admin_tenant_name, self._args.auth_host, self._args.auth_port, self._args.auth_protocol, self._cb) else: self._devices = [] return self._devices
def _create_input(self, input_file, output_file, devices, i, restrict=None): if isinstance(devices[0], DeviceDict): devices = DeviceConfig.populate_cfg(devices) with open(input_file, 'wb') as f: data = dict(out=output_file, netdev=devices, instance=i) if restrict: data['restrict'] = restrict pickle.dump(data, f) f.flush()
def devices(self): if self._args.device_config_file: self._devices = DeviceConfig.fom_file( self._args.device_config_file) elif self._args.api_server: self._devices = DeviceConfig.fom_api_server( [self._args.api_server], self._args.admin_user, self._args.admin_password, self._args.admin_tenant_name, self._args.auth_host, self._args.auth_port, self._args.auth_protocol, self._cb) elif self._args.disc_server_port: apis = self.get_api_svrs() if apis: self._devices = DeviceConfig.fom_api_server( self.get_api_svrs(), self._args.admin_user, self._args.admin_password, self._args.admin_tenant_name, self._args.auth_host, self._args.auth_port, self._args.auth_protocol, self._cb) else: self._devices = [] return self._devices
from git_module import GitModule def restart(config): git_module = GitModule( url = config.getRepoURL() ) git_module.checkout( config.getGitRef( ) ) git_module.runTests( "tests/run_tests" ) #git_module.install( ) #os.fork+++ #choose one of the following depending on how your SDS is connected #ser = serial.Serial('/dev/tty.wchusbserial1430', baudrate=9600, stopbits=1, parity="N", timeout=2) #ser = serial.Serial('/dev/ttyUSB0', baudrate=9600, stopbits=1, parity="N", timeout=2) #ser = serial.Serial('/dev/ttyAMA0', baudrate=9600, stopbits=1, parity="N", timeout=2) config = DeviceConfig( config_file ) device_id = config.getDeviceID( ) client_pm10 = FriskbyClient(config , "%s_PM10" % device_id) client_pm25 = FriskbyClient(config , "%s_PM25" % device_id) sampler = Sampler( SDS011 , sample_time = SAMPLE_TIME , sleep_time = 0.50 ) while True: data = sampler.collect( ) client_pm10.post( data[0].mean() ) client_pm25.post( data[1].mean() ) if config.updateRequired(): restart(config)
temp_c = float(temp_string) / 1000.0 else: print("Could not read temperature @ {}".format(datetime.now())) temp_c = self.temp_readings[-1] else: # Simulating a temp around 21 deg C temp_c = 21 + (random.random() * 3) - 1.5 self.temp_readings.append(temp_c) if len(self.temp_readings) > self.AVERAGE_INTERVAL: self.temp_readings = self.temp_readings[1:] temp_readings_sorted = sorted(self.temp_readings) temp_m = temp_readings_sorted[0] if len(temp_readings_sorted) > 3: temp_m = temp_readings_sorted[3] temp_c = round(temp_c, 1) temp_m = round(temp_m, 1) return temp_c, temp_m if __name__ == '__main__': device_config = DeviceConfig() reader = Temperature(device_config) for i in range(100): temp_c, temp_m = reader.get() print("read: {}, min: {}".format(temp_c, temp_m)) time.sleep(0.1)
def test_create(self): with self.assertRaises(IOError): config = DeviceConfig("file/not/found") with open(self.config_file , "w") as f: f.write("No - not valid JSON") with self.assertRaises(ValueError): deviceconfig = DeviceConfig(self.config_file) conf = {"key" : "value"} with open(self.config_file , "w") as f: f.write(json.dumps( conf )) with self.assertRaises(KeyError): config = DeviceConfig(self.config_file) conf = {"git_repo" : "github" , "git_ref" : "master" , "sensor_list" : ["A","B","C"] , "post_key" : "Key", "post_path" : "XYZ" , "server_url" : "http:???" , "config_path" : "xyz" , "device_id" : "dev0"} with open(self.config_file , "w") as f: f.write(json.dumps( conf )) config = DeviceConfig( self.config_file ) config_file2 = os.path.join( self.tmpdir , "config2") config.save( filename = config_file2 ) self.assertEqual( config.getServerURL( ) , "http:???") self.assertEqual( config.getDeviceID( ) , "dev0") config2 = DeviceConfig( config_file2 ) os.unlink( config_file2 ) config2.save( ) self.assertTrue( os.path.isfile( config_file2 ) ) self.assertTrue( "github" , config2.getRepoURL( ) ) with open("conf","w") as f: f.write(json.dumps( conf )) conf2 = {"git_repo" : "github" , "git_ref" : "master" , "sensor_list" : ["A","B","C"] , "post_key" : "KeyX", "post_path" : "XYZ" , "server_url" : "http:???" , "config_path" : "xyz" , "device_id" : "dev0"} with open("conf2","w") as f: f.write(json.dumps( conf2 )) c1 = DeviceConfig( "conf" ) c2 = DeviceConfig( "conf" ) self.assertEqual( c1 , c2 ) c2 = DeviceConfig( "conf2" ) self.assertFalse( c1 == c2 )
def test_client(self): # The post_key supplied here is not valid for anything, but we pass the "must have post_key test". device_config = DeviceConfig.download( "https://friskby.herokuapp.com/sensor/api/device/FriskPI03/", post_key="Key") client = FriskbyClient(device_config, "SensorID", "/tmp")
def test_create(self): with self.assertRaises(IOError): config = DeviceConfig("file/not/found") with open(self.config_file , "w") as f: f.write("No - not valid JSON") with self.assertRaises(ValueError): deviceconfig = DeviceConfig(self.config_file) conf = {"key" : "value"} with open(self.config_file , "w") as f: f.write(json.dumps( conf )) with self.assertRaises(KeyError): config = DeviceConfig(self.config_file) conf_no_key = {"git_follow" : True, "git_repo" : "github" , "git_ref" : "master" , "sensor_list" : ["A","B","C"] , "post_path" : "XYZ" , "server_url" : "http:???" , "config_path" : "xyz" , "device_id" : "dev0"} with open(self.config_file , "w") as f: f.write(json.dumps( conf_no_key )) with self.assertRaises(KeyError): config = DeviceConfig( self.config_file ) config = DeviceConfig( self.config_file , post_key = "Key") conf = {"git_follow" : True, "git_repo" : "github" , "git_ref" : "master" , "sensor_list" : ["A","B","C"] , "post_key" : "Key", "post_path" : "XYZ" , "server_url" : "http:???" , "config_path" : "xyz" , "device_id" : "dev0"} with open(self.config_file , "w") as f: f.write(json.dumps( conf )) config = DeviceConfig( self.config_file ) config_file2 = os.path.join( self.tmpdir , "config2") config.save( filename = config_file2 ) self.assertEqual( config.getServerURL( ) , "http:???") self.assertEqual( config.getDeviceID( ) , "dev0") config2 = DeviceConfig( config_file2 ) os.unlink( config_file2 ) config2.save( ) self.assertTrue( os.path.isfile( config_file2 ) ) self.assertTrue( "github" , config2.getRepoURL( ) ) with open("conf","w") as f: f.write(json.dumps( conf )) conf2 = {"git_follow" : True , "git_repo" : "github" , "git_ref" : "master" , "sensor_list" : ["A","B","C"] , "post_key" : "KeyX", "post_path" : "XYZ" , "server_url" : "http:???" , "config_path" : "xyz" , "device_id" : "dev0"} with open("conf2","w") as f: f.write(json.dumps( conf2 )) c1 = DeviceConfig( "conf" ) c2 = DeviceConfig( "conf" ) self.assertEqual( c1 , c2 ) self.assertFalse( c1 != c2 ) c2 = DeviceConfig( "conf2" ) self.assertFalse( c1 == c2 ) self.assertTrue( c1 != c2 ) c1.data["git_follow"] = True self.assertTrue( c1.updateRequired( ) )
def test_url_get(self): with self.assertRaises(requests.ConnectionError): deviceconfig = DeviceConfig.download( "http://does/not/exist") deviceconfig = DeviceConfig.download( "https://friskby.herokuapp.com/sensor/api/device/FriskPI03/") deviceconfig.save( filename = self.config_file )
def test_client(self): device_config = DeviceConfig.download( "https://friskby.herokuapp.com/sensor/api/device/FriskPI03/") client = FriskbyClient(device_config, "SensorID")