Start device connection

Start device connection

This section shows how to start the device for connection to the Neuko Cloud IoT platform.

Neuko SDK will determine on where is the device to be connected, either to Provisioning endpoint or Perpetual endpoint, based on the lifecycles as described in this guide.

Make sure the following store has been programmed:

import { 
  Device, 
  DeviceIdentifier, 
  DeviceIdentifierStore, 
  CertificateStore, 
  ConnectionStore 
} from "@neukolabs/device-sdk-js";
import { readFileSync, writeFileSync, unlinkSync, existsSync } from 'fs';
import path from 'path';

// refer to the docs https://docs.neuko.io/user-guide/connect-to-neuko-iot/environment/programming-device-store/
class deviceIdentifier extends DeviceIdentifierStore {
    // ... some content inside
}

// refer to the docs https://docs.neuko.io/user-guide/connect-to-neuko-iot/environment/programming-certificate-store/
class deviceCertificate extends CertificateStore {
    // ... some content inside
}

// refer to the docs https://docs.neuko.io/user-guide/connect-to-neuko-iot/environment/programming-connection-store/
class deviceConnection extends ConnectionStore {
    // ... some content inside
}

async function main() {
    const device: Device = new Device();
    device.identifierStore = new deviceIdentifier();
    device.connectionStore = new deviceConnection();
    device.certificateStore = new deviceCertificate();

    // start the device
    await device.start();
}

async function exit() {
    // stop the device 
    await device.stop();
}

 

import sys
import asyncio
import concurrent.futures
from neuko.device.identifierStore import DeviceIdentifierStore
from neuko.connection.certificateStore import CertificateStore
from neuko.connection.connectionStore import ConnectionStore
from neuko.device.model import DeviceIdentifier
from neuko.device.device import Device, DeviceState

# refer to the docs https://docs.neuko.io/user-guide/connect-to-neuko-iot/environment/programming-device-store/
class deviceIdentifier(DeviceIdentifierStore):
    # ... some content here

# refer to the docs https://docs.neuko.io/user-guide/connect-to-neuko-iot/environment/programming-certificate-store/
class deviceCertificate(CertificateStore):
    # ... some content here

# refer to the docs https://docs.neuko.io/user-guide/connect-to-neuko-iot/environment/programming-connection-store/
class deviceConnection(ConnectionStore):
    # ... some content here

class MyDevice:
    def __init__(self) -> None:
        self.device = None
        self.RUNNING = False
        self._ready = False

    def init(self):
        self.device = Device(deviceIdentifier(), deviceConnection(), deviceCertificate())
        self.device.start_threadsafe()

    async def start(self):
        self.RUNNING = True
        while self.RUNNING:
            if (self._ready == False):
                if (self.gateway.state == DeviceState.READY):
                    self._ready = True
                    self._initalizeDevices()
            await asyncio.sleep(1)

    async def stop(self):
        self.RUNNING = False
        await self.gateway.stop()



async def forever():
    mydevice = MyDevice()
    mydevice.init()
    await mydevice.start()

def main():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(forever())
        


if __name__ == "__main__":
    main()

 

Scroll to Top