Make fd library resolve all requests by waiting for an ack and then resolving the promise.
This commit is contained in:
parent
917c46d876
commit
7632245332
|
@ -25,6 +25,12 @@ export interface fdOutgoingMessage {
|
||||||
export interface fdIncomingMessage {
|
export interface fdIncomingMessage {
|
||||||
action: Action;
|
action: Action;
|
||||||
success: boolean;
|
success: boolean;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
export interface fdResult {
|
||||||
|
action: Action;
|
||||||
|
image: string;
|
||||||
|
time: number;
|
||||||
}
|
}
|
||||||
export declare class FD {
|
export declare class FD {
|
||||||
private bin;
|
private bin;
|
||||||
|
@ -37,16 +43,17 @@ export declare class FD {
|
||||||
private client;
|
private client;
|
||||||
private socketAvailable;
|
private socketAvailable;
|
||||||
private socketConnected;
|
private socketConnected;
|
||||||
|
private waiting;
|
||||||
constructor(bin: string, width: number, height: number, host: string, port: number);
|
constructor(bin: string, width: number, height: number, host: string, port: number);
|
||||||
private startDisplay;
|
private startDisplay;
|
||||||
private startClient;
|
private startClient;
|
||||||
private logstd;
|
private logstd;
|
||||||
private logsterr;
|
private logsterr;
|
||||||
private test;
|
|
||||||
private send;
|
private send;
|
||||||
private receive;
|
private receive;
|
||||||
load(image: string, x: number, y: number, w: number, h: number): Promise<boolean>;
|
load(image: string, x: number, y: number, w: number, h: number): Promise<fdResult>;
|
||||||
display(image: string, exposure: number[]): Promise<boolean>;
|
display(image: string, exposure: number[]): Promise<fdResult>;
|
||||||
stop(image: string): Promise<boolean>;
|
stop(image: string): Promise<fdResult>;
|
||||||
|
isConnected(): boolean;
|
||||||
}
|
}
|
||||||
export {};
|
export {};
|
||||||
|
|
|
@ -28,6 +28,7 @@ class FD {
|
||||||
constructor(bin, width, height, host, port) {
|
constructor(bin, width, height, host, port) {
|
||||||
this.socketAvailable = false;
|
this.socketAvailable = false;
|
||||||
this.socketConnected = false;
|
this.socketConnected = false;
|
||||||
|
this.waiting = null;
|
||||||
this.bin = bin;
|
this.bin = bin;
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
|
@ -37,7 +38,6 @@ class FD {
|
||||||
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.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.startDisplay();
|
||||||
this.startClient();
|
this.startClient();
|
||||||
this.test();
|
|
||||||
}
|
}
|
||||||
async startDisplay() {
|
async startDisplay() {
|
||||||
this.log.info(`Launching fd binary ${this.bin}`);
|
this.log.info(`Launching fd binary ${this.bin}`);
|
||||||
|
@ -53,12 +53,6 @@ class FD {
|
||||||
this.client.connect(this.port, this.host, async function () {
|
this.client.connect(this.port, this.host, async function () {
|
||||||
this.socketConnected = true;
|
this.socketConnected = true;
|
||||||
this.log.info(`TCP socket client connected to ${this.host}:${this.port}`);
|
this.log.info(`TCP socket client connected to ${this.host}:${this.port}`);
|
||||||
await (0, delay_1.delay)(1000);
|
|
||||||
const msg = {
|
|
||||||
action: Action.LOAD,
|
|
||||||
image: './test.jpg'
|
|
||||||
};
|
|
||||||
this.send(msg);
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
this.client.on('data', function (data) {
|
this.client.on('data', function (data) {
|
||||||
this.receive(data);
|
this.receive(data);
|
||||||
|
@ -79,10 +73,6 @@ class FD {
|
||||||
logsterr(data) {
|
logsterr(data) {
|
||||||
this.log.error(`[shell] ${data}`);
|
this.log.error(`[shell] ${data}`);
|
||||||
}
|
}
|
||||||
async test() {
|
|
||||||
await (0, delay_1.delay)(10000);
|
|
||||||
this.shell.kill();
|
|
||||||
}
|
|
||||||
send(msg) {
|
send(msg) {
|
||||||
const json = JSON.stringify(msg);
|
const json = JSON.stringify(msg);
|
||||||
this.log.info(json);
|
this.log.info(json);
|
||||||
|
@ -91,7 +81,10 @@ class FD {
|
||||||
receive(json) {
|
receive(json) {
|
||||||
const msg = JSON.parse(json);
|
const msg = JSON.parse(json);
|
||||||
this.log.info(msg);
|
this.log.info(msg);
|
||||||
return msg;
|
if (this.waiting != null) {
|
||||||
|
this.waiting(msg);
|
||||||
|
this.waiting = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
async load(image, x, y, w, h) {
|
async load(image, x, y, w, h) {
|
||||||
const msg = {
|
const msg = {
|
||||||
|
@ -102,8 +95,24 @@ class FD {
|
||||||
w,
|
w,
|
||||||
h
|
h
|
||||||
};
|
};
|
||||||
|
const startTime = +new Date();
|
||||||
|
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));
|
||||||
this.send(msg);
|
this.send(msg);
|
||||||
return true;
|
return promise;
|
||||||
}
|
}
|
||||||
async display(image, exposure) {
|
async display(image, exposure) {
|
||||||
const msg = {
|
const msg = {
|
||||||
|
@ -111,16 +120,51 @@ class FD {
|
||||||
image,
|
image,
|
||||||
exposure
|
exposure
|
||||||
};
|
};
|
||||||
|
const startTime = +new Date();
|
||||||
|
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));
|
||||||
this.send(msg);
|
this.send(msg);
|
||||||
return true;
|
return promise;
|
||||||
}
|
}
|
||||||
async stop(image) {
|
async stop(image) {
|
||||||
const msg = {
|
const msg = {
|
||||||
action: Action.STOP,
|
action: Action.STOP,
|
||||||
image
|
image
|
||||||
};
|
};
|
||||||
|
const startTime = +new Date();
|
||||||
|
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));
|
||||||
this.send(msg);
|
this.send(msg);
|
||||||
return true;
|
return promise;
|
||||||
|
}
|
||||||
|
isConnected() {
|
||||||
|
return this.socketConnected;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.FD = FD;
|
exports.FD = FD;
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -34,7 +34,14 @@ export interface fdOutgoingMessage {
|
||||||
|
|
||||||
export interface fdIncomingMessage {
|
export interface fdIncomingMessage {
|
||||||
action : Action,
|
action : Action,
|
||||||
success : boolean
|
success : boolean,
|
||||||
|
error? : string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface fdResult {
|
||||||
|
action : Action,
|
||||||
|
image : string,
|
||||||
|
time : number
|
||||||
}
|
}
|
||||||
|
|
||||||
export class FD {
|
export class FD {
|
||||||
|
@ -49,6 +56,8 @@ export class FD {
|
||||||
private socketAvailable : boolean = false;
|
private socketAvailable : boolean = false;
|
||||||
private socketConnected : boolean = false;
|
private socketConnected : boolean = false;
|
||||||
|
|
||||||
|
private waiting : Function = null;
|
||||||
|
|
||||||
constructor (bin: string, width : number, height : number, host : string, port : number) {
|
constructor (bin: string, width : number, height : number, host : string, port : number) {
|
||||||
this.bin = bin;
|
this.bin = bin;
|
||||||
this.width = width;
|
this.width = width;
|
||||||
|
@ -59,7 +68,6 @@ export class FD {
|
||||||
this.shell = new Shell([ this.bin, `${this.width}`, `${this.height}`, `${this.port}` ], 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.startDisplay();
|
||||||
this.startClient();
|
this.startClient();
|
||||||
this.test();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async startDisplay () {
|
private async startDisplay () {
|
||||||
|
@ -77,12 +85,6 @@ export class FD {
|
||||||
this.client.connect(this.port, this.host, async function () {
|
this.client.connect(this.port, this.host, async function () {
|
||||||
this.socketConnected = true;
|
this.socketConnected = true;
|
||||||
this.log.info(`TCP socket client connected to ${this.host}:${this.port}`);
|
this.log.info(`TCP socket client connected to ${this.host}:${this.port}`);
|
||||||
await delay(1000);
|
|
||||||
const msg : fdOutgoingMessage = {
|
|
||||||
action : Action.LOAD,
|
|
||||||
image : './test.jpg'
|
|
||||||
};
|
|
||||||
this.send(msg);
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
this.client.on('data', function (data : string) {
|
this.client.on('data', function (data : string) {
|
||||||
|
@ -109,24 +111,22 @@ export class FD {
|
||||||
this.log.error(`[shell] ${data}`);
|
this.log.error(`[shell] ${data}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async test () {
|
|
||||||
await delay(10000);
|
|
||||||
this.shell.kill();
|
|
||||||
}
|
|
||||||
|
|
||||||
private send (msg : fdOutgoingMessage) {
|
private send (msg : fdOutgoingMessage) {
|
||||||
const json : string = JSON.stringify(msg);
|
const json : string = JSON.stringify(msg);
|
||||||
this.log.info(json);
|
this.log.info(json);
|
||||||
this.client.write(json);
|
this.client.write(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
private receive (json : string) : fdIncomingMessage {
|
private receive (json : string) {
|
||||||
const msg : fdIncomingMessage = JSON.parse(json);
|
const msg : fdIncomingMessage = JSON.parse(json);
|
||||||
this.log.info(msg);
|
this.log.info(msg);
|
||||||
return msg;
|
if (this.waiting != null) {
|
||||||
|
this.waiting(msg);
|
||||||
|
this.waiting = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async load (image : string, x : number, y : number, w : number, h : number) : Promise<boolean> {
|
public async load (image : string, x : number, y : number, w : number, h : number) : Promise<fdResult> {
|
||||||
const msg : fdOutgoingMessage = {
|
const msg : fdOutgoingMessage = {
|
||||||
action : Action.LOAD,
|
action : Action.LOAD,
|
||||||
image,
|
image,
|
||||||
|
@ -135,27 +135,76 @@ export class FD {
|
||||||
w,
|
w,
|
||||||
h
|
h
|
||||||
};
|
};
|
||||||
|
const startTime : number = +new Date();
|
||||||
|
const promise : Promise<fdResult> = new Promise(function (resolve : Function, reject : Function) {
|
||||||
|
this.waiting = function (msg : fdIncomingMessage) {
|
||||||
|
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));
|
||||||
this.send(msg);
|
this.send(msg);
|
||||||
return true;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async display (image : string, exposure : number[]) : Promise<boolean> {
|
public async display (image : string, exposure : number[]) : Promise<fdResult> {
|
||||||
const msg : fdOutgoingMessage = {
|
const msg : fdOutgoingMessage = {
|
||||||
action : Action.DISPLAY,
|
action : Action.DISPLAY,
|
||||||
image,
|
image,
|
||||||
exposure
|
exposure
|
||||||
};
|
};
|
||||||
|
const startTime : number = +new Date();
|
||||||
|
const promise : Promise<fdResult> = new Promise(function (resolve : Function, reject : Function) {
|
||||||
|
this.waiting = function (msg : fdIncomingMessage) {
|
||||||
|
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));
|
||||||
this.send(msg);
|
this.send(msg);
|
||||||
return true;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async stop (image : string) : Promise<boolean> {
|
public async stop (image : string) : Promise<fdResult> {
|
||||||
const msg : fdOutgoingMessage = {
|
const msg : fdOutgoingMessage = {
|
||||||
action : Action.STOP,
|
action : Action.STOP,
|
||||||
image
|
image
|
||||||
};
|
};
|
||||||
|
const startTime : number = +new Date();
|
||||||
|
const promise : Promise<fdResult> = new Promise(function (resolve : Function, reject : Function) {
|
||||||
|
this.waiting = function (msg : fdIncomingMessage) {
|
||||||
|
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));
|
||||||
this.send(msg);
|
this.send(msg);
|
||||||
return true;
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
public isConnected () : boolean {
|
||||||
|
return this.socketConnected;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue