Ejemplo n.º 1
0
    def test_PublishLogMessages(self):
        log_test_a = ["a", "info message a", "error message a"]
        log_test_b = ["b", "info message b", "error message b"]

        # Message definition
        msg_spec = LogMessage()

        # Create a Messaging Endpoint and a MessageFormatAdapter.
        ep = Endpoint()
        adapt = MessageFormatAdapter(ep, MessageFormatAdapter.SEND_ON_COMPLETE,
                                     msg_spec)

        # Create a stream for the msg field.
        log_stream = adapt.CreateStream(LogMessage.DATA_KEY_MSG,
                                        ExtLogging.WRITES_PER_LOG)

        # Configure the ExtLogging class.
        ExtLogging.ConfigGlobal(level=ExtLogging.INFO, stream=log_stream)

        # Create ExtLogger instances.
        log_a = ExtLogging.LoggerGet(log_test_a[0])
        log_b = ExtLogging.LoggerGet(log_test_b[0])

        # Register the log message type and topic.
        self.MsgEx.RegisterMessageType(msg_spec)

        # Initialize the Messaging Service on the first run
        self.MsgEx.SvcInit()

        log_a.info(log_test_a[1])
        log_a.error(log_test_a[2])

        log_b.info(log_test_b[1])
        log_b.error(log_test_b[2])

        # Run the Service again to publish the messages.
        self.MsgEx.SvcRun()

        utime.sleep(1)

        # Run the Service again to receive the message.
        self.MsgEx.SvcRun()

        # Receive both published log messages.
        recv_msg = ep.MessageGet(msg_spec.Type, msg_spec.Subtype)
        print("Received message: {}".format(recv_msg))
        recv_msg = ep.MessageGet(msg_spec.Type, msg_spec.Subtype)
        print("Received message: {}".format(recv_msg))
Ejemplo n.º 2
0
    def test_LoggingToFileMaxLines(self):
        file = "extlog"
        name = "test"
        text = "hi"
        file_limit = 2
        line_limit = 10

        ExtLogging.ConfigGlobal(level=ExtLogging.INFO,
                                stream=TestStream(),
                                dir=self.DIR,
                                file_prefix=file,
                                line_limit=line_limit,
                                file_limit=file_limit,
                                print_enabled=True,
                                timestamp_enabled=True)

        log = ExtLogging.Create(name)
        print(log)

        for f in range(0, file_limit):
            for l in range(0, line_limit):
                log.info(text + str(l))

        ExtLogging.Mngr.PrintList()

        file_list = ExtLogging.Mngr.List()
        print(file_list)

        count = ExtLogging.Mngr.Count()
        self.assertEqual(count, file_limit)
        for i in range(0, count):
            print("[UT] Checking file: {}".format(file_list[i]))
            f = open(file_list[i], 'r')
            for l in range(0, line_limit):
                exc_occurred = False
                try:
                    line = f.readline()
                    print("[UT] Read line: {}".format(line))
                except OSError:
                    exc_occurred = True

                self.assertFalse(exc_occurred)
                self.assertTrue(text in line)
                self.assertTrue(name in line)
                self.assertTrue("[" in line)

            f.close()
