From 027e4b3a7f39fc84b01f7b44c42f88f889d89eb6 Mon Sep 17 00:00:00 2001
From: clerie <git@clerie.de>
Date: Sun, 19 Jun 2022 14:16:45 +0200
Subject: [PATCH] Add infrastructure for managing configuration

---
 fieldpoc/config.py       | 22 ++++++++++++++++++++++
 fieldpoc/dect.py         | 23 +++++++++++++++++++----
 fieldpoc/fieldpoc.py     | 30 ++++++++++++++++++++++++++++++
 fieldpoc/run.py          | 11 ++++++++---
 fieldpoc_config.json     |  7 +++++++
 fieldpoc_extensions.json |  1 +
 6 files changed, 87 insertions(+), 7 deletions(-)
 create mode 100644 fieldpoc/config.py
 create mode 100644 fieldpoc/fieldpoc.py
 create mode 100644 fieldpoc_config.json
 create mode 100644 fieldpoc_extensions.json

diff --git a/fieldpoc/config.py b/fieldpoc/config.py
new file mode 100644
index 0000000..6234722
--- /dev/null
+++ b/fieldpoc/config.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+
+class DectConfig:
+    def __init__(self, c):
+        self._c = c
+
+    def __getattr__(self, name):
+            if name in self._c.keys():
+                return self._c.get(name)
+            else:
+                raise AttributeError()
+
+    def check(self):
+        return True
+
+class Config:
+    def __init__(self, c):
+        self._c = c
+        self.dect = DectConfig(c.get("dect", {}))
+
+    def check(self):
+        return self.dect.check()
diff --git a/fieldpoc/dect.py b/fieldpoc/dect.py
index d79c216..2bec9a6 100644
--- a/fieldpoc/dect.py
+++ b/fieldpoc/dect.py
@@ -3,7 +3,22 @@
 import mitel_ommclient2
 import time
 
-def run():
-    while True:
-        print("meow")
-        time.sleep(2)
+class Dect:
+    def __init__(self, fp):
+        self.fp = fp
+
+    def _init_client(self):
+        self.c = mitel_ommclient2.OMMClient2(
+            host=self.fp.config.dect.host,
+            username=self.fp.config.dect.username,
+            password=self.fp.config.dect.password,
+            ommsync=True,
+        )
+
+    def run(self):
+        self._init_client()
+
+        while True:
+            print("meow")
+            print(self.c.ping())
+            time.sleep(2)
diff --git a/fieldpoc/fieldpoc.py b/fieldpoc/fieldpoc.py
new file mode 100644
index 0000000..d034ad7
--- /dev/null
+++ b/fieldpoc/fieldpoc.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+import json
+import pathlib
+import threading
+
+from . import config
+from . import dect
+
+class FieldPOC:
+    config = None
+    extensions = None
+
+    def __init__(self, config_file_path, extensions_file_path):
+        self.config_file_path = pathlib.Path(config_file_path)
+        self._load_config()
+        self.extensions_file_path = pathlib.Path(extensions_file_path)
+        self._load_extensions()
+
+        self._dect = dect.Dect(self)
+
+    def run(self):
+        self._dect_thread = threading.Thread(target=self._dect.run)
+        self._dect_thread.start()
+
+    def _load_config(self):
+        self.config = config.Config(json.loads(self.config_file_path.read_text()))
+
+    def _load_extensions(self):
+        self.extensions = json.loads(self.extensions_file_path.read_text())
diff --git a/fieldpoc/run.py b/fieldpoc/run.py
index 9732ce8..f021ac1 100644
--- a/fieldpoc/run.py
+++ b/fieldpoc/run.py
@@ -1,11 +1,16 @@
 #!/usr/bin/env python3
 
-import threading
+import argparse
+from . import fieldpoc
 
-from . import dect
+ap = argparse.ArgumentParser(prog="fieldpoc")
+ap.add_argument("-c", "--config", dest="config_file_path", default="fieldpoc_config.json", help="Path to the fieldpoc config file")
+ap.add_argument("-e", "--extensions", dest="extensions_file_path", default="fieldpoc_extensions.json", help="Path to the fieldpoc extensions file")
 
 def run():
-    threading.Thread(target=dect.run).start()
+    args = ap.parse_args()
+    fp = fieldpoc.FieldPOC(**args.__dict__)
+    fp.run()
 
 if __name__ == "__main__":
     run()
diff --git a/fieldpoc_config.json b/fieldpoc_config.json
new file mode 100644
index 0000000..e8f4ce3
--- /dev/null
+++ b/fieldpoc_config.json
@@ -0,0 +1,7 @@
+{
+  "dect": {
+    "host": "192.168.0.100",
+    "username": "omm",
+    "password": "xxx"
+  }
+}
diff --git a/fieldpoc_extensions.json b/fieldpoc_extensions.json
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/fieldpoc_extensions.json
@@ -0,0 +1 @@
+{}