UNPKG

@swrve/smarttv-sdk

Version:

Swrve marketing engagement platform SDK for SmartTV OTT devices

186 lines (185 loc) 6.11 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const base_1 = __importDefault(require("./base")); const IPlatform_1 = require("./IPlatform"); const SwrveLogger_1 = __importDefault(require("../SwrveLogger")); class WebOSPlatform extends base_1.default { constructor(config) { super(); this.webOSKeyMapping = { 36: "Return", 38: "Up", 40: "Down", 37: "Left", 39: "Right", 13: "Enter", 8: "Return", 413: "Stop", 417: "FastForward", 412: "Rewind", 415: "Play", 19: "Pause", 461: "Back", }; // this platform does not need a proxy. this.needsProxy = false; if (config) { this.config = config; if (config.customKeyMappingWebOS) { this.webOSKeyMapping = config.customKeyMappingWebOS; } } } name() { var _a, _b; if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.customDeviceName) { return (_b = this.config) === null || _b === void 0 ? void 0 : _b.customDeviceName; } else { return { name: "LG", variation: "webOS", }; } } init(deviceProperties = []) { super.init(deviceProperties); return deviceProperties.reduce((promise, property) => promise.then(() => this.getDeviceProperty(property)), Promise.resolve()); } getKeymapping() { return this.webOSKeyMapping; } get timezone() { if (this._timezone === undefined) { this.setTimezoneAndCountry(); } return this._timezone || ""; } get countryCode() { if (this._countryCode === undefined) { this.setTimezoneAndCountry(); } return this._countryCode || ""; } get appStore() { var _a, _b; return (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.customAppStore) !== null && _b !== void 0 ? _b : "lgappstv"; } get os() { var _a, _b; return (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.customOS) !== null && _b !== void 0 ? _b : "webos"; } get model() { return this._model || ""; } get firmware() { return this._firmware || ""; } exit() { webOS.platformBack(); } get osVersion() { var _a, _b; if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.customOSVersion) { return (_b = this.config) === null || _b === void 0 ? void 0 : _b.customOSVersion; } else { if (this._osVersion === undefined) { try { const info = JSON.parse(PalmSystem.deviceInfo); this._osVersion = info.platformVersion; } catch (e) { this._osVersion = ""; } } return this._osVersion || ""; } } getTimezone() { if (this._timezone === undefined) { return new Promise((resolve, reject) => webOS.service.request("luna://com.palm.systemservice", { method: "time/getSystemTime", onSuccess: (response) => { resolve((this._timezone = response.timezone)); }, onFailure: reject, })); } return Promise.resolve(this._timezone || ""); } getFirmware() { if (this._firmware === undefined) { return this.loadSystemInfo().then(() => this._firmware || ""); } return Promise.resolve(this._firmware || ""); } openLink(link) { webOSDev.launch({ id: webOSDev.APP.BROWSER, params: { target: link, }, onSuccess: () => { SwrveLogger_1.default.info("launch application control succeed"); }, onFailure: (e) => { SwrveLogger_1.default.error("launch application control failed. reason: ", e); }, }); } initNetworkListener() { return webOSDev.connection.getStatus({ onSuccess: (res) => { if (res.isInternetConnectionAvailable === false) { this.triggerNetworkChange(IPlatform_1.NETWORK_DISCONNECTED); } else { this.triggerNetworkChange(IPlatform_1.NETWORK_CONNECTED); } }, onFailure: (err) => { SwrveLogger_1.default.error("Failed to get network state", err.errorCode, err.errorText); }, subscribe: true, }); } removeNetworkListener(handle) { handle.cancel(); } setTimezoneAndCountry() { const systemInfo = webOS.systemInfo(); if (this._timezone === undefined) { this._timezone = systemInfo.timezone; } this._countryCode = systemInfo.country; } getDeviceProperty(property) { switch (property) { case "timezone": return this.getTimezone(); case "firmware": return this.getFirmware(); default: return undefined; } } loadSystemInfo() { return new Promise((resolve, reject) => webOS.service.request("luna://com.webos.service.tv.systemproperty", { method: "getSystemInfo", parameters: { keys: ["modelName", "firmwareVersion"], }, onSuccess: (response) => { this._model = response.modelName; this._firmware = response.firmwareVersion; resolve(); }, onFailure: reject, })); } } exports.default = WebOSPlatform;