Package: echonetlite
This package provides anEchonetLite
middleware module for Python3. The current implementation only provides
quite limited functions to implement Echonet Lite server and client.Echonet Lite specificationis available
from the Echonet web site.Programming an Echonet Lite NodeServer NodeA simple temperature server code is included in the examples directory
asexamples/server-temp.py.importstructfromechonetlite.interfacesimportmonitorfromechonetliteimportmiddlewarefromechonetlite.protocolimport*classMyTemperature(middleware.NodeSuperObject):def__init__(self,eoj):super(MyTemperature,self).__init__(eoj=eoj)# self.property[EPC_MANUFACTURE_CODE] = ...self._add_property(EPC_TEMPERATURE,[0,0])self.get_property_map+=[EPC_TEMPERATURE]monitor.schedule_loopingcall(1,self._update_temperature)def_update_temperature(self):# update temperature value hereval=270self._properties[EPC_TEMPERATURE]=struct.pack('!h',val)# Create local devicesprofile=middleware.NodeProfile()# profile.property[EPC_MANUFACTURE_CODE] = ...# profile.property[EPC_IDENTIFICATION_NUMBER] = ...temperature=MyTemperature(eoj=EOJ(clsgrp=CLSGRP_CODE['SENSOR'],cls=CLS_SE_CODE['TEMPERATURE'],instance_id=1))# Start the Echonet Lite message loopmonitor.start(node_id='172.16.254.66',devices={str(profile.eoj):profile,str(temperature.eoj):temperature})A server (a local Echonet Lite device) should be defined as a subclass
of themiddleware.NodeSuperObjectclass that handles some basic
requests required for all the Echonet Lite devices.Since a temperature sensor device must provide theEPC_TEMPERATUREproperty, that property is created in the__init__()function. Also,
to respond the GET request from client nodes, theEPC_TEMPERATUREvalue is appended to theget_property_mapvariable.Theinterfaces.monitorvariable is the core instance of this module.
It handles all the event loop and callback processing. This module used
theTwistedframework as its underlying
layer.Theinterfaces.monitor.schedule_loopingcall()function registers a
function periodically called. In this example, the_update_temperature()function that is meant to update the internal
temperature value is registered and called every second.In the_update_temperature()function, the property value for theEPC_TEMPERATUREcode is updated. Based on the specification, the
temperature value is encoded into 2 bytes of data.An Echonet Lite node must have one NodeProfile device. Themiddleware.NodeProfileclass provides a basic NodeProfile device
function.Finally, by calling themonitor.start()function providing the node
IP address and device list, the Echonet Lite server that provides two
devices one is a NodeProfile device and the other is a temperature
sensor device starts workingClient NodeA simple temperature client that can communication with the above simple
temperature server is included in the examples directory asexamples/client-temp.py.importstructfromechonetlite.interfacesimportmonitorfromechonetliteimportmiddlewarefromechonetlite.protocolimport*classTemperature(middleware.RemoteDevice):def__init__(self,eoj,node_id):super(Temperature,self).__init__(eoj=eoj)self._node_id=node_idmonitor.schedule_loopingcall(10,self._request_temperature,from_device=controller,to_eoj=self.eoj,to_node_id=self._node_id)self.add_listener(EPC_TEMPERATURE,self._on_did_receive_temperature)def_request_temperature(self,from_device,to_eoj,to_node_id):from_device.send(esv=ESV_CODE['GET'],props=[Property(epc=EPC_TEMPERATURE),],to_eoj=to_eoj,to_node_id=to_node_id)def_on_did_receive_temperature(self,from_node_id,from_eoj,to_device,esv,prop):ifesvnotinESV_RESPONSE_CODES:return(val,)=struct.unpack('!h',bytearray(prop.edt))print('Temperature is',val/10)classMyProfile(middleware.NodeProfile):def__init__(self,eoj=None):super(MyProfile,self).__init__(eoj=eoj)# profile.property[EPC_MANUFACTURE_CODE] = ...# profile.property[EPC_IDENTIFICATION_NUMBER] = ...defon_did_find_device(self,eoj,from_node_id):if(eoj.clsgrp==CLSGRP_CODE['SENSOR']andeoj.cls==CLS_SE_CODE['TEMPERATURE']):returnTemperature(eoj,from_node_id)returnNoneprofile=MyProfile()controller=middleware.Controller(instance_id=1)monitor.start(node_id='172.16.254.1',devices={str(profile.eoj):profile,str(controller.eoj):controller})TheTemperatureclass is a placeholder to register functions to
request a temperature value and to receive its response. In the__init__()function, two functionsrequest_temperatureandon_temperatureare registered for these purposes.When writing a client node, you need to handle new device discovery
case. In theon_did_find_device()function, you will receive an EOJ
and node IP address when the middleware find a new device. You need to
check the EOJ and create a new device entry (theTemperatureclass
in this case).CodeThe source code is available athttps://github.com/keiichishima/echonetliteBug ReportsPlease submit bug reports or patches through the GitHub interfaces.AuthorKeiichi SHIMA / IIJ Innovation Institute Inc. / WIDE project