Created
December 4, 2016 18:20
-
-
Save MZachmann/14a5a93302f4c52f109d41a31bffc89a to your computer and use it in GitHub Desktop.
Angular2 Lamp Service Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Http, Headers, ConnectionBackend, BaseRequestOptions, Response, ResponseOptions } from '@angular/http'; | |
import { Observable } from 'rxjs'; | |
import { Injectable, Inject } from '@angular/core'; | |
export class Lamp{ | |
public name : string; | |
public address : string; | |
public deviceClass : string; | |
public type : string; | |
public bright : string; | |
public isDimmer : boolean; | |
public color : string; | |
} | |
@Injectable() | |
export class LampsService { | |
private headers = new Headers(); | |
private http : Http; | |
public baseUrl:string; | |
public responsive : string; | |
public myDoc : Document; | |
constructor(@Inject(Http) httpService) { | |
this.http = httpService; | |
this.baseUrl = this.getRemoteHost(); | |
} | |
// do this to support 192.168.0.92 for testing but allow that or lamps.home for production | |
// and then we can use DNS mapping to support lamps.home | |
getRemoteHost() : string | |
{ | |
var x = location.hostname; | |
// this lets us debug on a local machine (and use hardcoded IP) while on the PI it uses the location.hostname | |
if( x == 'localhost') | |
{ | |
x = "192.168.0.92"; | |
} | |
return "http://" + x; | |
} | |
// this parses the entire device list and creates a list of lamps rather slowly | |
parseAllDevices(deviceList : string) : Lamp[] | |
{ | |
var parser = new DOMParser(); | |
this.myDoc = parser.parseFromString(deviceList, "text/xml"); | |
var children : NodeList = this.myDoc.childNodes[0].childNodes; | |
var len = children.length; | |
var theList : Lamp[] = []; | |
for(var i = 0; i<len; i++) | |
{ | |
var kid : Node = children.item(i); | |
// the first one is named 'root' and the rest are named 'node' | |
if(kid.nodeName == 'node') | |
{ | |
var subKids : NodeList = kid.childNodes; | |
var kidLen = subKids.length; | |
var newLamp = new Lamp(); | |
// if it has an attribute nodeDefId="DimmerLampSwitch" then it is a Dimmer Switch | |
var attribs : NamedNodeMap = kid.attributes; | |
var theId : Attr = attribs.getNamedItem('nodeDefId'); | |
newLamp.isDimmer = false; | |
if(theId == null || !(theId.value.startsWith("DimmerLamp") || theId.value.startsWith("RelayLamp"))) | |
{ | |
// don't take anything but lamps | |
continue; | |
} | |
if(theId != null && (theId.value == "DimmerLampSwitch" || theId.value == "DimmerLampSwitch_ADV")) | |
{ | |
newLamp.isDimmer = true; | |
} | |
// walk through the various properties of the lamp | |
for(var j = 0; j<kidLen; j++) | |
{ | |
var subChild = subKids.item(j); | |
switch(subChild.nodeName) | |
{ | |
case 'name' : | |
newLamp.name = subChild.textContent; | |
break; | |
case 'address' : | |
newLamp.address = subChild.textContent; | |
break; | |
case 'deviceClass' : | |
newLamp.deviceClass = subChild.textContent; | |
break; | |
case 'type' : | |
newLamp.type = subChild.textContent; | |
break; | |
// property contains a bunch of attributes of which we want the value attribute | |
case 'property' : | |
var attrib : Attr = subChild.attributes.getNamedItem('value'); | |
if(attrib != null) | |
{ | |
newLamp.bright = attrib.value; | |
} | |
this.setLampColor(newLamp); | |
break; | |
} | |
//console.log(subChild.nodeName); | |
} | |
theList.push(newLamp); | |
} | |
} | |
return theList.sort( (a : Lamp, b : Lamp) => { | |
if (a.name > b.name) { | |
return 1; | |
} | |
return -1; | |
}); | |
} | |
addCredentials() : Headers | |
{ | |
// Authentication is done by nginx on the proxy system | |
// var username = "admin"; | |
// var password = "password for isy system"; | |
var headers = new Headers(); | |
// headers.append('Authorization', 'Basic ' + btoa(username + ':' + password)); | |
return headers; | |
} | |
setLampColor(lamp: Lamp) | |
{ | |
lamp.color = (lamp.bright != "0") ? "red" : "green"; | |
} | |
getAllpages() : Observable<Response> { | |
var myheaders : Headers = this.addCredentials(); | |
return this.http.get(this.baseUrl + '/rest/nodes/devices', {headers : myheaders}).catch(this.handleError); | |
} | |
handleError(error: any) { | |
console.error(error); | |
return Observable.throw(error.json().error || 'Server error'); | |
} | |
turnOnLamp(lamp: Lamp) : Observable<Response> | |
{ | |
var nodeId : string = encodeURIComponent(lamp.address); | |
var pathAll : string = "/rest/nodes/" + nodeId + "/cmd/DON"; | |
var myheaders : Headers = this.addCredentials(); | |
lamp.bright = "255"; | |
this.setLampColor(lamp); | |
return this.http.get(this.baseUrl + pathAll, {headers : myheaders}).catch(this.handleError); | |
} | |
turnOffLamp(lamp : Lamp) : Observable<Response> | |
{ | |
var myheaders : Headers = this.addCredentials(); | |
var nodeId : string = encodeURIComponent(lamp.address); | |
var pathAll : string = "/rest/nodes/" + nodeId + "/cmd/DOF"; | |
lamp.bright = "0"; | |
this.setLampColor(lamp); | |
return this.http.get(this.baseUrl + pathAll, {headers : myheaders}).catch(this.handleError); | |
} | |
setBrightness(lamp : Lamp, brightness : number) : Observable<Response> | |
{ | |
var myheaders : Headers = this.addCredentials(); | |
var nodeId : string = encodeURIComponent(lamp.address); | |
var pathAll : string = "/rest/nodes/" + nodeId + "/cmd/DON/" + brightness; | |
this.setLampColor(lamp); | |
return this.http.get(this.baseUrl + pathAll, {headers : myheaders}).catch(this.handleError); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment