UNPKG

lavva.exalushome

Version:

Library implementing communication and abstraction layers for ExalusHome system

187 lines 7.65 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { Api } from "../../../../Api"; import { DataFrame, Method, Status } from "../../../../DataFrame"; import { DependencyContainer } from "../../../../DependencyContainer"; import { ControllerExtensionsService } from "../../../Controller/ControllerExtensionsService"; import { ExalusConnectionService } from "../../../ExalusConnectionService"; import { ResponseResult } from "../../../FieldChangeResult"; import { ChatService, ChatServiceErrorCode } from "../../ChatService"; import { ChatGptClientError, ControllerActivity } from "./IGptChat"; export class GptChat { constructor() { this._chatSubscriptions = []; this._connection = Api.Get(ExalusConnectionService.ServiceName); let removeSubCallback = this._connection.SubscribeTo("/homemessaging/gpt/notify/message/new", (frame) => { const data = frame.Data; const res = new GptChatMessage(); res.DateTime = data.DateTime; res.Guid = data.Guid; res.MessageText = data.MessageText ? data.MessageText : ""; res.SenderName = data.SenderName ? data.SenderName : "n/a"; if (data.ControllerActivities != null) { for (let [activity, activityDictionaryParams] of Object.entries(data.ControllerActivities)) { const activityKey = ControllerActivity[activity]; const activityParamMap = new Map(); for (let [paramKey, paramVal] of Object.entries(activityDictionaryParams)) { activityParamMap.set(paramKey, paramVal); } res.ControllerActivities.set(activityKey, activityParamMap); } } if (data.Errors != null) { for (let [k, v] of Object.entries(data.Errors)) { res.Errors.set(ChatGptClientError[k], v); } } this._chatSubscriptions.forEach(handler => { handler(res); }); }); } IsGptChatAvailableAsync() { return __awaiter(this, void 0, void 0, function* () { return (yield Api.Get(ControllerExtensionsService.ServiceName).GetExtensionsInfoAsync()).any(e => e.ExtensionGuid == "80daeab9-104f-4c1b-9464-61f5fc9bf2bf"); }); } GetChatConfigurationAsync() { return __awaiter(this, void 0, void 0, function* () { try { const result = yield this._connection.SendAndWaitForResponseAsync(new GetConfigurationRequest(), 8000, false); if (result == null || result.Status == null) return new ResponseResult(ChatServiceErrorCode.FatalError, `Cannot get chat configuration! No data in result`); if (result.Status != Status.OK) return new ResponseResult(ChatServiceErrorCode.Error, `Cannot get chat configuration! Controller responded with code: ${result.Status}`); if (result.Data == null) return new ResponseResult(ChatServiceErrorCode.NoData, `Cannot get chat configuration! Controller responded with success, but no data in response!`); return result.Data; } catch (error) { return new ResponseResult(ChatServiceErrorCode.FatalError, `Cannot get chat configuration! ${error}`); } }); } SetChatConfigurationAsync(config) { return __awaiter(this, void 0, void 0, function* () { var _a; try { const result = yield this._connection.SendAndWaitForResponseAsync(new SetConfigurationRequest(config), 8000, false); if (result == null || result.Status == null) return Status.FatalError; return result.Status; } catch (error) { (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Error(ChatService.ServiceName, `Cannot set chat configuration! ${error}`); return Status.FatalError; } }); } SendMessageAsync(message) { return __awaiter(this, void 0, void 0, function* () { var _a; try { const result = yield this._connection.SendAndWaitForResponseAsync(new SendMessagesRequest(message), 8000, false); if (result == null || result.Status == null) return Status.FatalError; return result.Status; } catch (error) { (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Error(ChatService.ServiceName, `Cannot send message! ${error}`); return Status.FatalError; } }); } SubscribeToMessages(messageHandler) { var _a; (_a = this._chatSubscriptions) === null || _a === void 0 ? void 0 : _a.push(messageHandler); } UnsubscribeFromMessages(messageHandler) { if (this._chatSubscriptions) this._chatSubscriptions = this._chatSubscriptions.filter(h => h !== messageHandler); } } class SendMessagesRequest extends DataFrame { constructor(message) { super(); this.Resource = "/homemessaging/gpt/message"; this.Method = Method.Put; this.Data = message; } } class GetConfigurationRequest extends DataFrame { constructor() { super(); this.Resource = "/homemessaging/gpt/configuration"; this.Method = Method.Get; } } class SetConfigurationRequest extends DataFrame { constructor(data) { super(); this.Resource = "/homemessaging/gpt/configuration"; this.Method = Method.Put; this.Data = data; } } class GptChatResponse { constructor() { this.Guid = ""; this.MessageText = ""; this.SenderName = ""; this.DateTime = new Date().toDateString(); } } class GptChatMessage { constructor() { this._messageActions = new Map(); this._errors = new Map(); this._guid = ""; this._senderName = ""; this._messageText = ""; this._dateTime = ""; } get ControllerActivities() { return this._messageActions; } get Errors() { return this._errors; } get Guid() { return this._guid; } get SenderName() { return this._senderName; } get MessageText() { return this._messageText; } get DateTime() { return this._dateTime; } set ControllerActivities(value) { this._messageActions = value; } set Errors(value) { this._errors = value; } set Guid(value) { this._guid = value; } set SenderName(value) { this._senderName = value; } set MessageText(value) { this._messageText = value; } set DateTime(value) { this._dateTime = value; } } //# sourceMappingURL=GptChat.js.map