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_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(c2))
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)