Skip to content

Latest commit

 

History

History
175 lines (151 loc) · 4.8 KB

README.md

File metadata and controls

175 lines (151 loc) · 4.8 KB

IoT-Ticket Typescript client

Table of contents

Requirements

Installation

  • Clone the project by using command:
git clone https://github.com/anhminh10a2hoa/IoT-Typescript-Client
  • After cloning the project, open your terminal and navigate to the root of the project folder location.
  • Install all dependencies used:
npm install
  • Create a file named app.ts in the /src folder.
  • Follow Usage to create your own file before starting the client:
npm start

Testing

Simple testing

  • The application is using Jest for testing
  • NOTE: If you want to run all of the test cases, create a file named .env in root folder and its contents like that:
USERNAME_TICKET=your_username
PASSWORD_TICKET=your_password
  • Run testing:
npm test

Coverage testing

  • If you want to check how many percentages this test cases cover of the application, run:
npm run coverage

Usage

Account

Registering a device

const client = new Client("https://my.iot-ticket.com/api/v1/", "username", "password");

const newDevice = new Device();
newDevice.setName("DeviceName");
newDevice.setManufacturer("Manufacturer");
newDevice.setType("DeviceType");
const d1 = new DeviceAttribute();
d1.setKey("OS");
d1.setValue("Windows 7");
const d2 = new DeviceAttribute();
d2.setKey("Screensize");
d2.setValue("1960*1800");
newDevice.setAttributes(d1, d2);

client.registerDevice(newDevice);

Get a device

const device = await client.getDevice("device_id");
console.log(device.toString());

Get list of devices

const listOfDevices = await client.getListDevices(100, 0);
for(let d of listOfDevices.getItems()) {
  let deviceCastType = Object.assign(new Device(), d)
  console.log(deviceCastType.toString());
}

Get list of datanodes

const listOfDataNodes = await client.getDataNodeList("device_id", 100, 0);
for(let dn of listOfDataNodes.getItems()) {
  let dataNodeCastType = Object.assign(new DataNode(), dn)
  console.log(dataNodeCastType.toString());
}

Get all quota

const quota = await client.getAllQuota();
console.log(quota.toString());

Get device quota

const deviceQuota = await client.getDeviceQuota("device_id");
console.log(deviceQuota.toString());

Delete device quota

client.deleteDevice("device_id");

Read statistical data

const criteria = new StatisticalDataQueryCriteria();
criteria.setDeviceId("device_id")
criteria.setFromdate(1383228800000)
criteria.setTodate(1550831791000)
criteria.setGrouping(Grouping.Year);
criteria.setDatanodes(["Temperature","sensor"]);

const statisticalData = await client.readStatisticalData(criteria);
for(let statisticalDataRead of statisticalData.getDatanodeReads()) {
  let statisticalDataReadCastType = Object.assign(new StatisticalDataRead(), statisticalDataRead)
  console.log(statisticalDataReadCastType.toString());
  for(let value of statisticalDataReadCastType.getValues()) {
    let valueCastType = Object.assign(new StatisticalDataReadValue(), value)
    console.log(valueCastType.toStringSDRV());
  }
}

Read process data

const criteria = new DataQueryCriteria();
criteria.setDeviceId("device_id")
criteria.setFromdate(1383228800000)
criteria.setTodate(1550831791000)
criteria.setDatanodes(["/MainEngine/Core/Temperature"]);

const processData = await client.readProcessData(criteria);
for(let dataNodeRead of processData.getDatanodeReads()) {
  let dataNodeReadCastType = Object.assign(new DataNodeRead(), dataNodeRead)
  console.log(dataNodeReadCastType.toString());
  for(let value of dataNodeReadCastType.getValues()) {
    let valueCastType = Object.assign(new DataNodeReadValue(), value)
    console.log(valueCastType.toStringDNRV());
  }
}

Write process data

const dataArray = new Array<DataNodeWriteValue>();
const d1 = new DataNodeWriteValue();
d1.setName("Temperature");
d1.setPath("/MainEngine/Core");
d1.setValue(70);
d1.setTimestampMilliseconds(1414488510057);
d1.setUnit("c");

const d2 = new DataNodeWriteValue();
d2.setName("Latitude");
d2.setPath("/MainEngine/Core");
d2.setValue(63);
d2.setDataType(DataType.LongType);

dataArray.push(d1);
dataArray.push(d2);

client.writeProcessData("device_id", dataArray)

API documentation