9a77bc6e1c8d3100b53c6cc17b8a02e2e9d5dea1
[khatus.git] / x3 / src / lib / khatus_state.ml
1 module Cache = Khatus_cache
2 module Msg = Khatus_msg
3 module Time = Khatus_time
4
5 type t =
6 { node : string
7 ; modul : string
8 ; time : Time.t
9 ; cache : Cache.t
10 }
11
12 let init ~node ~modul =
13 { node
14 ; modul
15 ; time = Time.init
16 ; cache = Cache.create ()
17 }
18
19 (* TODO: Should probably wrap state update in result. *)
20 let update ({node; modul = _; time; cache} as t) ~msg =
21 Msg.handle_data msg ~otherwise:t ~f:(fun ~node:src_node ~modul ~key ~value ->
22 let time =
23 match (modul, key) with
24 | ("khatus_sensor_datetime", ["epoch"]) when src_node = node ->
25 Time.of_string value (* Raises if value is not a number *)
26 | (_, _) ->
27 time
28 in
29 Cache.update cache ~node:src_node ~modul ~key ~value ~time;
30 {t with time}
31 )
This page took 0.05842 seconds and 4 git commands to generate.