Live
(require (planet jeeve/live)) |
1 Values
Extracts a value in a web page.
Returns error-value if value not found or internet connection failed.
Example to get wind velocity on a spot location (via winds-up web page)
Open the web site
Inspect HTML code
Which gives
> (live-web-number "http://www.winds-up.com/index.php?p=spots&id=21" "Aujourd" "[0-9]+" -1 3)
Returns wind velocity read as an integer on winds-up web site at 3 lines after that containing "Aujourd" word. Otherwise returns -1.
Another examples:
Wind velocity at Belle-Ile, France via meteociel web site
> (live-web-number "http://www.meteociel.fr/observations-meteo/vent.php" "Belle-Ile" "[0-9]+ km/h")
Water level web service
(define (water-level id-station) (lambda () (live-web-number (string-append "http://www.vigicrues.gouv.fr/niveau3.php?idstation=" (number->string id-station) "&idspc=21&typegraphe=h&AffProfondeur=24" "&AffRef=auto&AffPrevi=non&nbrstations=3&ong=2") "<tr><th>Date</th><th>" "[0-9]+\\.[0-9]+")))
2 Graphics
procedure
label : string? function : (-> number?) rate : number? title : string?
Graphic evolution of value in web page.
An example to display water level at Cavaillon in France (id-station = 142)
> (live-graph "Water level (m)" (water-level 142) (* 5 60) ; refresh each 5 minutes "Cavaillon")
An other example to obtain pressure level in Paris
(define (pressure-paris) (live-web-number "http://www.station-meteo.com/historique-fr75020-paris/" "<td>Pression atmosphérique" "[0-9]+\\.[0-9]+" -1 7))
With axis scale parameterize
(parameterize ([delta-x (* 12 3600)] ; 12 hours [delta-y 0.01]) ; 1 % (live-graph "Level pressure (hPa)" pressure-paris (* 5 60) "Paris"))
Use live-dashboard function to plot multiple web services
procedure
labels : (listof (listof string?)) functions : (listof (listof (-> number?))) rate : number? title : string?
This function displays graphics in rows and columns disposition. Each sublist is a row.
> (live-dashboard '(("Ners" "Besseges")) (list (list (water-level 38) (water-level 31))) (* 5 60) "Water level (m)")
The same but with one function of label parameter
procedure
(live-dashboard-one-ft labels function rate title) → any/c labels : (listof (listof string?)) function : (-> string? number?) rate : number? title : string?
Example:
> (live-dashboard-one-ft '(("Paris" "Marseille")) pressure (* 15 60) "Pressure level (hPa)")
(pressure "Paris") and (pressure "Marseille") will be called every 15 minutes.
3 Database
Data displayed in graphs can be saved in real time in a database. For this you can use new-value-event parameter with insert-into-db procedure.
First, describe your dabatase schema in a db-struct structure
struct
(struct db-struct ( connection table-name field-label field-date-time field-value)) connection : connection? table-name : string? field-label : string? field-date-time : string? field-value : string?
Then, use this function in new-value-event handler
procedure
db : db-struct? label : string? seconds : exact-integer? value : string?
Example with a table named T_Data containing 3 fields F_Label, F_DateTime and F_Value:
(define my-db (db-struct (odbc-connect #:dsn "BdLive" #:user "" #:password "") "T_Data" "F_Label" "F_DateTime" "F_Value")) (parameterize ([new-value-event (lambda (label seconds value) (insert-into-db my-db label seconds value))]) (live-graph "test" (lambda () 10) 2 "demo"))