UNPKG

lavva.exalushome

Version:

Library implementing communication and abstraction layers for ExalusHome system

113 lines 6.34 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 { Helpers } from "../Helpers"; import { ExalusConnectionService } from "./ExalusConnectionService"; import { BrokerEnvironment } from "./IExalusConnectionService"; import { LoggerService } from "./Logging/LoggerService"; export class InstallationInfo { constructor(installationId, installationType, brokerUrl) { this.InstallationId = installationId; this.InstallationType = installationType; this.BrokerUrl = brokerUrl; } } export var InstallationType; (function (InstallationType) { InstallationType[InstallationType["Lavva"] = 0] = "Lavva"; InstallationType[InstallationType["Wisniowski"] = 1] = "Wisniowski"; InstallationType[InstallationType["ExalusHome"] = 2] = "ExalusHome"; })(InstallationType || (InstallationType = {})); export class AndroidAutoCarPlayService { GetServiceName() { return AndroidAutoCarPlayService.ServiceName; } SetInstallation(installation) { return __awaiter(this, void 0, void 0, function* () { if (installation.InstallationId === undefined || installation.InstallationType === undefined) return Promise.reject(new Error("Invalid installation info, InstallationId and InstallationType are required")); if (installation.InstallationType != InstallationType.ExalusHome && installation.BrokerUrl === undefined) return Promise.reject(new Error("Broker url is required for non-ExalusHome installations")); Api.Get(LoggerService.ServiceName).Info(AndroidAutoCarPlayService.ServiceName, `Setting Android Auto/CarPlay installation: ${installation.InstallationType} (${installation.InstallationId}) broker: ${installation.BrokerUrl}`); if (!this.IsAndroidAutoAvailable() && !(Helpers.IsIosNative() && window.webkit.messageHandlers.carPlay != undefined)) { Api.Get(LoggerService.ServiceName).Warning(AndroidAutoCarPlayService.ServiceName, "Android Auto/CarPlay is not available on this platform!"); return Promise.resolve(); } if (installation.InstallationType === InstallationType.ExalusHome) { let ex = Api.Get(ExalusConnectionService.ServiceName); installation.ControllerSerial = ex.GetControllerSerialNumber(); installation.ControllerPin = ex.GetControllerPin(); let serv = yield ex.GetConnectedBrokerInfoAsync(5000); switch (serv.Env) { case BrokerEnvironment.Development: installation.BrokerUrl = "dev-broker.tr7.pl:4431"; break; default: installation.BrokerUrl = "services-broker.tr7.pl"; break; } try { let token = yield this.GetTokenFromController(); if (token) installation.AuthToken = token; else Api.Get(LoggerService.ServiceName).Warning(AndroidAutoCarPlayService.ServiceName, "Failed to get auth token from controller, Android Auto/CarPlay integration might not work properly!"); } catch (err) { Api.Get(LoggerService.ServiceName).Error(AndroidAutoCarPlayService.ServiceName, "Error while getting auth token from controller, Android Auto/CarPlay integration might not work properly! \n" + err); } } if (this.IsAndroidAutoAvailable()) navigator.AndroidAuto.SetInstallation(installation); else if (Helpers.IsIosNative() && window.webkit.messageHandlers.carPlay != undefined) window.webkit.messageHandlers.carPlay.postMessage(installation); else return Promise.reject(new Error("Android Auto/CarPlay is not available")); return Promise.resolve(); }); } GetTokenFromController() { return __awaiter(this, void 0, void 0, function* () { var _a; if (this.IsAndroidAutoAvailable() || (Helpers.IsIosNative() && window.webkit.messageHandlers.carPlay != undefined)) { let response = yield Api.Get(ExalusConnectionService.ServiceName).SendAndWaitForResponseAsync(new GetTokenRequest(), 12000, false); switch (response === null || response === void 0 ? void 0 : response.Status) { case Status.OK: return Promise.resolve(((_a = response === null || response === void 0 ? void 0 : response.Data) === null || _a === void 0 ? void 0 : _a.Token) || null); } } return Promise.resolve(null); }); } IsAndroidAutoAvailable() { return navigator.AndroidAuto !== undefined; } ClearInstallation() { if (this.IsAndroidAutoAvailable()) navigator.AndroidAuto.SetInstallation(null); if (Helpers.IsIosNative() && window.webkit.messageHandlers.carPlay != undefined) window.webkit.messageHandlers.carPlay.postMessage(null); else return Promise.reject(new Error("Android Auto/CarPlay is not available")); return Promise.resolve(); } } AndroidAutoCarPlayService.ServiceName = "AndroidAutoCarPlayService"; class GetTokenRequest extends DataFrame { constructor() { super(); this.Resource = "/webapibridge/get-token"; this.Method = Method.Get; } } class TokenResponse { } //# sourceMappingURL=AndroidAutoCarPlayService.js.map