Merge pull request #228288 from Cynerd/bigclown

This commit is contained in:
Sandro
2023-07-19 15:51:25 +02:00
committed by GitHub
9 changed files with 651 additions and 0 deletions

View File

@@ -609,6 +609,7 @@
./services/misc/autorandr.nix
./services/misc/autosuspend.nix
./services/misc/bazarr.nix
./services/misc/bcg.nix
./services/misc/beanstalkd.nix
./services/misc/bees.nix
./services/misc/bepasty.nix
@@ -666,6 +667,7 @@
./services/misc/mediatomb.nix
./services/misc/metabase.nix
./services/misc/moonraker.nix
./services/misc/mqtt2influxdb.nix
./services/misc/n8n.nix
./services/misc/nitter.nix
./services/misc/nix-gc.nix

View File

@@ -0,0 +1,175 @@
{
config,
lib,
pkgs,
...
}:
with lib;
let
cfg = config.services.bcg;
configFile = (pkgs.formats.yaml {}).generate "bcg.conf.yaml" (
filterAttrsRecursive (n: v: v != null) {
inherit (cfg) device name mqtt;
retain_node_messages = cfg.retainNodeMessages;
qos_node_messages = cfg.qosNodeMessages;
base_topic_prefix = cfg.baseTopicPrefix;
automatic_remove_kit_from_names = cfg.automaticRemoveKitFromNames;
automatic_rename_kit_nodes = cfg.automaticRenameKitNodes;
automatic_rename_generic_nodes = cfg.automaticRenameGenericNodes;
automatic_rename_nodes = cfg.automaticRenameNodes;
}
);
in
{
options = {
services.bcg = {
enable = mkEnableOption (mdDoc "BigClown gateway");
package = mkOption {
default = pkgs.python3Packages.bcg;
defaultText = literalExpression "pkgs.python3Packages.bcg";
description = mdDoc "Which bcg derivation to use.";
type = types.package;
};
environmentFiles = mkOption {
type = types.listOf types.path;
default = [];
example = [ "/run/keys/bcg.env" ];
description = mdDoc ''
File to load as environment file. Environment variables from this file
will be interpolated into the config file using envsubst with this
syntax: `$ENVIRONMENT` or `''${VARIABLE}`.
This is useful to avoid putting secrets into the nix store.
'';
};
verbose = mkOption {
type = types.enum ["CRITICAL" "ERROR" "WARNING" "INFO" "DEBUG"];
default = "WARNING";
description = mdDoc "Verbosity level.";
};
device = mkOption {
type = types.str;
description = mdDoc "Device name to configure gateway to use.";
};
name = mkOption {
type = with types; nullOr str;
default = null;
description = mdDoc ''
Name for the device.
Supported variables:
* `{ip}` IP address
* `{id}` The ID of the connected usb-dongle or core-module
`null` can be used for automatic detection from gateway firmware.
'';
};
mqtt = {
host = mkOption {
type = types.str;
default = "127.0.0.1";
description = mdDoc "Host where MQTT server is running.";
};
port = mkOption {
type = types.port;
default = 1883;
description = mdDoc "Port of MQTT server.";
};
username = mkOption {
type = with types; nullOr str;
default = null;
description = mdDoc "MQTT server access username.";
};
password = mkOption {
type = with types; nullOr str;
default = null;
description = mdDoc "MQTT server access password.";
};
cafile = mkOption {
type = with types; nullOr str;
default = null;
description = mdDoc "Certificate Authority file for MQTT server access.";
};
certfile = mkOption {
type = with types; nullOr str;
default = null;
description = mdDoc "Certificate file for MQTT server access.";
};
keyfile = mkOption {
type = with types; nullOr str;
default = null;
description = mdDoc "Key file for MQTT server access.";
};
};
retainNodeMessages = mkOption {
type = types.bool;
default = false;
description = mdDoc "Specify that node messages should be retaied in MQTT broker.";
};
qosNodeMessages = mkOption {
type = types.int;
default = 1;
description = mdDoc "Set the guarantee of MQTT message delivery.";
};
baseTopicPrefix = mkOption {
type = types.str;
default = "";
description = mdDoc "Topic prefix added to all MQTT messages.";
};
automaticRemoveKitFromNames = mkOption {
type = types.bool;
default = true;
description = mdDoc "Automatically remove kits.";
};
automaticRenameKitNodes = mkOption {
type = types.bool;
default = true;
description = mdDoc "Automatically rename kit's nodes.";
};
automaticRenameGenericNodes = mkOption {
type = types.bool;
default = true;
description = mdDoc "Automatically rename generic nodes.";
};
automaticRenameNodes = mkOption {
type = types.bool;
default = true;
description = mdDoc "Automatically rename all nodes.";
};
rename = mkOption {
type = with types; attrsOf str;
default = {};
description = mdDoc "Rename nodes to different name.";
};
};
};
config = mkIf cfg.enable {
environment.systemPackages = with pkgs; [
python3Packages.bcg
python3Packages.bch
];
systemd.services.bcg = let
envConfig = cfg.environmentFiles != [];
finalConfig = if envConfig
then "$RUNTIME_DIRECTORY/bcg.config.yaml"
else configFile;
in {
description = "BigClown Gateway";
wantedBy = [ "multi-user.target" ];
wants = mkIf config.services.mosquitto.enable [ "mosquitto.service" ];
after = [ "network-online.target" ];
preStart = ''
umask 077
${pkgs.envsubst}/bin/envsubst -i "${configFile}" -o "${finalConfig}"
'';
serviceConfig = {
EnvironmentFile = cfg.environmentFiles;
ExecStart="${cfg.package}/bin/bcg -c ${finalConfig} -v ${cfg.verbose}";
RuntimeDirectory = "bcg";
};
};
};
}

