From ace4face5aa4ae47c9039d70523d91cc4c61a09e Mon Sep 17 00:00:00 2001
From: Tobias Reisinger <tobias@msrg.cc>
Date: Mon, 29 Apr 2024 04:19:02 +0200
Subject: [PATCH] Improve configuration

---
 .env.example                                  |  4 +-
 .gitignore                                    |  4 +-
 Makefile                                      | 13 +---
 config/PklProject                             |  8 ++
 config/PklProject.deps.json                   |  4 +
 ...{emgauwa-controller.pkl => controller.pkl} | 78 ++++++++++---------
 config/{emgauwa-core.pkl => core.pkl}         |  2 +-
 config/lib/controller.pkl                     |  1 +
 emgauwa-lib/src/settings.rs                   |  3 +
 9 files changed, 66 insertions(+), 51 deletions(-)
 create mode 100644 config/PklProject
 create mode 100644 config/PklProject.deps.json
 rename config/{emgauwa-controller.pkl => controller.pkl} (82%)
 rename config/{emgauwa-core.pkl => core.pkl} (78%)

diff --git a/.env.example b/.env.example
index fb629ae..f86ad04 100644
--- a/.env.example
+++ b/.env.example
@@ -1,3 +1,3 @@
-#EMGAUWA_CONTROLLER_LOGGING_LEVEL=INFO
+#EMGAUWA_CONTROLLER__LOGGING__LEVEL=DEBUG
 
-#EMGAUWA_CORE_LOGGING_LEVEL=INFO
+#EMGAUWA_CORE__LOGGING__LEVEL=DEBUG
diff --git a/.gitignore b/.gitignore
index 8d95db4..77c0b68 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,8 +1,10 @@
 /target
+/pkl-out
 
 /.env
 /api.http
 
 /emgauwa-*.sqlite
 /emgauwa-*.sqlite-*
-/emgauwa-*.json
\ No newline at end of file
+/emgauwa-*.json
+
diff --git a/Makefile b/Makefile
index db699c4..c452a85 100644
--- a/Makefile
+++ b/Makefile
@@ -1,11 +1,3 @@
-ifneq (,$(wildcard ./.env))
-    include .env
-    export
-endif
-
-DATABASE_URL ?= sqlite://emgauwa-dev.sqlite
-export DATABASE_URL
-
 sqlx:
 	cargo sqlx database drop -y
 	cargo sqlx database create
@@ -15,13 +7,16 @@ sqlx:
 build-rpi:
 	cross build --target arm-unknown-linux-gnueabihf
 
-emgauwa-%.json: config/emgauwa-%.pkl config/lib/%.pkl config/lib/common.pkl $(wildcard .env)
+emgauwa-%.json: config/%.pkl config/lib/%.pkl
 	pkl eval -f json -o $@ $<
 
 configs:
 	$(MAKE) emgauwa-core.json
 	$(MAKE) emgauwa-controller.json
 
+pkl-package:
+	pkl project package config --output-path "./pkl-out"
+
 clean:
 	rm -f emgauwa-controller.json
 	rm -f emgauwa-controller.sqlite
