# -*- coding: utf-8 -*- from twisted.web import resource, http from globals import * from plugin import * from Sensors import sensors from __init__ import _ from Components.config import configfile, config import os import datetime ######################################################## class FC2web(resource.Resource): title = "FanControl2 Webinterface" isLeaf = False def render(self, req): req.setHeader('Content-type', 'text/html') req.setHeader('charset', 'UTF-8') """ rendering server response """ command = req.args.get("cmd",None) html = "" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "Fan Control 2 - Info\n" html += "" html += "\n" html += "
\n" html += "
\n" html += "

\n" html += "
Fan Control 2 - Info

\n" html += "
\n" html += "

" html += BoxStatus() if os.path.exists(config.plugins.FanControl.LogPath.value + "FC2data.csv"): html += "\n" html += "

\n" html += "\n" html += "\n" html += "\n" % (_("Temperature"),FC2werte[0]) html += "\n" % (_("Speed"),FC2werte[1]) html += "\n" % (_("Voltage"),FC2werte[2]) html += "\n" % FC2werte[3] html += "\n" html += "
%s: %4.1f C%s: %4d rpm%s: %03dPWM: %03d
\n" html += "\n" html += "\n" html += "\n" % _("Sensors") templist = sensors.getSensorsList(sensors.TYPE_TEMPERATURE) tempcount = len(templist) for count in range(tempcount): if sensors.getSensorName(count) == "undefined": N = TempName[count] else: N = sensors.getSensorName(count) html += "" % (N,sensors.getSensorValue(count)) if FC2werte[4] > 0: html += "\n" % FC2werte[4] html += "\n" html += "
%s C%dHDD %d
\n" html += "\n" html += "\n" for count in range(0,12): tmp = ("
-" if FC2stunde[count] == "-" else FC2stunde[count]) html +="\n" % (count,tmp) html += "\n" for count in range(12,24): tmp = ("
-" if FC2stunde[count] == "-" else FC2stunde[count]) html +="\n" % (count,tmp) html += "

%02d:00
%s

%02d:00
%s

