Package: neuko-device-sdk
Device SDK for PythonThis document provides information about the Neuko SDK that can be installed as a dependency in an IoT device.Pre-requisitesNeuko account (sign uphere)Defined device type schema (referdocumentation)Bootstrap certificates that can downloaded after define a device type schema (step 2)Device StateDevice state is the condition of the hardware at any moment. Typically, the state will be watched, executed and updated under certain circumstances. You can imagine the state of a digital board as below:{"digital_input":{"pin_0":true,"pin_1":false},"digital_output":{"pin_0":true,"pin_1":true}}The above example tells us that the digital board has 2 states:digital inputdigital outputAlso, each state has 2 attributes - pin 0 and pin 1.The Neuko Python SDK works by managing the state's attributes of the device between actual physical and its virtual representation in cloud.Prior to that, the SDK supports provisioning of a new device during 1st time connection.InstallationChecking minimum requirementThe SDK requires Python 3.6 and above.python--versionInstallationpipinstallneuko-device-sdkUsageImport packagefromneuko.device.deviceimportDevicefromneuko.iot.bootstrapimportBootstrapClientfromneuko.iot.neukoimportNeukoClientExtend DeviceIdentifierStore classclassDeviceIdentifierStoreObject(DeviceIdentifierStore):defgetAccountId(self)->str:return"<Neuko Account Id>"defgetProjectId(self)->str:return"<Neuko Project Id>"defgetDeviceSchemaId(self)->str:return"<Device Serial Number / Id>"defgetDeviceId(self)->str:return"<Neuko Device Type Schema Id>"Extend ConnectionStore classclassConnectionStoreObject(ConnectionStore):asyncdefgetPerpetualConnectionSettings(self,deviceIdentifier:DeviceIdentifier)->str:fd=open("./my-secure-directory/neuko-device-connection-settings.json",mode="r")raw=json.load(fd)fd.close()returnrawasyncdefsavePerpetualConnectionSettings(self,deviceIdentifier:DeviceIdentifier,settings:str)->bool:fd=open("./my-secure-directory/neuko-device-connection-settings.json",mode="w")json.dump(settings,fd)fd.close()returnTrueasyncdefdeletePerpetualConnectionSettings(self,deviceIdentifier:DeviceIdentifier)->bool:returnTrueasyncdefisPerpetualConnectionSettingsExists(self,deviceIdentifier:DeviceIdentifier)->bool:returnFalse}Extend CertificateStore classclassCertificateStoreObject(CertificateStore):asyncdefgetBootstrapCertificateAuthority(self,deviceIdentifier:DeviceIdentifier)->str:return"./my-secure-directory/certificates/cert.ca.pem"asyncdefgetBootstrapChainCertificate(self,deviceIdentifier:DeviceIdentifier)->str:return"./my-secure-directory/certificates/bootstrap-certificate.pem.crt"asyncdefgetBootstrapPrivateKey(self,deviceIdentifier:DeviceIdentifier)->str:return"./my-secure-directory/certificates/bootstrap-private.pem.key"asyncdefgetPerpetualCertificateAuthority(self,deviceIdentifier:DeviceIdentifier)->str:fd=open("./my-secure-directory/certificates/cert.ca.pem",mode="r")raw=fd.read()fd.close()returnrawasyncdefgetPerpetualChainCertificate(self,deviceIdentifier:DeviceIdentifier)->str:fd=open("./my-secure-directory/certificates/certificate.pem.crt",mode="r")raw=fd.read()fd.close()returnrawasyncdefgetPerpetualPrivateKey(self,deviceIdentifier:DeviceIdentifier)->str:fd=open("./my-secure-directory/certificates/cert.ca.pem",mode="r")raw=fd.read()fd.close()returnrawasyncdefsavePerpetualCertificateAuthority(self,deviceIdentifier:DeviceIdentifier,certificate:str)->None:fd=open("./my-secure-directory/certificates/cert.ca.pem",mode="w")fd.write(certificate)fd.close()asyncdefsavePerpetualChainCertificate(self,deviceIdentifier:DeviceIdentifier,certificate:str)->None:fd=open("./my-secure-directory/certificates/certificate.pem.crt"", mode="w")fd.write(certificate)fd.close()asyncdefsavePerpetualPrivateKey(self,deviceIdentifier:DeviceIdentifier,certificate:str)->None:fd=open("./my-secure-directory/certificates/cert.ca.pem",mode="w")fd.write(certificate)fd.close()Create Device class instancedevice=Device(DeviceIdentifierStoreObject(),ConnectionStoreObject(),CertificateStoreObject())device.start_threadsafe()Methodsstart()This function start the SDK or in other words starts the virtual/twin of the device. The function also provisions the device with Neuko registry if it is yet to be registered.
A provisioned device will stay in its perpetual state.ImportantOnly called this function after you have registered (by useEffect method) the handler to be invoked when any of the telemetric state has any changed request.useEffect(context, listener, stateName: str, attributeTree: str = "*")Use effect attaches a listener or function handler to any state's attributes. The parameters details are as below:context - Class or any object of context. (eg. this)Function that will be invoked when the value of interest attribute changed. The function must return true if the process success. Otherwise return false.stateName - the name of the state.attributeTree - Dot notation representing state attribute. For example, if you have state as below{"state_name_1":{"attr_0":true,"attr_1":{"deep_attr_0":false}}}Thedeep_attr_0tree isattr_1.deep_attr_0Exampledefcallback(data:TelemetricStateChangeParameter):logging.debug(f'data:{data}')returnTruedevice.useEffect(self,callback,"digital_input","pin_0")device.useEffect(self,callback,"digital_input","pin_1")//orusewildcardtoinvokethelistenerforanyattributedevice.useEffect(self,callback,"digital_input","*");updateTelemetricState(stateName: string, value: object)Call this function when the state of actual device changed. The function will synchronize with its virtual/twin on cloud.Exampledevice.updateTelemetricState("digital_output",{"pin_0":false,"pin_1":false,})