Ejemplo n.º 3
0
    def SetupScanMode(self):

        self.Notifyer = Notifyer(self.RgbLed)

        # Set service dependencies.
        self.Time.SvcDependencies(
            {self.NetCon: Service.DEP_TYPE_RUN_ALWAYS_BEFORE_RUN})
        self.MoistSensor.SvcDependencies(
            {self.Time: Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN})

        self.Notifyer.SvcDependencies(
            {self.MoistSensor: Service.DEP_TYPE_RUN_ALWAYS_BEFORE_RUN})

        self.TempSensor.SvcDependencies(
            {self.Time: Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN})
        self.LightSensor.SvcDependencies(
            {self.Time: Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN})

        self.MsgEx.SvcDependencies({
            self.Time:
            Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN,
            self.NetCon:
            Service.DEP_TYPE_RUN_ALWAYS_BEFORE_INIT,
            self.TempSensor:
            Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN,
            self.LightSensor:
            Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN
        })

        # Register all services to the scheduler.
        self.Scheduler.ServiceRegister(self.Time)
        self.Scheduler.ServiceRegister(self.NetCon)
        self.Scheduler.ServiceRegister(self.MsgEx)
        self.Scheduler.ServiceRegister(self.MoistSensor)
        self.Scheduler.ServiceRegister(self.Notifyer)
        self.Scheduler.ServiceRegister(self.LightSensor)
        self.Scheduler.ServiceRegister(self.TempSensor)

        # Create message specifications.
        self.TempMsgSpec = SensorReportTemp()
        self.MoistMsgSpec = SensorReportMoist()
        self.LightMsgSpec = SensorReportLight()
        self.LogMsgSpec = LogMessage()

        # Create MessageFormatAdapters and couple them with their message specs.
        self.TempAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE,
            self.TempMsgSpec)
        self.MoistAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE,
            self.MoistMsgSpec)
        self.LightAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE,
            self.LightMsgSpec)
        self.LogAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE, self.LogMsgSpec)

        # Register message specs for exchange.
        self.MsgEx.RegisterMessageType(self.TempMsgSpec)
        self.MsgEx.RegisterMessageType(self.MoistMsgSpec)
        self.MsgEx.RegisterMessageType(self.LightMsgSpec)
        self.MsgEx.RegisterMessageType(self.LogMsgSpec)

        # Create observers for the sensor data.
        self.TempObserver = self.TempAdapt.CreateObserver(
            SensorReportTemp.DATA_KEY_SENSOR_REPORT_SAMPLES,
            self.SamplesPerMessage)
        self.MoistObserver = self.MoistAdapt.CreateObserver(
            SensorReportMoist.DATA_KEY_SENSOR_REPORT_SAMPLES,
            self.SamplesPerMessage)
        self.LightObserver = self.LightAdapt.CreateObserver(
            SensorReportLight.DATA_KEY_SENSOR_REPORT_SAMPLES,
            self.SamplesPerMessage)

        # Set the notification ranges and colors.
        moist_th_range = NotificationRange(self.MOIST_RANGE_MIN,
                                           self.MOIST_RANGE_MAX)
        moist_color_map = {
            RgbLed.RGB_COLOR_RED:
            NotificationRange(self.MOIST_RANGE_MAX, self.MOIST_RANGE_MIN),
            RgbLed.RGB_COLOR_BLUE:
            NotificationRange(self.MOIST_RANGE_MIN, self.MOIST_RANGE_MAX)
        }
        self.MoistNotif = Notification(moist_th_range, moist_color_map,
                                       self.MOIST_NOTIF_PRIO,
                                       self.MOIST_NOTIF_TIME)

        self.Notifyer.NotificationRegister(self.MoistNotif)

        # Link the observers to the sensors.
        self.TempSensor.ObserverAttachNewSample(self.TempObserver)
        self.MoistSensor.ObserverAttachNewSample(self.MoistObserver)
        self.MoistSensor.ObserverAttachNewSample(self.MoistNotif)
        self.LightSensor.ObserverAttachNewSample(self.LightObserver)

        # Create a stream for the log messages.
        self.LogStream = self.LogAdapt.CreateStream(
            LogMessage.DATA_KEY_LOG_MSG, ExtLogging.WRITES_PER_LOG)

        # Configure the ExtLogging class.
        ExtLogging.ConfigGlobal(level=ExtLogging.INFO, stream=self.LogStream)

        # Set service modes.
        self.MsgEx.SvcMode = Service.MODE_RUN_ONCE
        self.TempSensor.SvcMode = Service.MODE_RUN_ONCE
        self.LightSensor.SvcMode = Service.MODE_RUN_ONCE

        # Set intervals for periodic services.
        self.MoistSensor.SvcIntervalSet(self.MoistSensorReadInterval)
        self.Notifyer.SvcIntervalSet(self.NotificationInterval)
