2022-02-06 21:10:06 +01:00
|
|
|
import std/os
|
|
|
|
import std/re
|
|
|
|
import std/math
|
|
|
|
import strutils
|
|
|
|
import i3bar_base
|
|
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
proc getObject(temp: int): i3barData =
|
|
|
|
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 data = i3barData(
|
|
|
|
full_text: text,
|
|
|
|
color: foreground,
|
|
|
|
border: colour
|
|
|
|
)
|
|
|
|
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
|
2022-02-06 21:10:06 +01:00
|
|
|
sleep(10000)
|
|
|
|
|
|
|
|
main()
|