View File

@@ -0,0 +1,253 @@
{
config,
lib,
pkgs,
...
}:
with lib;
let
cfg = config.services.mqtt2influxdb;
filterNull = filterAttrsRecursive (n: v: v != null);
configFile = (pkgs.formats.yaml {}).generate "mqtt2influxdb.config.yaml" (
filterNull {
inherit (cfg) mqtt influxdb;
points = map filterNull cfg.points;
}
);
pointType = types.submodule {
options = {
measurement = mkOption {
type = types.str;
description = mdDoc "Name of the measurement";
};
topic = mkOption {
type = types.str;
description = mdDoc "MQTT topic to subscribe to.";
};
fields = mkOption {
type = types.submodule {
options = {
value = mkOption {
type = types.str;
default = "$.payload";
description = mdDoc "Value to be picked up";
};
type = mkOption {
type = with types; nullOr str;
default = null;
description = mdDoc "Type to be picked up";
};
};
};
description = mdDoc "Field selector.";
};
tags = mkOption {
type = with types; attrsOf str;
default = {};
description = mdDoc "Tags applied";
};
};
};
defaultPoints = [
{
measurement = "temperature";
topic = "node/+/thermometer/+/temperature";
fields.value = "$.payload";
tags = {
id = "$.topic[1]";
channel = "$.topic[3]";
};
}
{
measurement = "relative-humidity";
topic = "node/+/hygrometer/+/relative-humidity";
fields.value = "$.payload";
tags = {
id = "$.topic[1]";
channel = "$.topic[3]";
};
}
{
measurement = "illuminance";
topic = "node/+/lux-meter/0:0/illuminance";
fields.value = "$.payload";
tags = {
id = "$.topic[1]";
};
}
{
measurement = "pressure";
topic = "node/+/barometer/0:0/pressure";
fields.value = "$.payload";
tags = {
id = "$.topic[1]";
};
}
{
measurement = "co2";
topic = "node/+/co2-meter/-/concentration";
fields.value = "$.payload";
tags = {
id = "$.topic[1]";
};
}
{
measurement = "voltage";
topic = "node/+/battery/+/voltage";
fields.value = "$.payload";
tags = {
id = "$.topic[1]";
};
}
{
measurement = "button";
topic = "node/+/push-button/+/event-count";
fields.value = "$.payload";
tags = {
id = "$.topic[1]";
channel = "$.topic[3]";
};
}
{
measurement = "tvoc";
topic = "node/+/voc-lp-sensor/0:0/tvoc";
fields.value = "$.payload";
tags = {
id = "$.topic[1]";
};
}
];
in {
options = {
services.mqtt2influxdb = {
enable = mkEnableOption (mdDoc "BigClown MQTT to InfluxDB bridge.");
environmentFiles = mkOption {
type = types.listOf types.path;
default = [];
example = [ "/run/keys/mqtt2influxdb.env" ];
description = mdDoc ''
File to load as environment file. Environment variables from this file
will be interpolated into the config file using envsubst with this
syntax: `$ENVIRONMENT` or `''${VARIABLE}`.
This is useful to avoid putting secrets into the nix store.
'';
};
mqtt = {
host = mkOption {
type = types.str;
default = "127.0.0.1";
description = mdDoc "Host where MQTT server is running.";
};
port = mkOption {
type = types.port;
default = 1883;
description = mdDoc "MQTT server port.";
};
username = mkOption {
type = with types; nullOr str;
default = null;
description = mdDoc "Username used to connect to the MQTT server.";
};
password = mkOption {
type = with types; nullOr str;
default = null;
description = mdDoc ''
MQTT password.
It is highly suggested to use here replacement through
environmentFiles as otherwise the password is put world readable to
the store.
'';
};
cafile = mkOption {
type = with types; nullOr path;
default = null;
description = mdDoc "Certification Authority file for MQTT";
};
certfile = mkOption {
type = with types; nullOr path;
default = null;
description = mdDoc "Certificate file for MQTT";
};
keyfile = mkOption {
type = with types; nullOr path;
default = null;
description = mdDoc "Key file for MQTT";
};
};
influxdb = {
host = mkOption {
type = types.str;
default = "127.0.0.1";
description = mdDoc "Host where InfluxDB server is running.";
};
port = mkOption {
type = types.port;
default = 8086;
description = mdDoc "InfluxDB server port";
};
database = mkOption {
type = types.str;
description = mdDoc "Name of the InfluxDB database.";
};
username = mkOption {
type = with types; nullOr str;
default = null;
description = mdDoc "Username for InfluxDB login.";
};
password = mkOption {
type = with types; nullOr str;
default = null;
description = mdDoc ''
Password for InfluxDB login.
It is highly suggested to use here replacement through
environmentFiles as otherwise the password is put world readable to
the store.
'';
};
ssl = mkOption {
type = types.bool;
default = false;
description = mdDoc "Use SSL to connect to the InfluxDB server.";
};
verify_ssl = mkOption {
type = types.bool;
default = true;
description = mdDoc "Verify SSL certificate when connecting to the InfluxDB server.";
};
};
points = mkOption {
type = types.listOf pointType;
default = defaultPoints;
description = mdDoc "Points to bridge from MQTT to InfluxDB.";
};
};
};
config = mkIf cfg.enable {
systemd.services.bigclown-mqtt2influxdb = let
envConfig = cfg.environmentFiles != [];
finalConfig = if envConfig
then "$RUNTIME_DIRECTORY/mqtt2influxdb.config.yaml"
else configFile;
in {
description = "BigClown MQTT to InfluxDB bridge";
wantedBy = ["multi-user.target"];
wants = mkIf config.services.mosquitto.enable ["mosquitto.service"];
preStart = ''
umask 077
${pkgs.envsubst}/bin/envsubst -i "${configFile}" -o "${finalConfig}"
'';
serviceConfig = {
EnvironmentFile = cfg.environmentFiles;
ExecStart = "${cfg.package}/bin/mqtt2influxdb -dc ${finalConfig}";
RuntimeDirectory = "mqtt2influxdb";
};
};
};
}

