@@ -86,6 +86,32 @@ define(['./web-dashboard.xml'], function(dashboardTemplate) {
8686 return Math . round ( ( Math . pow ( 10 , value / 10000 ) - 1 ) * 100 ) / 100 ;
8787 }
8888
89+ function forEachPropertyType ( things , type , fn , thingIds ) {
90+ if ( isEmpty ( thingIds ) ) {
91+ thingIds = Object . keys ( things ) ;
92+ }
93+ for ( var i = 0 ; i < thingIds . length ; i ++ ) {
94+ var thingId = thingIds [ i ] ;
95+ var thing = things [ thingId ] ;
96+ if ( ! thing ) {
97+ continue ;
98+ }
99+ var property = undefined ;
100+ var propertyName = undefined ;
101+ for ( var name in thing . properties ) {
102+ property = thing . properties [ name ] ;
103+ var propType = property [ '@type' ] ;
104+ if ( propType === type ) {
105+ propertyName = name ;
106+ break ;
107+ }
108+ }
109+ if ( propertyName ) {
110+ fn ( thing , thingId , property , propertyName ) ;
111+ }
112+ }
113+ }
114+
89115 var extensionId = 'web-dashboard' ;
90116 var extensionName = 'Dashboard' ;
91117
@@ -112,13 +138,6 @@ define(['./web-dashboard.xml'], function(dashboardTemplate) {
112138 this . processThings ( config , things , properties ) ;
113139 } ) ) ;
114140 } ,
115- toggleFullScreen : function ( ) {
116- if ( window . innerHeight == screen . height ) {
117- document . exitFullscreen ( ) ;
118- } else {
119- document . body . requestFullscreen ( ) ;
120- }
121- } ,
122141 onDataChange : function ( ) {
123142 console . log ( 'onDataChange' ) ;
124143 Promise . all ( [
@@ -136,52 +155,17 @@ define(['./web-dashboard.xml'], function(dashboardTemplate) {
136155 for ( var i = 0 ; i < tilesDef . length ; i ++ ) {
137156 var tileDef = tilesDef [ i ] ;
138157 var type = tileDef . type ;
139- var title = tileDef . title || tileDef . type ;
140- var unit = undefined ;
141158 var values = [ ] ;
142159 var paths = [ ] ;
143- var thingIds = tileDef . thingIds ;
144- if ( isEmpty ( thingIds ) ) {
145- thingIds = Object . keys ( things ) ;
146- }
147- for ( var j = 0 ; j < thingIds . length ; j ++ ) {
148- var thingId = thingIds [ j ] ;
149- var thing = things [ thingId ] ;
150- if ( ! thing ) {
151- continue ;
152- }
153- if ( ( type === 'auto' ) || ! isValidValue ( type ) ) {
154- var thingType = thing [ '@type' ] [ 0 ] ;
155- type = typeByCapability [ thingType ] ;
156- }
157- if ( ! type ) {
158- continue ;
159- }
160- if ( ! isValidValue ( title ) && ( thingIds . length === 1 ) && isValidValue ( thing . title ) ) {
161- title = thing . title ;
162- }
163- var propertyName = undefined ;
164- for ( var propName in thing . properties ) {
165- var prop = thing . properties [ propName ] ;
166- var propType = prop [ '@type' ] ;
167- if ( propType === type ) {
168- propertyName = propName ;
169- if ( ! isValidValue ( unit ) && ( thingIds . length === 1 ) && isValidValue ( prop . unit ) ) {
170- unit = prop . unit ;
171- }
172- break ;
173- }
174- }
175- if ( ! propertyName ) {
176- continue ;
160+ forEachPropertyType ( things , type , function ( thing , thingId , property , propertyName ) {
161+ if ( thing . archiveData && ! property . configuration ) {
162+ paths . push ( thingId + '/' + propertyName ) ;
177163 }
178164 var props = properties [ thingId ] ;
179165 if ( props && isValidValue ( props [ propertyName ] ) ) {
180166 values . push ( props [ propertyName ] ) ;
181167 }
182- paths . push ( thingId + '/' + propertyName ) ;
183- //console.log('thing', JSON.stringify(thing, undefined, 2), JSON.stringify(props, undefined, 2));
184- }
168+ } , tileDef . thingIds ) ;
185169 var value = undefined ;
186170 if ( values . length > 0 ) {
187171 var valueType = typeof values [ 0 ] ;
@@ -192,16 +176,12 @@ define(['./web-dashboard.xml'], function(dashboardTemplate) {
192176 }
193177 }
194178 console . log ( 'value: ' + value + ', type: ' + type , values ) ;
195- if ( unitByType [ type ] ) {
196- unit = unitByType [ type ] ;
197- }
198179 var tile = assignMap ( { } , tileDef , {
199- title : oneOf ( title , type , 'n/a' ) ,
180+ title : oneOf ( tileDef . title , type , 'n/a' ) ,
200181 paths : paths ,
201182 count : values . length ,
202- value : formatValue ( value , type ) ,
203- icon : ( typeof value === 'boolean' ) ,
204- unit : formatUnit ( unit )
183+ value : value ,
184+ unit : formatUnit ( unitByType [ type ] )
205185 } ) ;
206186 //console.log('tile', JSON.stringify(tile, undefined, 2), JSON.stringify(tileDef, undefined, 2));
207187 tiles . push ( tile ) ;
@@ -210,6 +190,33 @@ define(['./web-dashboard.xml'], function(dashboardTemplate) {
210190 //console.log('tiles', JSON.stringify(tiles, undefined, 2));
211191 this . tiles = tiles ;
212192 } ,
193+ formatValue : function ( tile ) {
194+ return formatValue ( tile . value , tile . type ) ;
195+ } ,
196+ onTileClicked : function ( tile ) {
197+ //console.log('onTileClicked() ' + tile.value + ' (' + (typeof tile.value) + ')');
198+ if ( typeof tile . value !== 'boolean' ) {
199+ return Promise . reject ( 'Unsupported value' ) ;
200+ }
201+ app . getThingsById ( ) . then ( function ( things ) {
202+ var promises = [ ] ;
203+ tile . value = ! tile . value ;
204+ forEachPropertyType ( things , tile . type , function ( thing , thingId , property , propertyName ) {
205+ if ( ! property . readOnly ) {
206+ var valueByName = { } ;
207+ valueByName [ propertyName ] = tile . value ;
208+ promises . push ( fetch ( '/things/' + thingId + '/properties' , {
209+ method : 'PUT' ,
210+ body : JSON . stringify ( valueByName )
211+ } ) ) ;
212+ }
213+ } , tile . thingIds ) ;
214+ return Promise . all ( promises ) ;
215+ } ) . then ( function ( ) {
216+ toaster . toast ( 'Things updated' ) ;
217+ //app.clearCache();
218+ } ) ;
219+ } ,
213220 openHistoricalData : function ( paths ) {
214221 console . log ( 'paths: ' + paths ) ;
215222 if ( paths . length === 1 ) {
@@ -224,16 +231,6 @@ define(['./web-dashboard.xml'], function(dashboardTemplate) {
224231 }
225232 } ) ;
226233
227- addPageComponent ( dashboardVue ) ;
228-
229- menu . pages . push ( {
230- id : extensionId ,
231- name : extensionName
232- } ) ;
233-
234- homePage . pages . push ( {
235- id : extensionId ,
236- name : extensionName
237- } ) ;
234+ addPageComponent ( dashboardVue , 'fa-columns' ) ;
238235
239236} ) ;
0 commit comments