Browse Source

ffho-aptimer: first shot

Karsten Böddeker 8 years ago
parent
commit
c62dad4972

+ 38 - 0
ffho/ffho-aptimer/Makefile

@@ -0,0 +1,38 @@
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=ffho-aptimer
+PKG_VERSION:=1
+PKG_RELEASE:=$(GLUON_VERSION).$(GLUON_SITE_CODE)-$(GLUON_RELEASE).$(GLUON_CONFIG_VERSION)
+
+PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/ffho-aptimer
+  SECTION:=ffho
+  CATEGORY:=FFHO
+  TITLE:=Timer for the client wifi
+  DEPENDS:=+gluon-core
+  MAINTAINER:=Freifunk Hochstift <kontakt@hochstift.freifunk.net>
+  URL:=https://git.c3pb.de/freifunk-pb/ffho-packages
+endef
+
+define Package/ffho-aptimer/description
+	Timer for the client wifi
+endef
+
+define Build/Prepare
+	mkdir -p $(PKG_BUILD_DIR)
+endef
+
+define Build/Configure
+endef
+
+define Build/Compile
+endef
+
+define Package/ffho-aptimer/install
+  $(CP) ./files/* $(1)/
+endef
+
+$(eval $(call BuildPackage,ffho-aptimer))

+ 25 - 0
ffho/ffho-aptimer/ReadMe.md

@@ -0,0 +1,25 @@
+ffho-aptimer
+============
+
+Timer for the client wifi
+
+/etc/config/aptimer
+-------------------
+```
+config aptimer 'settings'
+        option enabled '1'
+        option type '$type'
+
+config $type '$day'
+        list on '06:00'
+        list off '23:00'
+```
+
+**$type=day**
+- $day=all
+
+**$type=week**
+- $day=[Mon|Tue|Wed|Thu|Fri|Sat|Sun]
+
+**$type=month**
+- $day=[01-31]

+ 7 - 0
ffho/ffho-aptimer/files/etc/config/aptimer

@@ -0,0 +1,7 @@
+#config aptimer 'settings'
+#	option enabled '1'
+#	option type 'day'
+#
+#config day 'all'
+#	list on '06:00'
+#	list off '23:00'

+ 1 - 0
ffho/ffho-aptimer/files/usr/lib/micron.d/aptimer

@@ -0,0 +1 @@
+* * * * * /usr/sbin/aptimer

+ 58 - 0
ffho/ffho-aptimer/files/usr/sbin/aptimer

@@ -0,0 +1,58 @@
+#!/usr/bin/lua
+local timestamp = os.time()
+local uci = require('luci.model.uci').cursor()
+
+function compare(list, value)
+  if not list then
+    return nil
+  end
+
+  for _, v in ipairs(list) do
+    if v == value then
+      return true
+    end
+  end
+
+  return false
+end
+
+function getDay()
+  local type = uci:get('aptimer','settings','type')
+  if type == 'day' then return 'all'
+  elseif type == 'week' then return os.date('%a', timestamp)
+  elseif type == 'month' then return os.date('%d', timestamp)
+  else return nil
+  end
+end
+
+function apSet(value)
+  local execWifi = false
+  local radios = {'client_radio0', 'client_radio1'}
+  for _, radio in ipairs(radios) do
+    if uci:get('wireless', radio) then
+      uci:set('wireless', radio, 'disabled', value and 0 or 1)
+      execWifi = true
+    end
+  end
+
+  if execWifi then
+    uci:save('wireless')
+    os.execute('wifi')
+  end
+end
+
+if uci:get_bool('aptimer','settings','enabled') then
+  local day = getDay()
+  local current = os.date("%H:%M", timestamp)
+
+  local off = compare(uci:get_list('aptimer', day, 'off'), current)
+  local on = compare(uci:get_list('aptimer', day, 'on'), current)
+
+  if on and not off then
+    apSet(true)
+  end
+
+  if off and not on then
+    apSet(false)
+  end
+end