| 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 | ) |