Creating an IzoT Python Application

Creating an IzoT Python Application

The following example Python 3 code defines an interface for an IzoT device, initializes the ISI engine with the appropriate values, and then starts that device application. The device in this example will automatically enroll itself. If a Lamp Controller device is later added to the network, this will handle the connections of the datapoints, such that changing the value on the Keypad will update the light level on the Keypad device, and also on the connected Lamp Controller device. More details are available in the subsequent sections. This example code is a subset of the full keypad.py example that is found in the examples keypad section. This smaller example only connects the lights to a Lamp Controller; the full example also connects a temperature and a humidity sensor to an Environment Sensor device.

from izot.examples.common.framework.framework import Framework from izot.examples.common.framework.framework import FrameworkMenu from izot.examples.common.framework.framework import ApplicationType import izot.device from izot.resources.profiles.iotKeypad import iotKeypad from izot.resources.profiles.iotAnalogOutput import iotAnalogOutput   PROGRAM_ID = '9F:FF:FF:05:00:70:00:01'    # Program Id for Keypad application APP_NAME = 'keypad_example' APP_DESCRIPTION = 'The IzoT Keypad example application' APP_TYPE = ApplicationType.KEYPAD   def main ():      global app      global keypad      global lux        framework = Framework (APP_NAME, APP_TYPE, APP_DESCRIPTION, PROGRAM_ID)      app = framework.app        # Get the app object from the framework      keypad = app.block (profile = iotKeypad())      lux    = app.block (profile = iotAnalogOutput())        #      # For automatic ISI connections      # - the keypad is an ISI host   for the 'Light Level' function      # - the keypad is an ISI client for the 'Keypad'      function      def on_user_interface(sender, argument):          framework.isi.state = argument.event_code    # Update the isi engine 'state' in the framework          if argument.event_code == izot.device.isi.IsiEvent.WARM:              auto_enrollment_start()      # Event handler registration      app.isi.OnUserInterface += on_user_interface            def on_enrollment (sender, argument):          """ Checks whether incoming enrollment invitation is acceptable """          if argument.enrollment.type_id == lux_assembly.enrollment.type_id:              argument.result = lux_assembly          elif argument.enrollment.type_id == keypad_assembly.enrollment.type_id:              argument.result = keypad_assembly      # Event handler registration      app.isi.OnEnrollment += on_enrollment            # ISI Assembly objects      global lux_assembly      global keypad_assembly      keypad_assembly = izot.device.isi.Assembly(          assembly   = (keypad.nvoLoadControl, keypad.nviLoadStatus),          enrollment = izot.device.isi.Enrollment (              direction = izot.device.isi.IsiDirection.VARIOUS,              type_id = keypad.nvoLoadControl))      lux_assembly = izot.device.isi.Assembly (          assembly   = lux.nviAnalog,          enrollment = izot.device.isi.Enrollment (              direction = izot.device.isi.IsiDirection.INPUT,              type_id = lux.nviAnalog))      framework.assembly1 = keypad_assembly      framework.assembly2 = lux_assembly            # Configure, and start, the IzoT application      framework.app_start ()            # Add the standard console menu      framework_menu = FrameworkMenu(framework)            # Run the application      try:          done = False          while not done:              app.service(0.100)  # Allow the IzoT stack time to service its events                            # Test for user input              if kbhit(0.0):                  done = framework_menu.execute()      finally:          app.stop ()                 # Stop the IzoT applicationtext