urbit/pkg/arvo/app/weather.hoon

160 lines
3.9 KiB
Plaintext
Raw Normal View History

:: weather [landscape]:
::
:: holds latlong, gets weather data from API, passes it on to subscribers
::
/+ *server, default-agent, verb, dbug
2019-11-22 00:07:52 +03:00
=, format
::
|%
::
+$ card card:agent:gall
+$ versioned-state
$% state-zero
==
+$ state-zero [%0 data=json time=@da location=@t timer=(unit @da)]
--
=| state-zero
=* state -
2020-04-01 23:25:54 +03:00
%+ verb |
2020-03-12 03:33:48 +03:00
%- agent:dbug
2019-11-22 00:07:52 +03:00
^- agent:gall
=<
2019-11-26 23:52:22 +03:00
|_ bol=bowl:gall
2019-11-22 00:07:52 +03:00
+* this .
weather-core +>
2019-11-26 23:52:22 +03:00
wc ~(. weather-core bol)
def ~(. (default-agent this %|) bol)
++ on-init [~ this]
2019-11-22 00:07:52 +03:00
++ on-save !>(state)
++ on-load
|= old=vase
:_ this(state !<(state-zero old))
[%pass /bind/weather %arvo %e %disconnect [~ /'~weather']]~
2019-11-22 00:07:52 +03:00
::
++ on-poke
|= [=mark =vase]
^- (quip card _this)
=^ cards state
?+ mark (on-poke:def mark vase)
%json (poke-json:wc !<(json vase))
2019-11-22 00:07:52 +03:00
==
[cards this]
::
++ on-watch
|= =wire
^- (quip card _this)
?. ?=([%all ~] wire) (on-watch:def wire)
=/ jon
%- pairs:enjs:format
:* ['location' s+location]
::
?. ?=([%o *] data) ~
~(tap by p.data)
==
:_ this
[%give %fact ~ %json !>(jon)]~
2019-11-22 00:07:52 +03:00
::
++ on-arvo
|= [=wire =sign-arvo]
^- (quip card:agent:gall _this)
?: ?=(%bound +<.sign-arvo)
[~ this]
?: ?=(%wake +<.sign-arvo)
=^ cards state
(wake:wc wire error.sign-arvo)
[cards this]
?: ?=(%http-response +<.sign-arvo)
=^ cards state
(http-response:wc wire client-response.sign-arvo)
[cards this]
(on-arvo:def wire sign-arvo)
::
++ on-leave on-leave:def
++ on-peek on-peek:def
++ on-agent on-agent:def
++ on-fail on-fail:def
--
::
|_ bol=bowl:gall
::
++ poke-json
|= jon=json
^- (quip card _state)
?. ?=(%s -.jon)
[~ state]
=/ str=@t +.jon
=/ req=request:http (request-wttr str)
2019-11-22 00:07:52 +03:00
=/ out *outbound-config:iris
=/ lismov=(list card)
[%pass /[(scot %da now.bol)] %arvo %i %request req out]~
2019-11-22 00:07:52 +03:00
?~ timer
:- %+ weld lismov
^- (list card)
:~ [%pass /timer %arvo %b %wait (add now.bol ~h3)]
[%give %fact ~[/all] %json !>((frond:enjs:format %location jon))]
==
2020-01-04 00:06:42 +03:00
%= state
2019-11-22 00:07:52 +03:00
location str
timer `(add now.bol ~h3)
==
:_ state(location str)
%+ weld lismov
^- (list card)
[%give %fact ~[/all] %json !>((frond:enjs:format %location jon))]~
2019-11-22 00:07:52 +03:00
::
++ request-wttr
2019-11-22 00:07:52 +03:00
|= location=@t
^- request:http
=/ base 'https://wttr.in/'
=/ url=@t (cat 3 (cat 3 base location) '?format=j1')
2019-11-22 00:07:52 +03:00
=/ hed [['Accept' 'application/json']]~
[%'GET' url hed *(unit octs)]
::
++ http-response
|= [=wire response=client-response:iris]
^- (quip card _state)
:: ignore all but %finished
?. ?=(%finished -.response)
[~ state]
=/ data=(unit mime-data:iris) full-file.response
?~ data
:: data is null
[~ state]
=/ ujon=(unit json) (de-json:html q.data.u.data)
?~ ujon
[~ state]
?> ?=(%o -.u.ujon)
?: (gth 200 status-code.response-header.response)
[~ state]
2020-06-12 03:15:32 +03:00
=/ error (~(get by p.u.ujon) 'error')
?^ error
~& "fetching weather failed: {<u.error>}"
[~ state]
=/ jon=json
%+ frond:enjs:format %weather
2020-06-12 03:15:32 +03:00
%- pairs:enjs:format
:~ [%current-condition (~(got by p.u.ujon) 'current_condition')]
[%weather (~(got by p.u.ujon) 'weather')]
[%nearest-area (~(got by p.u.ujon) 'nearest_area')]
==
:- [%give %fact ~[/all] %json !>(jon)]~
2019-11-22 00:07:52 +03:00
%= state
data jon
time now.bol
==
::
++ wake
|= [wir=wire err=(unit tang)]
^- (quip card _state)
?~ err
=/ req=request:http (request-wttr location)
2019-11-22 00:07:52 +03:00
=/ out *outbound-config:iris
:_ state(timer `(add now.bol ~h3))
:~ [%pass /[(scot %da now.bol)] %arvo %i %request req out]
[%pass /timer %arvo %b %wait (add now.bol ~h3)]
==
%- (slog u.err)
[~ state]
::
--