Create index page.
This commit is contained in:
parent
6f36cf0532
commit
62c2f4dc35
|
|
@ -2,12 +2,46 @@ package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
|
|
||||||
|
"datahouse/models/sensor"
|
||||||
|
"datahouse/models/temperature"
|
||||||
|
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type SensorPrint struct {
|
||||||
|
Name string
|
||||||
|
Value string
|
||||||
|
Unit string
|
||||||
|
}
|
||||||
|
|
||||||
type MainController struct {
|
type MainController struct {
|
||||||
beego.Controller
|
beego.Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MainController) Get() {
|
func (c *MainController) Get() {
|
||||||
|
c.Data["sensors"] = getLastTemps()
|
||||||
c.TplNames = "index.tpl"
|
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
|
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"}}
|
{{define "meta"}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{define "body"}}
|
{{define "body"}}
|
||||||
<p>Index</p>
|
<br/>
|
||||||
<!-- Afficher un resumé de tout les capteurs -->
|
{{range $key, $val := .sensors}}
|
||||||
<!-- dernièrer mesure prise de chaque capteur -->
|
<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}}
|
{{end}}
|
||||||
|
|
|
||||||
|
|
@ -20,4 +20,5 @@
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
<br/>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue