fix aqi values not being written, handle ephem/weather with sub -180 degree from the foci side

f8b0060 · gmorell · 2022-09-17 08:36

13 files +35 -2

Files changed

modified apps/room_air_quality/lib/room_air_quality/aqi_worker.ex
+14 −2
@@ -50,6 +50,11 @@ defmodule RoomAirQuality.Worker do
50 50 Phoenix.PubSub.broadcast(RoomSanctum.PubSub, "aqi", {:aqi, id, file, complete, total})
51 51 end
52 52
53 + defp intify(val) when val == "", do: nil
54 + defp intify(val) when is_binary(val), do: val |> String.to_integer
55 + defp intify(val) when is_float(val), do: val |> Kernel.trunc
56 + defp intify(val), do: val
57 +
53 58 defp write_data(result, type, id) do
54 59 datetime = NaiveDateTime.local_now()
55 60 Logger.info("AQI::#{id} writing bundle")
@@ -231,6 +236,12 @@ defmodule RoomAirQuality.Worker do
231 236 )
232 237 |> Map.put(:inserted_at, datetime)
233 238 |> Map.put(:updated_at, datetime)
239 +
240 + |> Map.put(:ozone_aqi, data.ozone_aqi |> intify)
241 + |> Map.put(:pm10_aqi, data.pm10_aqi |> intify)
242 + |> Map.put(:pm25_aqi, data.pm25_aqi |> intify)
243 + |> Map.put(:no2_aqi, data.no2_aqi |> intify)
244 +
234 245 end)
235 246 |> Enum.map(fn x ->
236 247 Repo.insert(
@@ -240,6 +251,7 @@ defmodule RoomAirQuality.Worker do
240 251 )
241 252 end)
242 253 end
254 + Logger.info("AQI::#{id} completed writing bundle")
243 255 end
244 256
245 257 # defp build_todays_url(ts \\ DateTime.utc_now()) do
@@ -257,7 +269,7 @@ defmodule RoomAirQuality.Worker do
257 269 file_str = sh |> Timex.format!("%Y%m%d%H", :strftime)
258 270 date_str = sh |> Timex.format!("%Y%m%d", :strftime)
259 271
260 − "https://s3-us-west-1.amazonaws.com//files.airnowtech.org/airnow/#{sh.year}/#{date_str}/HourlyAQObs_#{file_str}.dat"
272 + "https://s3-us-west-1.amazonaws.com//files.airnowtech.org/airnow/#{sh.year}/#{date_str}/HourlyAQObs_#{file_str}.dat" |> IO.inspect
261 273 end
262 274
263 275 def handle_cast(:refresh_db_cfg, state) do
@@ -272,7 +284,7 @@ defmodule RoomAirQuality.Worker do
272 284 # case HTTPoison.get(build_todays_url()) do
273 285 # {:ok, result} ->
274 286 # write_data(result, :hourly, state.id)
275 − # {:noreply, state}
287 + # {:noreply, state}rec
276 288 #
277 289 # {:error, info} ->
278 290 # Logger.info(info.reason)
modified apps/room_ephem/lib/room_ephem/ephem_worker.ex
+11 −0
@@ -9,10 +9,21 @@ defmodule RoomEphem.Worker do
9 9
10 10 @ttl :timer.hours(8)
11 11
12 + defp normalize_ll(val) do
13 + cond do
14 + val < -180 -> val+360
15 + true -> val
16 + end
17 + end
18 +
12 19 @decorate cacheable(cache: RoomZeus.Cache, opts: [ttl: @ttl])
13 20 def query_ephem(_name, query) do
14 21 foci = Configuration.get_foci!(query.foci_id)
15 22 {lat, lon} = foci.place.coordinates
23 + lat = normalize_ll(lat)
24 + lon = normalize_ll(lon)
25 +
26 +
16 27 tz = WhereTZ.lookup(lat, lon)
17 28 {:ok, sunrise} = Solarex.Sun.rise(Date.utc_today(), lat, lon)
18 29 {:ok, sunset} = Solarex.Sun.set(Date.utc_today(), lat, lon)
modified apps/room_weather/lib/room_weather/weather_worker.ex
+10 −0
@@ -26,10 +26,20 @@ defmodule RoomWeather.Worker do
26 26 {:ok, %{id: opts[:name], src: c}}
27 27 end
28 28
29 + defp normalize_ll(val) do
30 + cond do
31 + val < -180 -> val+360
32 + true -> val
33 + end
34 + end
35 +
29 36 def handle_call({:query_weather, query}, _from, state) do
30 37 foci = Configuration.get_foci!(query.foci_id)
31 38 {lat, lon} = foci.place.coordinates
32 39
40 + lat = normalize_ll(lat)
41 + lon = normalize_ll(lon)
42 +
33 43 case HTTPoison.get(@root_url, [],
34 44 follow_redirect: true,
35 45 params: %{

Parents: 0c49bca