DataHouse/controllers/default.go

84 lines
1.6 KiB
Go

package controllers
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/sensor"
temperatureTmp "git.kingpenguin.tk/chteufleur/datahouse.git/models/temperature/temp"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/variables"
"strconv"
"time"
)
var (
_, timezoneOffset = time.Now().Zone()
log = logs.NewLogger(10000)
)
func init() {
log.SetLogger(variables.LogType, variables.LogParams)
}
type SensorPrint struct {
Name string
Mac string
Value string
Unit string
Horodate string
}
type MainController struct {
beego.Controller
}
func (c *MainController) Prepare() {
sess := c.GetSession(variables.SessionName)
if sess != nil {
c.Data["IsAuthentificated"] = true
}
c.Data["version"] = variables.Version
}
func (c *MainController) Get() {
c.Data["sensors"] = getLastTemps()
c.TplNames = "index.tpl"
}
func getLastTemps() []SensorPrint {
sensors := sensor.GetAllSensor()
var values []SensorPrint
for _, s := range sensors {
sens := new(SensorPrint)
sens.Name = s.Description
if sens.Name == "" {
sens.Name = s.SensorMAC
}
sens.Mac = s.SensorMAC
t := temperatureTmp.GetTemp(s.Id)
sens.Value = strconv.FormatInt(t.Value, 10)
sens.Unit = "°C"
horodatTmp := t.HorodateGMT
horodatTmp = horodatTmp.Add(time.Duration(timezoneOffset) * time.Second)
h, m, _ := horodatTmp.Clock()
hStr := strconv.Itoa(h)
if h < 10 {
hStr = "0" + hStr
}
mStr := strconv.Itoa(m)
if m < 10 {
mStr = "0" + mStr
}
sens.Horodate = hStr + "h" + mStr
values = append(values, *sens)
}
return values
}