11var netatmo = {
22 id : 'netatmo' ,
33 lang : config . lang || 'nl' ,
4- params : config && config . netatmo && config . netatmo . params || "access_token=" ,
4+ location : '.netatmo' ,
5+ params : "access_token=" ,
56 access_token : null ,
6- refresh_token : config && config . netatmo && config . netatmo . refresh_token ,
7+ refreshToken : config && config . netatmo && config . netatmo . refresh_token ,
8+ refreshInterval : config && config . netatmo && config . netatmo . refreshInterval || 1 ,
9+ hideLoadTimer : config && config . netatmo && config . netatmo . hideLoadTimer ,
10+ fadeInterval : 1000 ,
711 translate :{
812 sensorType : {
913 'CO2' : 'wi-na' ,
@@ -20,11 +24,17 @@ var netatmo = {
2024 auth_endpoint : 'oauth2/token' ,
2125 data_endpoint : 'api/getstationsdata'
2226 } ,
23- location : '.netatmo' ,
24- fadeInterval : config . weather . fadeInterval || 1000 ,
2527 init : function ( ) {
26- netatmo . load . data ( ) ;
27-
28+ netatmo . loader = $ ( '#netatmo-module .loadTimer .loader' ) [ 0 ] ;
29+ netatmo . border = $ ( '#netatmo-module .loadTimer .border' ) [ 0 ] ;
30+ netatmo . α = 0 ;
31+ netatmo . t = netatmo . refreshInterval * 60 * 1000 / 360 ;
32+ if ( netatmo . hideLoadTimer ) {
33+ $ ( ".loadTimer" ) . hide ( ) ;
34+ }
35+ // run timer
36+ netatmo . update . load ( ) ;
37+ // add string format method
2838 if ( ! String . prototype . format ) {
2939 String . prototype . format = function ( ) {
3040 var args = arguments ;
@@ -37,58 +47,85 @@ var netatmo = {
3747 } ;
3848 }
3949 } ,
50+ update : {
51+ load : function ( ) {
52+ return Q . fcall (
53+ netatmo . load . token , netatmo . render . error
54+ ) . then (
55+ netatmo . load . data , netatmo . render . error
56+ ) . then (
57+ netatmo . render . all
58+ ) . then (
59+ netatmo . update . wait
60+ ) ; //.done();
61+ } ,
62+ wait : function ( ) {
63+ netatmo . α ++ ;
64+ netatmo . α %= 360 ;
65+ var r = ( netatmo . α * Math . PI / 180 )
66+ , x = Math . sin ( r ) * 125
67+ , y = Math . cos ( r ) * - 125
68+ , mid = ( netatmo . α > 180 ) ? 1 : 0
69+ , anim = 'M 0 0 v -125 A 125 125 1 '
70+ + mid + ' 1 '
71+ + x + ' '
72+ + y + ' z' ;
73+ netatmo . loader . setAttribute ( 'd' , anim ) ;
74+ netatmo . border . setAttribute ( 'd' , anim ) ;
75+ if ( r === 0 ) {
76+ // refresh data
77+ netatmo . update . load ( ) ;
78+ } else {
79+ // wait further
80+ setTimeout ( netatmo . update . wait , netatmo . t ) ;
81+ }
82+ }
83+ } ,
4084 load : {
41- data : function ( ) {
42- Q . fcall ( function ( ) {
43- // call for token
44- return Q (
45- $ . ajax ( {
46- type : 'POST' ,
47- url : netatmo . api . base + netatmo . api . auth_endpoint ,
48- data : 'grant_type=refresh_token'
49- + '&refresh_token=' + netatmo . refresh_token
50- + '&client_id=' + config . netatmo . client_id
51- + '&client_secret=' + config . netatmo . client_secret
52- } )
53- ) ;
54- } , function ( reason ) {
55- console . log ( "error " + reason ) ;
56- } ) . then ( function ( data ) {
57- // call for station data
58- console . log ( "Done: " + data ) ;
59- netatmo . refresh_token = data . refresh_token ;
60- netatmo . access_token = data . access_token ;
61- return Q (
62- $ . ajax ( {
63- url : netatmo . api . base + netatmo . api . data_endpoint ,
64- data : netatmo . params + netatmo . access_token
65- } )
66- ) ;
67- } , function ( reason ) {
68- console . log ( "error " + reason ) ;
69- } ) . then ( function ( data ) {
70- netatmo . render . all ( data ) ;
71- } ) . done ( ) ;
85+ token : function ( ) {
86+ return Q (
87+ $ . ajax ( {
88+ type : 'POST' ,
89+ url : netatmo . api . base + netatmo . api . auth_endpoint ,
90+ data : 'grant_type=refresh_token'
91+ + '&refresh_token=' + netatmo . refreshToken
92+ + '&client_id=' + config . netatmo . client_id
93+ + '&client_secret=' + config . netatmo . client_secret
94+ } )
95+ ) ;
96+ } ,
97+ data : function ( data ) {
98+ // call for station data
99+ console . log ( "Netatmo-Module: token loaded " + data . access_token ) ;
100+ netatmo . refreshToken = data . refresh_token ;
101+ netatmo . accessToken = data . access_token ;
102+ return Q (
103+ $ . ajax ( {
104+ url : netatmo . api . base + netatmo . api . data_endpoint ,
105+ data : netatmo . params + netatmo . accessToken
106+ } )
107+ ) ;
72108 }
73109 } ,
74110 html :{
75111 moduleWrapper : '<div class="modules">{0}</div>' ,
76112 module : '<div class="module"><div class="data">{0}</div><div class="name small">{1}</div></div>' ,
77113 dataWrapper : '<table class>{0}</table>' ,
78114 data : '<tr><td class="small">{0}</td><td class="value small">{1}</td></tr>'
79- // data: '<div><span class="small i2con ">{0}</span> <span class="value small">{1}</span></div>'
80115 } ,
81116 render : {
82117 all : function ( data ) {
83118 var sContent = '' ;
84119 var device = data . body . devices [ 0 ] ;
120+ console . log ( "Netatmo-Module: data loaded, last updated " + new Date ( 1000 * device . dashboard_data . time_utc ) ) ;
85121 // render modules
86122 sContent += netatmo . render . modules ( device ) ;
87123 // place content
88124 $ ( netatmo . location ) . updateWithText (
89125 sContent ,
90126 netatmo . fadeInterval
91127 ) ;
128+ return Q ( { } ) ;
92129 } ,
93130 modules : function ( device ) {
94131 var sResult = '' ;
@@ -142,6 +179,13 @@ var netatmo = {
142179 } ,
143180 data : function ( clazz , dataType , value ) {
144181 return netatmo . html . data . format ( dataType , value . toFixed ( 1 ) ) ;
182+ } ,
183+ error : function ( reason ) {
184+ console . log ( "error " + reason ) ;
185+ $ ( netatmo . location ) . updateWithText (
186+ "could not load data: " + reason . responseJSON . error ,
187+ netatmo . fadeInterval
188+ ) ;
145189 }
146190 }
147191} ;
0 commit comments