linx-sdk/src/linx-sdk-module/index.ts

85 lines
2.3 KiB
TypeScript

import { LogLevel, Method, Route, UserData, UserResponse } from "./models";
import Utils from "./utils";
import io from "socket.io-client";
export default class LinxSdkModule {
utils: Utils;
user: UserData;
socket: any;
constructor(apiKey: string, host: string) {
this.utils = new Utils(host);
}
async init(user, password): Promise<boolean> {
try {
const login: UserResponse = await this.utils.request(Method.POST, Route.LOGIN, {
login: user,
password: password,
});
if (!login.success) {
Utils.log("Failed to initialize SDK", LogLevel.ERROR);
return false;
} else {
this.user = login.data;
Utils.log("The SDK initialized successfully");
this.getGroups();
return true;
}
} catch (err) {
Utils.log(JSON.stringify(err.message), LogLevel.ERROR);
return false;
}
}
establishWsConnection() {
const hubAddress = this.user.account.configuration.hub_address;
this.socket = io(hubAddress, { rejectUnauthorized: false, secure: true });
this.socket.on("connect", () => {
this.sendArs(true);
Utils.log("The connection with the WS server was successfully established");
});
["connect_timeout", "connect_error", "disconnect"].forEach((handler) => {
this.socket.on(handler, (message: string) => {
Utils.log(message, LogLevel.ERROR);
});
});
}
sendArs(ars: boolean) {
this.socket.emit(
"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: [],
};
this.socket.emit("group-monitoring", JSON.stringify(monitoringObject));
}
async getGroups() {
const getGroupsRoute = Route.GROUPS.replace("$1", this.user.account.id.toString());
const groups: UserResponse = await this.utils.request(Method.GET, getGroupsRoute);
console.log("groups", groups);
}
}