@bondzai/gateway_sdk v0.0.18
WSP-SDK JS Doc
installation
You need to be authentificated to deeplomath's github npm package repository.
In order to do that, you'll need to generate a Github PErsonnal Access Token with at the read:packages
scope.
Then, you'll need to add this token to a environment variable called GITHUB_PACKAGES_TOKEN
.
for exemple you can add this line to your .bashrc
or .bash_profile
file
export GITHUB_PACKAGES_TOKEN=....
And you'll need to add a .npmrc
file to your project with this content
@deeplomath:registry=https://npm.pkg.github.com
always-auth=true
//npm.pkg.github.com/:_authToken=${GITHUB_PACKAGES_TOKEN}
Then you can install the SDK using the following
yarn add @deeplomath/wsp-sdk
or
npm install --save @deeplomath/wsp-sdk
Usage
- Application setups listeners needed. See Client Events.
- Application tries to connect to the proxy using Connect method.
- Application subscribe to a device using SubToDevice method.
- Application is now ready to send and recieve messages from devices. See Sending message and Receiving message
SDK Client
Connexion to proxy
In order to connect to proxy, you first need to instantiate an WSPClientSDK
object.
Then you can connect using the Connect method with a config argument containing host
and port
keys.
import { WSPClientSDK } from "@deeplomath/wsp-sdk";
const WSPClient = new WSPClientSDK();
WSPClient.Connect({
host: "proxy-host",
port: proxy-port
});
Client Events
- OnConnected
- Triggered each time the client is connected to the proxy
- Callback type :
() => void
WSPClient.OnConnected(() => { // Do what you have to do when client get connected to proxy connected = true; });
- OnDisconnected
- Triggered each time the client is disconnected to the proxy
- Callback type :
() => void
WSPClient.OnDisconnected(() => { // Do what you have to do when client get disconnected from proxy connected = false; });
- OnDeviceListUpdate
- Triggered each time a device disconnect or connect to the proxy
- Callback type :
(device_list: string[]) => void
WSPClient.OnDeviceListUpdate(list => { // Do what you have to do when device list is updated deviceList = list; });
- OnDeviceMessage
- Triggered when the client is subscribed to a device and it send's a message to the proxy
- Callback type :
(device_name: string, message: IWSPMessage) => void
WSPClient.OnDeviceMessage((device_name: string, message: WSPMessage) => { // Do what you have to do with the message recieved from subscibed devices console.log(message.ToJsonObj()); });
Client Methods
Connect
- Used to connect to the proxy
- Type :
(config: IWSPConfig) => void
const WSPClient = new WSPClientSDK(); WSPClient.Connect({ host: "proxy-host", port: proxy-port });
RequestDeviceList
- Used to request the device list connected to the proxy. Then response will come as a OnDeviceListUpdate event
- Type :
() => void
- Notes : When the client connect to the proxy or when a device connect or disconnect from the proxy, it'll automatically sends the device list. So requesting device list is not requiered.
WSPClient.RequestDeviceList(); WSPClient.OnDeviceListUpdate(list => console.log(list));
SubToDevice
- Used to subscribe to a specific device connected to proxy. When subscribed to a device, every messages send from the device to the proxy is forwarded to the client and recieved in OnDeviceMessage.
- Type :
(device_name: string) => void
WSPClient.SubToDevice("device_name"); WSPClient.OnDeviceMessage((device_name: string, message: WSPMessage) => { if(device_name === "device_name") console.log(message.ToJsonObj()); });
- UnsubFromDevice
- Used to unsub from a subscribed device. After that, the client will no more recieve device message sent to the proxy.
- Type :
(device_name: string) => void
WSPClient.UnsubFromDevice("device_name");
- SendToDevice
- Used to send a message to a device.
- Type :
(device_name: string, data: IWSPMessage) => void
const msg = new WSPEventMessage(WSPOperationID.EVT_CUSTOM_1); msg.AddPayload([0x1, 0x2, 0x3]); WSPClient.SendToDevice("device_name", msg);
- SendRequest
- Used to send request to proxy.
- Type :
(action: string, device_name: string, message: any) => void
- Notes : You shouldn't need to use this methods as wrapper exists for proxy's requests.
WSPClient.SendRequest("get-device-list");
Messages
A message is an object containing a header and payloads. Here is the interface of a basic message
interface ISWPMessageHeader {
session: string,
mod: WSPMessageMode,
operation: WSPOperationID,
type: WSPMessageType,
id: number
};
interface IWSPMessagePayload {
header: IWSPEventHeader,
event: IWSPEventPayload
};
interface IWSPMessage {
header: ISWPMessageHeader;
payloads: IWSPMessagePayload[];
};
Event Messages
In order to handle events, you have the events messages type. An event message is a type of message that can be send or received from the device through the proxy. There's different type of event messages separeted into operations which are listed below.
Operations
Those are the differents operation available to be sent to the device through a Event Message.
enum WSPOperationID {
EVT_EXCEPTION = 0,
EVT_DATA_IN = 1,
EVT_CMD_STATUS = 256,
EVT_LOG = 257,
EVT_POST_PROC = 512,
EVT_CUSTOM_1 = 3840,
EVT_CUSTOM_2 = 3841,
EVT_CUSTOM_3 = 3842
};
Payload Interfaces
interface IWSPEventDataIn extends IWSPEventPayload {
datasource: number;
buffer: any[];
};
interface IWSPEventException extends IWSPEventPayload {
error: number;
msg: string;
};
interface IWSPEventCMDStatus extends IWSPEventPayload {
mod: number;
op: number;
status: number;
};
interface IWSPEventLog extends IWSPEventPayload {
appid: number;
mod: number;
timestamp: number;
level: number;
msg: string;
};
interface IWSPEventPostProc extends IWSPEventPayload {
key: BinaryData;
array_size: number;
buffer: any[];
};
interface IWSPEventCustom extends IWSPEventPayload {
data: any
};
Sending an event message to a device
To send an event message to a device.
You'll first need to create the message by constructing a WSPEventMessage
object and passing the operation ID as constructor argument.
The you can populate the payload of the event, giving it the data it requests.
Finally you can send the object to the device.
const device = "device_name";
// Sending 3 bytes in a message of operation custom 2
const msg = new WSPEventMessage(WSPOperationID.EVT_CUSTOM_2);
msg.AddPayload([0x1, 0x2, 0x3]);
WSPClient.SendToDevice(device, msg);
// Sending a Float32Array buffer in a message of operation Data In
const data = new Float32Array([1.3456, 2.444, 0.1211, 4.5563]);
const msg = new WSPEventMessage(WSPOperationID.EVT_DATA_IN);
msg.AddPayload(0, Array.from(data));
WSPClient.SendToDevice(device, msg);
Receiving an event message from a device
The proxy will forward every messages sent from the device to the proxy when the client is subscribe to that device. For now we only support Event messages type, so every messages received will be of type event, but the type is in the message header. (see ISWPMessageHeader) The operation of the event message can be check in the header (see ISWPMessageHeader) and then the payload can be cast to the rigth payload interface (see Payload Interfaces)
const device = "device_name";
WSPClient.SubToDevice(device);
WSPClient.OnDeviceMessage((device_name: string, message: WSPMessage) => {
if(device_name === device && message.header.operation === WSPOperationID.EVT_CUSTOM_3) {
const ev = message as WSPEventMessage;
ev.payloads.forEach((payload: IWSPMessagePayload) => {
const d = payload.event as IWSPEventCustom;
console.log(d.data);
});
}
});