d2jsapi
v27.0.4
Published
D2000 SCADA javascript/typescript client API. https://d2000.ipesoft.com/
Readme
d2jsapi
TypeScript/JavaScript client API of the D2000 SCADA system for web applications served by SmartWeb. It wraps the CometD client and gives real-time object subscriptions, archive and EDA reads, RPC calls (including stateful RPC conversations), object value writes, service messages, password change and WebAuthn passkey management.
npm install d2jsapiimport {D2WebApiClient} from "d2jsapi";
const d2Api = new D2WebApiClient({url: "/api/cometd"});
d2Api.connect(state => console.log("handshaked:", state.handshaked));
d2Api.subscribeObject("M.Temperature", object => console.log(object.unival));
const result = await d2Api.rpc("E.Demo", "Sum", 1, 2, {type: "real", returnAs: "sum"}).call();The public API is the D2WebApi interface in src/d2Api.ts. User documentation lives in the SmartWeb
documentation (section D2JsApi of the SmartWeb Framework guide).
Build and publish
npm install
npm run build # tsc -> dist/
npm run publish-lib # build + npm publishApp channel (internal API)
Warning: the app channel is an internal interface between IPESOFT web applications and the D2Connector process. It is not meant for third-party applications, its messages are not publicly documented and its shape may change between versions without notice.
An app channel is a bidirectional connection between a web application and an application registered in the
D2Connector process under a channelGroupId, a path such as /d2/eda. It carries JSON text messages whose content is
not interpreted by SmartWeb or by the D2000 Java API. The usage mirrors an RPC conversation: the channel has to be
opened first, and only after the server confirmed it can messages be sent and received.
openAppChannel(channelGroupId, params) opens a channel. params.handler receives the channel events and the optional
params.timeout (milliseconds, default 35 000) bounds the wait for the server confirmation. The returned promise
resolves with the channel handle after the server confirmed the channel. Every call opens a new, independent
channel, even for the same channelGroupId: two instances of the same component on one page get two channels and two
separate states in D2Connector.
The handle (AppChannel) has the attributes channelGroupId, instanceId (sequence number of the channel within the
connection), id (the channel group id without its leading /, then /instanceId, e.g. d2/eda/1) and protocolVersion, the method isOpened(),
send(json, timeout?) to send one JSON message (a string) and close() to close the channel.
protocolVersion is the protocol version of the channel group in D2Connector, sent with the server confirmation
(0 - the group is not versioned, undefined - the server does not send it). The server does not check it: the
application compares it with the version it was built for and decides whether it can use the channel.
The handler (AppChannelHandler) implements:
onMessage(channel, json)- a message from D2Connector as a string; messages arrive in sending order and never afteronClose,onClose(channel, reason)- the channel is closed;reason(AppChannelCloseReasonEnum) isCLOSEDafterclose(), orTERMINATEDwhen the server or D2Connector closed it or the connection was re-established (rehandshake),onError(channel, error)- optional; a server-side error after the channel was opened.erroris anAppChannelErrorwithcode(AppChannelErrorCodeEnum:NOT_ALLOWED,NOT_SUPPORTED,OPEN_FAILED,INTERNAL).
If opening fails, the promise is rejected with an AppChannelError with the same codes (NOT_ALLOWED - the app
is not allowed by the server configuration, NOT_SUPPORTED - the D2000 Java API on the server has no app channel
support, OPEN_FAILED - D2Connector refused the channel or the server timed out, INTERNAL), or with a plain
Error on a client-side timeout and when the connection is not handshaked.
const channel = await d2Api.openAppChannel("/d2/eda", {
handler: {
onMessage(channel, json) {
const message = JSON.parse(json);
// handle the message from D2Connector
},
onClose(channel, reason) {
if (reason === AppChannelCloseReasonEnum.TERMINATED) {
// reopen after the connection is handshaked again
}
},
onError(channel, error) {
console.error(channel.id, error.code, error.message);
}
},
timeout: 35000
});
// the application's reply arrives through onMessage
await channel.send(JSON.stringify({channel: "/d2/echo", id: 1, type: "request", data: {text: "Hello"}}));
await channel.close();A channel is bound to the server connection in the same way as an RPC conversation. When the connection is lost
for longer than the CometD session timeout on the server (network outage, sleeping computer, SmartWeb restart,
logout), the client performs a new handshake (rehandshake) and every open channel gets onClose with
AppChannelCloseReasonEnum.TERMINATED. Channels are not restored automatically because their state in D2Connector no longer exists. The
application opens a new channel after the connection is back (connection state change to handshaked, see
registerStateChangeHandler). Do not open the new channel directly inside onClose: at that moment the connection
is not handshaked yet.
On the server the channel group id must be allowed in the SmartWeb configuration list
smartweb.application.cometApi.accessFilter.allowedApps. The list holds wildcard patterns matched
case-insensitively against the id with its leading / (* matches across / too) and is empty by default,
which disables app channels.
smartweb:
application:
cometApi:
accessFilter:
allowedApps: ["/d2/eda", "/d2/object/*"]