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.
},
);