Create index page.
This commit is contained in:
parent
6f36cf0532
commit
62c2f4dc35
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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}}
|
||||
|
|
|
|||
|
|
@ -20,4 +20,5 @@
|
|||
</div>
|
||||
</form>
|
||||
{{end}}
|
||||
<br/>
|
||||
{{end}}
|
||||
|
|
|
|||
Loading…
Reference in New Issue