View File

@@ -0,0 +1,54 @@
{ lib
, buildPythonPackage
, fetchFromGitHub
, appdirs
, click
, colorama
, intelhex
, packaging
, pyaml
, pyftdi
, pyserial
, requests
, schema
}:
buildPythonPackage rec {
pname = "bcf";
version = "1.9.0";
src = fetchFromGitHub {
owner = "hardwario";
repo = "bch-firmware-tool";
rev = "v${version}";
sha256 = "i28VewTB2XEZSfk0UeCuwB7Z2wz4qPBhzvxJIYkKwJ4=";
};
postPatch = ''
sed -ri 's/@@VERSION@@/${version}/g' \
bcf/__init__.py setup.py
'';
propagatedBuildInputs = [
appdirs
click
colorama
intelhex
packaging
pyaml
pyftdi
pyserial
requests
schema
];
pythonImportsCheck = [ "bcf" ];
doCheck = false; # Project provides no tests
meta = with lib; {
homepage = "https://github.com/hardwario/bch-firmware-tool";
description = "HARDWARIO Firmware Tool";
platforms = platforms.linux;
license = licenses.mit;
maintainers = with maintainers; [ cynerd ];
};
}

View File

@@ -0,0 +1,49 @@
{ lib
, buildPythonPackage
, fetchFromGitHub
, appdirs
, click
, click-log
, paho-mqtt
, pyaml
, pyserial
, schema
, simplejson
}:
buildPythonPackage rec {
pname = "bcg";
version = "1.17.0";
src = fetchFromGitHub {
owner = "hardwario";
repo = "bch-gateway";
rev = "v${version}";
sha256 = "2Yh5MeIv+BIxjoO9GOPqq7xTAFhyBvnxPy7DeO2FrkI=";
};
postPatch = ''
sed -ri 's/@@VERSION@@/${version}/g' \
bcg/__init__.py setup.py
'';
propagatedBuildInputs = [
appdirs
click
click-log
paho-mqtt
pyaml
pyserial
schema
simplejson
];
pythonImportsCheck = [ "bcg" ];
meta = with lib; {
homepage = "https://github.com/hardwario/bch-gateway";
description = "HARDWARIO Gateway (Python Application «bcg»)";
platforms = platforms.linux;
license = licenses.mit;
maintainers = with maintainers; [ cynerd ];
};
}

