Skip to content

Commit

Permalink
AQ handle non-int aqi
Browse files Browse the repository at this point in the history
  • Loading branch information
pNre committed Dec 29, 2023
1 parent 46fdc07 commit 4a28f13
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions hera/module_air_quality/module_air_quality.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type iaqi =
type data =
{ city : city
; dominentpol : string
; aqi : int
; aqi : Jsonaf.t
; iaqi : iaqi
; time : time
}
Expand All @@ -42,13 +42,22 @@ let uri path =
Uri.make ~scheme:"https" ~host:"api.waqi.info" ~path ~query:[ "token", [ token ] ] ()
;;

let description_of_aqi = function
| `Number n ->
(match Int.of_string_opt n with
| Some x when x <= 50 -> "Good"
| Some x when x <= 100 -> "Moderate"
| Some x when x <= 150 -> "Unhealthy-ish"
| Some x when x <= 200 -> "*Unhealthy*"
| Some x when x <= 300 -> "*Very Unhealthy*"
| Some _ -> "*Hazardous, RUN*"
| None -> "-")
| _ -> "-"
;;

let string_of_aqi = function
| x when x <= 50 -> "Good"
| x when x <= 100 -> "Moderate"
| x when x <= 150 -> "Unhealthy-ish"
| x when x <= 200 -> "*Unhealthy*"
| x when x <= 300 -> "*Very Unhealthy*"
| _ -> "*Hazardous, RUN*"
| `Number v | `String v -> v
| _ -> "?"
;;

let description_of_concern = function
Expand Down Expand Up @@ -77,14 +86,14 @@ let handle_success chat_id body =
let text =
sprintf
"Air quality in %s\n\
AQI: *%d*, *%s*\n\
AQI: *%s*, *%s*\n\
Main pollutant: *%s*\n\
PM10: *%s*\n\
PM2.5: *%s*\n\n\
_Updated on %s_"
data.city.name
aqi
(string_of_aqi aqi)
(description_of_aqi aqi)
(description_of_concern main)
pm10
pm25
Expand All @@ -104,7 +113,7 @@ let get_air_quality ~chat_id ~city =

(* Bot module *)
let register () = ()
let help () = "*Air quality*\n`aq [city] [state] [country]`"
let help () = "*Air quality*\n`aq [city]`"

let on_update update =
match Telegram.parse_update update with
Expand Down

0 comments on commit 4a28f13

Please sign in to comment.