Ejemplo n.º 4
0
    def SetupCalibMode(self):

        # Set service dependencies.
        self.Time.SvcDependencies(
            {self.NetCon: Service.DEP_TYPE_RUN_ALWAYS_BEFORE_RUN})
        self.MsgEx.SvcDependencies({
            self.Time:
            Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN,
            self.NetCon:
            Service.DEP_TYPE_RUN_ALWAYS_BEFORE_INIT
        })
        self.MoistSensor.SvcDependencies(
            {self.Time: Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN})

        # Register all services to the scheduler.
        self.Scheduler.ServiceRegister(self.Time)
        self.Scheduler.ServiceRegister(self.NetCon)
        self.Scheduler.ServiceRegister(self.MsgEx)
        self.Scheduler.ServiceRegister(self.MoistSensor)

        # Create message specifications.
        self.MoistCalibLowMsgSpec = SensorReportMoistCalibLow()
        self.MoistCalibHighMsgSpec = SensorReportMoistCalibHigh()
        self.LogMsgSpec = LogMessage()

        # Create MessageFormatAdapters and couple them with their message specs.
        self.MoistCalibLowAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE,
            self.MoistCalibLowMsgSpec)
        self.MoistCalibHighAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE,
            self.MoistCalibHighMsgSpec)
        self.LogAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE, self.LogMsgSpec)

        # Register message specs for exchange.
        self.MsgEx.RegisterMessageType(self.MoistCalibLowMsgSpec)
        self.MsgEx.RegisterMessageType(self.MoistCalibHighMsgSpec)
        self.MsgEx.RegisterMessageType(self.LogMsgSpec)

        self.CalibObserver = CalibrationObserver()

        self.CalibLowStream = self.MoistCalibLowAdapt.CreateStream(
            SensorReportMoistCalibLow.DATA_KEY_SENSOR_REPORT_SAMPLES,
            self.SamplesPerMessage)
        self.CalibHighStream = self.MoistCalibHighAdapt.CreateStream(
            SensorReportMoistCalibHigh.DATA_KEY_SENSOR_REPORT_SAMPLES,
            self.SamplesPerMessage)

        # Link the observers to the sensors.
        self.MoistSensor.ObserverAttachNewSample(self.CalibObserver)

        # Create a stream for the log messages.
        self.LogStream = self.LogAdapt.CreateStream(
            LogMessage.DATA_KEY_LOG_MSG, ExtLogging.WRITES_PER_LOG)

        # Configure the ExtLogging class.
        ExtLogging.ConfigGlobal(level=ExtLogging.INFO, stream=self.LogStream)

        # Set service modes.
        self.MsgEx.SvcMode = Service.MODE_RUN_ONCE

        # Set intervals for periodic services.
        self.MoistSensor.SvcIntervalSet(self.MoistSensorReadInterval)

        return
    def setUp(self):
        # Configure the URL fields.
        MessageSpecification.Config(self.UrlFields)

        # Create objects.
        filter_depth = len(self.TempSamples) / 2
        dummy_temp_sensor = DummySensor.DummySensor(self.TempSamples)
        dummy_moist_sensor = DummySensor.DummySensor(self.MoistSamples)
        wlan_ap = WLAN()
        self.Time = SystemTime.InstanceGet()
        self.NetCon = NetCon(self.DIR, self.ApCfg, NetCon.MODE_STATION,
                             wlan_ap)
        self.TempSensor = Sensor.Sensor(self.DIR, SensorReportTemp.NAME_TEMP,
                                        filter_depth, dummy_temp_sensor)
        self.MoistSensor = Sensor.Sensor(self.DIR,
                                         SensorReportMoist.NAME_MOIST,
                                         filter_depth, dummy_moist_sensor)
        self.MqttClient = MQTTClient(self.ID, self.BROKER, self.PORT)
        self.MsgEx = MessageExchange(self.DIR, self.MqttClient, self.ID,
                                     self.RETRIES)
        self.MsgEp = Endpoint()
        self.Scheduler = ServiceScheduler()

        # Set service dependencies.
        self.Time.SvcDependencies(
            {self.NetCon: Service.DEP_TYPE_RUN_ALWAYS_BEFORE_RUN})
        self.MsgEx.SvcDependencies({
            self.Time:
            Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN,
            self.NetCon:
            Service.DEP_TYPE_RUN_ALWAYS_BEFORE_INIT
        })
        self.TempSensor.SvcDependencies(
            {self.Time: Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN})
        self.MoistSensor.SvcDependencies(
            {self.Time: Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN})

        # Register all services to the scheduler.
        self.Scheduler.ServiceRegister(self.Time)
        self.Scheduler.ServiceRegister(self.NetCon)
        self.Scheduler.ServiceRegister(self.MsgEx)
        self.Scheduler.ServiceRegister(self.TempSensor)
        self.Scheduler.ServiceRegister(self.MoistSensor)

        # Create message specifications.
        self.TempMsgSpec = SensorReportTemp()
        self.MoistMsgSpec = SensorReportMoist()
        self.LogMsgSpec = LogMessage()

        # Create a Messaging Endpoint and MessageFormatAdapters.
        self.TempAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE,
            self.TempMsgSpec)
        self.MoistAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE,
            self.MoistMsgSpec)
        self.LogAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE, self.LogMsgSpec)

        # Register message specs.
        self.MsgEx.RegisterMessageType(self.TempMsgSpec)
        self.MsgEx.RegisterMessageType(self.MoistMsgSpec)
        self.MsgEx.RegisterMessageType(self.LogMsgSpec)

        # Create observers for the sensor data.
        self.TempObserver = self.TempAdapt.CreateObserver(
            SensorReportTemp.DATA_KEY_SENSOR_REPORT_ARRAY,
            self.SamplesPerMessage)
        self.MoistObserver = self.MoistAdapt.CreateObserver(
            SensorReportMoist.DATA_KEY_SENSOR_REPORT_ARRAY,
            self.SamplesPerMessage)

        # Link the observers to the sensors.
        self.TempSensor.ObserverAttachNewSample(self.TempObserver)
        self.MoistSensor.ObserverAttachNewSample(self.MoistObserver)

        # Create a stream for the log messages.
        self.LogStream = self.LogAdapt.CreateStream(
            LogMessage.DATA_KEY_LOG_MSG, ExtLogging.WRITES_PER_LOG)

        # Configure the ExtLogging class.
        ExtLogging.ConfigGlobal(level=ExtLogging.INFO, stream=self.LogStream)

        # Configure the station settings to connect to a WLAN AP.
        self.NetCon.StationSettingsStore(self.ApCfg["ssid"], self.ApCfg["pwd"])

        # Declare test variables.
        self.RecvMsgCount = 0
        self.RecvTopic = None
        self.RecvMsg = None
