This commit is contained in:
Ian Hollier 2021-02-15 13:56:18 -08:00
Родитель 0c07dea59a
Коммит 11662c0f09
7 изменённых файлов: 1309 добавлений и 0 удалений

Просмотреть файл

@ -1,2 +1,8 @@
# iot-central-compute
A simple way to do compute and data transformation on data sent to Azure IoT Central
# Introduction
# How it works
#

55
device/device.js Normal file
Просмотреть файл

@ -0,0 +1,55 @@
'use strict';
const iotHubTransport = require('azure-iot-device-mqtt').Mqtt;
const Client = require('azure-iot-device').Client;
const Message = require('azure-iot-device').Message;
const ProvisioningTransport = require('azure-iot-provisioning-device-mqtt').Mqtt;
const SymmetricKeySecurityClient = require('azure-iot-security-symmetric-key').SymmetricKeySecurityClient;
const ProvisioningDeviceClient = require('azure-iot-provisioning-device').ProvisioningDeviceClient;
const chance = new require('chance')();
//
// For the public clouds the address of the provisioning host would be: global.azure-devices-provisioning.net
//
const provisioningHost = "global.azure-devices-provisioning.net";
const scopeId = "0ne0005C8ED";
const deviceId = "compute01";
const deviceKey = "mVH9OWKnlewc7zFyrDaPIZ+kB4cMxahBg/1urqAN1Fo=";
const modelId = "dtmi:ianhollNew:compute6jl;1";
const provisioningSecurityClient = new SymmetricKeySecurityClient(deviceId, deviceKey);
const provisioningClient = ProvisioningDeviceClient.create(provisioningHost, scopeId, new ProvisioningTransport(), provisioningSecurityClient);
provisioningClient.setProvisioningPayload({ modelId: `{${modelId}}` });
// Register the device.
provisioningClient.register(function(err, result) {
if (err) {
console.log("error registering device: " + err);
} else {
console.log('registration succeeded');
console.log('assigned hub=' + result.assignedHub);
console.log('deviceId=' + result.deviceId);
console.log('payload=' + JSON.stringify(result.payload));
var connectionString = 'HostName=' + result.assignedHub + ';DeviceId=' + result.deviceId + ';SharedAccessKey=' + deviceKey;
var hubClient = Client.fromConnectionString(connectionString, iotHubTransport);
hubClient.open(function(err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Client connected');
let telemetryInterval = setInterval(() => {
// send in the CSV data to IoT Central application
var message = new Message(`{"data":"${chance.floating({min: -20, max: 50, fixed: 2})}, ${chance.floating({min: 0, max: 100, fixed: 2})}, ${chance.latitude({fixed: 4})}, ${chance.longitude({fixed: 4})}"}`);
hubClient.sendEvent(message, function(err, res) {
if (err) console.log('send error: ' + err.toString());
if (res) console.log('send status: ' + res.constructor.name);
});
}, 5000);
}
});
}
});

1069
device/package-lock.json сгенерированный Normal file

Разница между файлами не показана из-за своего большого размера Загрузить разницу

14
device/package.json Normal file
Просмотреть файл

@ -0,0 +1,14 @@
{
"name": "sample-device",
"version": "0.0.1",
"description": "Sample device to illustrate compute functionality onn IoT Central",
"author": "Ian Hollier",
"license": "MIT",
"dependencies": {
"azure-iot-device": "1.17.3",
"azure-iot-device-mqtt": "1.15.3",
"azure-iot-provisioning-device": "1.8.7",
"azure-iot-provisioning-device-mqtt": "1.7.7",
"azure-iot-security-symmetric-key": "1.7.7"
}
}

0
model.json Normal file
Просмотреть файл

11
package-lock.json сгенерированный Normal file
Просмотреть файл

@ -0,0 +1,11 @@
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"chance": {
"version": "1.1.7",
"resolved": "https://registry.npmjs.org/chance/-/chance-1.1.7.tgz",
"integrity": "sha512-bua/2cZEfzS6qPm0vi3JEvGNbriDLcMj9lKxCQOjUcCJRcyjA7umP0zZm6bKWWlBN04vA0L99QGH/CZQawr0eg=="
}
}
}

154
test.json Normal file
Просмотреть файл

@ -0,0 +1,154 @@
{
"rxTime": 1612991300918,
"device": 72308628870335005,
"status": {
"seqNum": 10653712,
"externalPower": false,
"charging": false,
"vehicleVoltage": 0.07,
"batteryVoltage": 3.847,
"temperature": 23.375,
"lightLevel": 3066,
"vehicleInput1": 0.07,
"vehicleInput2": false,
"signal": -1,
"tamper": 0,
"batteryState": "NORMAL",
"batteryLevel": 40,
"lowSignal": false,
"gpsFail": false
},
"stateFlags": {
"moving": false,
"tamper": false,
"ignition": false
},
"slaveReports": [{
"rxTime": 1612991300920,
"device": 72308628872034205,
"status": {
"seqNum": 2768952080,
"externalPower": false,
"charging": false,
"batteryVoltage": 2.97,
"temperature": 23.625,
"humidity": 36.1875,
"lastUpdate": 1612991229000,
"batteryState": "NORMAL",
"batteryLevel": 80,
"lowSignal": false,
"gpsFail": false
},
"rssi": 45,
"nextUpdateTime": 1612994900908,
"model": "ION-TAG-e4bl-1-0"
},
{
"rxTime": 1612991300922,
"device": 72308628872034205,
"status": {
"seqNum": 2768955408,
"externalPower": false,
"charging": false,
"lastUpdate": 1612990447000,
"batteryState": "NORMAL",
"batteryLevel": 80,
"lowSignal": false,
"gpsFail": false
},
"rssi": 47,
"nextUpdateTime": 1612994900908,
"timeSeriesData": {
"temperature": [{
"time": 1612987747000,
"value": 23.6171875
},
{
"time": 1612988647000,
"value": 23.5234375
},
{
"time": 1612989547000,
"value": 23.5
},
{
"time": 1612990447000,
"value": 23.4921875
}
]
},
"model": "ION-TAG-e4bl-1-0"
},
{
"rxTime": 1612991300924,
"device": 72308628872034205,
"status": {
"seqNum": 2768956944,
"externalPower": false,
"charging": false,
"lastUpdate": 1612990447000,
"batteryState": "NORMAL",
"batteryLevel": 80,
"lowSignal": false,
"gpsFail": false
},
"rssi": 47,
"nextUpdateTime": 1612994900908,
"timeSeriesData": {
"humidity": [{
"time": 1612987747000,
"value": 35.99609375
},
{
"time": 1612988647000,
"value": 36.08984375
},
{
"time": 1612989547000,
"value": 36.08984375
},
{
"time": 1612990447000,
"value": 36.15625
}
]
},
"model": "ION-TAG-e4bl-1-0"
}
],
"pos": {
"type": 8700,
"time": 1612991262201,
"lat": 34.09472666,
"lon": -84.20772013,
"cep": 50
},
"slaveList": [
72308628872034205
],
"nextUpdateTime": 1612994900908,
"live": true,
"slaveDetails": [
],
"model": "HGA4-1-0"
}