UNPKG

lavva.exalushome

Version:

Library implementing communication and abstraction layers for ExalusHome system

123 lines 5.31 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 { ExalusConnectionService } from './ExalusConnectionService'; import { RemoteStorageDataEntry } from './IRemoteStorageService'; import { LocalStorageService } from './LocalStorageService'; export class RemoteStorageService { constructor() { this._connection = null; this._localDb = null; this._connection = Api.Get(ExalusConnectionService.ServiceName); this._localDb = Api.Get(LocalStorageService.ServiceName); } GetLocalDbName(resourceGuid, isGlobalForAllUsers) { return `${resourceGuid}_${isGlobalForAllUsers}`; } SaveAsync(resourceGuid, isGlobalForAllUsers, data) { return __awaiter(this, void 0, void 0, function* () { let req = new SaveConfigRequestFrame(resourceGuid, isGlobalForAllUsers, data); let result = yield this._connection.SendAndWaitForResponseAsync(req, 35000, false); switch (result.Status) { case Status.OK: const dat = new RemoteStorageDataEntryResponse(); dat.ResourceGuid = resourceGuid; dat.Data = JSON.stringify(data); dat.IsGlobal = isGlobalForAllUsers; this._localDb.Save(RemoteStorageService.ServiceName, this.GetLocalDbName(resourceGuid, isGlobalForAllUsers), dat); break; } return result.Status; }); } ReadAsync(resourceGuid, isGlobalForAllUsers, skipLocalChache) { return __awaiter(this, void 0, void 0, function* () { let req = new ConfigRequestFrame(resourceGuid, isGlobalForAllUsers); if (!skipLocalChache) { const res = this._localDb.Read(RemoteStorageService.ServiceName, this.GetLocalDbName(resourceGuid, isGlobalForAllUsers)); if (res != null) { const d = res; const r = new RemoteStorageDataEntry(); r.Data = JSON.parse(d.Data); r.IsGlobal = d.IsGlobal; r.ResourceGuid = d.ResourceGuid; return r; } } let result = (yield this._connection.SendAndWaitForResponseAsync(req, 35000, false)); switch (result.Status) { case Status.OK: const ret = new RemoteStorageDataEntry(); ret.ResourceGuid = result.ResourceGuid; ret.Data = JSON.parse(result.Data); ret.IsGlobal = result.IsGlobal; return ret; default: return result.Status; } }); } RemoveAsync(resourceGuid, isGlobalForAllUsers) { return __awaiter(this, void 0, void 0, function* () { let req = new RemoveConfigRequestFrame(resourceGuid, isGlobalForAllUsers); let result = yield this._connection.SendAndWaitForResponseAsync(req, 35000, false); return result.Status; }); } GetServiceName() { return RemoteStorageService.ServiceName; } } RemoteStorageService.ServiceName = "RemoteStorageService"; class SaveConfigRequestFrame extends DataFrame { constructor(resourceGuid, isGlobalForAllUsers, data) { super(); this.ResourceGuid = ""; this.Global = false; this.Resource = "/sync/config/"; this.Method = Method.Put; this.ResourceGuid = resourceGuid; this.Global = isGlobalForAllUsers; this.Data = JSON.stringify(data); } } class ConfigRequestFrame extends DataFrame { constructor(resourceGuid, isGlobalForAllUsers) { super(); this.ResourceGuid = ""; this.Global = false; this.Resource = "/sync/config/"; this.Method = Method.Get; this.ResourceGuid = resourceGuid; this.Global = isGlobalForAllUsers; } } class RemoveConfigRequestFrame extends DataFrame { constructor(resourceGuid, isGlobalForAllUsers) { super(); this.ResourceGuid = ""; this.Global = false; this.Resource = "/sync/config/"; this.Method = Method.Delete; this.ResourceGuid = resourceGuid; this.Global = isGlobalForAllUsers; } } export class RemoteStorageDataEntryResponse extends DataFrame { constructor() { super(...arguments); this.ResourceGuid = ""; this.Data = ""; this.IsGlobal = false; this.InsertTime = new Date(); } } //# sourceMappingURL=RemoteStorageService.js.map