2024-04-27 17:30:02 +00:00
|
|
|
"use strict";
|
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2024-05-08 21:08:47 +00:00
|
|
|
exports.FD = void 0;
|
2024-04-27 17:30:02 +00:00
|
|
|
const net_1 = __importDefault(require("net"));
|
|
|
|
const log_1 = require("../log");
|
|
|
|
const shell_1 = require("../shell");
|
|
|
|
const delay_1 = require("../delay");
|
2024-04-27 18:08:56 +00:00
|
|
|
var Action;
|
|
|
|
(function (Action) {
|
|
|
|
Action[Action["NONE"] = 0] = "NONE";
|
|
|
|
Action[Action["LOAD"] = 1] = "LOAD";
|
|
|
|
Action[Action["DISPLAY"] = 2] = "DISPLAY";
|
|
|
|
Action[Action["STOP"] = 3] = "STOP";
|
2024-05-08 21:08:47 +00:00
|
|
|
})(Action || (Action = {}));
|
2024-04-27 18:08:56 +00:00
|
|
|
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";
|
2024-05-08 21:08:47 +00:00
|
|
|
})(Mode || (Mode = {}));
|
2024-04-27 17:30:02 +00:00
|
|
|
class FD {
|
2024-08-24 14:23:15 +00:00
|
|
|
constructor(bin, width, height, host, port, mock = false) {
|
2024-04-27 17:30:02 +00:00
|
|
|
this.socketAvailable = false;
|
|
|
|
this.socketConnected = false;
|
2024-05-15 15:22:50 +00:00
|
|
|
this.waiting = null;
|
2024-08-24 14:23:15 +00:00
|
|
|
this.mock = false;
|
2024-04-27 17:30:02 +00:00
|
|
|
this.bin = bin;
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
this.host = host;
|
|
|
|
this.port = port;
|
2024-08-24 14:23:15 +00:00
|
|
|
this.mock = mock;
|
2024-04-27 17:30:02 +00:00
|
|
|
this.log = (0, log_1.createLog)('fd');
|
2024-08-24 14:23:15 +00:00
|
|
|
if (!this.mock)
|
|
|
|
this.shell = new shell_1.Shell([this.bin, `${this.width}`, `${this.height}`, `${this.port}`], this.logstd.bind(this), this.logsterr.bind(this), null, true);
|
2024-04-27 17:30:02 +00:00
|
|
|
this.startDisplay();
|
|
|
|
this.startClient();
|
2024-08-24 14:23:15 +00:00
|
|
|
//this.test();
|
2024-04-27 17:30:02 +00:00
|
|
|
}
|
|
|
|
async startDisplay() {
|
|
|
|
this.log.info(`Launching fd binary ${this.bin}`);
|
2024-08-24 14:23:15 +00:00
|
|
|
if (!this.mock)
|
|
|
|
this.shell.execute();
|
2024-04-27 17:30:02 +00:00
|
|
|
}
|
|
|
|
async startClient() {
|
2024-08-24 14:23:15 +00:00
|
|
|
if (this.mock) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-04-27 17:30:02 +00:00
|
|
|
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}`);
|
2024-04-27 18:08:56 +00:00
|
|
|
}.bind(this));
|
|
|
|
this.client.on('data', function (data) {
|
|
|
|
this.receive(data);
|
2024-04-27 17:30:02 +00:00
|
|
|
}.bind(this));
|
|
|
|
this.client.on('close', () => {
|
|
|
|
this.log.info('Closing connection');
|
2024-05-15 18:34:24 +00:00
|
|
|
this.socketConnected = false;
|
2024-04-27 17:30:02 +00:00
|
|
|
});
|
|
|
|
this.client.on('error', (err) => {
|
2024-05-15 18:34:24 +00:00
|
|
|
this.log.error('Error in socket client', err);
|
|
|
|
this.socketConnected = false;
|
2024-08-24 14:23:15 +00:00
|
|
|
if (!this.mock)
|
|
|
|
this.shell.kill();
|
2024-04-27 17:30:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
logstd(data) {
|
2024-05-15 18:34:24 +00:00
|
|
|
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;
|
|
|
|
}
|
2024-04-27 17:30:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
logsterr(data) {
|
2024-04-27 18:08:56 +00:00
|
|
|
this.log.error(`[shell] ${data}`);
|
2024-04-27 17:30:02 +00:00
|
|
|
}
|
2024-04-27 18:08:56 +00:00
|
|
|
send(msg) {
|
|
|
|
const json = JSON.stringify(msg);
|
|
|
|
this.log.info(json);
|
2024-08-24 14:23:15 +00:00
|
|
|
if (!this.mock)
|
|
|
|
this.client.write(json);
|
2024-04-27 18:08:56 +00:00
|
|
|
}
|
|
|
|
receive(json) {
|
|
|
|
const msg = JSON.parse(json);
|
|
|
|
this.log.info(msg);
|
2024-05-15 15:22:50 +00:00
|
|
|
if (this.waiting != null) {
|
|
|
|
this.waiting(msg);
|
|
|
|
this.waiting = null;
|
|
|
|
}
|
2024-04-27 18:08:56 +00:00
|
|
|
}
|
2024-05-08 21:08:47 +00:00
|
|
|
async load(image, x, y, w, h) {
|
2024-04-27 18:08:56 +00:00
|
|
|
const msg = {
|
|
|
|
action: Action.LOAD,
|
|
|
|
image,
|
2024-05-15 18:34:24 +00:00
|
|
|
position: {
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
w,
|
|
|
|
h
|
|
|
|
}
|
2024-04-27 18:08:56 +00:00
|
|
|
};
|
2024-05-15 15:22:50 +00:00
|
|
|
const startTime = +new Date();
|
2024-08-24 14:23:15 +00:00
|
|
|
if (this.mock) {
|
|
|
|
return {
|
|
|
|
action: Action.LOAD,
|
|
|
|
image,
|
|
|
|
time: (+new Date()) - startTime
|
|
|
|
};
|
|
|
|
}
|
2024-05-15 15:22:50 +00:00
|
|
|
const promise = new Promise(function (resolve, reject) {
|
|
|
|
this.waiting = function (msg) {
|
|
|
|
if (msg.action == Action.LOAD && msg.success) {
|
|
|
|
return resolve({
|
|
|
|
action: Action.LOAD,
|
|
|
|
image,
|
|
|
|
time: (+new Date()) - startTime
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (typeof msg.error !== 'undefined') {
|
|
|
|
return reject(new Error(msg.error));
|
|
|
|
}
|
|
|
|
return reject(new Error(`Error loading ${image}`));
|
|
|
|
};
|
|
|
|
}.bind(this));
|
2024-04-27 18:08:56 +00:00
|
|
|
this.send(msg);
|
2024-05-15 18:34:24 +00:00
|
|
|
return await promise;
|
2024-04-27 18:08:56 +00:00
|
|
|
}
|
2024-05-15 18:34:24 +00:00
|
|
|
async display(image, exposure = []) {
|
2024-04-27 18:08:56 +00:00
|
|
|
const msg = {
|
|
|
|
action: Action.DISPLAY,
|
|
|
|
image,
|
|
|
|
exposure
|
|
|
|
};
|
2024-05-15 15:22:50 +00:00
|
|
|
const startTime = +new Date();
|
2024-08-24 14:23:15 +00:00
|
|
|
if (this.mock) {
|
|
|
|
for (let exp of exposure) {
|
|
|
|
await (0, delay_1.delay)(exp);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
action: Action.DISPLAY,
|
|
|
|
image,
|
|
|
|
time: (+new Date()) - startTime
|
|
|
|
};
|
|
|
|
}
|
2024-05-15 15:22:50 +00:00
|
|
|
const promise = new Promise(function (resolve, reject) {
|
|
|
|
this.waiting = function (msg) {
|
|
|
|
if (msg.action == Action.DISPLAY && msg.success) {
|
|
|
|
return resolve({
|
|
|
|
action: Action.DISPLAY,
|
|
|
|
image,
|
|
|
|
time: (+new Date()) - startTime
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (typeof msg.error !== 'undefined') {
|
|
|
|
return reject(new Error(msg.error));
|
|
|
|
}
|
|
|
|
return reject(new Error(`Error displaying ${image}`));
|
|
|
|
};
|
|
|
|
}.bind(this));
|
2024-04-27 18:08:56 +00:00
|
|
|
this.send(msg);
|
2024-05-15 18:34:24 +00:00
|
|
|
return await promise;
|
2024-04-27 18:08:56 +00:00
|
|
|
}
|
2024-05-08 21:08:47 +00:00
|
|
|
async stop(image) {
|
|
|
|
const msg = {
|
|
|
|
action: Action.STOP,
|
|
|
|
image
|
|
|
|
};
|
2024-05-15 15:22:50 +00:00
|
|
|
const startTime = +new Date();
|
2024-08-24 14:23:15 +00:00
|
|
|
if (this.mock) {
|
|
|
|
return {
|
|
|
|
action: Action.STOP,
|
|
|
|
image,
|
|
|
|
time: (+new Date()) - startTime
|
|
|
|
};
|
|
|
|
}
|
2024-05-15 15:22:50 +00:00
|
|
|
const promise = new Promise(function (resolve, reject) {
|
|
|
|
this.waiting = function (msg) {
|
|
|
|
if (msg.action == Action.STOP && msg.success) {
|
|
|
|
return resolve({
|
|
|
|
action: Action.STOP,
|
|
|
|
image,
|
|
|
|
time: (+new Date()) - startTime
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (typeof msg.error !== 'undefined') {
|
|
|
|
return reject(new Error(msg.error));
|
|
|
|
}
|
|
|
|
return reject(new Error(`Error loading ${image}`));
|
|
|
|
};
|
|
|
|
}.bind(this));
|
2024-05-08 21:08:47 +00:00
|
|
|
this.send(msg);
|
2024-05-15 18:34:24 +00:00
|
|
|
return await promise;
|
2024-05-15 15:22:50 +00:00
|
|
|
}
|
|
|
|
isConnected() {
|
|
|
|
return this.socketConnected;
|
2024-05-08 21:08:47 +00:00
|
|
|
}
|
2024-05-15 18:34:24 +00:00
|
|
|
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);
|
2024-08-24 14:23:15 +00:00
|
|
|
this.log.warn('QUITTING!!!!');
|
2024-05-15 18:34:24 +00:00
|
|
|
process.exit();
|
|
|
|
}
|
2024-04-27 17:30:02 +00:00
|
|
|
}
|
|
|
|
exports.FD = FD;
|
2024-07-12 13:51:39 +00:00
|
|
|
module.exports = { FD };
|
2024-04-27 17:30:02 +00:00
|
|
|
//# sourceMappingURL=index.js.map
|