2015-09-05 18:40:13 +02:00
|
|
|
#!/usr/bin/lua
|
|
|
|
|
|
|
|
local socket = require("socket")
|
|
|
|
|
|
|
|
local devicepath = "/sys/class/ddbridge/ddbridge0"
|
|
|
|
local interval = 30
|
|
|
|
local nValues = 2880
|
|
|
|
local LogFile = "/tmp/Temperatur.log"
|
|
|
|
local HighTemp = 50
|
|
|
|
local LowTemp = 45
|
|
|
|
|
2018-09-26 13:05:45 +02:00
|
|
|
function ReadAttribute(attribute)
|
|
|
|
local value = 0
|
|
|
|
local ddbridge = io.open(devicepath.."/"..attribute,"r");
|
|
|
|
if ddbridge then
|
|
|
|
value = ddbridge:read("*l")
|
|
|
|
ddbridge:close()
|
|
|
|
end
|
|
|
|
return value
|
|
|
|
end
|
|
|
|
|
2015-09-05 18:40:13 +02:00
|
|
|
function ReadTemp(sensor)
|
|
|
|
local temp = 0
|
2018-09-26 13:05:45 +02:00
|
|
|
local file;
|
|
|
|
|
|
|
|
if sensor > 3 then
|
|
|
|
sensor = sensor - 4
|
|
|
|
if sensor == 0 then
|
|
|
|
file = io.open(devicepath.."/temp")
|
|
|
|
else
|
|
|
|
file = io.open(devicepath.."/templ"..sensor)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
file = io.open(devicepath.."/temp"..sensor)
|
|
|
|
end
|
|
|
|
|
2015-09-05 18:40:13 +02:00
|
|
|
if file then
|
|
|
|
temp = file:read()
|
2018-09-26 13:05:45 +02:00
|
|
|
if temp == "no sensor" then
|
2015-09-05 18:40:13 +02:00
|
|
|
temp = 0
|
|
|
|
else
|
|
|
|
temp = math.floor(tonumber(temp)/1000)
|
|
|
|
end
|
|
|
|
file:close()
|
|
|
|
end
|
|
|
|
return temp
|
|
|
|
end
|
|
|
|
|
|
|
|
function sleep(sec)
|
|
|
|
socket.select(nil,nil,sec)
|
|
|
|
end
|
|
|
|
|
|
|
|
function SetFan(value)
|
|
|
|
local gpio = io.open("/sys/class/gpio/gpio106/value","w");
|
|
|
|
if gpio then
|
|
|
|
gpio:write(value)
|
|
|
|
gpio:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local gpio = io.open("/sys/class/gpio/export","w");
|
|
|
|
if gpio then
|
|
|
|
gpio:write("106")
|
|
|
|
gpio:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
local NumSensors = 0
|
|
|
|
local i
|
|
|
|
local temps
|
|
|
|
local temp
|
|
|
|
local Sensor = {}
|
|
|
|
|
|
|
|
local TempList = {}
|
|
|
|
local StartIndex = 1
|
|
|
|
local count = interval
|
2015-09-09 19:08:06 +02:00
|
|
|
local fanstate = -1
|
|
|
|
|
2016-11-22 12:11:25 +01:00
|
|
|
sleep(30)
|
|
|
|
|
2018-09-26 13:05:45 +02:00
|
|
|
local devid = ReadAttribute("devid0")
|
|
|
|
if devid == "0307dd01" then
|
|
|
|
fanstate = 1
|
|
|
|
local devid1 = ReadAttribute("devid1")
|
|
|
|
if devid1 == "0009dd01" or devid1 == "000add01" then
|
|
|
|
fanstate = -2
|
|
|
|
else
|
|
|
|
local fs = tonumber(ReadAttribute("fanspeed1"))
|
|
|
|
if fs > 0 and fs < 17000 then
|
|
|
|
fanstate = -2
|
2016-08-31 17:57:15 +02:00
|
|
|
end
|
2018-09-26 13:05:45 +02:00
|
|
|
end
|
|
|
|
end
|
2015-09-05 18:40:13 +02:00
|
|
|
|
2018-09-26 13:05:45 +02:00
|
|
|
for i = 0,8,1 do
|
2015-09-05 18:40:13 +02:00
|
|
|
temp = ReadTemp(i)
|
2018-09-26 13:05:45 +02:00
|
|
|
if temp > 0 then
|
2015-09-05 18:40:13 +02:00
|
|
|
Sensor[NumSensors] = i
|
|
|
|
NumSensors = NumSensors + 1
|
|
|
|
end
|
|
|
|
end
|
2015-09-24 16:20:44 +02:00
|
|
|
|
|
|
|
local fh = io.open(LogFile,"w")
|
|
|
|
if fh then
|
|
|
|
fh:write(NumSensors..","..interval..","..fanstate.."\n")
|
|
|
|
fh:close()
|
|
|
|
end
|
|
|
|
|
2015-09-05 18:40:13 +02:00
|
|
|
if NumSensors == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
while true do
|
2016-11-22 12:11:25 +01:00
|
|
|
sleep(1)
|
2015-09-05 18:40:13 +02:00
|
|
|
temps = ""
|
|
|
|
for i = 0, NumSensors - 1, 1 do
|
|
|
|
temp = ReadTemp(Sensor[i])
|
2018-09-26 13:05:45 +02:00
|
|
|
|
2015-09-05 18:40:13 +02:00
|
|
|
if temp == 0 then
|
|
|
|
print(" fanControl Error ")
|
|
|
|
return
|
|
|
|
end
|
2018-09-26 13:05:45 +02:00
|
|
|
|
|
|
|
if i == 0 then
|
|
|
|
temps = temp
|
2015-09-05 18:40:13 +02:00
|
|
|
else
|
|
|
|
temps = temps .. "," .. temp
|
|
|
|
end
|
2018-09-26 13:05:45 +02:00
|
|
|
|
2015-09-05 18:40:13 +02:00
|
|
|
if fanstate == 0 and temp >= HighTemp then
|
|
|
|
SetFan(1)
|
|
|
|
fanstate = 1
|
|
|
|
elseif fanstate == 1 and temp < LowTemp then
|
|
|
|
SetFan(0)
|
|
|
|
fanstate = 0
|
|
|
|
end
|
2018-09-26 13:05:45 +02:00
|
|
|
|
2015-09-05 18:40:13 +02:00
|
|
|
end
|
2018-09-26 13:05:45 +02:00
|
|
|
|
|
|
|
count = count - 1
|
2015-09-05 18:40:13 +02:00
|
|
|
if count == 0 then
|
|
|
|
count = interval
|
2018-09-26 13:05:45 +02:00
|
|
|
|
2015-09-05 18:40:13 +02:00
|
|
|
if #TempList < nValues then
|
2018-09-26 13:05:45 +02:00
|
|
|
TempList[#TempList+1] = temps
|
2015-09-05 18:40:13 +02:00
|
|
|
else
|
|
|
|
TempList[StartIndex] = temps
|
|
|
|
StartIndex = StartIndex + 1
|
|
|
|
if StartIndex > nValues then StartIndex = 1 end
|
|
|
|
end
|
2018-09-26 13:05:45 +02:00
|
|
|
|
2015-09-05 18:40:13 +02:00
|
|
|
TmpLogFile = os.tmpname()
|
2018-09-26 13:05:45 +02:00
|
|
|
|
2015-09-24 16:20:44 +02:00
|
|
|
fh = io.open(TmpLogFile,"w")
|
2015-09-05 18:40:13 +02:00
|
|
|
if fh then
|
|
|
|
fh:write(NumSensors..","..interval..","..fanstate.."\n")
|
2018-09-26 13:05:45 +02:00
|
|
|
|
2015-09-05 18:40:13 +02:00
|
|
|
for i = StartIndex-1, 1, -1 do
|
|
|
|
fh:write(TempList[i].."\n")
|
|
|
|
end
|
2018-09-26 13:05:45 +02:00
|
|
|
|
2015-09-05 18:40:13 +02:00
|
|
|
for i = #TempList, StartIndex, -1 do
|
|
|
|
fh:write(TempList[i].."\n")
|
|
|
|
end
|
2018-09-26 13:05:45 +02:00
|
|
|
|
2015-09-05 18:40:13 +02:00
|
|
|
fh:close()
|
|
|
|
os.remove(LogFile)
|
|
|
|
os.rename(TmpLogFile,LogFile)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
collectgarbage()
|
|
|
|
end
|