Improve overview and experiment naming
[khatus.git] / x3 / src / lib / khatus_state.ml
diff --git a/x3/src/lib/khatus_state.ml b/x3/src/lib/khatus_state.ml
new file mode 100644 (file)
index 0000000..9a77bc6
--- /dev/null
@@ -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}
+  )
This page took 0.024823 seconds and 4 git commands to generate.