diff --git a/nsense.nimble b/nsense.nimble index 3a1642e..81495ec 100644 --- a/nsense.nimble +++ b/nsense.nimble @@ -3,7 +3,7 @@ import tables import strutils # Package -version = "0.6.5" +version = "0.6.6" author = "Skynet Core" description = "A new awesome nimble package" license = "MIT" @@ -25,7 +25,7 @@ task nsense, "Run nsense service": task static, "Build static musl binaries": let dir = getCurrentDir() exec "docker run --rm -v " & dir & - ":/home/nim/nord-sense smartcoder/nim:v1.4 bash -c '" & + ":/home/nim/nord-sense smartcoder/nim:v1.2 bash -c '" & "sudo apk update && sudo apk upgrade && sudo apk add sqlite-static &&" & " cd /home/nim/nord-sense && nimble build --gcc.exe:gcc --gcc.linkerexe:gcc" & " --passL:-static --dynlibOverride:libsqlite3.so --passL:/usr/lib/libsqlite3.a -d:release --opt:size -y'" @@ -54,5 +54,5 @@ task purge, "Removing service from system": exec selfExe() & " " & dir & "/res/uninstall/" & hostOS & ".nims" task clean, "clean artifacts": - exec "rm -rf nsensepkg nsense tests/test1 here.pid" + exec "sudo rm -rf settings.db run etc nsensepkg nsense usr tests/test1 here.pid" echo "Done" diff --git a/res/install/linux.sh b/res/install/linux.sh index 909bc5f..757fbd1 100755 --- a/res/install/linux.sh +++ b/res/install/linux.sh @@ -31,7 +31,7 @@ bin=${executable##*/} if [ "$bin" = "systemd" ]; then - if systemctl status nsense 2>&1 1>/dev/null; then + if systemctl is-active nsense 2>&1 1>/dev/null; then systemctl stop nsense fi diff --git a/res/service/systemd/nsense.service b/res/service/systemd/nsense.service index 877804a..2f8824d 100644 --- a/res/service/systemd/nsense.service +++ b/res/service/systemd/nsense.service @@ -4,7 +4,7 @@ Description=Acer nsense service [Service] Type=simple WorkingDirectory=/opt/nsense -ExecStart=/opt/nsense/bin/nsense -p /run/nsense.pid -l error -f +ExecStart=/opt/nsense/bin/nsense -p /run/nsense.pid -l debug -f ExecStop=sh -c 'kill -s TERM `cat /opt/nsense/run/nsense.pid`' ExecReload=sh -c 'kill -s HUP `cat /opt/nsense/run/nsense.pid`' PIDFile=/run/nsense.pid diff --git a/res/uninstall/linux.sh b/res/uninstall/linux.sh index 5b4eb3b..16b4863 100755 --- a/res/uninstall/linux.sh +++ b/res/uninstall/linux.sh @@ -5,15 +5,11 @@ set -x echo "nsense Linux uninstaller script" -if [ -d /opt/nsense ]; then - rm -rf /opt/nsense -fi - executable=$(readlink /proc/1/exe) bin=${executable##*/} if [ "$bin" = "systemd" ]; then - if systemctl status nsense 2>&1 1>/dev/null; then + if systemctl is-active nsense 2>&1 1>/dev/null; then systemctl stop nsense fi systemctl disable nsense @@ -26,4 +22,8 @@ else exit 1 fi +if [ -d /opt/nsense ]; then + rm -rf /opt/nsense +fi + systemctl daemon-reload diff --git a/src/nsense.nim b/src/nsense.nim index 3942970..c4b985e 100644 --- a/src/nsense.nim +++ b/src/nsense.nim @@ -50,6 +50,7 @@ when isMainModule: if not config.ready(): # check if config for current model exists or /etc/config(.yml*) exists + log.info(&"resolving config {configFile} for {model}...") configFile = resolveConfig(wd, model, configFile) config = if configFile.len > 0: loadConfig(configFile) @@ -69,7 +70,13 @@ when isMainModule: timerFd = sel.registerTimer(int(config.pollTickMs), oneshot = false,0) # once per second zones = initZones(config.zones) - let + let + switchToAuto = proc(): void = + if config.ready(): + log.info("reset to auto mode...") + for (i, zone) in config.zones.pairs: + for (j, fan) in zone.fans.pairs: + ctrl.send(fan.address, fan.auto) loadAndReset = proc(): void = if configFile.len > 0: config = loadConfig(configFile) @@ -86,6 +93,7 @@ when isMainModule: let handlerSwitch = { hupFd: proc(timeStr: string): void = + switchToAuto() log.info(&"{timeStr}: SIGHUP received. Updating configuration ...") loadAndReset() , @@ -94,11 +102,13 @@ when isMainModule: loadAndReset() , pauseFd: proc(timeStr: string): void = + switchToAuto() unregisterTimerAndReset() log.info(&"{timeStr}: SIGSTOP received. Going idle ...") , timerFd: proc(timeStr: string): void = if not config.ready(): + log.info(&"{timeStr}: config wasn't loaded correctly...") unregisterTimerAndReset() return ## iterate through zones and compare temp with level bounds @@ -126,13 +136,15 @@ when isMainModule: state.reset() , termFd: proc(timeStr: string): void = + switchToAuto() unregisterTimerAndReset() - log.info("{timeStr}: SIGTERM received. Quiting ... ") + log.info(&"{timeStr}: SIGTERM received. Quiting ... ") quit(0) , killFd: proc(timeStr: string): void = + switchToAuto() unregisterTimerAndReset() - log.info("{timeStr}: SIGKILL received. Quiting ... ") + log.info(&"{timeStr}: SIGKILL received. Quiting ... ") quit(1) , }.toTable diff --git a/src/nsensepkg/misc.nim b/src/nsensepkg/misc.nim index 087117b..574a238 100644 --- a/src/nsensepkg/misc.nim +++ b/src/nsensepkg/misc.nim @@ -59,18 +59,18 @@ proc resolveConfig*(wd: string, model: string, configFile: string): string = except OSError: quit("failed to create '" & configDir & "' :" & getCurrentExceptionMsg(),-1) # try to find something from ready to use shared configs - let - match = model.multiReplace(replacements).toUpper() - for file in walkDir(joinPath(wd, shareConfigsDir)): - let configFile = file.path.splitFile.name. - split('.')[0].multiReplace(replacements).toUpper() - if configFile == match: - let dest = joinPath(configDir, configFileName) - try: - copyFile(file.path, dest) - return dest - except OSError: - quit("failed to copy '" & configFile & "' into '" & dest & "': " & getCurrentExceptionMsg(),-1) + let + match = model.multiReplace(replacements).toUpper() + for file in walkDir(joinPath(wd, shareConfigsDir)): + let configFile = file.path.splitFile.name. + split('.')[0].multiReplace(replacements).toUpper() + if configFile == match: + let dest = joinPath(configDir, configFileName) + try: + copyFile(file.path, dest) + return dest + except OSError: + quit("failed to copy '" & configFile & "' into '" & dest & "': " & getCurrentExceptionMsg(),-1) return "" proc configFilePath*(wd: string): string =