Skip to main content

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

  1. Neuko account (sign up here)
  2. Defined device type schema (refer documentation)
  3. 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:

  1. digital input
  2. 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:

  1. context - Class or any object of context. (eg. this)

  2. Function that will be invoked when the value of interest attribute changed. The function must return true if the process success. Otherwise return false.

  3. stateName - the name of the state.

  4. 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

neuko-device-sdk-1.2.0.tar.gz (40.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

neuko_device_sdk-1.2.0-py3-none-any.whl (38.6 kB view details)

Uploaded Python 3

File details

Details for the file neuko-device-sdk-1.2.0.tar.gz.

File metadata

  • Download URL: neuko-device-sdk-1.2.0.tar.gz
  • Upload date:
  • Size: 40.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for neuko-device-sdk-1.2.0.tar.gz
Algorithm Hash digest
SHA256 ce3e5bd446d26d2692d5d30ebbd284d885138fa3bab0c64d6d275392927e7f17
MD5 40b686fad90d2e6697566aa3b11931c1
BLAKE2b-256 651d3b11a4d39c6129dddb9a8dd917394156fd324446bce8d60c32c4fb495063

See more details on using hashes here.

File details

Details for the file neuko_device_sdk-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: neuko_device_sdk-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 38.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for neuko_device_sdk-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d44f0426191675fd198bfdc5515bcd4b51b290f7ff951f9f46fff38ff6c305fc
MD5 b61eeb062472d810e2618f2bc15c2444
BLAKE2b-256 83698dd1202b06295cd0e7cc665fa10065999544c5ce025557fcbbaade3401b8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page