1
+ /*========================================
2
+ MiCasa Verde VeraLite Device Demo
3
+ ==========================================*/
4
+
5
+ //Change the IP below to the IP of your Vera unit
6
+ var MiCasaIP = "192.168.50.155" ;
7
+ var MiCasaPort = "3480" ;
8
+
9
+ //Main function auto loads when iViewer is started
10
+ //Make sure you don't have more than one CF.userMain declared in your any scripts your project uses.
11
+ CF . userMain = function ( ) {
12
+ //calls getDevices with a basejoin of 100
13
+ getDevices ( 100 ) ;
14
+ getRooms ( 200 ) ;
15
+ } ;
16
+
17
+ //Gets the url/host before sending the JSON request to MiCasaVerde
18
+ geturl = function ( ) {
19
+ return "http://" + MiCasaIP + ":" + MiCasaPort + "/" ;
20
+ } ;
21
+
22
+ micasarpc = function ( method , callback ) {
23
+ var host = self . geturl ( ) ;
24
+ var url = host + method ;
25
+
26
+ CF . request ( url , function ( status , headers , body ) {
27
+ var data = JSON . parse ( body ) ;
28
+ callback ( data ) ;
29
+ } ) ;
30
+ } ;
31
+
32
+ //Sets up room names for each list
33
+ getRooms = function ( join ) {
34
+ var baseJoin = join ;
35
+
36
+ micasarpc ( "data_request?id=user_data&output_format=json" , function ( data ) {
37
+
38
+ RoomArray = [ ] ;
39
+
40
+ for ( var i = 0 ; i < data . rooms . length ; i ++ ) {
41
+
42
+ var roomid = data . rooms [ i ] . id ;
43
+ var roomname = data . rooms [ i ] . name ;
44
+
45
+ CF . setJoin ( "s" + baseJoin , roomname ) ;
46
+ baseJoin ++ ;
47
+ } ;
48
+ } ) ;
49
+ } ;
50
+
51
+ //called when iViewer is started and gets all devices of Mi Casa Verde. Then Assigns names and device id's to buttons
52
+ getDevices = function ( join ) {
53
+ var baseJoin = join ;
54
+
55
+ micasarpc ( "data_request?id=user_data&output_format=json" , function ( data ) {
56
+
57
+ DeviceArray = [ ] ;
58
+
59
+ for ( var i = 0 ; i < data . devices . length ; i ++ ) {
60
+
61
+ var deviceid = data . devices [ i ] . id ;
62
+ var name = data . devices [ i ] . name ;
63
+ var roomid = data . devices [ i ] . room ;
64
+
65
+ if ( data . devices [ i ] . invisible == null ) {
66
+ // Add to array to add to list in one go later
67
+ DeviceArray . push ( {
68
+ s10 : name ,
69
+ Room : roomid ,
70
+ s11 : {
71
+ tokens : {
72
+ "[id]" : deviceid
73
+ }
74
+ } ,
75
+ d12 : {
76
+ tokens : {
77
+ "[id]" : deviceid
78
+ }
79
+ } ,
80
+ d13 : {
81
+ tokens : {
82
+ "[id]" : deviceid
83
+ }
84
+ }
85
+ } ) ;
86
+ } ;
87
+ } ;
88
+
89
+ //add all devices found to Room list
90
+ for ( var i = 0 ; i < DeviceArray . length ; i ++ ) {
91
+ if ( DeviceArray [ i ] . Room == "1" ) {
92
+ CF . listAdd ( "l" + baseJoin , [ DeviceArray [ i ] ] ) ;
93
+ } ;
94
+ if ( DeviceArray [ i ] . Room == "2" ) {
95
+ CF . listAdd ( "l" + ( baseJoin + 1 ) , [ DeviceArray [ i ] ] ) ;
96
+ } ;
97
+ if ( DeviceArray [ i ] . Room == "3" ) {
98
+ CF . listAdd ( "l" + ( baseJoin + 2 ) , [ DeviceArray [ i ] ] ) ;
99
+ } ;
100
+ if ( DeviceArray [ i ] . Room == "4" ) {
101
+ CF . listAdd ( "l" + ( baseJoin + 3 ) , [ DeviceArray [ i ] ] ) ;
102
+ } ;
103
+ } ;
104
+
105
+ //get the status of all devices
106
+ UpdateAllDevices ( ) ;
107
+ } ) ;
108
+ } ;
109
+
110
+ DeviceOn = function ( list , listIndex , join ) {
111
+
112
+ CF . getJoin ( list + ":" + listIndex + ":" + join , function ( j , v , t ) {
113
+ micasarpc ( ( "data_request?id=action&output_format=json&DeviceNum=" + t [ "[id]" ] + "&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1" ) , function ( data ) {
114
+
115
+ //data gives back the job id, can use: http://ip_address:3480/data_request?id=jobstatus&job=6&plugin=zwave to pull back job success
116
+
117
+ } ) ;
118
+ } ) ;
119
+
120
+ //delay for 1 second then get the device status
121
+ setTimeout ( function ( ) { UpdateAllDevices ( ) } , 1000 ) ;
122
+ } ;
123
+
124
+ DeviceOff = function ( list , listIndex , join ) {
125
+
126
+ CF . getJoin ( list + ":" + listIndex + ":" + join , function ( j , v , t ) {
127
+ micasarpc ( ( "data_request?id=action&output_format=json&DeviceNum=" + t [ "[id]" ] + "&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0" ) , function ( data ) {
128
+
129
+ //data gives back the job id, can use: http://ip_address:3480/data_request?id=jobstatus&job=6&plugin=zwave to pull back job success/fail
130
+
131
+ } ) ;
132
+ } ) ;
133
+
134
+ //delay for 1 second then get the device status
135
+ setTimeout ( function ( ) { UpdateAllDevices ( ) } , 1000 ) ;
136
+ } ;
137
+
138
+ GetDeviceStatus = function ( list , listIndex , join ) {
139
+ CF . getJoin ( list + ":" + listIndex + ":" + join , function ( j , v , t ) {
140
+ micasarpc ( ( "data_request?id=status&output_format=json&DeviceNum=" + t [ "[id]" ] ) , function ( data ) {
141
+
142
+ var devicestatus = data [ "Device_Num_" + t [ "[id]" ] ] . states [ 0 ] . value ;
143
+
144
+ SetDeviceStatus ( list , listIndex , devicestatus ) ;
145
+ } ) ;
146
+ } ) ;
147
+ } ;
148
+
149
+ SetDeviceStatus = function ( list , listIndex , status ) {
150
+ if ( status === "0" ) {
151
+ CF . setJoin ( list + ":" + listIndex + ":s11" , "Resources/light_Off.jpg" ) ;
152
+ } ;
153
+
154
+ if ( status === "1" ) {
155
+ CF . setJoin ( list + ":" + listIndex + ":s11" , "Resources/light_On.jpg" ) ;
156
+ } ;
157
+ } ;
158
+
159
+ UpdateAllDevices = function ( ) {
160
+ var listjoin = 100 ;
161
+ var maxrooms = ( listjoin + 4 ) ;
162
+
163
+ //gets each list and then updates all devices in each list
164
+ for ( listjoin ; listjoin < maxrooms ; listjoin ++ ) {
165
+ var list = ( "l" + listjoin ) ;
166
+
167
+ CF . listInfo ( list , function ( list , count , first , numVisible , scrollPosition ) {
168
+ //loop through the list and set the devices status.
169
+ var listcount = count ;
170
+
171
+ for ( var i = 0 ; i < listcount ; i ++ ) {
172
+ GetDeviceStatus ( list , i , "s11" ) ;
173
+ } ;
174
+ } ) ;
175
+ } ;
176
+ } ;
0 commit comments