Skip to content

Commit

Permalink
fix(main): compare lowercase names for subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
wzr1337 committed Jan 17, 2017
1 parent d3b51c4 commit da3e9f7
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/expressapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class viwiWebSocket {
}

error(code:number, err:Error):void {
this.ws.send(JSON.stringify({type: "error", code: "500", data: err.message}));
this.ws.send(JSON.stringify({type: "error", code: code, data: err.message}));
}

subscribeAck(event:string):void {
Expand Down
18 changes: 9 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ fs.readdir(path.join(__dirname, "plugins"), (err:NodeJS.ErrnoException, files: s
console.log("Loading Plugin:", service.name);
service.resources.map((resource:Resource) => {
let basePath = "/" + service.name.toLowerCase() + "/" + resource.name.toLowerCase() + "/";
console.log("Registering endpoint:", service.name);
server.app.get(basePath, resourceGET(service, resource)); //READ
server.app.post(basePath, resourcePOST(service, resource)); //CREATE
server.app.post(basePath + ':id', elementPOST(service, resource)); //READ
Expand All @@ -59,7 +58,8 @@ fs.readdir(path.join(__dirname, "plugins"), (err:NodeJS.ErrnoException, files: s
* @param resource The resource name.
*/
const elementGET = (service:Service, resource:Resource) => {
let elementPath = pathof(service, resource) + "/:id"
let elementPath = pathof(service, resource) + "/:id";
if(resource.getElement) { console.log("GET ", elementPath, "registered") };
return (req: express.Request, res: express.Response, next: express.NextFunction) => {

if(!resource.getElement) {
Expand Down Expand Up @@ -93,7 +93,7 @@ const elementGET = (service:Service, resource:Resource) => {
*/
const resourceGET = (service:Service, resource:Resource) => {
let resourcePath = pathof(service, resource);
console.log("GET", resourcePath, "registered");
if(resource.getResource ) { console.log("GET ", resourcePath, "registered") };
return (req: express.Request, res: express.Response, next: express.NextFunction) => {
if(!resource.getResource) {
res.status(501).send("Not Implemented");
Expand Down Expand Up @@ -131,7 +131,7 @@ const resourceGET = (service:Service, resource:Resource) => {
*/
const resourcePOST = (service:Service, resource:Resource) => {
let resourcePath = pathof(service, resource);
console.log("POST", resourcePath, "registered");
if(resource.createElement) { console.log("POST ", resourcePath, "registered") };
return (req: express.Request, res: express.Response, next: express.NextFunction) => {
if(!resource.createElement) {
res.status(501).send("Not Implemented");
Expand All @@ -148,7 +148,7 @@ const resourcePOST = (service:Service, resource:Resource) => {
*/
const elementDELETE = (service:Service, resource:Resource) => {
let elementPath = pathof(service, resource) + "/:id"
console.log("DELETE", elementPath, "registered");
if(resource.deleteElement) { console.log("DELETE", elementPath, "registered") };
return (req: express.Request, res: express.Response, next: express.NextFunction) => {

if(!resource.deleteElement) {
Expand Down Expand Up @@ -182,7 +182,7 @@ const elementDELETE = (service:Service, resource:Resource) => {
*/
const elementPOST = (service:Service, resource:Resource) => {
let elementPath = pathof(service, resource) + "/:id"
console.log("POST", elementPath, "registered");
if(resource.updateElement) { console.log("POST ", elementPath, "registered") };
return (req: express.Request, res: express.Response, next: express.NextFunction) => {

// find the element requested by the client
Expand Down Expand Up @@ -220,20 +220,20 @@ const handleWebSocketMessages = (service:Service, resource:Resource, ws:WebSocke
msg = JSON.parse(message);
}
catch(err) {
_viwiWebSocket.error(500, new Error(err));
_viwiWebSocket.error(400, new Error(err));
return;
}
switch (msg.type) {
case "subscribe":
console.log("New subscription:", msg.event);
let captureGroups = msg.event.match(URIREGEX);
if (captureGroups && (service.name === captureGroups[1] || "$") && (resource.name === captureGroups[2] || "$")) {
if (captureGroups && (service.name.toLowerCase() === captureGroups[1].toLowerCase()) && (resource.name.toLowerCase() === captureGroups[2].toLowerCase())) {
if (resource.elementSubscribable) {
let elementId = captureGroups[3];
if (elementId) {
// this is an element subscription
let element = resource.getElement(elementId);
if (element) {
console.log("New subscription:", msg.event);
element.takeUntil(unsubscriptions.map(topic => {topic === msg.event}))
.subscribe(
(data:any) => {
Expand Down
96 changes: 84 additions & 12 deletions src/plugins/media/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { BehaviorSubject, Subject } from '@reactivex/rxjs';
import { Service, Resource, resourceAction } from "../viwiPlugin";
import { Service, Resource } from "../viwiPlugin";

class Media implements Service {
private _resources:Renderer[]=[];
private _resources:Resource[]=[];

constructor() {
this._resources.push(new Renderer(this))
this._resources.push(new Renderers(this));
this._resources.push(new Collections(this))
}

get name() {
Expand All @@ -17,35 +18,51 @@ class Media implements Service {
}
}

class Renderer implements Resource {
interface RendererObject {
id: string;
name: string;
uri: string;
media?: Object;
currentMediaItem?: Object;
offset?: number;
scan?: "off"|"up"|"down";
state?: "idle"|"play"|"pause"|"stop"|"ff"|"fr";
repeat?: "off"|"repeatall"|"repeatone";
shuffle?: "on"|"off";
type?: "track"|"video"|"image";
}

class Renderers implements Resource {
private _name:string;
private _renderers:BehaviorSubject<{}>[] = [];
private _change:Subject<resourceAction> = new Subject();
private _change:Subject<string> = new Subject();

constructor(private service:Service) {
this._name = "renderers";

const rendererId = "d6ebfd90-d2c1-11e6-9376-df943f51f0d8";//uuid.v1(); // FIXED for now
let netfluxRenderer = new BehaviorSubject<{}>({
uri: "/" + this.service.name.toLowerCase() + "/" + this._name.toLowerCase() + "/" + rendererId,
//const collections = service.resources.map<Collections>(resource => resource.name === "collections");
//const initialCollection = collections.map( element => element.name === "default");
let netfluxRenderer = new BehaviorSubject<RendererObject>({
uri: "/" + this.service.name.toLowerCase() + "/" + this.name.toLowerCase() + "/" + rendererId,
id: rendererId,
name: "Netflux",
state: "idle",
offset: 0
offset: 0,
media: "initialCollection"
});
this._renderers.push(netfluxRenderer);
this._change.next(resourceAction.add);
this._change.next("add");
}

get name():string {
return this._name;
return this.constructor.name;
};

get elementSubscribable():Boolean {
return true;
};

get change():Subject<resourceAction> {
get change():Subject<string> {
return this._change;
}

Expand Down Expand Up @@ -93,4 +110,59 @@ class Renderer implements Resource {
}
}


interface CollectionObject {
id: string;
name: string;
uri: string;
items?: Object[];
}
class Collections implements Resource {
private _collections:BehaviorSubject<{}>[] = [];
private _change:Subject<string> = new Subject();

constructor(private service:Service) {

const rendererId = "deadbeef-d2c1-11e6-9376-df943f51f0d8";//uuid.v1(); // FIXED for now
let initialCollection = new BehaviorSubject<CollectionObject>({
uri: "/" + this.service.name.toLowerCase() + "/" + this.name.toLowerCase() + "/" + rendererId,
id: rendererId,
name: "default",
items: []
});
this._collections.push(initialCollection);
this._change.next("add");
}

get name():string {
return this.constructor.name;
};

get elementSubscribable():Boolean {
return true;
};

get change():Subject<string> {
return this._change;
}

getElement(elementId:string):BehaviorSubject<{}> {
// find the element requested by the client
return this._collections.find((element:BehaviorSubject<{}>) => {
return (<{id:string}>element.getValue()).id === elementId;
});
};

getResource(offset?:string|number, limit?:string|number):BehaviorSubject<{}>[]{
// retriev all element
let resp:BehaviorSubject<{}>[];

if((typeof offset === "number" && typeof limit === "number") || (typeof limit === "number" && !offset) || (typeof offset === "number" && !limit) || (!offset && !limit)) {
resp = this._collections.slice(<number>offset, <number>limit);
}

return resp;
};
}

export = Media;
8 changes: 1 addition & 7 deletions src/plugins/viwiPlugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@ declare namespace viwiPlugin {
resources:Resource[];
}

enum resourceAction {
"add",
"move",
"remove"
}

interface Resource {
name:string;
change:Subject<resourceAction>;
change:Subject<"add"|"move"|"remove">;

//@TODO: will returning promises make sense???
getResource?(offset?:string|number, limit?:string|number):BehaviorSubject<any>[]; //GET /<service>/<resource>/
Expand Down

0 comments on commit da3e9f7

Please sign in to comment.