Node.js SDK Manual
October 21, 2024
Environment
-
IDE
- VS Code
-
Runtime
- Node 6 or above
-
Installation
npm install edgesync360-edgehub-edge-nodejs-sdk
EdgeAgent
Constructor(EdgeAgentOptions options)
New EdgeAgent object
const edgeSDK = require("edgesync360-edgehub-edge-nodejs-sdk");
let options = {
connectType: edgeSDK.constant.connectType.DCCS,
DCCS: {
// If ConnectType is DCCS, the following options must be entered
credentialKey: "",
APIUrl: "",
},
MQTT: {
// If ConnectType is MQTT, the following options must be entered
hostName: "127.0.0.1",
port: 1883,
username: "admin",
password: "admin",
protocolType: edgeSDK.constant.protocol.TCP,
},
AzureIotHub: {
hostName: "",
sasToken: "",
},
useSecure: false,
autoReconnect: true,
reconnectInterval: 1000,
nodeId: "6579dba6-9d19-44a2-b645-c2319c8f1315", // getting from datahub portal
type: edgeSDK.constant.edgeType.Gateway, // Configure the edge as a Gateway or Device. The default setting is Gateway.
deviceId: "Device1", // If the type is Device, the DeviceID must be input.
heartbeat: 60000, // The default is 60 seconds.
dataRecover: true, // Whether to recover data when disconnected
ovpnPath: "", // Set the path of the .ovpn file.
};
let edgeAgent = new edgeSDK.EdgeAgent(options);
Azure IoT Hub
Due to the connection limit of the Azure IoT Hub SDK, the C# SDK can only connect to the Azure IoT Hub using a SASToken. Therefore, you can only connect to the Edge Hub using a SCADA device.
Event
EdgeAgent has three subscription events.
- Connected: When EdgeAgent is connected to the IoTHub.
- Disconnected: When EdgeAgent is disconnected from the IoTHub.
- Message Received: When EdgeAgent receives an MQTT message from the cloud. The message types are as follows:
- WriteValue: Change the tag value from the cloud.
- ConfigAck: Response to config uploads from the edge to the cloud.
edgeAgent.events.on('connected', () => {
console.log('Connection success!');
});
edgeAgent.events.on('disconnected', () => {
console.log('Disconnected...');
});
edgeAgent.events.on('messageReceived', (msg) => {
switch (msg.type) {
case edgeSDK.constant.messageType.writeValue:
for (let device of msg.message.deviceList) {
console.log('DeviceID:' + device.ID);
for (let tag of device.tagList) {
console.log('TagName:' + tag.name + , 'Value:' + tag.value);
}
}
break;
case edgeSDK.constant.messageType.configAck:
console.log('Upload Config Result:' + msg.message);
break;
}
});
Connect([callback])
Connect to the IoTHub. When successfully connected, a connected event will be triggered.
edgeAgent.connect();
- Promise example
edgeAgent.connect().then(
(result) => {
// If connected successfully, result return true, and vice versa.
// Do something...
},
(error) => {
// If connection is unsuccessful, return an error object containing an error message.
// Do something...
},
);
- Callback example
edgeAgent.connect((error, result) => {
// If connected successfully without error, error return null, result return true, and vice versa.
// Do something...
});
Disconnect([callback])
Disconnect from the IoTHub. When successfully disconnected, a disconnected event will be triggered.
edgeAgent.disconnect();
- Promise example
edgeAgent.disconnect().then(
(result) => {
// If disconnected successfully, result return true, and vice versa.
// Do something...
},
(error) => {
// If disconnection is unsuccessful, return an error object containing an error message.
// Do something...
},
);
- Callback example
edgeAgent.disconnect((error, result) => {
// If disconnected successfully without error, error return null, result return true, and vice versa.
// Do something...
});
uploadConfig(action, edgeConfig, [callback])
Upload Node/Device/Tag Config with Action Type (Create/Update/Delete).
- Promise example
let edgeConfig = new edgeSDK.EdgeConfig();
// set node config
// set device config
// set tag config
edgeAgent.uploadConfig(edgeSDK.constant.actionType.create, edgeConfig).then(
(result) => {
//If uploaded successfully, result return true, and vice versa.
},
(error) => {
//If config uploaded unsuccessfully, return an error object containing an error message.
},
);
- Callback example
let edgeConfig = new edgeSDK.EdgeConfig();
// set node config
// set device config
// set tag config
edgeAgent.uploadConfig(
edgeSDK.constant.actionType.create,
edgeConfig,
(error, result) => {
//If uploaded successfully without error, error return null, result return true, and vice versa.
//Do something...
},
);
Node Config:
let nodeConfig = new edgeSDK.NodeConfig();
edgeConfig.node = nodeConfig;
Device Config:
let deviceConfig = new edgeSDK.DeviceConfig();
// The required properties are as follows:
deviceConfig.ID = "Device1";
deviceConfig.name = "Device 1";
deviceConfig.type = "Smart Device";
// The optional properties are as follows:
deviceConfig.description = "Device 1";
If the optional properties are not required, skip the properties configuration.
Analog Tag Config:
let analogTagConfig = new edgeSDK.AnalogTagConfig();
analogTagConfig.name = "ATag1";
analogTagConfig.description = "ATag1";
analogTagConfig.readOnly = false;
analogTagConfig.arraySize = 0;
analogTagConfig.spanHigh = 1000;
analogTagConfig.spanLow = 0;
analogTagConfig.engineerUnit = "";
analogTagConfig.integerDisplayFormat = 4;
analogTagConfig.fractionDisplayFormat = 2;
deviceConfig.analogTagList.push(analogTagConfig);
Discrete Tag Config:
let discreteTagConfig = new edgeSDK.DiscreteTagConfig();
discreteTagConfig.name = "DTag1";
discreteTagConfig.description = "DTag1";
discreteTagConfig.arraySize = 0;
discreteTagConfig.state0 = "0";
discreteTagConfig.state1 = "1";
//discreteTagConfig.state2 = '';
//discreteTagConfig.state3 = '';
//discreteTagConfig.state4 = '';
//discreteTagConfig.state5 = '';
//discreteTagConfig.state6 = '';
//discreteTagConfig.state7 = '';
deviceConfig.discreteTagList.push(discreteTagConfig);
Text Tag Config:
let textTagConfig = new edgeSDK.TextTagConfig();
textTagConfig.name = "TTag1";
textTagConfig.description = "TTag1";
textTagConfig.readyOnly = false;
textTagConfig.arraySize = 0;
deviceConfig.textTagList.push(textTagConfig);
BlockConfig
Create block config:
let analogTagConfig = new edgeSDK.AnalogTagConfig();
analogTagConfig.name = "ATag1";
analogTagConfig.description = "ATag1";
analogTagConfig.readOnly = false;
analogTagConfig.arraySize = 0;
analogTagConfig.spanHigh = 1000;
analogTagConfig.spanLow = 0;
analogTagConfig.engineerUnit = "";
analogTagConfig.integerDisplayFormat = 4;
analogTagConfig.fractionDisplayFormat = 2;
let discreteTagConfig = new edgeSDK.DiscreteTagConfig();
discreteTagConfig.name = "DTag1";
discreteTagConfig.description = "DTag1";
discreteTagConfig.arraySize = 0;
discreteTagConfig.state0 = "0";
discreteTagConfig.state1 = "1";
let textTagConfig = new edgeSDK.TextTagConfig();
textTagConfig.name = "TTag1";
textTagConfig.description = "TTag1";
textTagConfig.readyOnly = false;
textTagConfig.arraySize = 0;
let blockConfig = new edgeSDK.BlockConfig();
blockConfig.blockType = "Pump";
blockConfig.analogTagList.push(analogTagConfig);
blockConfig.discreteTagList.push(discreteTagConfig);
blockConfig.textTagList.push(textTagConfig);
This block config contain 3 tag, and the block type is Pump, using this block config create two block:
deviceConfig.addBlock("Pump01", blockConfig);
deviceConfig.addBlock("Pump02", blockConfig);
This will add 6 tag to your device, the first 3 tag will have a prefix of Pump01 and the last 3 tag will have a prefix of Pump02, for example:
sendData(data, callback)
Send the tag value to the cloud.
let data = new edgeSDK.EdgeData();
for (let i = 1; i {
// If data sending is successful, result return true, and vice versa.
},
(error) => {
// If data sending is unsuccessful, return an error object containing an error message.
},
);
- Callback example
edgeAgent.sendData(data, (error, result) {
//If data sending is successful without error, error return null, result return true, and vice versa.
//Do something...
});
Send the array tag value to the cloud.
let data = new edgeSDK.EdgeData();
// analog array tag
let AryTag = new edgeSDK.EdgeDataTag();
AryTag.deviceID = "Device1";
AryTag.tagName = "ArrayAnalogTag1";
AryTag.value = {
0: 0.5,
1: 1.42,
2: 2.89,
};
data.tagList.push(AryTag);
// discrete array tag
let AryTag = new edgeSDK.EdgeDataTag();
AryTag.deviceID = "Device1";
AryTag.tagName = "ArrayDiscreteTag1";
AryTag.value = {
0: 0,
1: 1,
2: 2,
};
data.tagList.push(AryTag);
// text array tag
let AryTag = new edgeSDK.EdgeDataTag();
AryTag.deviceID = "Device1";
AryTag.tagName = "ArrayTextTag1";
AryTag.value = {
0: "zero",
1: "one",
2: "two",
};
data.tagList.push(AryTag);
sendDeviceStatus(deviceStatus, [callback])
Send the device status to the cloud when the status is changed.
- Promise example
let deviceStatus = new edgeSDK.EdgeDeviceStatus();
for (let i = 1; i {
//If device status sending is successful, result return true, and vice versa.
},
(error) => {
//If device status sending is unsuccessful, return an error object containing an error message.
},
);
- Callback example
let deviceStatus = new edgeSDK.EdgeDeviceStatus();
for (let i = 1; i <= 2; i++) {
let device = new edgeSDK.DeviceStatus();
device.ID = 'Device' + i;
device.status = 1; // offline = 0, online = 1
deviceStatus.deviceList.push(device);
}
edgeAgent.sendDeviceStatus(deviceStatus, (error, result) {
//If device status sending is successful, error return null, result return true, and vice versa.
});
Sample Code
- GitHub
Release Note
- [1.0.2] - 2024-10-21
- Add
- Support Azure IoT Hub device connection which is created from EdgeHub
- Add
- [1.0.1] - 2024-09-09
- Add
- Support
BlockTypethat is defined in WISE-PaaS MQTT v1.0.16
- Support
- Add
- [1.0.0] - 2024-08-08
- Add
EdgeSync360 EdgeHub EdgeSDKProject initialized, derived from WISE-PaaS DataHub EdgeSDK
- Add
Release Note (Archived: WISE-PaaS DataHub EdgeSDK)
- [1.0.6] - 2021-08-06
- Fixed
- Fixed the problem of redundant connection when SDK reconnect.
- Fixed
- [1.0.5] - 2021-03-08
- Fixed
- Fixed the issue that automatically reconnected after disconnecting actively
- Fixed
- [1.0.4] - 2020-11-25
- Fixed
- Fix the problem of connection with SSL
- Fixed
- [1.0.3] - 2020-05-18
- Added
- Support OpenVPN in Linux
- Support “Delsert” action of UploadConfig
- Added
- [1.0.2] - 2020-03-05
- Added
- Add data type checking mechanism of the sendData function
- Automatic rounding down of the tag value by “FractionDisplayFormat”
- Retain the last config upload for internal data processing
- Added
- [1.0.1] - 2020-01-21