diff --git a/controllers/teleinfo.go b/controllers/teleinfo.go index 1ea6204..5898e3c 100644 --- a/controllers/teleinfo.go +++ b/controllers/teleinfo.go @@ -8,6 +8,7 @@ import ( "html/template" "strconv" + "time" ) //—————————————————————————————————————————————————————————————————————————————— @@ -62,7 +63,8 @@ func (c *ViewTeleinfoController) Prepare() { } func (c *ViewTeleinfoController) Get() { - c.Data["compteurs"] = teleinfo.GetAllCompteur() + compteurs := teleinfo.GetAllCompteur() + c.Data["compteurs"] = compteurs adresse := c.Ctx.Input.Param(":compteur") cpt := teleinfo.GetCompteurByAdresse(adresse) @@ -75,13 +77,47 @@ func (c *ViewTeleinfoController) Get() { desc = cpt.AdresseCompteur } c.Data["compteurDescription"] = desc - dataPower := formatDataSensorTeleInfo(teleinfo.GetAllDataForCompteur(cpt.AdresseCompteur)) + compteurs := teleinfo.GetAllDataForCompteur(cpt.AdresseCompteur) + c.Data["lastHorodate"] = getHorodateHighchart(compteurs[len(compteurs)-1].HorodateGMT) + dataPower := formatDataSensorTeleInfo(compteurs) c.Data["dataPower"] = template.JS(dataPower) } c.TplName = "teleinfo.tpl" } +func (c *ViewTeleinfoController) Post() { + currentTimeStr := c.GetString("currentTime") + adresse := c.Ctx.Input.Param(":compteur") + currentTime, err := strconv.ParseInt(currentTimeStr, 10, 64) + if err == nil { + dateGMT := time.Unix(currentTime/1000-int64(timezoneOffset), 0) + data := teleinfo.GetNextData(adresse, dateGMT) + dataStr := formatDataToJsonTeleInfo(data) + c.Ctx.Output.Body([]byte(dataStr)) + } +} + +func getHorodateHighchart(date time.Time) string { + return strconv.FormatInt((date.Unix()+int64(timezoneOffset))*1000, 10) + +} + +func formatDataToJsonTeleInfo(values []teleinfo.TeleinfoTable) string { + ret := "" + ret += "{\"values\": [" + for i := 0; i < len(values); i++ { + if i > 0 { + ret += "," + } + horodate := getHorodateHighchart(values[i].HorodateGMT) + value := strconv.FormatInt(values[i].PuissanceApparente, 10) + ret += "[" + horodate + "," + value + "]" + } + ret += "]}" + return ret +} + func formatDataSensorTeleInfo(values []teleinfo.TeleinfoTable) string { ret := "" ret += "{name : \"Puissance apparente (VA)\",marker : {enabled : true, radius : 3}, data : [" diff --git a/models/teleinfo/teleinfo.go b/models/teleinfo/teleinfo.go index d6997d7..f109cb3 100644 --- a/models/teleinfo/teleinfo.go +++ b/models/teleinfo/teleinfo.go @@ -124,6 +124,38 @@ func GetLastDataForCompteur(adresseCompteur string) *TeleinfoTable { return data } +func GetNextData(addressCompteur string, from time.Time) []TeleinfoTable { + o := orm.NewOrm() + o.Using(database.Alias) + + var dataArray []TeleinfoTable + var maps []orm.Params + _, err := o.QueryTable(new(TeleinfoTable)).Filter("AdresseCompteur", addressCompteur).Filter("HorodateGMT__gt", from).OrderBy("HorodateGMT").Values(&maps) + if err == nil { + for _, m := range maps { + dataTime := utils.GetTime(m, "HorodateGMT") + if dataTime.After(from) { + d := new(TeleinfoTable) + d.Id = utils.GetInt(m, "Id") + d.HorodateGMT = dataTime + + d.AdresseCompteur = utils.GetString(m, "AdresseCompteur") + d.OptionTarifaire = optionTarifaireTranslate[utils.GetString(m, "OptionTarifaire")] + d.OptionBase = utils.GetInt(m, "OptionBase") + + d.IntensiteSouscrite = utils.GetInt(m, "IntensiteSouscrite") + d.IntensiteInstantanne = utils.GetInt(m, "IntensiteInstantanne") + d.IntensiteMax = utils.GetInt(m, "IntensiteMax") + + d.PuissanceApparente = utils.GetInt(m, "PuissanceApparente") + + dataArray = append(dataArray, *d) + } + } + } + return dataArray +} + func DeleteDataCompteur(adresseCompteur string) { o := orm.NewOrm() o.Using(database.Alias) diff --git a/views/teleinfo.tpl b/views/teleinfo.tpl index 0f6ea67..5e7685d 100644 --- a/views/teleinfo.tpl +++ b/views/teleinfo.tpl @@ -4,93 +4,125 @@ - - {{end}} {{define "extrajs"}} + {{end}} {{define "body"}}