Neuko device SDK for Python hardware
Project description
Device SDK for Python
This document provides information about the Neuko SDK that can be installed as a dependency in an IoT device.
Pre-requisites
- Neuko account (sign up here)
- Defined device type schema (refer documentation)
- Bootstrap certificates that can downloaded after define a device type schema (step 2)
Device State
Device 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 input
- digital output
Also, 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.
Installation
Checking minimum requirement
The SDK requires Python 3.6 and above.
python --version
Installation
pip install neuko-device-sdk
Usage
Import package
from neuko.device.device import Device
from neuko.iot.bootstrap import BootstrapClient
from neuko.iot.neuko import NeukoClient
Extend DeviceIdentifierStore class
class DeviceIdentifierStoreObject(DeviceIdentifierStore):
def getAccountId(self) -> str:
return "<Neuko Account Id>"
def getProjectId(self) -> str:
return "<Neuko Project Id>"
def getDeviceSchemaId(self) -> str:
return "<Device Serial Number / Id>"
def getDeviceId(self) -> str:
return "<Neuko Device Type Schema Id>"
Extend ConnectionStore class
class ConnectionStoreObject(ConnectionStore):
async def getPerpetualConnectionSettings(self, deviceIdentifier: DeviceIdentifier) -> str:
fd = open("./my-secure-directory/neuko-device-connection-settings.json", mode="r")
raw = json.load(fd)
fd.close()
return raw
async def savePerpetualConnectionSettings(self, deviceIdentifier: DeviceIdentifier, settings: str) -> bool:
fd = open("./my-secure-directory/neuko-device-connection-settings.json", mode="w")
json.dump(settings, fd)
fd.close()
return True
async def deletePerpetualConnectionSettings(self, deviceIdentifier: DeviceIdentifier) -> bool:
return True
async def isPerpetualConnectionSettingsExists(self, deviceIdentifier: DeviceIdentifier) -> bool:
return False
}
Extend CertificateStore class
class CertificateStoreObject(CertificateStore):
async def getBootstrapCertificateAuthority(self, deviceIdentifier: DeviceIdentifier) -> str:
return "./my-secure-directory/certificates/cert.ca.pem"
async def getBootstrapChainCertificate(self, deviceIdentifier: DeviceIdentifier) -> str:
return "./my-secure-directory/certificates/bootstrap-certificate.pem.crt"
async def getBootstrapPrivateKey(self, deviceIdentifier: DeviceIdentifier) -> str:
return "./my-secure-directory/certificates/bootstrap-private.pem.key"
async def getPerpetualCertificateAuthority(self, deviceIdentifier: DeviceIdentifier) -> str:
fd = open("./my-secure-directory/certificates/cert.ca.pem", mode="r")
raw = fd.read()
fd.close()
return raw
async def getPerpetualChainCertificate(self, deviceIdentifier: DeviceIdentifier) -> str:
fd = open("./my-secure-directory/certificates/certificate.pem.crt", mode="r")
raw = fd.read()
fd.close()
return raw
async def getPerpetualPrivateKey(self, deviceIdentifier: DeviceIdentifier) -> str:
fd = open("./my-secure-directory/certificates/cert.ca.pem", mode="r")
raw = fd.read()
fd.close()
return raw
async def savePerpetualCertificateAuthority(self, deviceIdentifier: DeviceIdentifier, certificate: str) -> None:
fd = open("./my-secure-directory/certificates/cert.ca.pem", mode="w")
fd.write(certificate)
fd.close()
async def savePerpetualChainCertificate(self, deviceIdentifier: DeviceIdentifier, certificate: str) -> None:
fd = open("./my-secure-directory/certificates/certificate.pem.crt"", mode="w")
fd.write(certificate)
fd.close()
async def savePerpetualPrivateKey(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 instance
device = Device(DeviceIdentifierStoreObject(), ConnectionStoreObject(), CertificateStoreObject())
device.start_threadsafe()
Methods
start()
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.
Important Only 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
}
}
}
The deep_attr_0 tree is attr_1.deep_attr_0
Example
def callback(data: TelemetricStateChangeParameter):
logging.debug(f'data: {data}')
return True
device.useEffect(self, callback, "digital_input", "pin_0")
device.useEffect(self, callback, "digital_input", "pin_1")
// or use wildcard to invoke the listener for any attribute
device.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.
Example
device.updateTelemetricState("digital_output", {
"pin_0": false,
"pin_1": false,
})
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for neuko_device_sdk-1.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 372245775aeda303bd31b2f0fab9f427c143bd640b93fbcc792966127cd653e5 |
|
MD5 | 5249f21618ab47c7199290223be67142 |
|
BLAKE2b-256 | ebc82d760b56552f1608c31014151fde7428e646acef6dbec8f2513cc8af9f24 |