Create index page.

This commit is contained in:
Chteufleur 2015-09-28 21:56:50 +02:00
parent 6f36cf0532
commit 62c2f4dc35
4 changed files with 66 additions and 3 deletions

View File

@ -2,12 +2,46 @@ package controllers
import (
"github.com/astaxie/beego"
"datahouse/models/sensor"
"datahouse/models/temperature"
"strconv"
)
type SensorPrint struct {
Name string
Value string
Unit 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)
sens.Value = strconv.FormatInt(t.Value, 10)
sens.Unit = "°C"
values = append(values, *sens)
}
return values
}

View File

@ -54,3 +54,21 @@ func GetAllTempForSensor(sensorId int64) ([]TempTable) {
}
return dataArray
}
func GetLastTempForSensor(sensorId int64) (*TempTable) {
o := orm.NewOrm()
o.Using(database.Alias)
data := new(TempTable)
var maps []orm.Params
_, err := o.QueryTable(new(TempTable)).Filter("SensorID", sensorId).OrderBy("HorodateGMT").Values(&maps)
if err == nil {
for _, m := range maps {
data.Id = utils.GetInt(m, "Id")
data.HorodateGMT = utils.GetTime(m, "HorodateGMT")
data.SensorID = utils.GetInt(m, "SensorID")
data.Value = utils.GetInt(m, "Value")
}
}
return data
}

View File

@ -2,7 +2,17 @@
{{define "meta"}}
{{end}}
{{define "body"}}
<p>Index</p>
<!-- Afficher un resumé de tout les capteurs -->
<!-- dernièrer mesure prise de chaque capteur -->
<br/>
{{range $key, $val := .sensors}}
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">{{$val.Name}}</h3>
</div>
<div class="panel-body">
<div class="form-group">
<p>{{$val.Value}} {{$val.Unit}}</p>
</div>
</div>
</div>
{{end}}
{{end}}

View File

@ -20,4 +20,5 @@
</div>
</form>
{{end}}
<br/>
{{end}}