Split incoming & outgoing MQTT servers
This commit is contained in:
parent
6b5c41b23f
commit
d886bd9586
47
module.nix
47
module.nix
|
@ -9,7 +9,7 @@ let
|
||||||
|
|
||||||
in {
|
in {
|
||||||
options.services.snooper = with types; {
|
options.services.snooper = with types; {
|
||||||
enable = mkEnableOption "Enable Snooper notifiaction server.";
|
enable = mkEnableOption "Enable Snooper notification server.";
|
||||||
|
|
||||||
verbose = mkEnableOption "Generate verbose logs and output.";
|
verbose = mkEnableOption "Generate verbose logs and output.";
|
||||||
|
|
||||||
|
@ -24,6 +24,30 @@ in {
|
||||||
};
|
};
|
||||||
|
|
||||||
mqtt = {
|
mqtt = {
|
||||||
|
incoming = {
|
||||||
|
host = mkOption {
|
||||||
|
type = str;
|
||||||
|
description = "Hostname of the MQTT server.";
|
||||||
|
};
|
||||||
|
|
||||||
|
port = mkOption {
|
||||||
|
type = port;
|
||||||
|
description = "Port on which the MQTT server is listening.";
|
||||||
|
default = 1883;
|
||||||
|
};
|
||||||
|
|
||||||
|
username = mkOption {
|
||||||
|
type = str;
|
||||||
|
description = "User as which to connect to the MQTT server.";
|
||||||
|
};
|
||||||
|
|
||||||
|
password-file = mkOption {
|
||||||
|
type = str;
|
||||||
|
description =
|
||||||
|
"File (on the local host) containing the password for the MQTT server.";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
outgoing = {
|
||||||
host = mkOption {
|
host = mkOption {
|
||||||
type = str;
|
type = str;
|
||||||
description = "Hostname of the MQTT server.";
|
description = "Hostname of the MQTT server.";
|
||||||
|
@ -47,6 +71,7 @@ in {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
config = mkIf cfg.enable {
|
config = mkIf cfg.enable {
|
||||||
systemd.services.snooper = {
|
systemd.services.snooper = {
|
||||||
|
@ -54,14 +79,24 @@ in {
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
DynamicUser = true;
|
DynamicUser = true;
|
||||||
LoadCredential = [ "mqtt.passwd:${cfg.mqtt.password-file}" ];
|
LoadCredential = [
|
||||||
|
"mqtt-incoming.passwd:${cfg.mqtt.incoming.password-file}"
|
||||||
|
"mqtt-outgoing.passwd:${cfg.mqtt.outgoing.password-file}"
|
||||||
|
];
|
||||||
ExecStart = pkgs.writeShellScript "snooper-server.sh"
|
ExecStart = pkgs.writeShellScript "snooper-server.sh"
|
||||||
(concatStringsSep " " ([
|
(concatStringsSep " " ([
|
||||||
"snooper-server"
|
"snooper-server"
|
||||||
"--mqtt-host=${cfg.mqtt.host}"
|
|
||||||
"--mqtt-port=${toString cfg.mqtt.port}"
|
"--incoming-mqtt-host=${cfg.mqtt.incoming.host}"
|
||||||
"--mqtt-user=${cfg.mqtt.username}"
|
"--incoming-mqtt-port=${toString cfg.mqtt.incoming.port}"
|
||||||
"--mqtt-password-file=$CREDENTIALS_DIRECTORY/mqtt.passwd"
|
"--incoming-mqtt-user=${cfg.mqtt.incoming.username}"
|
||||||
|
"--incoming-mqtt-password-file=$CREDENTIALS_DIRECTORY/mqtt-incoming.passwd"
|
||||||
|
|
||||||
|
"--outgoing-mqtt-host=${cfg.mqtt.outgoing.host}"
|
||||||
|
"--outgoing-mqtt-port=${toString cfg.mqtt.outgoing.port}"
|
||||||
|
"--outgoing-mqtt-user=${cfg.mqtt.outgoing.username}"
|
||||||
|
"--outgoing-mqtt-password-file=$CREDENTIALS_DIRECTORY/mqtt-outgoing.passwd"
|
||||||
|
|
||||||
"--notification-topic=${cfg.notification-topic}"
|
"--notification-topic=${cfg.notification-topic}"
|
||||||
] ++ (map (topic: "--event-topic=${topic}") cfg.event-topics)
|
] ++ (map (topic: "--event-topic=${topic}") cfg.event-topics)
|
||||||
++ (optional cfg.verbose "--verbose")));
|
++ (optional cfg.verbose "--verbose")));
|
||||||
|
|
|
@ -48,25 +48,36 @@
|
||||||
{:keys [options _ errors summary]} (parse-opts args required-args cli-opts)]
|
{:keys [options _ errors summary]} (parse-opts args required-args cli-opts)]
|
||||||
(when (:help options) (msg-quit 0 (usage summary)))
|
(when (:help options) (msg-quit 0 (usage summary)))
|
||||||
(when (seq errors) (msg-quit 1 (usage summary errors)))
|
(when (seq errors) (msg-quit 1 (usage summary errors)))
|
||||||
(let [{:keys [mqtt-host
|
(let [{:keys [incoming-mqtt-host
|
||||||
mqtt-port
|
incoming-mqtt-port
|
||||||
mqtt-user
|
incoming-mqtt-user
|
||||||
mqtt-password-file
|
incoming-mqtt-password-file
|
||||||
|
outgoing-mqtt-host
|
||||||
|
outgoing-mqtt-port
|
||||||
|
outgoing-mqtt-user
|
||||||
|
outgoing-mqtt-password-file
|
||||||
notification-topic
|
notification-topic
|
||||||
event-topic
|
event-topic
|
||||||
verbose]} options
|
verbose]} options
|
||||||
catch-shutdown (async/chan)
|
catch-shutdown (async/chan)
|
||||||
mqtt-client (mqtt/connect-json! :host mqtt-host
|
incoming-client (mqtt/connect-json! :host incoming-mqtt-host
|
||||||
:port mqtt-port
|
:port incoming-mqtt-port
|
||||||
:username mqtt-user
|
:username incoming-mqtt-user
|
||||||
:password (-> mqtt-password-file
|
:password (-> incoming-mqtt-password-file
|
||||||
|
(slurp)
|
||||||
|
(str/trim)))
|
||||||
|
outgoing-client (mqtt/connect-json! :host outgoing-mqtt-host
|
||||||
|
:port outgoing-mqtt-port
|
||||||
|
:username outgoing-mqtt-user
|
||||||
|
:password (-> outgoing-mqtt-password-file
|
||||||
(slurp)
|
(slurp)
|
||||||
(str/trim)))
|
(str/trim)))
|
||||||
logger (log/print-logger)]
|
logger (log/print-logger)]
|
||||||
(when verbose
|
(when verbose
|
||||||
(println (format "launching snooper server to listen on %s and report events on %s"
|
(println (format "launching snooper server to listen on %s and report events on %s"
|
||||||
event-topic notification-topic)))
|
event-topic notification-topic)))
|
||||||
(snooper/listen! :mqtt-client mqtt-client
|
(snooper/listen! :incoming-mqtt-client incoming-client
|
||||||
|
:outgoing-mqtt-client outgoing-client
|
||||||
:notification-topic notification-topic
|
:notification-topic notification-topic
|
||||||
:event-topics event-topic
|
:event-topics event-topic
|
||||||
:logger logger
|
:logger logger
|
||||||
|
@ -76,5 +87,6 @@
|
||||||
(<!! catch-shutdown)
|
(<!! catch-shutdown)
|
||||||
(println "stopping snooper server")
|
(println "stopping snooper server")
|
||||||
;; Stopping the MQTT will stop tattler
|
;; Stopping the MQTT will stop tattler
|
||||||
(mqtt/stop! mqtt-client)
|
(mqtt/stop! incoming-client)
|
||||||
|
(mqtt/stop! outgoing-client)
|
||||||
(System/exit 0))))
|
(System/exit 0))))
|
||||||
|
|
|
@ -84,12 +84,13 @@
|
||||||
:urgency criticality}))
|
:urgency criticality}))
|
||||||
|
|
||||||
(defn listen!
|
(defn listen!
|
||||||
[& {mqtt-client :mqtt-client
|
[& {incoming-client :incoming-mqtt-client
|
||||||
|
outgoing-client :outgoing-mqtt-client
|
||||||
notification-topic :notification-topic
|
notification-topic :notification-topic
|
||||||
event-topics :event-topics
|
event-topics :event-topics
|
||||||
logger :logger
|
logger :logger
|
||||||
verbose :verbose}]
|
verbose :verbose}]
|
||||||
(let [incoming (map (partial mqtt/subscribe! mqtt-client) event-topics)
|
(let [incoming (map (partial mqtt/subscribe! incoming-client) event-topics)
|
||||||
valid-evt? (t/validator MotionEvent)]
|
valid-evt? (t/validator MotionEvent)]
|
||||||
(go-loop [evts (alts! incoming)]
|
(go-loop [evts (alts! incoming)]
|
||||||
(let [evt (first evts)]
|
(let [evt (first evts)]
|
||||||
|
@ -98,7 +99,7 @@
|
||||||
(valid-evt? evt) (do (log/info! logger (format "received motion event id %s from %s"
|
(valid-evt? evt) (do (log/info! logger (format "received motion event id %s from %s"
|
||||||
(:id evt)
|
(:id evt)
|
||||||
(:topic evt)))
|
(:topic evt)))
|
||||||
(mqtt/send! mqtt-client notification-topic
|
(mqtt/send! outgoing-client notification-topic
|
||||||
(verbose-pthru verbose (translate-event (:payload evt))))
|
(verbose-pthru verbose (translate-event (:payload evt))))
|
||||||
(recur (alts! incoming)))
|
(recur (alts! incoming)))
|
||||||
:else (do (log/error! logger (format "invalid motion event: %s" evt))
|
:else (do (log/error! logger (format "invalid motion event: %s" evt))
|
||||||
|
|
Loading…
Reference in New Issue