wmtools/temperature.nim

69 lines
1.6 KiB
Nim
Raw Normal View History

2022-02-06 21:10:06 +01:00
import std/os
import std/re
import std/math
import strutils
2022-04-28 21:58:40 +02:00
import base
2022-02-06 21:10:06 +01:00
proc getThermalZones(): seq[string] =
var zones: seq[string] = @[]
let dirname = re("thermal_zone[\\d]+")
for file in walkDir("/sys/class/thermal/"):
if contains(file.path,dirname):
let state = readFile(file.path & "/mode")
if contains(state,re"enabled"):
zones.add(file.path)
return zones
proc getTemp(zone: string): int =
let temp = strip(readFile(zone & "/temp"))
return parseInt(temp)
proc getAverageTemp(zones: seq[string]): int =
var temps: int = 0
for zone in zones:
let temp = getTemp(zone)
temps += temp
let avgtemp = ceil((temps / len(zones))/1000)
return toInt(round(avgtemp,0))
2022-04-28 21:58:40 +02:00
proc getObject(temp: int): Info =
2022-02-06 21:10:06 +01:00
var icon = ""
var colour = foreground
case temp:
of 40..59:
colour = yellow
icon = ""
of 60.. 200:
colour = red
icon = ""
else:
colour = green
icon = ""
let text = "<span foreground='" & colour & "'>" & icon & "</span> " & $temp & "°C"
let main_text = icon & " " & $temp & "°C"
2022-04-28 21:58:40 +02:00
var data = newInfo()
data.title = "Temp : "
data.full_text = main_text
data.html_text = text
data.color = foreground
data.border = colour
2022-04-28 21:58:40 +02:00
data.selected_background = colour
data.selected_color = black
2022-02-06 21:10:06 +01:00
return data
proc main() =
let zones = getThermalZones()
2022-02-12 16:19:43 +01:00
var last_temp = 0
2022-02-06 21:10:06 +01:00
while true:
let temp = getAverageTemp(zones)
2022-02-12 16:19:43 +01:00
if temp != last_temp:
let data = getObject(temp)
outputJSON(data)
last_temp = temp
if stoploop:
break
2022-02-06 21:10:06 +01:00
sleep(10000)
main()