\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" % Version html += "\n" % (config.plugins.FanControl.temp.value,config.plugins.FanControl.tempmax.value) html += "\n" % (config.plugins.FanControl.minRPM.value,config.plugins.FanControl.maxRPM.value) html += "\n" html += "
Version: %s Settings: %s-%s C%s-%s rpm
\n" html += "\n" html += "\n" html += "
\n" return html ########################################################## class FC2webLog(resource.Resource): title = "FanControl2 Webinterface" isLeaf = True def render(self, req): command = req.args.get("cmd",None) html = "" if command is None: req.setHeader('Content-type', 'text/html') req.setHeader('charset', 'UTF-8') """ rendering server response """ html = "" html += "" html += "" html += "" html += "" html += "" html += "" html += "" html += "Fan Control 2 - Logging" html += "" html += "\n" html += "
\n" html += "

\n" html += "
Fan Control 2 - Logging

\n" html += "
\n" html += "

" html += BoxStatus() if os.path.exists(config.plugins.FanControl.LogPath.value + "FC2data.csv"): html += "\n" html += "

\n" html += "" html += "
Data Logging " if config.plugins.FanControl.EnableDataLog.value: html += "%s" % _("active") html += "
" html += "" html += "" % _("turn off") html += "
" else: html += "%s" % _("not active") html += "
" html += "" html += "" % _("turn on") html += "
" if os.path.exists(config.plugins.FanControl.LogPath.value + "FC2data.csv"): s = os.stat(config.plugins.FanControl.LogPath.value + "FC2data.csv") if int(s.st_size/1024) == 0: html += "
" + _("Filesize : %d %sByte") % (int(s.st_size), "") else: html += "
" + _("Filesize : %d %sByte") % (int(s.st_size/1024), "k") s = os.statvfs(config.plugins.FanControl.LogPath.value) html += "
" + _("Disk free : %d MByte") % (int(s.f_bsize * s.f_bavail / 1024 / 1024)) html += "
" html += "" html += "" % _("Download") html += "
" html += "
" html += "" html += "" % _("Delete") html += "
" html += "
" html += "" html += "" % _("Delete older 48h") html += "
" else: html += "
" + _("File %s does not exists") % "FC2data.csv" html += "
Event Logging " if config.plugins.FanControl.EnableEventLog.value: html += "%s" % _("active") html += "
" html += "" html += "" % _("turn off") html += "
" else: html += "%s" % _("not active") html += "
" html += "" html += "" % _("turn on") html += "
" if os.path.exists(config.plugins.FanControl.LogPath.value + "FC2events.txt"): s = os.stat(config.plugins.FanControl.LogPath.value + "FC2events.txt") if int(s.st_size/1024) == 0: html += "
" + _("Filesize : %d %sByte") % (int(s.st_size), "") else: html += "
" + _("Filesize : %d %sByte") % (int(s.st_size/1024), "k") s = os.statvfs(config.plugins.FanControl.LogPath.value) html += "
" + _("Disk free : %d MByte") % (int(s.f_bsize * s.f_bavail / 1024 / 1024)) html += "
" html += "" html += "" % _("Download") html += "
" html += "
" html += "" html += "" % _("Delete") html += "
" # html += "
" # html += "" # html += "" % _("Delete older 48h") # html += "
" else: html += "
" + _("File %s does not exists") % "FC2events.txt" html += "
" html += _("Logging-Path: %s") % config.plugins.FanControl.LogPath.value html += "
" + _("Auto-Delete older %s Days") % config.plugins.FanControl.DeleteData.value html += "" elif command[0] == "data": req.setResponseCode(http.OK) # req.setHeader('Content-type', 'application/octet-stream') req.setHeader('Content-type', 'application/vnd.ms-excel') req.setHeader('Content-Disposition', 'attachment;filename=FC2data.csv') req.setHeader('Content-Length', os.stat(config.plugins.FanControl.LogPath.value + "FC2data.csv").st_size) # req.setHeader('Content-Disposition', 'inline;filename=FC2data.csv') req.setHeader('charset', 'UTF-8') f = open(config.plugins.FanControl.LogPath.value + "FC2data.csv","r") html = f.read() f.close() elif command[0] == "datadel": if os.path.exists(config.plugins.FanControl.LogPath.value + "FC2data.csv"): os.remove(config.plugins.FanControl.LogPath.value + "FC2data.csv") CreateDataHead() html = LogRefresh() elif command[0] == "datadel48h": if os.path.exists(config.plugins.FanControl.LogPath.value + "FC2data.csv") and os.stat(config.plugins.FanControl.LogPath.value + "FC2data.csv").st_size > 10000: f = open(config.plugins.FanControl.LogPath.value + "FC2data.csv","a") s = f.tell() f.close() f = open(config.plugins.FanControl.LogPath.value + "FC2data.csv","r") f.seek(s-100) line = f.readline() line = f.readline() DT = line.split(";") DT = DT[0].split(" ") DD = DT[0].split(".") DD48h = datetime.date(int(DD[0]),int(DD[1]),int(DD[2])) - datetime.timedelta(2) Dfind = "%04d.%02d.%02d %s" % (DD48h.year,DD48h.month,DD48h.day,DT[1]) f.seek(0) line = f.readline() fw = open(config.plugins.FanControl.LogPath.value + "FC2data.csv.tmp","w") fw.write(HeadLine) for line in f.readlines(): DT = line.split(";") if DT[0] > Dfind: fw.write(line) f.close() fw.close() if os.path.exists(config.plugins.FanControl.LogPath.value + "FC2data.csv"): os.remove(config.plugins.FanControl.LogPath.value + "FC2data.csv") if os.path.exists(config.plugins.FanControl.LogPath.value + "FC2data.csv.tmp"): os.rename(config.plugins.FanControl.LogPath.value + "FC2data.csv.tmp",config.plugins.FanControl.LogPath.value + "FC2data.csv") html = LogRefresh() elif command[0] == "dataenable": config.plugins.FanControl.EnableDataLog.value = not config.plugins.FanControl.EnableDataLog.value CreateDataHead() config.plugins.FanControl.EnableDataLog.save() html = LogRefresh() elif command[0] == "events": req.setResponseCode(http.OK) req.setHeader('Content-type', 'application/octet-stream') req.setHeader('Content-Disposition', 'attachment;filename=FC2events.txt') req.setHeader('Content-Length', os.stat(config.plugins.FanControl.LogPath.value + "FC2events.txt").st_size) # req.setHeader('Content-Disposition', 'inline;filename=FC2events.txt') req.setHeader('charset', 'UTF-8') f = open(config.plugins.FanControl.LogPath.value + "FC2events.txt","r") html = f.read() f.close() elif command[0] == "eventsdel": if os.path.exists(config.plugins.FanControl.LogPath.value + "FC2events.txt"): os.remove(config.plugins.FanControl.LogPath.value + "FC2events.txt") html = LogRefresh() elif command[0] == "eventsdel48h": if os.path.exists(config.plugins.FanControl.LogPath.value + "FC2events.txt"): f = open(config.plugins.FanControl.LogPath.value + "FC2events.txt","a") s = f.tell() f.close() f = open(config.plugins.FanControl.LogPath.value + "FC2events.txt","r") f.seek(s-100) line = f.readline() line = f.readline() DT = line.split(";") DT = DT[0].split(" ") DD = DT[0].split(".") DD48h = datetime.date(int(DD[0]),int(DD[1]),int(DD[2])) - datetime.timedelta(2) Dfind = "%04d.%02d.%02d %s" % (DD48h.year,DD48h.month,DD48h.day,DT[1]) f.seek(0) line = f.readline() fw = open(config.plugins.FanControl.LogPath.value + "FC2events.txt.tmp","w") fw.write(HeadLine) for line in f.readlines(): DT = line.split(";") if DT[0] > Dfind: fw.write(line) f.close() fw.close() if os.path.exists(config.plugins.FanControl.LogPath.value + "FC2events.txt"): os.remove(config.plugins.FanControl.LogPath.value + "FC2events.txt") if os.path.exists(config.plugins.FanControl.LogPath.value + "FC2events.txt.tmp"): os.rename(config.plugins.FanControl.LogPath.value + "FC2events.txt.tmp",config.plugins.FanControl.LogPath.value + "FC2events.txt") html = LogRefresh() elif command[0] == "eventsenable": config.plugins.FanControl.EnableEventLog.value = not config.plugins.FanControl.EnableEventLog.value config.plugins.FanControl.EnableEventLog.save() html = LogRefresh() return html def LogRefresh(): h = "" h += "" h += "" h += "Execution completed... jump back..." h += "" h += "" return h def CreateDataHead(): if not os.path.exists(config.plugins.FanControl.LogPath.value + "FC2data.csv") and config.plugins.FanControl.EnableDataLog.value and Free(config.plugins.FanControl.LogPath.value): try: f = open(config.plugins.FanControl.LogPath.value + "FC2data.csv","w") try: f.write(HeadLine) except: f.close() except IOError: pass ########################################################## class FC2webChart(resource.Resource): title = "FanControl2 Webinterface" isLeaf = True def render(self, req): command = req.args.get("cmd",None) html = "" if os.path.exists(config.plugins.FanControl.LogPath.value + "FC2data.csv"): req.setHeader('Content-type', 'text/html') req.setHeader('charset', 'UTF-8') """ rendering server response """ f = open(config.plugins.FanControl.LogPath.value + "FC2data.csv","a") s = f.tell() f.close() if s < 150: html = "Not enough Data (wait 3min)!" return html f = open(config.plugins.FanControl.LogPath.value + "FC2data.csv","r") f.seek(s-100) line = f.readline() line = f.readline() DT = line.split(";") DT = DT[0].split(" ") DD = DT[0].split(".") DD48h = datetime.date(int(DD[0]),int(DD[1]),int(DD[2])) - datetime.timedelta(2) Dfind = "%04d.%02d.%02d %s" % (DD48h.year,DD48h.month,DD48h.day,DT[1]) f.seek(0) line = f.readline() Anzahl = 0 for line in f.readlines(): DT = line.split(";") if DT[0] > Dfind: Anzahl += 1 # Anzahl -= 1 html = "" html += "" html += "" html += "" html += "" html += "" html += "" html += "Fan Control 2 - Chart" html += "" html += "\n" html += "
\n" html += "

\n" html += "
Fan Control 2 - Chart

\n" html += "
\n" html += "

" html += BoxStatus() if os.path.exists(config.plugins.FanControl.LogPath.value + "FC2data.csv"): html += "\n" html += "

\n" html += "\n" html += "\n" % DT[0] html += "\n" html += "\n" html += "\n" % Anzahl html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" # html += "
\n" # html += "
\n" html += "\n" html += "\n" % DT[0] html += "\n" html += "\n" html += "\n" % Anzahl html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" html += "\n" f.seek(0) line = f.readline() html += "" else: html = "no Data!" html += "" html += "" return html def BoxStatus(): h = "" S = int(FC2werte[5]) if (S & 1)>0 : h += "\n" if (S & 2)>0 : h += "\n" if (S & 4)>0 : h += "\n" return h