lavva.exalushome
Version:
Library implementing communication and abstraction layers for ExalusHome system
231 lines • 9.37 kB
JavaScript
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());
});
};
export class DataMigrationService {
constructor() {
try {
const currentUrl = window.location.href;
const url = new URL(currentUrl);
const params = new URLSearchParams(url.search);
const encMig = params.get('mig_c_enc');
const migration = params.get('mig_controllers');
const migLang = params.get('mig_lang');
if (encMig !== null) {
const decodedData = decodeURIComponent(this.b64DecodeUnicode(decodeURIComponent(encMig)));
localStorage.setItem(DataMigrationService.AppUserControllersKeyName, decodedData);
}
if (migration !== null) {
localStorage.setItem(DataMigrationService.AppUserControllersKeyName, decodeURIComponent(migration));
}
if (migLang !== null) {
localStorage.setItem(DataMigrationService.AppSelectedLanguageKeyName, migLang);
}
}
catch (ex) {
console.error(ex);
}
}
waitForNavigatorObject(propertyName) {
return new Promise((resolve, reject) => {
function check() {
if (navigator[propertyName]) {
resolve(navigator[propertyName]);
}
else {
requestAnimationFrame(check);
}
}
// Optional: Set a timeout to reject the promise if the object is not found within a reasonable time.
setTimeout(() => {
reject(new Error(`Timed out waiting for navigator.${propertyName}`));
}, 10000); // Adjust the timeout duration as needed
check();
});
}
b64DecodeUnicode(str) {
return decodeURIComponent(Array.prototype.map.call(atob(str), function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
decodeUriJsonData(data) {
return JSON.parse(decodeURIComponent(data));
}
encodeUriJsonData(data) {
return encodeURIComponent(JSON.stringify(data));
}
IsExalusNativeMigrationAvailable() {
return navigator.exalusMigration !== undefined;
}
GetServiceName() {
return DataMigrationService.ServiceName;
}
IsMigrationDataAvailableAsync() {
return __awaiter(this, void 0, void 0, function* () {
try {
//Ios
if (this.isIosNavtie()) {
var res = yield DataMigrationService.GetIosMigrationStatus();
return res !== null ? res : false;
}
//Andorid
else {
if (navigator.userAgent.endsWith("LavvaAndroidNative")) {
if (navigator.exalusMigration === undefined || navigator.exalusMigration === null) {
const tmp = yield this.waitForNavigatorObject("exalusMigration");
}
}
if (this.IsExalusNativeMigrationAvailable()) {
let tmp = yield navigator.exalusMigration.IsMigrationDataAvailableAsync();
if (tmp == null)
return false;
else
return tmp.DataMigrated;
}
else if (localStorage.getItem(DataMigrationService.AppSelectedLanguageKeyName) !== null || localStorage.getItem(DataMigrationService.AppUserControllersKeyName) !== null)
return true;
else
return false;
}
}
catch (ex) {
console.error(ex);
return false;
}
});
}
GetControllersDataAsync() {
return __awaiter(this, void 0, void 0, function* () {
try {
//Ios
if (this.isIosNavtie()) {
let tmp = yield DataMigrationService.GetIosControllerList();
if (tmp === undefined)
return null;
else
return tmp;
}
//Andorid
else {
if (this.IsExalusNativeMigrationAvailable()) {
let controllers = yield navigator.exalusMigration.GetControllersDataAsync();
if (controllers === undefined)
return null;
else
return controllers;
}
let controllers = localStorage.getItem(DataMigrationService.AppUserControllersKeyName);
if (controllers !== null)
return JSON.parse(controllers);
else
return null;
}
}
catch (ex) {
console.error(ex);
return null;
}
});
}
GetLanguageCodeAsync() {
return __awaiter(this, void 0, void 0, function* () {
try {
//Ios
if (this.isIosNavtie()) {
let tmp = yield DataMigrationService.GetIosDefaultLanguage();
if (tmp === undefined)
return null;
else
return tmp;
}
else {
if (this.IsExalusNativeMigrationAvailable()) {
let tmp = yield navigator.exalusMigration.GetLanguageCodeAsync();
if (tmp === undefined)
return null;
else
return tmp;
}
let local = localStorage.getItem(DataMigrationService.AppSelectedLanguageKeyName);
if (local !== undefined && local !== null)
return local;
else
return null;
}
}
catch (ex) {
console.error(ex);
return null;
}
});
}
static GetIosMigrationStatus(timeout = 8000) {
let timer;
return new Promise((resolve, reject) => {
timer = setTimeout(() => {
reject(null);
}, timeout);
try {
window.nativeEvent.exalusControllerData.isMigrationAvaliable = (status) => {
clearTimeout(timer);
resolve(status);
};
window.webkit.messageHandlers.exalusControllerData.postMessage("IsMigrationAvaliable");
}
catch (err) {
reject(err);
}
});
}
static GetIosControllerList(timeout = 8000) {
let timer;
return new Promise((resolve, reject) => {
timer = setTimeout(() => {
reject(null);
}, timeout);
try {
window.nativeEvent.exalusControllerData.handleControllers = (data) => {
clearTimeout(timer);
resolve(data);
};
window.webkit.messageHandlers.exalusControllerData.postMessage("GetControllers");
}
catch (err) {
reject(err);
}
});
}
static GetIosDefaultLanguage(timeout = 8000) {
let timer;
return new Promise((resolve, reject) => {
timer = setTimeout(() => {
reject(null);
}, timeout);
try {
window.nativeEvent.exalusControllerData.handleLanguage = (lang) => {
clearTimeout(timer);
resolve(lang);
};
window.webkit.messageHandlers.exalusControllerData.postMessage("GetDefaultLanguage");
}
catch (err) {
reject(err);
}
});
}
isIosNavtie() {
var _a;
const isIOS = (window.webkit != null && window.webkit.messageHandlers != null);
const hasNativeEvent = ((_a = window.nativeEvent) === null || _a === void 0 ? void 0 : _a.exalusControllerData) !== undefined;
return isIOS && hasNativeEvent;
}
}
DataMigrationService.AppSelectedLanguageKeyName = "App___SelectedLanguage";
DataMigrationService.AppUserControllersKeyName = "App___UserControllersListDatabase";
DataMigrationService.ServiceName = "DataMigrationService";
//# sourceMappingURL=DataMigrationService.js.map