From 106eea47a24fb92e316d36bb5608a3ca6afb6f67 Mon Sep 17 00:00:00 2001 From: Bjørn Mork Date: Wed, 29 May 2019 13:19:20 +0200 Subject: add openwrt example package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bjørn Mork --- pkg/openwrt/Makefile | 45 +++++++++++++++++++++++ pkg/openwrt/files/obinsect.config | 28 ++++++++++++++ pkg/openwrt/files/obinsect.init | 77 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 pkg/openwrt/Makefile create mode 100644 pkg/openwrt/files/obinsect.config create mode 100644 pkg/openwrt/files/obinsect.init diff --git a/pkg/openwrt/Makefile b/pkg/openwrt/Makefile new file mode 100644 index 0000000..c7abb13 --- /dev/null +++ b/pkg/openwrt/Makefile @@ -0,0 +1,45 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=obinsect +PKG_RELEASE:=1 + +PKG_VERSION=0.01 +PKG_SOURCE_PROTO:=git +PKG_SOURCE_URL=https://github.com/bmork/obinsect.git +PKG_SOURCE_VERSION:=v$(PKG_VERSION) +PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_SOURCE_VERSION) +PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_SOURCE_SUBDIR) + +PKG_MAINTAINER:=Bjørn Mork + +PKG_LICENSE:=GPL-2.0 + +PKG_BUILD_PARALLEL:=1 + +include $(INCLUDE_DIR)/package.mk + + +define Package/obinsect + SECTION:=net + CATEGORY:=Network + DEPENDS:=+libjson-c +libmosquitto + TITLE:=AMS HAN M-BUS to MQTT proxy +endef + +define Package/obinsect/description + Read energy meter M-BUS HAN port and publish data to a MQTT broker +endef + +define Package/obinsect/install + $(INSTALL_DIR) $(1)/usr/sbin $(1)/usr/share/obinsect $(1)/etc/init.d/ $(1)/etc/config + $(INSTALL_BIN) $(PKG_BUILD_DIR)/obinsectd $(1)/usr/sbin/ + $(INSTALL_DATA) $(PKG_BUILD_DIR)/*.json $(1)/usr/share/obinsect/ + $(INSTALL_BIN) ./files/obinsect.init $(1)/etc/init.d/obinsect + $(INSTALL_CONF) ./files/obinsect.config $(1)/etc/config/obinsect +endef + +define Package/obinsect/conffiles +/etc/config/obinsect +endef + +$(eval $(call BuildPackage,obinsect)) diff --git a/pkg/openwrt/files/obinsect.config b/pkg/openwrt/files/obinsect.config new file mode 100644 index 0000000..09e221b --- /dev/null +++ b/pkg/openwrt/files/obinsect.config @@ -0,0 +1,28 @@ +config obinsect config + option serial /dev/ttyUSB0 + option broker 127.0.0.1 + list obisdefs "/usr/share/obinsect/aidon_v0001.json" + list obisdefs "/usr/share/obinsect/kfm_001.json" + list obisdefs "/usr/share/obinsect/kamstrup_v0001.json" + +config publish + option topic "/obinsect/value/power" + option value "Power" + +config publish + option topic "/obinsect/cumul/energy" + list value "CumulativeEnergy" + list value "MeterTime" + list value "timestamp" + +config publish + option topic "/obinsect/log/debug" + option value "debug" + +config publish + option topic "/obinsect/log/info" + option value "info" + +config publish + option topic "/obinsect/log/error" + option value "error" diff --git a/pkg/openwrt/files/obinsect.init b/pkg/openwrt/files/obinsect.init new file mode 100644 index 0000000..5e547ed --- /dev/null +++ b/pkg/openwrt/files/obinsect.init @@ -0,0 +1,77 @@ +#!/bin/sh /etc/rc.common +# Copyright (C) 2019 Bjørn Mork + +START=90 +STOP=01 + +USE_PROCD=1 +OBINSECT_CONF=/tmp/obinsect.conf + +add_topic() +{ + local _name="$1" + local _topic + local _value + local _tmp + + config_get _topic "$_name" 'topic' + config_get _value "$_name" 'value' + if [ "${_value% *}" == "$_value" ]; then + json_add_string "$_topic" "$_value" + else + json_add_array "$_topic" + for _tmp in $_value; do + json_add_string "" "$_tmp" + done + json_close_array + fi +} + +write_obinsect_conf() +{ + . /lib/functions.sh + . /usr/share/libubox/jshn.sh + config_load 'obinsect' + + json_init + json_add_object 'topicmap' + config_foreach add_topic 'publish' + json_close_object + + + config_get obisdefs 'config' 'obisdefs' + json_add_array 'obisdefs' + for obisdef in $obisdefs; do + json_add_string "" "$obisdef" + done + json_close_array + + + json_dump > "$OBINSECT_CONF" +} + +start_service() { + + write_obinsect_conf + config_get serial 'config' 'serial' + config_get broker 'config' 'broker' + + procd_open_instance + procd_set_param command /usr/sbin/obinsectd + procd_append_param command -c "$OBINSECT_CONF" + procd_append_param command -s "$serial" + procd_append_param command -b "$broker" + procd_set_param respawn + procd_close_instance +} + +service_running() { + pgrep -x /usr/sbin/obinsectd &> /dev/null +} + +reload_service() { + running || return 1 + write_obinsect_conf + return 0 +} + -- cgit v1.2.3