View File

@@ -0,0 +1,42 @@
{ lib
, buildPythonPackage
, fetchFromGitHub
, click
, click-log
, paho-mqtt
, pyaml
}:
buildPythonPackage rec {
pname = "bch";
version = "1.2.1";
src = fetchFromGitHub {
owner = "hardwario";
repo = "bch-control-tool";
rev = "v${version}";
sha256 = "/C+NbJ0RrWZ/scv/FiRBTh4h7u0xS4mHVDWQ0WwmlEY=";
};
propagatedBuildInputs = [
click
click-log
paho-mqtt
pyaml
];
postPatch = ''
sed -ri 's/@@VERSION@@/${version}/g' \
bch/cli.py setup.py
'';
pythonImportsCheck = [ "bch" ];
meta = with lib; {
homepage = "https://github.com/hardwario/bch-control-tool";
description = "HARDWARIO Hub Control Tool";
platforms = platforms.linux;
license = licenses.mit;
maintainers = with maintainers; [ cynerd ];
};
}

View File

@@ -0,0 +1,42 @@
{ lib
, buildPythonPackage
, fetchFromGitHub
, influxdb
, jsonpath-ng
, paho-mqtt
, py-expression-eval
, pyaml
, pycron
, schema
}:
buildPythonPackage rec {
pname = "mqtt2influxdb";
version = "1.5.2";
src = fetchFromGitHub {
owner = "hardwario";
repo = "bch-mqtt2influxdb";
rev = "v${version}";
sha256 = "YDgMoxnH4vCCa7b857U6iVBhYLxk8ZjytGziryn24bg=";
};
propagatedBuildInputs = [
influxdb
jsonpath-ng
paho-mqtt
py-expression-eval
pyaml
pycron
schema
];
pythonImportsCheck = [ "mqtt2influxdb" ];
meta = with lib; {
homepage = "https://github.com/hardwario/bch-mqtt2influxdb";
description = "Flexible MQTT to InfluxDB Bridge";
platforms = platforms.linux;
license = licenses.mit;
maintainers = with maintainers; [ cynerd ];
};
}

View File

@@ -0,0 +1,24 @@
{ lib,
buildPythonPackage,
fetchFromGitHub,
}:
buildPythonPackage rec {
pname = "py-expression-eval";
version = "0.3.14";
src = fetchFromGitHub {
owner = "axiacore";
repo = "py-expression-eval";
rev = "v${version}";
sha256 = "YxhZd8V6ofphcNdcbBbrT5mc37O9c6W1mfhsvFVC+KM=";
};
meta = with lib; {
homepage = "https://github.com/AxiaCore/py-expression-eval/";
description = "Python Mathematical Expression Evaluator";
platforms = platforms.linux;
license = licenses.mit;
maintainers = with maintainers; [ cynerd ];
};
}

View File

@@ -1269,6 +1269,12 @@ self: super: with self; {
bcdoc = callPackage ../development/python-modules/bcdoc { };
bcf = callPackage ../development/python-modules/bcf { };
bcg = callPackage ../development/python-modules/bcg { };
bch = callPackage ../development/python-modules/bch { };
bcrypt = if stdenv.hostPlatform.system == "i686-linux" then
callPackage ../development/python-modules/bcrypt/3.nix { }
else
@@ -7146,6 +7152,8 @@ self: super: with self; {
py-eth-sig-utils = callPackage ../development/python-modules/py-eth-sig-utils { };
py-expression-eval = callPackage ../development/python-modules/py-expression-eval { };
nwdiag = callPackage ../development/python-modules/nwdiag { };
oasatelematics = callPackage ../development/python-modules/oasatelematics { };
@@ -7754,6 +7762,8 @@ self: super: with self; {
micloud = callPackage ../development/python-modules/micloud { };
mqtt2influxdb = callPackage ../development/python-modules/mqtt2influxdb { };
msgraph-core = callPackage ../development/python-modules/msgraph-core { };
multipart = callPackage ../development/python-modules/multipart { };