12
« on: February 23, 2008, 03:41:56 pm »
Ok, Here's the code. Sorry to post it inline, but uploading attachments is still broke and the original I posted is no longer available. Have a look and let me know what you all think.
Thanks
Jason
NOAA Weather ruby code - current
0:require 'Ruby_Generic_Serial_Device'
1:class Command < Ruby_Generic_Serial_Device::RubyCommandWrapper
2:end
3:class Device_35 < Ruby_Generic_Serial_Device::RubySerialWrapper
4:#### 351 ####################################################################
5:def cmd_351(cmd=nil)
6:@returnParamArray.clear
7:### Auto check weather every quarter past the hour per NOAA
8:t = Time.now
9:
10:if t.min == 15
11: t -= (60 * 60)
12:
13: if $obs_time =~/(\d\d):\d\d:\d\d/
14: obs_hour = $1
15: end
16:
17: if t.hour != obs_hour.to_i
18: log("Quarter past the hour and weather update needed")
19: check_weather()
20: end
21:end
22:return @returnParamArray
23:end
24:#### 355 ####################################################################
25:def cmd_355(cmd=nil)
26:@returnParamArray.clear
27:###
28:# NOAA Weather check
29:# 2008 Jason Speckman
30:###
31:require 'net/http'
32:
33:# Init Vars
34:
35:$logFile = File.new("/var/log/pluto/NOAA_Weather.log", "w")
36:
37:$DEVICEDATA_ICAO_CONST = 249
38:$DEVICEDATA_UNITS_CONST = 250
39:
40:$units = ""
41:$identifier = ""
42:$location = ""
43:$obs_time = ""
44:$weather = ""
45:$temp = ""
46:$pressure = ""
47:$dew_point = ""
48:$heat_index = ""
49:$windchill = ""
50:$humidity = ""
51:$wind = ""
52:$visibility = ""
53:
54:### Check for identifier and units
55:if device_.devdata_ != nil
56: if device_.devdata_[$DEVICEDATA_ICAO_CONST] != nil
57: $identifier = device_.devdata_[$DEVICEDATA_ICAO_CONST]
58: log("ICAO identifier: "+$identifier)
59: else
60: log("No ICAO identifier specified")
61: end
62: if device_.devdata_[$DEVICEDATA_UNITS_CONST] != nil
63: $units = device_.devdata_[$DEVICEDATA_UNITS_CONST]
64: else
65: $units = "F"
66: end
67:end
68:
69:log("Units in degrees "+$units)
70:
71:if $units == "F"
72: $press_units = "in. Hg"
73:else
74: $press_units = "hPa"
75:end
76:
77:
78:### Tell DCE we are a climate device
79:log("Starting NOAA Weather Check")
80:log("Setup message intercept for DCERouter")
81:
82:###
83:# devIDfrom: device_.devid_ = my device id
84:# devIDto: -1000 = dcerouter
85:# priority:
86:# type: 1 = command, 2 = event
87:# id:
88:
89:$devIDfrom = device_.devid_
90:devIDto = -1000
91:priority = ""
92:type = 2
93:id = ""
94:
95:#cmd = Command.new(devIDfrom, devIDto, priority, type, id)
96:#cmd.params_[x] = y
97:#SendCommand(cmd)
98:
99:#ms = "/usr/pluto/bin/MessageSend dcerouter " + device_.devid_.to_s + " -1000 8 0 5 2 4 10"
100:#system(ms)
101:
102:log("Init complete")
103:
104:### Initial weather check
105:check_weather()
106:return @returnParamArray
107:end
108:#### 356 ####################################################################
109:def cmd_356(cmd=nil)
110:@returnParamArray.clear
111:if ($logFile != nil) then
112: $logFile.close
113:end
114:return @returnParamArray
115:end
116:#### PRIVATE METHODS ####################################################################
117:### Logging function
118:def log(word)
119: $logFile.print( word + "\n" )
120: $logFile.flush()
121:end
122:
123:### Weather check function
124:def check_weather()
125: log("Backing up previous data")
126: oldweather = $weather
127: oldtemp = $temp
128: oldpressure = $pressure
129: olddew_point = $dew_point
130: oldheat_index = $heat_index
131: oldwindchill = $windchill
132: oldhumidity = $humidity
133: oldwind = $wind
134: oldvisibility = $visibility
135:
136: log("Checking for weather updates")
137:
138:### Get xml file from NOAA
139:# xmlPath = "www.weather.gov/data/current_obs/"+$identifier+".xml"
140:# h = "GET "+xmlPath+" HTTP/1.0\r\n"
141:# log("HTTP Request: #{h}")
142:# conn_.Send(h)
143:# resv = ""
144:# while(true)
145:# buff = conn_.Recv(16384, 5000)
146:# if buff.length() == 0
147:# break
148:# end
149:# recv = recv + buff
150:# end
151: h = Net::HTTP.new('www.weather.gov', 80)
152: data_loc = "/data/current_obs/"+$identifier+".xml"
153: resp, data = h.get(data_loc, nil)
154:
155: if resp.message == "OK"
156:
157:# if recv =~ /^HTTP[^\r\n]+200\sOK.+?\r\n\r\n(.+)$/m
158:# data = $1
159:# log("Recieved:\n#{data}")
160:
161: data.each_line {|line|
162: if line =~/location>(.*)<\/location>/
163: $location = $1
164: end
165: if line =~/observation_time_rfc822>(.*)<\/observation_time_rfc822>/
166: $obs_time = $1
167: end
168: if line =~/weather>(.*)<\/weather>/
169: $weather = $1
170: end
171: if $units == "F"
172: if line =~/temp_f>(.*)<\/temp_f>/
173: $temp = $1
174: end
175: if line =~/pressure_in>(.*)<\/pressure_in>/
176: $pressure = $1
177: end
178: if line =~/dewpoint_f>(.*)<\/dewpoint_f>/
179: $dew_point = $1
180: end
181: if line =~/heat_index_f>(.*)<\/heat_index_f>/
182: $heat_index = $1
183: end
184: if line =~/windchill_f>(.*)<\/windchill_f>/
185: $windchill = $1
186: end
187: else
188: if line =~/temp_c>(.*)<\/temp_c>/
189: $temp = $1
190: end
191: if line =~/pressure_mb>(.*)<\/pressure_mb>/
192: $pressure = $1
193: end
194: if line =~/dewpoint_c>(.*)<\/dewpoint_c>/
195: $dew_point = $1
196: end
197: if line =~/heat_index_c>(.*)<\/heat_index_c>/
198: $heat_index = $1
199: end
200: if line =~/windchill_c>(.*)<\/windchill_c>/
201: $windchill = $1
202: end
203: end
204: if line =~/relative_humidity>(.*)<\/relative_humidity>/
205: $humidity = $1
206: end
207: if line =~/wind_string>(.*)<\/wind_string>/
208: $wind = $1
209: end
210: if line =~/visibility_mi>(.*)<\/visibility_mi>/
211: $visibility = $1
212: end
213: }
214: end
215:
216:### Send out events for weather changes
217: if $weather != oldweather
218: ms = "/usr/pluto/bin/MessageSend dcerouter "+device_.devid_.to_s+" -1001 2 75 13 \""+$weather+"\""
219: system(ms)
220: log("Message sent: #{ms}")
221: end
222: if $temp != oldtemp
223: ms = "/usr/pluto/bin/MessageSend dcerouter "+device_.devid_.to_s+" -1001 2 25 13 \""+$temp+"\""
224: system(ms)
225: log("Message sent: #{ms}")
226: end
227:
228: check_weather_done()
229:end
230:
231:### Post weather check
232:def check_weather_done()
233: log("Weather data from: "+$obs_time)
234: log("Weather check complete")
235:end
236:#### 395 ####################################################################
237:def cmd_395(cmd=nil)
238:@returnParamArray.clear
239:check_weather()
240:return @returnParamArray
241:end
242:#### 396 ####################################################################
243:def cmd_396(failed, cmd=nil)
244:@returnParamArray.clear
245:check_weather_done()
246:return @returnParamArray
247:end
248:#### START SETTERS ####################################################################
249:def initialize()
250:super
251:@returnParamArray=Array.new
252:end
253:#### END SETTERS ####################################################################
254:end