hdckit
Version:
A pure Node.js client for the OpenHarmony Device Connector
90 lines (89 loc) • 4.02 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const GetParametersCommand_1 = __importDefault(require("./command/GetParametersCommand"));
const contain_1 = __importDefault(require("licia/contain"));
const waitUntil_1 = __importDefault(require("licia/waitUntil"));
const ShellCommand_1 = __importDefault(require("./command/ShellCommand"));
const singleton_1 = __importDefault(require("licia/singleton"));
const filter_1 = __importDefault(require("licia/filter"));
const file_1 = require("./command/file");
const install_1 = require("./command/install");
const forward_1 = require("./command/forward");
const reverse_1 = require("./command/reverse");
const UiDriver_1 = __importDefault(require("./UiDriver"));
const HilogCommand_1 = require("./command/HilogCommand");
class Target {
constructor(client, connectKey) {
this.ready = false;
this.checkReady = (0, singleton_1.default)(async () => {
const transport = await this.client.connection(this.connectKey);
transport.send(Buffer.from('shell echo ready\n'));
const data = await transport.readAll();
if (!(0, contain_1.default)(data.toString(), 'E000004')) {
this.ready = true;
return true;
}
return false;
});
this.client = client;
this.connectKey = connectKey;
}
async transport() {
if (!this.ready) {
await (0, waitUntil_1.default)(this.checkReady, 10000, 1000);
}
return this.client.connection(this.connectKey);
}
getParameters() {
return this.transport().then((transport) => new GetParametersCommand_1.default(transport).execute());
}
shell(command) {
return this.transport().then((transport) => new ShellCommand_1.default(transport).execute(command));
}
sendFile(local, remote) {
return new file_1.FileSendCommand(this.connectKey, this.client.options.bin).execute(local, remote);
}
recvFile(remote, local) {
return new file_1.FileRecvCommand(this.connectKey, this.client.options.bin).execute(remote, local);
}
install(hap) {
return new install_1.InstallCommand(this.connectKey, this.client.options.bin).execute(hap);
}
uninstall(bundleName) {
return new install_1.UninstallCommand(this.connectKey, this.client.options.bin).execute(bundleName);
}
forward(local, remote) {
return this.transport().then((transport) => new forward_1.ForwardPortCommand(transport).execute(local, remote));
}
async listForwards() {
const forwards = await this.client.listForwards();
return (0, filter_1.default)(forwards, (forward) => forward.target === this.connectKey);
}
removeForward(local, remote) {
return this.transport().then((transport) => new forward_1.RemoveForwardPortCommand(transport).execute(local, remote));
}
reverse(remote, local) {
return this.transport().then((transport) => new reverse_1.ReversePortCommand(transport).execute(remote, local));
}
async listReverses() {
const reverses = await this.client.listReverses();
return (0, filter_1.default)(reverses, (reverse) => reverse.target === this.connectKey);
}
removeReverse(remote, local) {
return this.transport().then((transport) => new forward_1.RemoveForwardPortCommand(transport).execute(remote, local));
}
async createUiDriver(sdkPath, sdkVersion) {
return new UiDriver_1.default(this, sdkPath, sdkVersion);
}
async openHilog(options = {}) {
if (options.clear) {
const connection = await this.shell('hilog -r');
await connection.readAll();
}
return this.transport().then((transport) => new HilogCommand_1.HilogCommand(transport).execute());
}
}
exports.default = Target;