Able to send and receive messages over socket

This commit is contained in:
mmcwilliams 2024-04-27 12:08:56 -06:00
parent 2728986258
commit 8fc6bd43b9
3 changed files with 151 additions and 40 deletions

80
dist/fd/index.js vendored
View File

@ -3,11 +3,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FD = void 0;
exports.FD = exports.Mode = exports.Action = 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 || (exports.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 || (exports.Mode = Mode = {}));
;
class FD {
constructor(bin, width, height, host, port) {
this.socketAvailable = false;
@ -18,7 +36,7 @@ class FD {
this.host = host;
this.port = port;
this.log = (0, log_1.createLog)('fd');
this.shell = new shell_1.Shell([this.bin, `${this.width}`, `${this.height}`], this.logstd.bind(this), this.logsterr.bind(this), null, true);
this.shell = new shell_1.Shell([this.bin, `${this.width}`, `${this.height}`, `${this.port}`], this.logstd.bind(this), this.logsterr.bind(this), null, true);
this.startDisplay();
this.startClient();
this.test();
@ -37,23 +55,16 @@ class FD {
this.client.connect(this.port, this.host, async function () {
this.socketConnected = true;
this.log.info(`TCP socket client connected to ${this.host}:${this.port}`);
/*const data = {
action : 1,
image: 'filename.tif',
await (0, delay_1.delay)(1000);
const msg = {
action: Action.LOAD,
image: './test.jpg'
};
this.log.info('SENDING');
this.log.info(data);
this.client.write(JSON.stringify(data));
await delay(2000);
data.action = 2;
this.log.info('SENDING');
this.log.info(data);
this.client.write(JSON.stringify(data));*/
this.send(msg);
}.bind(this));
this.client.on('data', function (data) {
this.receive(data);
}.bind(this));
this.client.on('data', (data) => {
this.log.info('RECEIVED');
this.log.info(data.toString());
});
this.client.on('close', () => {
this.log.info('Closing connection');
});
@ -62,18 +73,49 @@ class FD {
});
}
logstd(data) {
this.log.info(data);
this.log.info(`[shell] ${data}`);
if (data.indexOf('TCP server listening on port') !== -1 && data.indexOf(`${this.port}`) !== -1) {
this.socketAvailable = true;
}
}
logsterr(data) {
this.log.error(data);
this.log.error(`[shell] ${data}`);
}
async test() {
await (0, delay_1.delay)(10000);
this.shell.kill();
}
send(msg) {
const json = JSON.stringify(msg);
this.log.info(json);
this.client.write(json);
}
receive(json) {
const msg = JSON.parse(json);
this.log.info(msg);
return msg;
}
load(image, x, y, w, h) {
const msg = {
action: Action.LOAD,
image,
x,
y,
w,
h
};
this.send(msg);
return true;
}
display(image, exposure) {
const msg = {
action: Action.DISPLAY,
image,
exposure
};
this.send(msg);
return true;
}
}
exports.FD = FD;
//# sourceMappingURL=index.js.map

View File

@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/fd/index.ts"],"names":[],"mappings":";;;;;;AACA,8CAAsB;AAEtB,gCAAkC;AAClC,oCAAiC;AACjC,oCAAiC;AAEjC,MAAa,EAAE;IAYd,YAAa,GAAW,EAAE,KAAc,EAAE,MAAe,EAAE,IAAa,EAAE,IAAa;QAH/E,oBAAe,GAAa,KAAK,CAAC;QAClC,oBAAe,GAAa,KAAK,CAAC;QAGzC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACj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,IAAI,CAAE,GAAuB;QACpC,MAAM,IAAI,GAAY,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QAC1C,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACpB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;IAEO,OAAO,CAAE,IAAa;QAC7B,MAAM,GAAG,GAAuB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACjD,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACnB,OAAO,GAAG,CAAC;IACZ,CAAC;IAEM,IAAI,CAAE,KAAc,EAAE,CAAU,EAAE,CAAU,EAAE,CAAU,EAAE,CAAU;QAC1E,MAAM,GAAG,GAAuB;YAC/B,MAAM,EAAG,MAAM,CAAC,IAAI;YACpB,KAAK;YACL,CAAC;YACD,CAAC;YACD,CAAC;YACD,CAAC;SACD,CAAC;QACF,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACf,OAAO,IAAI,CAAC;IACb,CAAC;IAEM,OAAO,CAAE,KAAc,EAAE,QAAmB;QAClD,MAAM,GAAG,GAAuB;YAC/B,MAAM,EAAG,MAAM,CAAC,OAAO;YACvB,KAAK;YACL,QAAQ;SACR,CAAC;QACF,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACf,OAAO,IAAI,CAAC;IACb,CAAC;CACD;AA/GD,gBA+GC"}

View File

@ -5,6 +5,38 @@ import { createLog } from '../log'
import { Shell } from '../shell';
import { delay } from '../delay';
export enum Action {
NONE,
LOAD,
DISPLAY,
STOP
};
export enum Mode {
RGB,
BW,
INVERT,
BW_INVERT,
RGB_CHANNELS,
INVERT_CHANNELS
};
export interface fdOutgoingMessage {
action : Action,
image : string,
mode? : Mode,
x? : number,
y? : number,
w? : number,
h? : number,
exposure? : number[]
}
export interface fdIncomingMessage {
action : Action,
success : boolean
}
export class FD {
private bin : string;
private width : number;
@ -24,7 +56,7 @@ export class FD {
this.host = host;
this.port = port;
this.log = createLog('fd');
this.shell = new Shell([ this.bin, `${this.width}`, `${this.height}` ], this.logstd.bind(this), this.logsterr.bind(this), null, true);
this.shell = new Shell([ this.bin, `${this.width}`, `${this.height}`, `${this.port}` ], this.logstd.bind(this), this.logsterr.bind(this), null, true);
this.startDisplay();
this.startClient();
this.test();
@ -45,24 +77,17 @@ export class FD {
this.client.connect(this.port, this.host, async function () {
this.socketConnected = true;
this.log.info(`TCP socket client connected to ${this.host}:${this.port}`);
/*const data = {
action : 1,
image: 'filename.tif',
await delay(1000);
const msg : fdOutgoingMessage = {
action : Action.LOAD,
image : './test.jpg'
};
this.log.info('SENDING');
this.log.info(data);
this.client.write(JSON.stringify(data));
await delay(2000);
data.action = 2;
this.log.info('SENDING');
this.log.info(data);
this.client.write(JSON.stringify(data));*/
this.send(msg);
}.bind(this));
this.client.on('data', (data : string) => {
this.log.info('RECEIVED');
this.log.info(data.toString());
});
this.client.on('data', function (data : string) {
this.receive(data);
}.bind(this));
this.client.on('close', () => {
this.log.info('Closing connection');
@ -74,19 +99,63 @@ export class FD {
}
private logstd (data : string ) {
this.log.info(data);
this.log.info(`[shell] ${data}`);
if (data.indexOf('TCP server listening on port') !== -1 && data.indexOf(`${this.port}`) !== -1) {
this.socketAvailable = true;
}
}
private logsterr (data : string) {
this.log.error(data);
this.log.error(`[shell] ${data}`);
}
private async test () {
await delay(10000);
this.shell.kill();
}
private send (msg : fdOutgoingMessage) {
const json : string = JSON.stringify(msg);
this.log.info(json);
this.client.write(json);
}
private receive (json : string) : fdIncomingMessage {
const msg : fdIncomingMessage = JSON.parse(json);
this.log.info(msg);
return msg;
}
public async load (image : string, x : number, y : number, w : number, h : number) : Promise<boolean> {
const msg : fdOutgoingMessage = {
action : Action.LOAD,
image,
x,
y,
w,
h
};
this.send(msg);
return true;
}
public async display (image : string, exposure : number[]) : Promise<boolean> {
const msg : fdOutgoingMessage = {
action : Action.DISPLAY,
image,
exposure
};
this.send(msg);
return true;
}
public async stop (image : string) : Promise<boolean> {
const msg : fdOutgoingMessage = {
action : Action.STOP,
image
};
this.send(msg);
return true;
}
}