comparison weather.tcl @ 535:edbc0190e82a

weather: Move some functions around into more logical location.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 07 Jul 2020 14:41:46 +0300
parents 3da1d036ae48
children 1044fbacb8a7
comparison
equal deleted inserted replaced
534:7512889f7b72 535:edbc0190e82a
124 } 124 }
125 return $uname 125 return $uname
126 } 126 }
127 127
128 128
129 #-------------------------------------------------------------------------
130 proc weather_update {} {
131 global weather_datafile weather_data
132
133 # Check if we can open the weather data file
134 if {![catch {set ufile [open $weather_datafile r 0600]} uerrmsg]} {
135 # Create dict
136 array unset weather_data
137 array set weather_data {}
138
139 set wtemp_min_val 500000
140 set wtemp_max_val -500000
141 set wtemp_min_key ""
142 set wtemp_max_key ""
143
144 # Read in the data
145 while {![eof $ufile]} {
146 gets $ufile uline
147 set udata [split $uline "|"]
148 if {[llength $udata] > 0} {
149 set utemp [lindex $udata 6]
150 set ukey [lindex $udata 0]
151
152 set weather_data($ukey) $udata
153
154 if {[string is double -strict $utemp]} {
155 if {$utemp < $wtemp_min_val} {
156 set wtemp_min_key $ukey
157 set wtemp_min_val $utemp
158 }
159 if {$utemp > $wtemp_max_val} {
160 set wtemp_max_key $ukey
161 set wtemp_max_val $utemp
162 }
163 }
164 }
165 }
166 close $ufile
167
168 # Store min/max
169 if {$wtemp_min_key != "" && $wtemp_max_key != ""} {
170 set weather_data(w_min) $weather_data($wtemp_min_key)
171 set weather_data(w_max) $weather_data($wtemp_max_key)
172 } else {
173 set weather_data(w_min) 0
174 set weather_data(w_max) 0
175 }
176 } else {
177 weather_log "Could not open data file: $uerrmsg"
178 }
179 }
180
181
182 #-------------------------------------------------------------------------
183 # Weather data update loop
184 proc weather_exec {} {
185 global weather_check_period weather_running
186
187 # Perform update
188 weather_update
189
190 # Schedule next update
191 set weather_running [clock seconds]
192 timer $weather_check_period weather_exec
193 }
194
195
196 #-------------------------------------------------------------------------
129 # Translate wind direction compass degree to name 197 # Translate wind direction compass degree to name
130 proc weather_get_wind_direction {uangle} { 198 proc weather_get_wind_direction {uangle} {
131 global weather_msg_wind_directions 199 global weather_msg_wind_directions
132 200
133 # If the data was not got, return empty value 201 # If the data was not got, return empty value
170 return "ERROR ($uvalue)" 238 return "ERROR ($uvalue)"
171 } 239 }
172 } 240 }
173 241
174 242
175 #-------------------------------------------------------------------------
176 # Produce one location of weather data as a string 243 # Produce one location of weather data as a string
177 proc weather_get_str {udata umsg} { 244 proc weather_get_str {udata umsg} {
178 global weather_msg_cloudiness 245 global weather_msg_cloudiness
246 global weather_msg_precipitation
179 247
180 array unset uvals 248 array unset uvals
181 set uvals(station) [lindex $udata 0] 249 set uvals(station) [lindex $udata 0]
182 set uvals(type) [lindex $udata 1] 250 set uvals(type) [lindex $udata 1]
183 set uvals(c_lat) [lindex $udata 2] 251 set uvals(c_lat) [lindex $udata 2]
192 set uvals(cloudiness) [weather_get_table_value $weather_msg_cloudiness [lindex $udata 10]] 260 set uvals(cloudiness) [weather_get_table_value $weather_msg_cloudiness [lindex $udata 10]]
193 set uvals(cloudiness_val) [weather_get_raw_table_value $weather_msg_cloudiness [lindex $udata 10]] 261 set uvals(cloudiness_val) [weather_get_raw_table_value $weather_msg_cloudiness [lindex $udata 10]]
194 set uvals(road_surface_temp) [lindex $udata 11] 262 set uvals(road_surface_temp) [lindex $udata 11]
195 set uvals(precipitation) [lindex $udata 12] 263 set uvals(precipitation) [lindex $udata 12]
196 set uvals(visibility) [lindex $udata 13] 264 set uvals(visibility) [lindex $udata 13]
265 set uvals(precipitation2) [weather_get_table_value $weather_msg_precipitation [lindex $udata 14]]
266 set uvals(precipitation_val) [weather_get_raw_table_value $weather_msg_precipitation [lindex $udata 14]]
197 267
198 if {[expr [clock seconds] - $uvals(vtime)] < 3600} { 268 if {[expr [clock seconds] - $uvals(vtime)] < 3600} {
199 set uvals(ctime) [clock format $uvals(vtime) -format "%H:%M"] 269 set uvals(ctime) [clock format $uvals(vtime) -format "%H:%M"]
200 } else { 270 } else {
201 set uvals(ctime) [clock format $uvals(vtime) -format "%H:%M (%d.%m.%Y)"] 271 set uvals(ctime) [clock format $uvals(vtime) -format "%H:%M (%d.%m.%Y)"]
220 290
221 # Get data by location key 291 # Get data by location key
222 proc weather_get_by_key {ukey} { 292 proc weather_get_by_key {ukey} {
223 global weather_data weather_msg_result 293 global weather_data weather_msg_result
224 return [weather_get_str $weather_data($ukey) $weather_msg_result] 294 return [weather_get_str $weather_data($ukey) $weather_msg_result]
225 }
226
227
228 #-------------------------------------------------------------------------
229 proc weather_update {} {
230 global weather_datafile weather_data
231
232 # Check if we can open the weather data file
233 if {![catch {set ufile [open $weather_datafile r 0600]} uerrmsg]} {
234 # Create dict
235 array unset weather_data
236 array set weather_data {}
237
238 set wtemp_min_val 500000
239 set wtemp_max_val -500000
240 set wtemp_min_key ""
241 set wtemp_max_key ""
242
243 # Read in the data
244 while {![eof $ufile]} {
245 gets $ufile uline
246 set udata [split $uline "|"]
247 if {[llength $udata] > 0} {
248 set utemp [lindex $udata 6]
249 set ukey [lindex $udata 0]
250
251 set weather_data($ukey) $udata
252
253 if {[string is double -strict $utemp]} {
254 if {$utemp < $wtemp_min_val} {
255 set wtemp_min_key $ukey
256 set wtemp_min_val $utemp
257 }
258 if {$utemp > $wtemp_max_val} {
259 set wtemp_max_key $ukey
260 set wtemp_max_val $utemp
261 }
262 }
263 }
264 }
265 close $ufile
266
267 # Store min/max
268 if {$wtemp_min_key != "" && $wtemp_max_key != ""} {
269 set weather_data(w_min) $weather_data($wtemp_min_key)
270 set weather_data(w_max) $weather_data($wtemp_max_key)
271 } else {
272 set weather_data(w_min) 0
273 set weather_data(w_max) 0
274 }
275 } else {
276 weather_log "Could not open data file: $uerrmsg"
277 }
278 }
279
280
281 #-------------------------------------------------------------------------
282 # Weather data update loop
283 proc weather_exec {} {
284 global weather_check_period weather_running
285
286 # Perform update
287 weather_update
288
289 # Schedule next update
290 set weather_running [clock seconds]
291 timer $weather_check_period weather_exec
292 } 295 }
293 296
294 297
295 #------------------------------------------------------------------------- 298 #-------------------------------------------------------------------------
296 # Script initialization 299 # Script initialization