2023-12-02 13:08:50 +00:00
|
|
|
import { Event, LogLevel, Method, Route, UserData, UserResponse } from "./models";
|
2023-10-30 07:25:41 +00:00
|
|
|
import Utils from "./utils";
|
|
|
|
import io from "socket.io-client";
|
2023-12-02 13:08:50 +00:00
|
|
|
import { EventEmitter } from "events";
|
2023-10-30 07:25:41 +00:00
|
|
|
|
|
|
|
export default class LinxSdkModule {
|
|
|
|
utils: Utils;
|
|
|
|
user: UserData;
|
2023-11-02 09:11:26 +00:00
|
|
|
groups: any;
|
|
|
|
#socket: any;
|
2023-12-02 13:08:50 +00:00
|
|
|
socket: any;
|
|
|
|
handler = new EventEmitter();
|
2023-10-30 07:25:41 +00:00
|
|
|
|
2023-11-02 09:11:26 +00:00
|
|
|
constructor(apiKey: string, host: string, debug: boolean = false) {
|
|
|
|
this.utils = new Utils(apiKey, host, debug);
|
2023-10-30 07:25:41 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 09:11:26 +00:00
|
|
|
async init(user: string, password: string): Promise<void> {
|
2023-10-30 07:25:41 +00:00
|
|
|
try {
|
|
|
|
const login: UserResponse = await this.utils.request(Method.POST, Route.LOGIN, {
|
|
|
|
login: user,
|
|
|
|
password: password,
|
|
|
|
});
|
2023-12-02 13:08:50 +00:00
|
|
|
if (!login.success) new Error("Failed to initialize SDK");
|
2023-11-02 09:11:26 +00:00
|
|
|
this.user = login.data;
|
|
|
|
this.utils.setToken(this.user.token);
|
|
|
|
this.groups = await this.getGroups();
|
|
|
|
await this.establishWsConnection();
|
|
|
|
this.utils.log("The SDK initialized successfully");
|
2023-10-30 07:25:41 +00:00
|
|
|
} catch (err) {
|
2023-11-02 09:11:26 +00:00
|
|
|
this.utils.log(JSON.stringify(err.message), LogLevel.ERROR);
|
|
|
|
throw new Error(err.message);
|
2023-10-30 07:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-02 13:08:50 +00:00
|
|
|
models() {
|
|
|
|
enum USER_EVENTS {
|
|
|
|
CONNECT = "connect",
|
|
|
|
}
|
|
|
|
return USER_EVENTS;
|
|
|
|
}
|
|
|
|
|
2023-10-30 07:25:41 +00:00
|
|
|
establishWsConnection() {
|
2023-11-02 09:11:26 +00:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
|
|
|
const hubAddress = this.user.account.configuration.hub_address;
|
2023-10-30 07:25:41 +00:00
|
|
|
|
2023-11-02 09:11:26 +00:00
|
|
|
this.#socket = io(hubAddress, { rejectUnauthorized: false, secure: true });
|
2023-10-30 07:25:41 +00:00
|
|
|
|
2023-12-02 13:08:50 +00:00
|
|
|
this.#socket.on(Event.CONNECT, () => {
|
2023-11-02 09:11:26 +00:00
|
|
|
this.sendArs(true);
|
|
|
|
this.utils.log("The connection with the WS server was successfully established");
|
2023-12-02 13:08:50 +00:00
|
|
|
this.handler.emit(Event.CONNECT);
|
2023-11-02 09:11:26 +00:00
|
|
|
resolve();
|
|
|
|
});
|
2023-10-30 07:25:41 +00:00
|
|
|
|
2023-12-02 13:08:50 +00:00
|
|
|
[Event.CONNECT_ERROR, Event.CONNECT_TIMEOUT, Event.DISCONNECT].forEach((eventType) => {
|
|
|
|
this.#socket.on(eventType, (message: string) => {
|
|
|
|
this.handler.emit(eventType);
|
2023-11-02 09:11:26 +00:00
|
|
|
reject(new Error("Failed to initialize HUB connection"));
|
|
|
|
});
|
2023-10-30 07:25:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sendArs(ars: boolean) {
|
2023-11-02 09:11:26 +00:00
|
|
|
this.#socket.emit(
|
2023-10-30 07:25:41 +00:00
|
|
|
"ars",
|
|
|
|
JSON.stringify({
|
|
|
|
ars,
|
|
|
|
userAgent: "Dispatcher",
|
|
|
|
asset_id: this.user.asset.id,
|
|
|
|
account_id: this.user.account.id,
|
|
|
|
asset_sip_id: this.user.asset.sip_id,
|
|
|
|
fake: false,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendGroupMonitoring() {
|
|
|
|
const monitoringObject = {
|
|
|
|
asset_alias: this.user.asset.name,
|
|
|
|
asset_id: this.user.asset.id,
|
|
|
|
asset_sip_id: this.user.asset.sip_id,
|
|
|
|
group_id: 1,
|
|
|
|
group_sip_id: 100190001,
|
|
|
|
request_ptt_groups_status: false,
|
|
|
|
scan_group_ids: [],
|
|
|
|
};
|
2023-11-02 09:11:26 +00:00
|
|
|
this.#socket.emit("group-monitoring", JSON.stringify(monitoringObject));
|
2023-10-30 07:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async getGroups() {
|
|
|
|
const getGroupsRoute = Route.GROUPS.replace("$1", this.user.account.id.toString());
|
|
|
|
const groups: UserResponse = await this.utils.request(Method.GET, getGroupsRoute);
|
2023-11-02 09:11:26 +00:00
|
|
|
if (!groups) throw new Error("Could not get the groups for your account");
|
|
|
|
return groups;
|
2023-10-30 07:25:41 +00:00
|
|
|
}
|
|
|
|
}
|