mirror of
https://github.com/gSpotx2f/luci-app-internet-detector.git
synced 2025-12-06 11:36:49 +03:00
v0.4. Daemon modules, LED control
This commit is contained in:
@@ -5,8 +5,8 @@
|
||||
include $(TOPDIR)/rules.mk
|
||||
|
||||
PKG_NAME:=internet-detector
|
||||
PKG_VERSION:=0.3.0
|
||||
PKG_RELEASE:=2
|
||||
PKG_VERSION:=0.4
|
||||
PKG_RELEASE:=1
|
||||
PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector>
|
||||
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
|
||||
|
||||
@@ -39,6 +39,8 @@ endef
|
||||
define Package/$(PKG_NAME)/install
|
||||
$(INSTALL_DIR) $(1)/usr/bin
|
||||
$(INSTALL_BIN) $(PKG_BUILD_DIR)/files/usr/bin/internet-detector $(1)/usr/bin/internet-detector
|
||||
$(INSTALL_DIR) $(1)/usr/lib/internet-detector
|
||||
$(INSTALL_DATA) $(PKG_BUILD_DIR)/files/usr/lib/internet-detector/mod_led_control.lua $(1)/usr/lib/internet-detector/mod_led_control.lua
|
||||
$(INSTALL_DIR) $(1)/etc/internet-detector
|
||||
$(INSTALL_BIN) $(PKG_BUILD_DIR)/files/etc/internet-detector/down-script $(1)/etc/internet-detector/down-script
|
||||
$(INSTALL_BIN) $(PKG_BUILD_DIR)/files/etc/internet-detector/run-script $(1)/etc/internet-detector/run-script
|
||||
|
||||
@@ -1,18 +1,25 @@
|
||||
config main 'config'
|
||||
option mode '2'
|
||||
list hosts '8.8.8.8'
|
||||
list hosts '1.1.1.1'
|
||||
option check_type '0'
|
||||
option tcp_port '53'
|
||||
|
||||
config main 'ui_config'
|
||||
option interval_up '6'
|
||||
option interval_down '1'
|
||||
option connection_attempts '1'
|
||||
option connection_timeout '1'
|
||||
|
||||
config main 'service_config'
|
||||
option interval_up '30'
|
||||
option interval_down '5'
|
||||
option connection_attempts '2'
|
||||
option connection_timeout '2'
|
||||
option enable_logger '1'
|
||||
option enable_up_script '0'
|
||||
option enable_down_script '0'
|
||||
option enable_run_script '0'
|
||||
option interval_up '30'
|
||||
option interval_down '5'
|
||||
option ui_interval_up '6'
|
||||
option ui_interval_down '1'
|
||||
list hosts '8.8.8.8'
|
||||
list hosts '1.1.1.1'
|
||||
option check_type '0'
|
||||
option connection_attempts '2'
|
||||
option connection_timeout '2'
|
||||
option ui_connection_attempts '1'
|
||||
option ui_connection_timeout '1'
|
||||
option tcp_port '53'
|
||||
|
||||
config module 'mod_led_control'
|
||||
option enabled '0'
|
||||
|
||||
@@ -35,11 +35,13 @@ local Config = {
|
||||
["connectionTimeout"] = 3,
|
||||
["UIConnectionTimeout"] = 1,
|
||||
["tcpPort"] = 53,
|
||||
["checkType"] = 0, -- 0: ping, 1: TCP
|
||||
["checkType"] = 0, -- 0: TCP, 1: ping
|
||||
["loggerLevel"] = "info",
|
||||
--["loggerCmd"] = "logger",
|
||||
["modules"] = {},
|
||||
}
|
||||
Config.configDir = "/etc/" .. Config.appName
|
||||
Config.modulesDir = "/usr/lib/" .. Config.appName
|
||||
Config.upScript = Config.configDir .. "/" .. "up-script"
|
||||
Config.downScript = Config.configDir .. "/" .. "down-script"
|
||||
Config.runScript = Config.configDir .. "/" .. "run-script"
|
||||
@@ -59,30 +61,28 @@ if not nixio then
|
||||
end
|
||||
|
||||
local uci = prequire("uci")
|
||||
if uci then
|
||||
|
||||
-- Load settings from UCI
|
||||
|
||||
local cursor = uci.cursor()
|
||||
Config.mode = cursor:get(Config.appName, "config", "mode")
|
||||
Config.enableLogger = cursor:get(Config.appName, "config", "enable_logger")
|
||||
Config.enableUpScript = cursor:get(Config.appName, "config", "enable_up_script")
|
||||
Config.enableDownScript = cursor:get(Config.appName, "config", "enable_down_script")
|
||||
Config.enableRunScript = cursor:get(Config.appName, "config", "enable_run_script")
|
||||
Config.intervalUp = tonumber(cursor:get(Config.appName, "config", "interval_up"))
|
||||
Config.intervalDown = tonumber(cursor:get(Config.appName, "config", "interval_down"))
|
||||
Config.hosts = cursor:get(Config.appName, "config", "hosts")
|
||||
Config.checkType = tonumber(cursor:get(Config.appName, "config", "check_type"))
|
||||
Config.connectionAttempts = tonumber(cursor:get(Config.appName, "config", "connection_attempts"))
|
||||
Config.connectionTimeout = tonumber(cursor:get(Config.appName, "config", "connection_timeout"))
|
||||
Config.UIConnectionAttempts = tonumber(cursor:get(Config.appName, "config", "ui_connection_attempts"))
|
||||
Config.UIConnectionTimeout = tonumber(cursor:get(Config.appName, "config", "ui_connection_timeout"))
|
||||
Config.tcpPort = tonumber(cursor:get(Config.appName, "config", "tcp_port"))
|
||||
|
||||
else
|
||||
io.stderr:write("libuci-lua does not exists! The default settings will be used...\n")
|
||||
if not uci then
|
||||
error("You need to install libuci-lua...")
|
||||
end
|
||||
|
||||
-- Load settings from UCI
|
||||
|
||||
local uciCursor = uci.cursor()
|
||||
Config.mode = uciCursor:get(Config.appName, "config", "mode")
|
||||
Config.hosts = uciCursor:get(Config.appName, "config", "hosts")
|
||||
Config.checkType = tonumber(uciCursor:get(Config.appName, "config", "check_type"))
|
||||
Config.tcpPort = tonumber(uciCursor:get(Config.appName, "config", "tcp_port"))
|
||||
Config.UIConnectionAttempts = tonumber(uciCursor:get(Config.appName, "ui_config", "connection_attempts"))
|
||||
Config.UIConnectionTimeout = tonumber(uciCursor:get(Config.appName, "ui_config", "connection_timeout"))
|
||||
Config.enableLogger = uciCursor:get(Config.appName, "service_config", "enable_logger")
|
||||
Config.enableUpScript = uciCursor:get(Config.appName, "service_config", "enable_up_script")
|
||||
Config.enableDownScript = uciCursor:get(Config.appName, "service_config", "enable_down_script")
|
||||
Config.enableRunScript = uciCursor:get(Config.appName, "service_config", "enable_run_script")
|
||||
Config.intervalUp = tonumber(uciCursor:get(Config.appName, "service_config", "interval_up"))
|
||||
Config.intervalDown = tonumber(uciCursor:get(Config.appName, "service_config", "interval_down"))
|
||||
Config.connectionAttempts = tonumber(uciCursor:get(Config.appName, "service_config", "connection_attempts"))
|
||||
Config.connectionTimeout = tonumber(uciCursor:get(Config.appName, "service_config", "connection_timeout"))
|
||||
|
||||
local function writeValueToFile(filePath, str)
|
||||
local retValue = false
|
||||
local fh = io.open(filePath, "w")
|
||||
@@ -114,6 +114,28 @@ local function writeLogMessage(level, msg)
|
||||
end
|
||||
end
|
||||
|
||||
local function loadModules()
|
||||
package.path = string.format("%s;%s/?.lua", package.path, Config.modulesDir)
|
||||
Config.modules = {}
|
||||
uciCursor:foreach(
|
||||
Config.appName,
|
||||
"module",
|
||||
function(s)
|
||||
local mod_name = s[".name"]
|
||||
if mod_name and s.enabled == "1" then
|
||||
local m = prequire(mod_name)
|
||||
if m then
|
||||
m.syslog = writeLogMessage
|
||||
m.writeValue = writeValueToFile
|
||||
m.readValue = readValueFromFile
|
||||
m:init(s)
|
||||
Config.modules[#Config.modules + 1] = m
|
||||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
local function runExternalScript(scriptPath, inetStat)
|
||||
if inetStat == nil then
|
||||
inetStat = ""
|
||||
@@ -164,7 +186,7 @@ local function tcpConnectToHost(host, port)
|
||||
end
|
||||
|
||||
local function checkHosts()
|
||||
local checkFunc = (Config.checkType == 1) and tcpConnectToHost or pingHost
|
||||
local checkFunc = (Config.checkType == 1) and pingHost or tcpConnectToHost
|
||||
local retCode = 1
|
||||
for k, v in ipairs(Config.parsedHosts) do
|
||||
for i = 1, Config.connectionAttempts do
|
||||
@@ -181,20 +203,20 @@ local function checkHosts()
|
||||
end
|
||||
|
||||
local function main()
|
||||
local last_status
|
||||
local current_status
|
||||
local lastStatus
|
||||
local currentStatus
|
||||
local interval = Config.intervalUp
|
||||
|
||||
while true do
|
||||
current_status = checkHosts()
|
||||
currentStatus = checkHosts()
|
||||
if not nixio.fs.access(Config.statusFile, "r") then
|
||||
writeValueToFile(Config.statusFile, current_status)
|
||||
writeValueToFile(Config.statusFile, currentStatus)
|
||||
end
|
||||
|
||||
if current_status == 0 then
|
||||
if currentStatus == 0 then
|
||||
interval = Config.intervalUp
|
||||
if last_status ~= nil and current_status ~= last_status then
|
||||
writeValueToFile(Config.statusFile, current_status)
|
||||
if lastStatus ~= nil and currentStatus ~= lastStatus then
|
||||
writeValueToFile(Config.statusFile, currentStatus)
|
||||
writeLogMessage("notice", "internet connected")
|
||||
if Config.enableUpScript == "1" then
|
||||
runExternalScript(Config.upScript)
|
||||
@@ -202,8 +224,8 @@ local function main()
|
||||
end
|
||||
else
|
||||
interval = Config.intervalDown
|
||||
if last_status ~= nil and current_status ~= last_status then
|
||||
writeValueToFile(Config.statusFile, current_status)
|
||||
if lastStatus ~= nil and currentStatus ~= lastStatus then
|
||||
writeValueToFile(Config.statusFile, currentStatus)
|
||||
writeLogMessage("notice", "internet disconnected")
|
||||
if Config.enableDownScript == "1" then
|
||||
runExternalScript(Config.downScript)
|
||||
@@ -211,11 +233,15 @@ local function main()
|
||||
end
|
||||
end
|
||||
|
||||
if Config.enableRunScript == "1" then
|
||||
runExternalScript(Config.runScript, current_status)
|
||||
for _, e in ipairs(Config.modules) do
|
||||
e:run(currentStatus, lastStatus)
|
||||
end
|
||||
|
||||
last_status = current_status
|
||||
if Config.enableRunScript == "1" then
|
||||
runExternalScript(Config.runScript, currentStatus)
|
||||
end
|
||||
|
||||
lastStatus = currentStatus
|
||||
nixio.nanosleep(interval)
|
||||
end
|
||||
end
|
||||
@@ -312,6 +338,19 @@ local function run()
|
||||
local pidValue = nixio.getpid()
|
||||
writeValueToFile(Config.pidFile, pidValue)
|
||||
writeLogMessage("info", "started")
|
||||
loadModules()
|
||||
|
||||
-- Loaded modules
|
||||
local modules = {}
|
||||
for _, v in ipairs(Config.modules) do
|
||||
modules[#modules + 1] = string.format("%s", v.name)
|
||||
end
|
||||
if #modules > 0 then
|
||||
writeLogMessage(
|
||||
"info", string.format("Loaded modules: %s", table.concat(modules, ", "))
|
||||
)
|
||||
end
|
||||
|
||||
main()
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
|
||||
local nixio = require("nixio")
|
||||
|
||||
local Module = {
|
||||
name = "mod_led_control",
|
||||
sysLedsDir = "/sys/class/leds",
|
||||
syslog = function(level, msg) return true end,
|
||||
writeValue = function(filePath, str) return false end,
|
||||
readValue = function(filePath) return nil end,
|
||||
ledName = nil,
|
||||
_enabled = false,
|
||||
_ledDir = nil,
|
||||
_ledMaxBrightnessFile = nil,
|
||||
_ledBrightnessFile = nil,
|
||||
_ledMaxBrightness = nil,
|
||||
}
|
||||
|
||||
function Module:resetLeds()
|
||||
local dir = nixio.fs.dir(self.sysLedsDir)
|
||||
if not dir then
|
||||
return
|
||||
end
|
||||
for led in dir do
|
||||
local brightness = string.format("%s/%s/brightness", self.sysLedsDir, led)
|
||||
if nixio.fs.access(brightness, "w") then
|
||||
self.writeValue(brightness, 0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Module:init(t)
|
||||
self.ledName = t.led_name
|
||||
if not self.ledName then
|
||||
return
|
||||
end
|
||||
self._ledDir = string.format("%s/%s", self.sysLedsDir, self.ledName)
|
||||
self._ledMaxBrightnessFile = self._ledDir .. "/max_brightness"
|
||||
self._ledBrightnessFile = self._ledDir .. "/brightness"
|
||||
self._ledMaxBrightness = self.readValue(self._ledMaxBrightnessFile) or 1
|
||||
if (not nixio.fs.access(self._ledDir, "r") or
|
||||
not nixio.fs.access(self._ledBrightnessFile, "r", "w")) then
|
||||
self._enabled = false
|
||||
self.syslog("warning", string.format('%s: "%s" is not available', self.name, self.ledName))
|
||||
else
|
||||
self._enabled = true
|
||||
-- Reset all LEDs
|
||||
--self:resetLeds()
|
||||
end
|
||||
end
|
||||
|
||||
function Module:getCurrentState()
|
||||
local state = self.readValue(self._ledBrightnessFile)
|
||||
if state and tonumber(state) > 0 then
|
||||
return tonumber(state)
|
||||
end
|
||||
end
|
||||
|
||||
function Module:on()
|
||||
self.writeValue(self._ledBrightnessFile, self._ledMaxBrightness)
|
||||
end
|
||||
|
||||
function Module:off()
|
||||
self.writeValue(self._ledBrightnessFile, 0)
|
||||
end
|
||||
|
||||
function Module:run(currentStatus, lastStatus)
|
||||
if not self._enabled then
|
||||
return
|
||||
end
|
||||
if currentStatus == 0 then
|
||||
if not self:getCurrentState() then
|
||||
self:on()
|
||||
end
|
||||
else
|
||||
if self:getCurrentState() then
|
||||
self:off()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return Module
|
||||
Reference in New Issue
Block a user