Ejemplo n.º 6
0
    def Setup(self):

        # Create driver instances.
        self.CapMoist = CapMoisture(Pins.CFG_HW_PIN_MOIST,
                                    Pins.CFG_HW_PIN_MOIST_EN)
        self.Mcp9700Temp = Mcp9700Temp(Pins.CFG_HW_PIN_TEMP,
                                       Pins.CFG_HW_PIN_TEMP_EN)
        self.Ldr = PhTLight(Pins.CFG_HW_PIN_LDR, Pins.CFG_HW_PIN_LDR_EN)
        # self.VBat = BatteryLevel(1, Pins.CFG_HW_PIN_VBAT_MEAS, Pins.CFG_HW_PIN_VBAT_MEAS_EN)

        self.RgbLed = RgbLed(Pins.CFG_HW_PIN_LED_RED,
                             Pins.CFG_HW_PIN_LED_GREEN,
                             Pins.CFG_HW_PIN_LED_BLUE)

        self.Button = UserButton(Pins.CFG_HW_PIN_BTN_0)

        # Create the WLAN station interface.
        wlan_ap = WLAN(network.STA_IF)

        self.Time = SystemTime.InstanceGet()
        self.NetCon.WlanInterface(wlan_ap, NetCon.MODE_STATION)

        self.TempSensor = Sensor.Sensor(self.DIR, SensorReportTemp.NAME_TEMP,
                                        self.FILTER_DEPTH, self.Mcp9700Temp,
                                        self.FILTER_DEPTH)

        self.MoistSensor = Sensor.Sensor(self.DIR,
                                         SensorReportMoist.NAME_MOIST,
                                         self.FILTER_DEPTH, self.CapMoist,
                                         self.FILTER_DEPTH)

        self.LightSensor = Sensor.Sensor(self.DIR,
                                         SensorReportLight.NAME_LIGHT,
                                         self.FILTER_DEPTH, self.PhTLight,
                                         self.FILTER_DEPTH)

        self.MqttClient = MQTTClient(self.ID, self.BROKER, self.PORT)
        self.MsgEx = MessageExchange(self.DIR, self.MqttClient, self.ID,
                                     self.RETRIES)

        self.Notifyer = Notifyer(self.RgbLed)

        self.MsgEp = Endpoint()

        self.Scheduler = ServiceScheduler(self.DEEPSLEEP_THRESHOLD_SEC)

        # Set service dependencies.
        self.Time.SvcDependencies(
            {self.NetCon: Service.DEP_TYPE_RUN_ALWAYS_BEFORE_RUN})
        self.MsgEx.SvcDependencies({
            self.Time:
            Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN,
            self.NetCon:
            Service.DEP_TYPE_RUN_ALWAYS_BEFORE_INIT
        })
        self.TempSensor.SvcDependencies(
            {self.Time: Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN})
        self.MoistSensor.SvcDependencies(
            {self.Time: Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN})
        self.LightSensor.SvcDependencies(
            {self.Time: Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN})
        self.Notifyer.SvcDependencies(
            {self.MoistSensor: Service.DEP_TYPE_RUN_ALWAYS_BEFORE_RUN})

        # Register all services to the scheduler.
        self.Scheduler.ServiceRegister(self.Time)
        self.Scheduler.ServiceRegister(self.NetCon)
        self.Scheduler.ServiceRegister(self.MsgEx)
        self.Scheduler.ServiceRegister(self.TempSensor)
        self.Scheduler.ServiceRegister(self.MoistSensor)
        self.Scheduler.ServiceRegister(self.LightSensor)
        self.Scheduler.ServiceRegister(self.Notifyer)

        # Create message specifications.
        self.TempMsgSpec = SensorReportTemp()
        self.MoistMsgSpec = SensorReportMoist()
        self.LightMsgSpec = SensorReportLight()
        self.LogMsgSpec = LogMessage()

        # Create MessageFormatAdapters and couple them with their message specs.
        # self.TempAdapt = MessageFormatAdapter(self.MsgEp,
        #                                       MessageFormatAdapter.SEND_ON_COMPLETE,
        #                                       self.TempMsgSpec)
        self.MoistAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE,
            self.MoistMsgSpec)
        self.LightAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE,
            self.LightMsgSpec)
        self.LogAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE, self.LogMsgSpec)

        # Register message specs for exchange.
        # self.MsgEx.RegisterMessageType(self.TempMsgSpec)
        self.MsgEx.RegisterMessageType(self.MoistMsgSpec)
        self.MsgEx.RegisterMessageType(self.LightMsgSpec)
        self.MsgEx.RegisterMessageType(self.LogMsgSpec)

        # Create observers for the sensor data.
        self.TempObserver = self.TempAdapt.CreateObserver(
            SensorReportTemp.DATA_KEY_SENSOR_REPORT_SAMPLES,
            self.SamplesPerMessage)
        self.MoistObserver = self.MoistAdapt.CreateObserver(
            SensorReportMoist.DATA_KEY_SENSOR_REPORT_SAMPLES,
            self.SamplesPerMessage)
        self.LightObserver = self.LightAdapt.CreateObserver(
            SensorReportLight.DATA_KEY_SENSOR_REPORT_SAMPLES,
            self.SamplesPerMessage)

        # Set the notification ranges and colors.
        moist_th_range = NotificationRange(self.MOIST_RANGE_MIN,
                                           self.MOIST_RANGE_MAX)
        moist_color_map = {
            RgbLed.RGB_COLOR_RED:
            NotificationRange(self.MOIST_RANGE_MAX, self.MOIST_RANGE_MIN),
            RgbLed.RGB_COLOR_BLUE:
            NotificationRange(self.MOIST_RANGE_MIN, self.MOIST_RANGE_MAX)
        }
        self.MoistNotif = Notification(moist_th_range, moist_color_map,
                                       self.MOIST_NOTIF_PRIO,
                                       self.MOIST_NOTIF_TIME)

        self.Notifyer.NotificationRegister(self.MoistNotif)

        # Link the observers to the sensors.
        self.TempSensor.ObserverAttachNewSample(self.TempObserver)
        self.MoistSensor.ObserverAttachNewSample(self.MoistObserver)
        self.MoistSensor.ObserverAttachNewSample(self.MoistNotif)
        self.LightSensor.ObserverAttachNewSample(self.LightObserver)

        # Create a stream for the log messages.
        self.LogStream = self.LogAdapt.CreateStream(
            LogMessage.DATA_KEY_LOG_MSG, ExtLogging.WRITES_PER_LOG)

        # Configure the ExtLogging class.
        ExtLogging.ConfigGlobal(level=ExtLogging.INFO, stream=self.LogStream)

        # Set intervals for all services.
        self.MsgEx.SvcIntervalSet(self.MsgExInterval)
        self.MoistSensor.SvcIntervalSet(self.EnvSensorReadInterval)
        self.TempSensor.SvcIntervalSet(self.EnvSensorReadInterval)
        self.LightSensor.SvcIntervalSet(self.EnvSensorReadInterval)
        self.Notifyer.SvcIntervalSet(self.NotificationInterval)
