X-Git-Url: https://git.xandkar.net/?p=khatus.git;a=blobdiff_plain;f=x3%2Fsrc%2Flib%2Fkhatus_state.ml;fp=x3%2Fsrc%2Flib%2Fkhatus_state.ml;h=9a77bc6e1c8d3100b53c6cc17b8a02e2e9d5dea1;hp=0000000000000000000000000000000000000000;hb=499c58a269a00e031302938b5a8f006f23aae451;hpb=4c703fadbdc17d1753d16841582636598f862416 diff --git a/x3/src/lib/khatus_state.ml b/x3/src/lib/khatus_state.ml new file mode 100644 index 0000000..9a77bc6 --- /dev/null +++ b/x3/src/lib/khatus_state.ml @@ -0,0 +1,31 @@ +module Cache = Khatus_cache +module Msg = Khatus_msg +module Time = Khatus_time + +type t = + { node : string + ; modul : string + ; time : Time.t + ; cache : Cache.t + } + +let init ~node ~modul = + { node + ; modul + ; time = Time.init + ; cache = Cache.create () + } + +(* TODO: Should probably wrap state update in result. *) +let update ({node; modul = _; time; cache} as t) ~msg = + Msg.handle_data msg ~otherwise:t ~f:(fun ~node:src_node ~modul ~key ~value -> + let time = + match (modul, key) with + | ("khatus_sensor_datetime", ["epoch"]) when src_node = node -> + Time.of_string value (* Raises if value is not a number *) + | (_, _) -> + time + in + Cache.update cache ~node:src_node ~modul ~key ~value ~time; + {t with time} + )