avneet723 / iot-central-rest-api-sample-companion-app

A sample mobile companion app for IoT Central, built with REST and React Native.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IoT Central REST API Sample Companion Application

This project contains a sample application demonstrating the use of the IoT Central REST APIs. It is composed of two parts - a simple CLI tool which generates QR codes containing the required data for device registration, and a mobile application which reads these codes, performs the registration, and provides some basic device-management functionality.

Prerequisites

Tools

This sample uses Node.js (v12+) for the CLI and Expo for the mobile application. It is recommended you set both up according to the platform you are using before continuing; you will need information from this setup in the following steps.

AAD Application

You will need an AAD application to authorize the mobile application; see here for instructions on how to set one up to connect to IoT Central.

In addition to the "Single-page application" authentication configuration, you will need to add a "Mobile and desktop applications" configuration containing your Expo callback URL. This should match the URL where Expo is hosting your application; for example, if you are using the tunnel functionality, it will look something like exp://{unique id}.anonymous.app.exp.direct:80.

Once you have created your AAD application, enter its client and tenant IDs in /app/src/providers/Auth.tsx.

IoT Central Application

In order to actually utilize the mobile application, you will need an IoT Central application.

Usage

CLI

The CLI utility can be executed by running node index <command> in its source folder. The commands are as follows:

  • init - This will initialize the CLI configuration in the current folder. It will walk you through a number of steps to identify the capability model or device template for the devices you will be registering.
  • device <id> - This will generate a QR code representing the device id, along with its desired template and any other device configuration provided.
  • template [id] - As per device, but will generate a code for registering the template instead. This is primarily for demonstration purposes, as the mobile application is designed to automatically register the template for a provided device when necessary.

Mobile Application

The mobile application can be executed by running npm start in its source folder. This will open a webpage serving the mobile app, which can be downloaded and run on your mobile device by scanning the presented QR code with the Expo mobile app (if the "LAN" option does not work, the "Tunnel" option may provide a functional alternative).

Once the mobile application is loaded, you will be immediately prompted to login. Once you have done so, select the Settings () tab and enter your IoT Central application subdomain.

The application has two main tabs, both of which mirror the capabilities of the other and provide basic CRUD operations on their respective entities: Devices () and Device Templates (). The base tabs provide a list of the existing entities in the application, with the ability to register a new entity via a QR code generated by the CLI tool (). Selecting an individual entity will load and display detailed information about that entity (which is also editable in the case of Devices) and provide the ability to delete that entity ().

Project Structure

/cli - CLI

  • /cmd - Implementations of each of the CLI sub-commands.
    • /src - Utilities and various components.
      • /tgt - Code to retrieve the device template or capability model from each potential target location.

/app - Mobile Application

Outside of configuration and top-level assets, all of the code for the mobile application is contained under /src, primarily in the form of React Native functional components.

  • /data - Components representing various DTDL entities, primarily used to display device data.
    • /Schema - Components used to display data as represented by DTDL schemas.
  • /providers - Components which provide general app-wide functionality via Context, including auth, settings, and storage.
    • /Api - The IoT Central public API component and data types.
  • /resource - Base components providing a generic entity-CRUD framework for each top-level tab. This allows the individual implementations to focus on the data access and display that is unique to that type of entity.
  • /tabs - Implementations of each top-level tab in the application.
    • /Devices - The Devices () tab, implemented using the /resource framework using /data components to display device details.
    • /Settings - The Settings () tab, allowing the user to edit app-wide settings (currently, only the IoT Central application).
    • /Templates - The Device Templates () tab, implemented using the /resource framework.
  • /utility - Various common components used throughout the application.

About

A sample mobile companion app for IoT Central, built with REST and React Native.

License:MIT License


Languages

Language:TypeScript 89.0%Language:JavaScript 11.0%