def start(self): # Make environment compatible with CPython. patch_system() log.info('Starting bootloader') self.bootloader = start_bootloader() log.info('Loading modules') from terkin.datalogger import TerkinDatalogger settings = load_settings(settings_file=self.config) log.info('Setting up Terkin') self.datalogger = TerkinDatalogger( settings, platform_info=self.bootloader.platform_info) self.datalogger.setup() log.info('Starting Terkin') try: if self.daemon: self.datalogger.start() else: self.datalogger.duty_cycle() except KeyboardInterrupt: self.shutdown()
def test_basic_cpython(caplog): # Use very basic settings without networking. import test.settings.basic as settings with FakeFS(): with caplog.at_level(logging.DEBUG): # Invoke bootloader. bootloader = invoke_umal() # Start datalogger with a single duty cycle on a fake filesystem. from terkin.datalogger import TerkinDatalogger datalogger = TerkinDatalogger( settings, platform_info=bootloader.platform_info) datalogger.setup() datalogger.duty_task() # Capture log output. captured = caplog.text # Proof it works by verifying log output. assert "Starting Terkin datalogger" in captured, captured assert "platform: linux2" in captured, captured
def test_basic_esp32(caplog): # Use very basic settings without networking. import test.settings.basic as settings with FakeFS(): with caplog.at_level(logging.DEBUG): # Invoke bootloader. bootloader = invoke_umal() # Start datalogger with a single duty cycle on a fake filesystem. from terkin.datalogger import TerkinDatalogger datalogger = TerkinDatalogger( settings, platform_info=bootloader.platform_info) datalogger.setup() datalogger.duty_task() # Capture log output. captured = caplog.text # Proof it works by verifying log output. assert "Starting Terkin datalogger" in captured, captured assert "platform: esp32" in captured, captured assert "[WiFi] Interface not enabled in settings." in captured, captured assert "[LoRa] Interface not enabled in settings." in captured, captured assert "[GPRS] Interface not enabled in settings." in captured, captured assert "Reading 0 sensor ports" in captured, captured assert "Sensor data: {}" in captured, captured assert "Telemetry status: SUCCESS (0/0)" in captured, captured
def test_wifi_cpython(monkeypatch, caplog): # Define platform and start bootloader. monkeypatch.setattr(sys, 'platform', 'linux2') bootloader = invoke_umal() # Use very basic settings without networking. import test.settings.wifi as settings # Start datalogger with a single duty cycle on a fake filesystem. from terkin.datalogger import TerkinDatalogger with FakeFS(): with caplog.at_level(logging.DEBUG): datalogger = TerkinDatalogger(settings, platform_info=bootloader.platform_info) datalogger.setup() datalogger.duty_task() # Capture log output. captured = caplog.text # Proof it works by verifying log output. assert "Starting Terkin datalogger" in captured, captured assert "platform: linux2" in captured, captured assert "WiFi STA: Preparing connection to network \"FooBarWiFi\"" in captured, captured assert "WiFi STA: Connected to \"FooBarWiFi\"" in captured, captured assert "Network stack ready" in captured, captured
def test_wifi_esp32(monkeypatch, caplog): # Define platform and start bootloader. monkeypatch.setattr(sys, 'platform', 'esp32') bootloader = invoke_umal() # Use very basic settings without networking. import test.settings.wifi as settings # Start datalogger with a single duty cycle on a fake filesystem. from terkin.datalogger import TerkinDatalogger with FakeFS(): with caplog.at_level(logging.DEBUG): datalogger = TerkinDatalogger(settings, platform_info=bootloader.platform_info) datalogger.setup() datalogger.duty_task() # Capture log output. captured = caplog.text # Proof it works by verifying log output. assert "Starting Terkin datalogger" in captured, captured assert "platform: esp32" in captured, captured assert "WiFi STA: Preparing connection to network \"FooBarWiFi\"" in captured, captured assert "WiFi STA: Connected to \"FooBarWiFi\"" in captured, captured assert "Network stack ready" in captured, captured assert "[LoRa] Interface not enabled in settings" in captured, captured assert "[GPRS] Interface not enabled in settings" in captured, captured assert "Reading 0 sensor ports" in captured, captured assert "Sensor data: {}" in captured, captured assert "Telemetry status: SUCCESS (0/0)" in captured, captured
def main(): """Start the data logger application.""" print('[main.py] INFO: Loading Terkin Datalogger') global bootloader global datalogger print('[main.py] INFO: Loading modules') from terkin.datalogger import TerkinDatalogger datalogger = TerkinDatalogger(settings, platform_info=bootloader.platform_info) datalogger.setup() try: datalogger.start() except KeyboardInterrupt: log.info("Received KeyboardInterrupt within main thread") if datalogger.device.networking is not None: datalogger.device.networking.stop_modeserver() datalogger.device.networking.wifi_manager.stop() # This helps the webserver to get rid of any listening sockets. # https://github.com/jczic/MicroWebSrv2/issues/8 datalogger.device.networking.stop_httpserver()
class TerkinApplication: """Start the data logger application.""" def __init__(self, config=None, daemon=False): self.config = config self.daemon = daemon self.bootloader = None self.datalogger = None def start(self): # Make environment compatible with CPython. patch_system() log.info('Starting bootloader') self.bootloader = start_bootloader() log.info('Loading modules') from terkin.datalogger import TerkinDatalogger settings = load_settings(settings_file=self.config) log.info('Setting up Terkin') self.datalogger = TerkinDatalogger( settings, platform_info=self.bootloader.platform_info) self.datalogger.setup() log.info('Starting Terkin') try: if self.daemon: self.datalogger.start() else: self.datalogger.duty_cycle() except KeyboardInterrupt: self.shutdown() def shutdown(self): log.info('Shutting down Terkin') if self.datalogger.device.status.networking: self.datalogger.device.networking.stop() def start_ui(self): # Make environment compatible with CPython. patch_system() from terkin.ui import TerkinUi ui = TerkinUi() try: ui.start() ui.example_listbox() #ui.example_menu() finally: ui.stop()
def test_uplink_wifi_mqtt(mosquitto, caplog, capmqtt): # Acquire settings with MQTT telemetry. import test.settings.telemetry_mqtt as settings # Use a fake filesystem. with FakeFS(): with caplog.at_level(logging.DEBUG): # Invoke bootloader. bootloader = invoke_umal() # Invoke datalogger with a single duty cycle. from terkin.datalogger import TerkinDatalogger datalogger = TerkinDatalogger( settings, platform_info=bootloader.platform_info) datalogger.setup() datalogger.duty_task() # Capture log output. captured = caplog.text # Proof it works by verifying log output. assert "Starting Terkin datalogger" in captured, captured assert "platform: esp32" in captured, captured assert "WiFi STA: Preparing connection to network \"FooBarWiFi\"" in captured, captured assert "WiFi STA: Connected to \"FooBarWiFi\"" in captured, captured assert "Network stack ready" in captured, captured assert "Reading 4 sensor ports" in captured, captured #assert "Sensor data: {}" in captured, captured assert "MQTT topic: mqttkit-1/testdrive/area-42/node-01/data.json" in captured, captured assert 'MQTT payload: {"system.memfree": 1000000, "system.temperature": 44.7053182608696' in captured, captured assert "Telemetry status: SUCCESS (1/1)" in captured, captured data_reference = { "system.memfree": 1000000, "system.temperature": 44.7053182608696, "system.wifi.rssi": -85.3, } # Wait some time for message to arrive. time.sleep(0.2) message_1 = capmqtt.buffer()[0] data_1 = json.loads(message_1) del data_1['system.time'] del data_1['system.uptime'] del data_1['system.runtime'] assert data_1 == data_reference, capmqtt.buffer()
def invoke_datalogger(caplog, settings, pycom=False, raspberrypi=False): # Use a fake filesystem. with FakeFS(): # Pycom mounts the main filesystem at "/flash". if pycom: os.mkdir('/flash') if raspberrypi: os.makedirs('/sys/firmware/devicetree/base') with open('/sys/firmware/devicetree/base/model', 'w') as f: f.write('Raspberry') os.makedirs('/sys/bus/w1/devices/w1_bus_master1') with open('/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves', 'w') as f: f.writelines(['28-ff641d8fdf18c1\n', '28-ff641d8fc3944f\n']) os.makedirs('/sys/bus/w1/devices/w1_bus_master1/28-ff641d8fdf18c1') with open( '/sys/bus/w1/devices/w1_bus_master1/28-ff641d8fdf18c1/w1_slave', 'w') as f: f.write('YES\nt=48187') os.makedirs('/sys/bus/w1/devices/w1_bus_master1/28-ff641d8fc3944f') with open( '/sys/bus/w1/devices/w1_bus_master1/28-ff641d8fc3944f/w1_slave', 'w') as f: f.write('YES\nt=48187') os.makedirs('/dev') with open('/dev/i2c-3', 'w') as f: f.write('nonsense') # Capture log output. with caplog.at_level(logging.DEBUG): # Invoke bootloader. bootloader = invoke_umal() # Invoke datalogger with a single duty cycle. from terkin.datalogger import TerkinDatalogger datalogger = TerkinDatalogger( settings, platform_info=bootloader.platform_info) datalogger.setup() datalogger.duty_cycle() return datalogger
def main(): """Start the data logger application.""" print('[main.py] INFO: Loading Terkin Datalogger') global bootloader global datalogger print('[main.py] INFO: Loading modules') from terkin.datalogger import TerkinDatalogger # Setup datalogger. datalogger = TerkinDatalogger(settings, platform_info=bootloader.platform_info) datalogger.setup() # Invoke datalogger. try: datalogger.start() except KeyboardInterrupt: log.info("Received KeyboardInterrupt")