1+ angular . module ( 'configurator' , [ ] )
2+ . service ( 'bridgeService' , [ "$http" , function ( $http ) {
3+ var self = this ;
4+ this . state = { base : window . location . origin + "/api/devices" , devices : [ ] , error : "" } ;
5+
6+ this . viewDevices = function ( ) {
7+ this . state . error = "" ;
8+ return $http . get ( this . state . base ) . then (
9+ function ( response ) {
10+ self . state . devices = response . data [ 0 ] . content ;
11+ } ,
12+ function ( error ) {
13+ if ( error . data ) {
14+ self . state . error = error . data . message ;
15+ } else {
16+ self . state . error = "If you're not seeing any devices, you may be running into problems with CORS. " +
17+ "You can work around this by running a fresh launch of Chrome with the --disable-web-security flag." ;
18+ }
19+ console . log ( error ) ;
20+ }
21+ ) ;
22+ } ;
23+
24+ this . addDevice = function ( id , name , type , onUrl , offUrl ) {
25+ this . state . error = "" ;
26+ if ( id ) {
27+ var putUrl = this . state . base + "/" + id ;
28+ return $http . put ( putUrl , {
29+ id : id ,
30+ name : name ,
31+ deviceType : type ,
32+ onUrl : onUrl ,
33+ offUrl : offUrl
34+ } ) . then (
35+ function ( response ) {
36+ self . viewDevices ( ) ;
37+ } ,
38+ function ( error ) {
39+ if ( error . data ) {
40+ self . state . error = error . data . message ;
41+ }
42+ console . log ( error ) ;
43+ }
44+ ) ;
45+ } else {
46+ return $http . post ( this . state . base , {
47+ name : name ,
48+ deviceType : type ,
49+ onUrl : onUrl ,
50+ offUrl : offUrl
51+ } ) . then (
52+ function ( response ) {
53+ self . viewDevices ( ) ;
54+ } ,
55+ function ( error ) {
56+ if ( error . data ) {
57+ self . state . error = error . data . message ;
58+ }
59+ console . log ( error ) ;
60+ }
61+ ) ;
62+ }
63+ } ;
64+
65+ this . deleteDevice = function ( id ) {
66+ this . state . error = "" ;
67+ return $http . delete ( this . state . base + "/" + id ) . then (
68+ function ( response ) {
69+ self . viewDevices ( ) ;
70+ } ,
71+ function ( error ) {
72+ if ( error . data ) {
73+ self . state . error = error . data . message ;
74+ }
75+ console . log ( error ) ;
76+ }
77+ ) ;
78+ } ;
79+
80+ this . editDevice = function ( id , name , type , onUrl , offUrl ) {
81+ this . device . id = id ;
82+ this . device . name = name ;
83+ this . device . onUrl = onUrl ;
84+ this . device . offUrl = offUrl ;
85+ } ;
86+ } ] )
87+
88+ . controller ( 'ViewingController' , [ "$scope" , "bridgeService" , function ( $scope , bridgeService ) {
89+ bridgeService . viewDevices ( ) ;
90+ $scope . bridge = bridgeService . state ;
91+ $scope . deleteDevice = function ( device ) {
92+ bridgeService . deleteDevice ( device . id ) ;
93+ } ;
94+ $scope . testUrl = function ( url ) {
95+ window . open ( url , "_blank" ) ;
96+ } ;
97+ $scope . setBridgeUrl = function ( url ) {
98+ bridgeService . state . base = url ;
99+ bridgeService . viewDevices ( ) ;
100+ } ;
101+ $scope . editDevice = function ( device ) {
102+ bridgeService . editDevice ( device . id , device . name , device . type , device . onUrl , device . offUrl ) ;
103+ } ;
104+ } ] )
105+
106+ . controller ( 'AddingController' , [ "$scope" , "bridgeService" , function ( $scope , bridgeService ) {
107+
108+ $scope . bridge = bridgeService . state ;
109+ $scope . device = { id : "" , name : "" , type : "switch" , onUrl : "" , offUrl : "" } ;
110+ $scope . vera = { base : "" , port : "3480" , id : "" } ;
111+ bridgeService . device = $scope . device ;
112+
113+ $scope . buildUrls = function ( ) {
114+ if ( $scope . vera . base . indexOf ( "http" ) < 0 ) {
115+ $scope . vera . base = "http://" + $scope . vera . base ;
116+ }
117+ $scope . device . onUrl = $scope . vera . base + ":" + $scope . vera . port
118+ + "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum="
119+ + $scope . vera . id ;
120+ $scope . device . offUrl = $scope . vera . base + ":" + $scope . vera . port
121+ + "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum="
122+ + $scope . vera . id ;
123+ } ;
124+
125+ $scope . testUrl = function ( url ) {
126+ window . open ( url , "_blank" ) ;
127+ } ;
128+
129+ $scope . addDevice = function ( ) {
130+ bridgeService . addDevice ( $scope . device . id , $scope . device . name , $scope . device . type , $scope . device . onUrl , $scope . device . offUrl ) . then (
131+ function ( ) {
132+ $scope . device . id = "" ;
133+ $scope . device . name = "" ;
134+ $scope . device . onUrl = "" ;
135+ $scope . device . offUrl = "" ;
136+ } ,
137+ function ( error ) {
138+ }
139+ ) ;
140+ }
141+ } ] )
142+
143+ . controller ( 'ErrorsController' , [ "$scope" , "bridgeService" , function ( $scope , bridgeService ) {
144+ $scope . bridge = bridgeService . state ;
145+ } ] ) ;
0 commit comments