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 {
|
||||
action: Action;
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}
|
||||
export interface fdResult {
|
||||
action: Action;
|
||||
image: string;
|
||||
time: number;
|
||||
}
|
||||
export declare class FD {
|
||||
private bin;
|
||||
|
@ -37,16 +43,17 @@ export declare class FD {
|
|||
private client;
|
||||
private socketAvailable;
|
||||
private socketConnected;
|
||||
private waiting;
|
||||
constructor(bin: string, width: number, height: number, host: string, port: number);
|
||||
private startDisplay;
|
||||
private startClient;
|
||||
private logstd;
|
||||
private logsterr;
|
||||
private test;
|
||||
private send;
|
||||
private receive;
|
||||
load(image: string, x: number, y: number, w: number, h: number): Promise<boolean>;
|
||||
display(image: string, exposure: number[]): Promise<boolean>;
|
||||
stop(image: string): Promise<boolean>;
|
||||
load(image: string, x: number, y: number, w: number, h: number): Promise<fdResult>;
|
||||
display(image: string, exposure: number[]): Promise<fdResult>;
|
||||
stop(image: string): Promise<fdResult>;
|
||||
isConnected(): boolean;
|
||||
}
|
||||
export {};
|
||||
|
|
|
@ -28,6 +28,7 @@ class FD {
|
|||
constructor(bin, width, height, host, port) {
|
||||
this.socketAvailable = false;
|
||||
this.socketConnected = false;
|
||||
this.waiting = null;
|
||||
this.bin = bin;
|
||||
this.width = width;
|
||||
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.startDisplay();
|
||||
this.startClient();
|
||||
this.test();
|
||||
}
|
||||
async startDisplay() {
|
||||
this.log.info(`Launching fd binary ${this.bin}`);
|
||||
|
@ -53,12 +53,6 @@ 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}`);
|
||||
await (0, delay_1.delay)(1000);
|
||||
const msg = {
|
||||
action: Action.LOAD,
|
||||
image: './test.jpg'
|
||||
};
|
||||
this.send(msg);
|
||||
}.bind(this));
|
||||
this.client.on('data', function (data) {
|
||||
this.receive(data);
|
||||
|
@ -79,10 +73,6 @@ class FD {
|
|||
logsterr(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);
|
||||
|
@ -91,7 +81,10 @@ class FD {
|
|||
receive(json) {
|
||||
const msg = JSON.parse(json);
|
||||
this.log.info(msg);
|
||||
return msg;
|
||||
if (this.waiting != null) {
|
||||
this.waiting(msg);
|
||||
this.waiting = null;
|
||||
}
|
||||
}
|
||||
async load(image, x, y, w, h) {
|
||||
const msg = {
|
||||
|
@ -102,8 +95,24 @@ class FD {
|
|||
w,
|
||||
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);
|
||||
return true;
|
||||
return promise;
|
||||
}
|
||||
async display(image, exposure) {
|
||||
const msg = {
|
||||
|
@ -111,16 +120,51 @@ class FD {
|
|||
image,
|
||||
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);
|
||||
return true;
|
||||
return promise;
|
||||
}
|
||||
async stop(image) {
|
||||
const msg = {
|
||||
action: Action.STOP,
|
||||
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);
|
||||
return true;
|
||||
return promise;
|
||||
}
|
||||
isConnected() {
|
||||
return this.socketConnected;
|
||||
}
|
||||
}
|
||||
exports.FD = FD;
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -34,7 +34,14 @@ export interface fdOutgoingMessage {
|
|||
|
||||
export interface fdIncomingMessage {
|
||||
action : Action,
|
||||
success : boolean
|
||||
success : boolean,
|
||||
error? : string
|
||||
}
|
||||
|
||||
export interface fdResult {
|
||||
action : Action,
|
||||
image : string,
|
||||
time : number
|
||||
}
|
||||
|
||||
export class FD {
|
||||
|
@ -49,6 +56,8 @@ export class FD {
|
|||
private socketAvailable : boolean = false;
|
||||
private socketConnected : boolean = false;
|
||||
|
||||
private waiting : Function = null;
|
||||
|
||||
constructor (bin: string, width : number, height : number, host : string, port : number) {
|
||||
this.bin = bin;
|
||||
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.startDisplay();
|
||||
this.startClient();
|
||||
this.test();
|
||||
}
|
||||
|
||||
private async startDisplay () {
|
||||
|
@ -77,12 +85,6 @@ 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}`);
|
||||
await delay(1000);
|
||||
const msg : fdOutgoingMessage = {
|
||||
action : Action.LOAD,
|
||||
image : './test.jpg'
|
||||
};
|
||||
this.send(msg);
|
||||
}.bind(this));
|
||||
|
||||
this.client.on('data', function (data : string) {
|
||||
|
@ -109,24 +111,22 @@ export class FD {
|
|||
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 {
|
||||
private receive (json : string) {
|
||||
const msg : fdIncomingMessage = JSON.parse(json);
|
||||
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 = {
|
||||
action : Action.LOAD,
|
||||
image,
|
||||
|
@ -135,27 +135,76 @@ export class FD {
|
|||
w,
|
||||
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);
|
||||
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 = {
|
||||
action : Action.DISPLAY,
|
||||
image,
|
||||
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);
|
||||
return true;
|
||||
return promise;
|
||||
}
|
||||
|
||||
public async stop (image : string) : Promise<boolean> {
|
||||
public async stop (image : string) : Promise<fdResult> {
|
||||
const msg : fdOutgoingMessage = {
|
||||
action : Action.STOP,
|
||||
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);
|
||||
return true;
|
||||
return promise;
|
||||
}
|
||||
|
||||
public isConnected () : boolean {
|
||||
return this.socketConnected;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue