Example #1
0
    def test_RunApMode(self):
        wlan_ap = WLAN()
        netcon = NetCon(self.DIR, self.ApCfg, NetCon.MODE_ACCESS_POINT,
                        wlan_ap)

        netcon.SvcRun()

        self.assertTrue(wlan_ap.active())

        netcon.StationSettingsReset()
Example #2
0
    def test_Constructor(self):
        wlan_ap = WLAN()
        netcon = NetCon(self.DIR, self.ApCfg, NetCon.MODE_ACCESS_POINT,
                        wlan_ap)

        self.assertIsInstance(netcon, NetCon)
        self.assertIsInstance(netcon, Service)
        self.assertEqual(netcon.WlanMode, NetCon.MODE_ACCESS_POINT)
        self.assertEqual(netcon.SvcMode, NetConService.NET_CON_SERVICE_MODE)

        netcon.StationSettingsReset()
Example #3
0
    def test_DeinitApMode(self):
        wlan_ap = WLAN()
        netcon = NetCon(self.DIR, self.ApCfg, NetCon.MODE_ACCESS_POINT,
                        wlan_ap)

        netcon.SvcRun()
        netcon.SvcDeinit()

        self.assertFalse(wlan_ap.active())
        self.assertFalse(wlan_ap.isconnected())

        netcon.StationSettingsReset()
Example #4
0
    def test_RunStationModeNoSettings(self):
        wlan_ap = WLAN()
        netcon = NetCon(self.DIR, self.ApCfg, NetCon.MODE_STATION, wlan_ap)

        exc_occurred = False
        try:
            netcon.SvcRun()
        except NetConExceptionNoStationSettings:
            exc_occurred = True

        self.assertTrue(exc_occurred)

        netcon.StationSettingsReset()
Example #5
0
    def test_StationSettingsStoreLoad(self):
        wlan_ap = WLAN()
        netcon = NetCon(self.DIR, self.ApCfg, NetCon.MODE_ACCESS_POINT,
                        wlan_ap)

        netcon.StationSettingsStore(self.ApCfg["ssid"], self.ApCfg["pwd"])

        ssid, pwd = netcon.StationSettingsLoad()

        self.assertEqual(self.ApCfg["ssid"], ssid)
        self.assertEqual(self.ApCfg["pwd"], pwd)
        self.assertTrue(
            TestUtil.FileExists(netcon.RootDir + NetCon.PATH_NET_CON))

        netcon.StationSettingsReset()
Example #6
0
    def test_DeinitStationMode(self):
        wlan_ap = WLAN()
        netcon = NetCon(self.DIR, self.ApCfg, NetCon.MODE_STATION, wlan_ap)

        netcon.StationSettingsStore(self.ApCfg["ssid"], self.ApCfg["pwd"])
        netcon.SvcRun()
        netcon.SvcDeinit()

        self.assertFalse(wlan_ap.active())
        self.assertFalse(netcon.IsConnected())

        netcon.StationSettingsReset()
Example #7
0
    def test_RunStationModeConnectAfterRetries(self):
        wlan_ap = WLAN()
        netcon = NetCon(self.DIR, self.ApCfg, NetCon.MODE_STATION, wlan_ap)

        wlan_ap.retries_set(NetCon.CONN_RETRIES - 1)
        netcon.StationSettingsStore(self.ApCfg["ssid"], self.ApCfg["pwd"])
        netcon.SvcRun()

        self.assertTrue(wlan_ap.active())
        self.assertTrue(netcon.IsConnected())

        netcon.StationSettingsReset()
Example #8
0
    def test_RunStationModeAlreadyConnected(self):
        wlan_ap = WLAN()
        netcon = NetCon(self.DIR, self.ApCfg, NetCon.MODE_STATION, wlan_ap)

        netcon.StationSettingsStore(self.ApCfg["ssid"], self.ApCfg["pwd"])
        netcon.SvcRun()

        exc_occurred = False
        try:
            netcon.SvcRun()
        except ServiceExceptionSuspend:
            exc_occurred = True

        self.assertTrue(exc_occurred)
        self.assertTrue(wlan_ap.active())
        self.assertTrue(netcon.IsConnected())

        netcon.StationSettingsReset()
class test_ComponentIntegration(unittest.TestCase):

    PRODUCT_NAME = "smartsensor"
    DIR = "./"
    ID = '3f7e12c9'
    RETRIES = 3
    BROKER = '192.168.0.103'
    PORT = 1883
    ApCfg = {"ssid": "test", "pwd": "123", "ip": "127.0.0.1"}

    MqttClient = None
    MsgEx = None
    Time = None
    UrlFields = {
        MessageSpecification.URL_FIELD_DEVICE_ID: ID,
        MessageSpecification.URL_FIELD_PRODUCT_NAME: PRODUCT_NAME
    }

    TempSamples = [20, 21, 25, 30, 35, 35, 20, 12, 10, 40]
    MoistSamples = [200, 300, 350, 360, 290, 500, 250, 300, 240, 320]

    SamplesPerMessage = const(1)
    MsgExInterval = const(20)
    SensorReadInterval = const(10)

    RecvTopic = None
    RecvMsg = None
    RecvMsgCount = 0

    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

    def tearDown(self):
        self.MsgEx.Reset()
        self.TempSensor.SamplesDelete()
        self.MoistSensor.SamplesDelete()
        self.NetCon.StationSettingsReset()
        self.Scheduler.Memory.Delete()

    @staticmethod
    def MqttMsgRecvCallback(topic, msg):
        test_ComponentIntegration.RecvTopic = topic
        test_ComponentIntegration.RecvMsg = msg
        test_ComponentIntegration.RecvMsgCount = test_ComponentIntegration.RecvMsgCount + 1

    def test_RunComponentIntegration(self):

        self.MsgEx.SvcIntervalSet(self.MsgExInterval)
        self.MoistSensor.SvcIntervalSet(self.SensorReadInterval)
        self.TempSensor.SvcIntervalSet(self.SensorReadInterval)

        n_deepsleep = 0
        deepsleep = True
        n = 10
        while deepsleep is True:
            deepsleep = False
            try:
                self.Scheduler.Run(n)
            except DeepSleepExceptionInitiated:
                n = self.Scheduler.Cycles
                deepsleep = True
                n_deepsleep += 1

        self.assertNotEqual(self.MsgEx.SvcLastRun, -1)
        self.assertNotEqual(self.NetCon.SvcLastRun, -1)
        self.assertNotEqual(self.Time.SvcLastRun, -1)
        self.assertNotEqual(self.TempSensor.SvcLastRun, -1)
        self.assertNotEqual(self.MoistSensor.SvcLastRun, -1)

        self.assertNotEqual(n_deepsleep, 0)
    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
Example #11
0
from DemoApp.DemoApp import DemoApp
from WebApp.WebApp import WebApp
from upyiot.comm.NetCon.NetCon import NetCon

# Other
from micropython import const
import machine
import ubinascii
import utime

ID = str(ubinascii.hexlify(machine.unique_id()).decode('utf-8'))
DIR = "./"
ApCfg = {"ssid": "SmartSensor-" + ID, "pwd": "mato", "ip": "192.168.0.200"}

if __name__ == '__main__':

    netcon = NetCon(DIR, ApCfg)
    app = None

    if netcon.StationSettingsAreSet():
        print("[App] Found station settings.")
        print("[App] Booting Main App.")
        app = DemoApp(netcon)
    else:
        print("[App] No station settings.")
        print("[App] Booting Web App.")
        app = WebApp(netcon)

    app.Setup()
    app.Run()