filmout_manager/dist/fd/index.js

249 lines
8.2 KiB
JavaScript
Raw Normal View History

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FD = void 0;
const net_1 = __importDefault(require("net"));
const log_1 = require("../log");
const shell_1 = require("../shell");
const delay_1 = require("../delay");
var Action;
(function (Action) {
Action[Action["NONE"] = 0] = "NONE";
Action[Action["LOAD"] = 1] = "LOAD";
Action[Action["DISPLAY"] = 2] = "DISPLAY";
Action[Action["STOP"] = 3] = "STOP";
})(Action || (Action = {}));
var Mode;
(function (Mode) {
Mode[Mode["RGB"] = 0] = "RGB";
Mode[Mode["BW"] = 1] = "BW";
Mode[Mode["INVERT"] = 2] = "INVERT";
Mode[Mode["BW_INVERT"] = 3] = "BW_INVERT";
Mode[Mode["RGB_CHANNELS"] = 4] = "RGB_CHANNELS";
Mode[Mode["INVERT_CHANNELS"] = 5] = "INVERT_CHANNELS";
})(Mode || (Mode = {}));
class FD {
constructor(bin, width, height, host, port, mock = false) {
this.socketAvailable = false;
this.socketConnected = false;
this.waiting = null;
this.mock = false;
this.bin = bin;
this.width = width;
this.height = height;
this.host = host;
this.port = port;
this.mock = mock;
this.log = (0, log_1.createLog)('fd');
2024-09-02 21:54:44 +00:00
if (!this.mock) {
this.shell = new shell_1.Shell([
this.bin,
'--width', `${this.width}`,
'--height', `${this.height}`,
'--port', `${this.port}`
], this.logstd.bind(this), this.logsterr.bind(this), null, true);
}
this.startDisplay();
this.startClient();
//this.test();
}
async startDisplay() {
this.log.info(`Launching fd binary ${this.bin}`);
if (!this.mock)
this.shell.execute();
}
async startClient() {
if (this.mock) {
return false;
}
this.client = new net_1.default.Socket();
this.log.info(`Waiting for TCP socket server on ${this.host}:${this.port}...`);
while (!this.socketAvailable) {
await (0, delay_1.delay)(10);
}
this.log.info(`TCP socket server on ${this.host}:${this.port} is available`);
this.client.connect(this.port, this.host, async function () {
this.socketConnected = true;
this.log.info(`TCP socket client connected to ${this.host}:${this.port}`);
}.bind(this));
this.client.on('data', function (data) {
this.receive(data);
}.bind(this));
this.client.on('close', () => {
this.log.info('Closing connection');
this.socketConnected = false;
});
this.client.on('error', (err) => {
this.log.error('Error in socket client', err);
this.socketConnected = false;
if (!this.mock)
this.shell.kill();
});
}
logstd(data) {
let parsed = null;
try {
parsed = JSON.parse(data);
}
catch (err) {
this.log.error(`Error parsing line ${data}`, err);
}
if (parsed !== null) {
this.log.info(`[shell] ${data}`);
if (typeof parsed['listening'] !== 'undefined' && parsed['listening'] === true
&& typeof parsed['port'] !== 'undefined' && parsed['port'] === this.port) {
this.socketAvailable = true;
}
}
}
logsterr(data) {
this.log.error(`[shell] ${data}`);
}
send(msg) {
const json = JSON.stringify(msg);
this.log.info(json);
if (!this.mock)
this.client.write(json);
}
receive(json) {
const msg = JSON.parse(json);
this.log.info(msg);
if (this.waiting != null) {
this.waiting(msg);
this.waiting = null;
}
}
async load(image, x, y, w, h) {
const msg = {
action: Action.LOAD,
image,
position: {
x,
y,
w,
h
}
};
const startTime = Date.now();
if (this.mock) {
return {
action: Action.LOAD,
image,
reported: (Date.now()) - startTime,
elapsed: (Date.now()) - startTime
};
}
const promise = new Promise(function (resolve, reject) {
this.waiting = function (msg) {
if (msg.action == Action.LOAD && msg.success) {
return resolve({
action: Action.LOAD,
image,
reported: msg.data,
elapsed: (Date.now()) - startTime
});
}
else if (typeof msg.error !== 'undefined') {
return reject(new Error(msg.error));
}
return reject(new Error(`Error loading ${image}`));
};
}.bind(this));
this.send(msg);
return await promise;
}
async display(image, exposure = []) {
const msg = {
action: Action.DISPLAY,
image,
exposure
};
const startTime = Date.now();
if (this.mock) {
for (let exp of exposure) {
await (0, delay_1.delay)(exp);
}
return {
action: Action.DISPLAY,
image,
reported: (Date.now()) - startTime,
elapsed: (Date.now()) - startTime
};
}
const promise = new Promise(function (resolve, reject) {
this.waiting = function (msg) {
if (msg.action == Action.DISPLAY && msg.success) {
return resolve({
action: Action.DISPLAY,
image,
reported: msg.data,
elapsed: (Date.now()) - startTime
});
}
else if (typeof msg.error !== 'undefined') {
return reject(new Error(msg.error));
}
return reject(new Error(`Error displaying ${image}`));
};
}.bind(this));
this.send(msg);
return await promise;
}
async stop(image) {
const msg = {
action: Action.STOP,
image
};
const startTime = Date.now();
if (this.mock) {
return {
action: Action.STOP,
image,
reported: (Date.now()) - startTime,
elapsed: (Date.now()) - startTime
};
}
const promise = new Promise(function (resolve, reject) {
this.waiting = function (msg) {
if (msg.action == Action.STOP && msg.success) {
this.log.info(msg.data);
return resolve({
action: Action.STOP,
image,
reported: msg.data,
elapsed: (Date.now()) - startTime
});
}
else if (typeof msg.error !== 'undefined') {
return reject(new Error(msg.error));
}
return reject(new Error(`Error loading ${image}`));
};
}.bind(this));
this.send(msg);
return await promise;
}
isConnected() {
return this.socketConnected;
}
async test() {
const img = '/Users/matthewmcwilliams9/src/filmout_display/img/4kSnake.png';
await (0, delay_1.delay)(2000);
await this.load(img, 100, 200, 640, 640);
await (0, delay_1.delay)(2000);
await this.display(img, [4000]);
await (0, delay_1.delay)(8000);
await this.load(img, 100, 200, 640, 640);
await (0, delay_1.delay)(1000);
await this.display(img);
await (0, delay_1.delay)(2000);
await this.stop(img);
this.log.warn('QUITTING!!!!');
process.exit();
}
}
exports.FD = FD;
module.exports = { FD };
//# sourceMappingURL=index.js.map