2017-12-18 20:39:51 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
class ApiController {
|
|
|
|
|
|
|
|
constructor( server, driver ) {
|
|
|
|
|
|
|
|
this.server = server;
|
|
|
|
this.driver = driver;
|
|
|
|
|
2017-12-25 21:05:14 +01:00
|
|
|
this.server.post( { path: '/api/writecanvas' }, this.writeCanvas.bind( this ) );
|
2017-12-18 20:39:51 +01:00
|
|
|
this.server.get( { path: '/api/write' }, this.write.bind( this ) );
|
2017-12-25 21:05:14 +01:00
|
|
|
this.server.get( { path: '/api/getdisplaysize' }, this.getDisplaySize.bind( this ) );
|
2017-12-18 20:39:51 +01:00
|
|
|
this.server.get( { path: '/api/setpixel/:x/:y/:r/:g/:b' }, this.setPixel.bind( this ) );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-25 21:05:14 +01:00
|
|
|
writeCanvas( request, resource, next ) {
|
2017-12-18 20:39:51 +01:00
|
|
|
|
|
|
|
this.driver.clearMatrix();
|
|
|
|
|
|
|
|
resource.setHeader( 'Access-Control-Allow-Origin', '*' );
|
|
|
|
|
|
|
|
let data = JSON.parse( request.params.data );
|
|
|
|
|
|
|
|
for ( let obj of data) {
|
|
|
|
this.driver.setPixel( obj.y, obj.x, obj.r, obj.g, obj.b );
|
|
|
|
}
|
|
|
|
|
|
|
|
let buffer = this.driver.getBuffer();
|
|
|
|
this.driver.write( buffer );
|
|
|
|
|
|
|
|
resource.json( 200, { 'msg': 'Ok' } );
|
|
|
|
return next();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
write( request, resource, next ) {
|
|
|
|
|
|
|
|
resource.setHeader( 'Access-Control-Allow-Origin', '*' );
|
|
|
|
|
|
|
|
let buffer = this.driver.getBuffer();
|
|
|
|
this.driver.write( buffer );
|
|
|
|
|
|
|
|
resource.json( 200, { 'msg': 'Ok' } );
|
|
|
|
return next();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-25 21:05:14 +01:00
|
|
|
getDisplaySize( request, resource, next ) {
|
|
|
|
|
|
|
|
resource.setHeader( 'Access-Control-Allow-Origin', '*' );
|
|
|
|
|
|
|
|
resource.json( 200, this.driver.getSize() );
|
|
|
|
return next();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-18 20:39:51 +01:00
|
|
|
setPixel( request, resource, next ) {
|
|
|
|
|
|
|
|
resource.setHeader( 'Access-Control-Allow-Origin', '*' );
|
|
|
|
|
|
|
|
this.driver.setPixel(
|
|
|
|
request.params.x,
|
|
|
|
request.params.y,
|
|
|
|
request.params.r,
|
|
|
|
request.params.g,
|
|
|
|
request.params.b
|
|
|
|
);
|
|
|
|
|
|
|
|
resource.json( 200, { 'msg': 'Ok' } );
|
|
|
|
return next();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.ApiController = ApiController;
|