DataHouse/controllers/default.go

70 lines
1.2 KiB
Go

package controllers
import (
"github.com/astaxie/beego"
"datahouse/models/sensor"
// "datahouse/models/temperature"
temperatureTmp "datahouse/models/temperature/temp"
"strconv"
"time"
)
var (
_, timezoneOffset = time.Now().Zone()
)
type SensorPrint struct {
Name string
Value string
Unit string
Horodate string
}
type MainController struct {
beego.Controller
}
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
}
// t := temperature.GetLastTempForSensor(s.Id)
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
}