appium-lg-webos-driver
Version:
LG WebOS support for Appium
160 lines • 7.17 kB
JavaScript
;
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _LGRemoteClient_instances, _LGRemoteClient_url, _LGRemoteClient_log, _LGRemoteClient_ws, _LGRemoteClient_keyCooldown, _LGRemoteClient_onMessage;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LGRemoteKeys = exports.LGRemoteClient = void 0;
const lodash_1 = __importDefault(require("lodash"));
const ws_1 = __importDefault(require("ws"));
const bluebird_1 = __importDefault(require("bluebird"));
const support_1 = require("appium/support");
const lg_socket_client_1 = require("./lg-socket-client");
const DEFAULT_KEY_COOLDOWN = 750;
class LGRemoteClient {
/**
*
* @param {LGRemoteClientOpts} opts
*/
constructor({ url, log = support_1.logger.getLogger('LGRemoteClient'), keyCooldown = DEFAULT_KEY_COOLDOWN, }) {
_LGRemoteClient_instances.add(this);
/**
* @type {string}
*/
_LGRemoteClient_url.set(this, void 0);
/**
* @type {import('@appium/types').AppiumLogger}
*/
_LGRemoteClient_log.set(this, void 0);
/**
* @type {WebSocket|undefined}
*/
_LGRemoteClient_ws.set(this, void 0);
/**
* @type {number}
*/
_LGRemoteClient_keyCooldown.set(this, void 0);
__classPrivateFieldSet(this, _LGRemoteClient_url, url, "f");
__classPrivateFieldSet(this, _LGRemoteClient_log, log, "f");
__classPrivateFieldSet(this, _LGRemoteClient_keyCooldown, keyCooldown, "f");
}
async connect() {
await new bluebird_1.default((res, rej) => {
/** @type {WebSocket} */
let ws;
const onOpen = () => {
ws.removeListener(lg_socket_client_1.WsEvent.ERROR, onError);
ws.on(lg_socket_client_1.WsEvent.MESSAGE, __classPrivateFieldGet(this, _LGRemoteClient_instances, "m", _LGRemoteClient_onMessage).bind(this));
res(ws);
};
/** @param {Error} err */
const onError = (err) => {
ws.removeListener(lg_socket_client_1.WsEvent.OPEN, onOpen);
rej(err);
};
ws = new ws_1.default(__classPrivateFieldGet(this, _LGRemoteClient_url, "f"))
.once(lg_socket_client_1.WsEvent.OPEN, onOpen)
.once(lg_socket_client_1.WsEvent.ERROR, onError);
__classPrivateFieldSet(this, _LGRemoteClient_ws, ws, "f");
});
}
async disconnect() {
await new bluebird_1.default((res, rej) => {
const onClose = () => {
__classPrivateFieldGet(this, _LGRemoteClient_ws, "f")?.removeListener(lg_socket_client_1.WsEvent.ERROR, onError);
__classPrivateFieldGet(this, _LGRemoteClient_ws, "f")?.removeListener(lg_socket_client_1.WsEvent.MESSAGE, __classPrivateFieldGet(this, _LGRemoteClient_instances, "m", _LGRemoteClient_onMessage));
res();
};
/** @param {Error} err */
const onError = (err) => {
__classPrivateFieldGet(this, _LGRemoteClient_ws, "f")?.removeListener(lg_socket_client_1.WsEvent.CLOSE, onClose);
rej(err);
};
__classPrivateFieldGet(this, _LGRemoteClient_ws, "f")?.once(lg_socket_client_1.WsEvent.CLOSE, onClose);
__classPrivateFieldGet(this, _LGRemoteClient_ws, "f")?.once(lg_socket_client_1.WsEvent.ERROR, onError);
__classPrivateFieldGet(this, _LGRemoteClient_ws, "f")?.close();
});
}
/**
*
* @template {SerializableObject} [P=any]
* @param {string} type
* @param {P} payload
*/
command(type, payload = /** @type {P} */ ({})) {
const cmdLines = [];
cmdLines.push(`type:${type}`);
for (const key of lodash_1.default.keys(payload)) {
cmdLines.push(`${key}:${payload[key]}`);
}
const msg = cmdLines.join('\n') + '\n\n';
__classPrivateFieldGet(this, _LGRemoteClient_log, "f").debug(`Sending ${type} command: ${msg.replaceAll('\n', '\\n')}`);
__classPrivateFieldGet(this, _LGRemoteClient_ws, "f")?.send(msg);
}
/**
*
* @param {import('../types').Delta} delta
*/
async movePointer({ dx, dy }) {
if (dx !== undefined && dy !== undefined) {
this.command('move', { dx, dy });
await bluebird_1.default.delay(__classPrivateFieldGet(this, _LGRemoteClient_keyCooldown, "f"));
}
else {
throw new TypeError(`Must include dx/dy params`);
}
}
/**
*
* @param {import('type-fest').LiteralUnion<LGRemoteKey, string>} name
*/
async pressKey(name) {
this.command('button', { name });
await bluebird_1.default.delay(__classPrivateFieldGet(this, _LGRemoteClient_keyCooldown, "f"));
}
}
exports.LGRemoteClient = LGRemoteClient;
_LGRemoteClient_url = new WeakMap(), _LGRemoteClient_log = new WeakMap(), _LGRemoteClient_ws = new WeakMap(), _LGRemoteClient_keyCooldown = new WeakMap(), _LGRemoteClient_instances = new WeakSet(), _LGRemoteClient_onMessage = function _LGRemoteClient_onMessage(data) {
__classPrivateFieldGet(this, _LGRemoteClient_log, "f").info(data);
};
/**
* @see https://webostv.developer.lge.com/develop/guides/magic-remote
*/
exports.LGRemoteKeys = Object.freeze(
/** @type {const} */ ({
HOME: 'HOME',
LEFT: 'LEFT',
RIGHT: 'RIGHT',
UP: 'UP',
DOWN: 'DOWN',
ENTER: 'ENTER',
BACK: 'BACK',
}));
/**
* @typedef {import('type-fest').ValueOf<typeof LGRemoteKeys>} LGRemoteKey
*/
/**
* @typedef LGRemoteClientOpts
* @property {string} url
* @property {import('@appium/types').AppiumLogger} [log]
* @property {number} [keyCooldown]
*/
/**
* @typedef {import('../types').SerializableObject} SerializableObject
*/
/**
* @template {SerializableObject} [P=any]
* @typedef {import('../types').InboundMsg<P>} Message
*/
//# sourceMappingURL=lg-remote-client.js.map