Ejemplo n.º 7
0
    def Setup(self):

        for dir in self.DIR_TREE.values():
            try:
                uos.mkdir(dir)
            except OSError:
                print("Cannot create directory '{}'".format(dir))

        # Configure the ExtLogging class.
        ExtLogging.ConfigGlobal(level=ExtLogging.DEBUG,
                                stream=None,
                                dir=self.DIR_TREE[self.DIR_LOG],
                                file_prefix="log_",
                                line_limit=1000,
                                file_limit=10)

        StructFile.SetLogger(ExtLogging.Create("SFile"))

        self.Log = ExtLogging.Create("Main")

        self.Log.info("Device ID: {}".format(DeviceId.DeviceId()))

        Version(self.DIR_TREE[self.DIR_SYS], self.VER_MAJOR, self.VER_MINOR,
                self.VER_PATCH)

        rst_reason = ResetReason.ResetReason()
        self.Log.debug("Reset reason: {}".format(
            ResetReason.ResetReasonToString(rst_reason)))

        # Create driver instances.
        self.DummySensorDriver = DummySensor(self.DummySamples)

        self.InternalTemp = InternalTemp()

        # TODO: Enable actual sensor drivers.
        # self.TempSensorDriver = Mcp9700Temp(temp_pin_nr=Pins.CFG_HW_PIN_TEMP,
        #                                     en_supply_obj=Supply(Pins.CFG_HW_PIN_TEMP_EN, 3.3, 300))
        #
        # self.VBatSensorDriver = VoltageSensor(pin_nr=Pins.CFG_HW_PIN_VBAT_LVL,
        #                                       en_supply_obj=Supply(Pins.CFG_HW_PIN_VBAT_LVL_EN, 3.3, 300))

        if self.NETWORK is self.KPN:
            self.LoraProtocol = LoraProtocol(
                self.KpnLoraConfig, directory=self.DIR_TREE[self.DIR_LORA])
        elif self.NETWORK is self.TTN:
            self.LoraProtocol = LoraProtocol(
                self.TtnLoraConfig, directory=self.DIR_TREE[self.DIR_LORA])
            if self.NETWORK_REG is self.ABP:
                self.LoraProtocol.Params.StoreSession(self.DevAddr,
                                                      self.AppSKey,
                                                      self.NwkSKey)
        else:
            raise Exception("No valid network LoRa selected.")

        self.DummySensor = Sensor.Sensor(self.DIR_TREE[self.DIR_SENSOR],
                                         "Dummy",
                                         self.FILTER_DEPTH,
                                         self.DummySensorDriver,
                                         samples_per_update=3,
                                         dec_round=True,
                                         store_data=True)

        self.TempSensor = Sensor.Sensor(
            self.DIR_TREE[self.DIR_SENSOR],
            "Temp",
            self.FILTER_DEPTH,
            self.
            InternalTemp,  # TODO: Replace InternalTemp driver with TempSensorDriver
            samples_per_update=2,
            dec_round=True,
            store_data=True)

        # self.BatteryVoltageSensor = Sensor.Sensor(self.DIR_TREE[self.DIR_SENSOR],
        #                                           "BatLvl",
        #                                           self.FILTER_DEPTH,
        #                                           self.VBatSensorDriver,
        #                                           samples_per_update=2,
        #                                           dec_round=False,
        #                                           store_data=True)

        self.MsgEx = MessageExchange(directory=self.DIR_TREE[self.DIR_MSG],
                                     proto_obj=self.LoraProtocol,
                                     send_retries=self.RETRIES,
                                     msg_size_max=self.LoraProtocol.Mtu,
                                     msg_send_limit=self.SEND_LIMIT)

        # Create the registration info spec and Registration service.
        # Link the Registration service to the Message Exchange service. The Message Exchange
        # service will activate the Registration service when it connects to the LoRa network.
        self.RegistrationInfo = RegistrationInfo()
        self.Registration = Registration(self.MsgEx, self.RegistrationInfo)
        self.MsgEx.AttachConnectionStateObserver(self.Registration)

        self.Scheduler = ServiceScheduler(
            deepsleep_threshold_sec=self.DEEPSLEEP_THRESHOLD_SEC,
            # deep_sleep_obj=PowerManager.PowerManager(),
            directory=self.DIR_TREE[self.DIR_SYS])

        # Set service dependencies.
        # There are no hard dependencies between the services.

        # Register all services to the scheduler.
        self.Scheduler.ServiceRegister(self.DummySensor)
        self.Scheduler.ServiceRegister(self.TempSensor)
        # self.Scheduler.ServiceRegister(self.BatteryVoltageSensor)
        self.Scheduler.ServiceRegister(self.MsgEx)
        self.Scheduler.ServiceRegister(self.Registration)

        Message.SetParser(CborParser())
        MessageTemplate.SectionsSet(Metadata.MSG_SECTION_META,
                                    Metadata.MSG_SECTION_DATA)
        MessageTemplate.MetadataTemplateSet(Metadata.Metadata,
                                            Metadata.MetadataFuncs)

        # Create message specifications.
        self.MoistReport = MoistureSensorReport()
        self.BatteryReport = BatterySensorReport()
        self.TempReport = TemperatureSensorReport()

        # Create MessageFormatters and couple them with their message specs.
        moist_report_meta = {
            Metadata.MSG_META_TYPE: self.MoistReport.Type,
            Metadata.MSG_META_SUBTYPE: self.MoistReport.Subtype,
        }
        self.MoistFmt = MessageFormatter(self.MsgEx,
                                         MessageFormatter.SEND_ON_CHANGE,
                                         self.MoistReport, moist_report_meta)

        battery_report_meta = {
            Metadata.MSG_META_TYPE: self.BatteryReport.Type,
            Metadata.MSG_META_SUBTYPE: self.BatteryReport.Subtype,
        }
        self.BatteryFmt = MessageFormatter(self.MsgEx,
                                           MessageFormatter.SEND_ON_CHANGE,
                                           self.BatteryReport,
                                           battery_report_meta)

        temp_report_meta = {
            Metadata.MSG_META_TYPE: self.TempReport.Type,
            Metadata.MSG_META_SUBTYPE: self.TempReport.Subtype,
        }
        self.TempFmt = MessageFormatter(self.MsgEx,
                                        MessageFormatter.SEND_ON_CHANGE,
                                        self.TempReport, temp_report_meta)

        # Register message specs for exchange.
        self.MsgEx.RegisterMessageType(self.MoistReport)
        self.MsgEx.RegisterMessageType(self.BatteryReport)
        self.MsgEx.RegisterMessageType(self.TempReport)
        self.MsgEx.RegisterMessageType(self.RegistrationInfo)

        # Create observers for the sensor data.
        self.MoistObserver = self.MoistFmt.CreateObserver(
            MoistureSensorReport.DATA_KEY_MEASUREMENTS)
        self.BatteryObserver = self.BatteryFmt.CreateObserver(
            BatterySensorReport.DATA_KEY_MEASUREMENTS)
        self.TempObserver = self.TempFmt.CreateObserver(
            TemperatureSensorReport.DATA_KEY_MEASUREMENTS)

        # Link the observers to the sensors.
        self.DummySensor.ObserverAttachNewSample(self.MoistObserver)
        self.TempSensor.ObserverAttachNewSample(self.TempObserver)

        self.Scheduler.RegisterCallbackBeforeDeepSleep(MainApp.BeforeSleep)

        # Set intervals for all services.
        self.MsgEx.SvcIntervalSet(self.MsgExInterval)
        self.MsgEx.DefaultIntervalSet(self.MsgExInterval)
        self.DummySensor.SvcIntervalSet(self.MoistReadInterval)
        self.TempSensor.SvcIntervalSet(self.SensorReadInterval)

        # Activate the Message Exchange to attempt to connect to the LoRa network
        # if no LoRaWAN session exists yet.
        if self.LoraProtocol.HasSession() is False:
            self.MsgEx.SvcActivate()

        # self.BatteryObserver.Update(100)

        self.Log.info("Finished initialization.")