diff --git a/config/PklProject b/config/PklProject
new file mode 100644
index 0000000..bc7911f
--- /dev/null
+++ b/config/PklProject
@@ -0,0 +1,8 @@
+amends "pkl:Project"
+
+package {
+  name = "emgauwa"
+  baseUri = "package://emgauwa.app/pkl/\(name)"
+  version = "0.1.0"
+  packageZipUrl = "https://emgauwa.app/pkl/\(name)@\(version).zip"
+}
\ No newline at end of file
diff --git a/config/PklProject.deps.json b/config/PklProject.deps.json
new file mode 100644
index 0000000..836079a
--- /dev/null
+++ b/config/PklProject.deps.json
@@ -0,0 +1,4 @@
+{
+  "schemaVersion": 1,
+  "resolvedDependencies": {}
+}
\ No newline at end of file
diff --git a/config/emgauwa-controller.pkl b/config/controller.pkl
similarity index 82%
rename from config/emgauwa-controller.pkl
rename to config/controller.pkl
index b0917f1..8eb74ce 100644
--- a/config/emgauwa-controller.pkl
+++ b/config/controller.pkl
@@ -13,59 +13,61 @@ permissions {
 }
 
 logging {
-  level = read?("env:EMGAUWA_CONTROLLER_LOGGING_LEVEL") ?? "DEBUG"
+  level = "INFO"
   file = "stdout"
 }
 
 relays {
   new {
     driver = "null"
-    pin = 24
-    inverted = true
-  }
-  new {
-    driver = "null"
-    pin = 23
-    inverted = true
-  }
-  new {
-    driver = "null"
-    pin = 22
-    inverted = true
-  }
-  new {
-    driver = "null"
-    pin = 27
-    inverted = true
-  }
-  new {
-    driver = "null"
-    pin = 18
-    inverted = true
-  }
-  new {
-    driver = "null"
-    pin = 17
-    inverted = true
-  }
-  new {
-    driver = "null"
-    pin = 15
-    inverted = true
-  }
-  new {
-    driver = "null"
-    pin = 14
+    pin = 0
     inverted = true
   }
   new {
     driver = "null"
     pin = 1
+    inverted = true
+  }
+  new {
+    driver = "null"
+    pin = 2
+    inverted = true
+  }
+  new {
+    driver = "null"
+    pin = 3
+    inverted = true
+  }
+  new {
+    driver = "null"
+    pin = 4
+    inverted = true
+  }
+  new {
+    driver = "null"
+    pin = 5
+    inverted = true
+  }
+  new {
+    driver = "null"
+    pin = 10
+    inverted = true
+    pulse = 10
+  }
+  new {
+    driver = "null"
+    pin = 11
+    inverted = true
+    pulse = 10
+  }
+  new {
+    driver = "null"
+    pin = 20
     inverted = false
   }
   new {
     driver = "null"
-    pin = 0
+    pin = 21
     inverted = false
   }
 }
diff --git a/config/emgauwa-core.pkl b/config/core.pkl
similarity index 78%
rename from config/emgauwa-core.pkl
rename to config/core.pkl
index 3e6caa4..dd63722 100644
--- a/config/emgauwa-core.pkl
+++ b/config/core.pkl
@@ -13,6 +13,6 @@ permissions {
 }
 
 logging {
-  level = read?("env:EMGAUWA_CORE_LOGGING_LEVEL") ?? "DEBUG"
+  level = "INFO"
   file = "stdout"
 }
diff --git a/config/lib/controller.pkl b/config/lib/controller.pkl
index 7533b98..51417d6 100644
--- a/config/lib/controller.pkl
+++ b/config/lib/controller.pkl
@@ -12,5 +12,6 @@ class RelayConfig {
   driver: "null"|"gpio"|"piface"
   pin: Number
   inverted: Boolean
+  pulse: Number(this >= 0)|Null = null
 }
 relays: Listing<RelayConfig>
diff --git a/emgauwa-lib/src/settings.rs b/emgauwa-lib/src/settings.rs
index 94d1343..bc966dd 100644
--- a/emgauwa-lib/src/settings.rs
+++ b/emgauwa-lib/src/settings.rs
@@ -49,9 +49,12 @@ pub fn load<T>(config_name: &str, env_prefix: &str) -> Result<T, EmgauwaError>
 where
 	for<'de> T: serde::Deserialize<'de>,
 {
+	let etc_file =
+		config::File::with_name(&format!("/etc/emgauwa/{}", config_name)).required(false);
 	let local_file = config::File::with_name(&format!("./emgauwa-{}", config_name)).required(false);
 
 	config::Config::builder()
+		.add_source(etc_file)
 		.add_source(local_file)
 		.add_source(
 			config::Environment::with_prefix(&format!("EMGAUWA_{}", env_prefix))