Skip to content

Commit

Permalink
feat(media/renderers): add minimal media Renderer based on BehaviorSu…
Browse files Browse the repository at this point in the history
…bject
  • Loading branch information
wzr1337 committed Jan 4, 2017
1 parent 238fda9 commit 4e6296e
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var paths = {
}

gulp.task("typescript", () => {
return tsProject.src()
return gulp.src("./src/**/*.ts")
.pipe(tsProject())
.js.pipe(gulp.dest(paths.bin));
});
Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
"author": "Dr. Patrick Bartsch",
"license": "MIT",
"dependencies": {
"@reactivex/rxjs": "^5.0.2"
"@reactivex/rxjs": "^5.0.2",
"@types/body-parser": "0.0.33",
"@types/express": "^4.0.34",
"@types/uuid": "^2.0.29",
"body-parser": "^1.15.2",
"express": "^4.14.0",
"http": "0.0.0",
"uuid": "^3.0.1"
},
"devDependencies": {
"@types/node": "^6.0.56",
Expand Down
63 changes: 63 additions & 0 deletions src/expressapp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as express from 'express';
import * as bodyParser from 'body-parser';

import http = require('http');



// create server and listen on provided port (on all network interfaces).
class Server {
public app: express.Express;
private _server:any;
private _port:number|string|boolean;

constructor (_port?:string) {
this.app = express();
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: false }));

// Get port from environment and store in Express.
this._port = this.normalizePort(process.env.PORT || _port || '3000');
this.app.set('port', this._port);

this._server = http.createServer(this.app);
}

init() {
this._server.listen(this._port);
this._server.on('listening', this.onListening);
}

/**
* Normalize a port into a number, string, or false.
*/
normalizePort(val: any): number|string|boolean {
let port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "listening" event.
*/
onListening = () => {
let addr = this._server.address();
let bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;

console.log('Listening on ' + bind);
}
};

export { Server };
102 changes: 90 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,99 @@
import { Observable, Subject } from '@reactivex/rxjs';
import { Observable, Observer, BehaviorSubject} from '@reactivex/rxjs';
import * as express from 'express';
import { Server } from "./expressapp";
import * as uuid from "uuid"

// set up the server
var server = new Server();
server.init(); // need to init

const rendererId = "d6ebfd90-d2c1-11e6-9376-df943f51f0d8";//uuid.v1(); // FIXED for now

var renderers = [
new BehaviorSubject<{}>({
uri: "/media/renderers/" + rendererId,
id: rendererId,
name: "Netflux",
state: "idle",
offset: 0
})
]

server.app.get('/media/renderers/?:id', (req: express.Request, res: express.Response, next: express.NextFunction) => {
// find the element requested by the client
let element = renderers.find((element:BehaviorSubject<{}>) => {
return (<{id:string}>element.getValue()).id === rendererId;
});

// respond
if(element){
res.status(200);
res.json({
status: "ok",
data: element.getValue()
});
}
else {
res.status(404).send();
}
});

var interval:NodeJS.Timer; //@TODO has to become per-renderer
server.app.post('/media/renderers/?:id', (req: express.Request, res: express.Response, next: express.NextFunction) => {

// find the element requested by the client
let element = renderers.find((element:BehaviorSubject<{}>) => {
return (<{id:string}>element.getValue()).id === rendererId;
});

if(element){
let renderer:any = element.getValue();

if (req.body.hasOwnProperty("state")) {
renderer.state = req.body.state;
if (req.body.state === "play") {
const speed = 1000;
interval = setInterval(() => {
renderer.offset = renderer.hasOwnProperty("offset") ? renderer.offset + speed : 0;
element.next(renderer);
}, speed);
}
else {
clearInterval(interval);
}
}

element.next(renderer); // @TODO: check diffs bevor updating without a need
res.status(200);
res.json({
status: "ok",
data: renderers[0].getValue()
});
}
else {
res.status(404).send();
}
});

server.app.get('/media/renderers/', (req: express.Request, res: express.Response, next: express.NextFunction) => {

// get all available renderes and map their representation to JSON compatible values
let resp = renderers.map((value) => {
return value.getValue();
});

res.status(200);
res.json({
status: "ok",
data: resp
});
});


// register an Object
var mySubject = new Subject();
var subscription = mySubject.subscribe(
var subscription = renderers[0].subscribe(
(x:any) => {
console.log('Next: ' + x);
console.log('Next: ' + JSON.stringify(x));
},
(err:any) => {
console.log('Error: ' + err);
Expand All @@ -16,11 +102,3 @@ var subscription = mySubject.subscribe(
console.log('Completed');
});


// push info into the Object

mySubject.next('foo');

setInterval(() => {
mySubject.next('foo');
}, 1000);
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"files": [
"src/main.ts"
"src/**/*.ts"
],
"compilerOptions": {
"noImplicitAny": true,
Expand All @@ -10,7 +10,8 @@
},
"types": [
"node",
"rxjs"
"express",
"uuid"
],
"typeroots": [
"../node_modules/@types",
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 4e6296e

Please sign in to comment.