Update dynamically teleinfo graph.

This commit is contained in:
Chteufleur 2017-01-14 10:48:38 +01:00
parent 8378253574
commit 213e1305d9
3 changed files with 187 additions and 87 deletions

View File

@ -8,6 +8,7 @@ import (
"html/template" "html/template"
"strconv" "strconv"
"time"
) )
//—————————————————————————————————————————————————————————————————————————————— //——————————————————————————————————————————————————————————————————————————————
@ -62,7 +63,8 @@ func (c *ViewTeleinfoController) Prepare() {
} }
func (c *ViewTeleinfoController) Get() { func (c *ViewTeleinfoController) Get() {
c.Data["compteurs"] = teleinfo.GetAllCompteur() compteurs := teleinfo.GetAllCompteur()
c.Data["compteurs"] = compteurs
adresse := c.Ctx.Input.Param(":compteur") adresse := c.Ctx.Input.Param(":compteur")
cpt := teleinfo.GetCompteurByAdresse(adresse) cpt := teleinfo.GetCompteurByAdresse(adresse)
@ -75,13 +77,47 @@ func (c *ViewTeleinfoController) Get() {
desc = cpt.AdresseCompteur desc = cpt.AdresseCompteur
} }
c.Data["compteurDescription"] = desc 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.Data["dataPower"] = template.JS(dataPower)
} }
c.TplName = "teleinfo.tpl" 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 { func formatDataSensorTeleInfo(values []teleinfo.TeleinfoTable) string {
ret := "" ret := ""
ret += "{name : \"Puissance apparente (VA)\",marker : {enabled : true, radius : 3}, data : [" ret += "{name : \"Puissance apparente (VA)\",marker : {enabled : true, radius : 3}, data : ["

View File

@ -124,6 +124,38 @@ func GetLastDataForCompteur(adresseCompteur string) *TeleinfoTable {
return data 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) { func DeleteDataCompteur(adresseCompteur string) {
o := orm.NewOrm() o := orm.NewOrm()
o.Using(database.Alias) o.Using(database.Alias)

View File

@ -4,93 +4,125 @@
<script type="text/javascript" src="/static/highstock/js/highstock.js"></script> <script type="text/javascript" src="/static/highstock/js/highstock.js"></script>
<script type="text/javascript" src="/static/highstock/js/themes/grid.js"></script> <script type="text/javascript" src="/static/highstock/js/themes/grid.js"></script>
<script type="text/javascript" src="/static/highstock/js/modules/exporting.js"></script> <script type="text/javascript" src="/static/highstock/js/modules/exporting.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script type="text/javascript">
$(function () {
chart1 = new Highcharts.StockChart({
chart: {
renderTo : 'graphe',
load : function () {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function () {
var x = (new Date()).getTime(), // current time
y = Math.round(Math.random() * 100);
series.addPoint([x, y], true, true);
}, 1000);
}
},
rangeSelector: {
buttons: [{
count: 3,
type: 'hour',
text: '3h'
},{
count: 6,
type: 'hour',
text: '6h'
},{
count: 12,
type: 'hour',
text: '12h'
}, {
count: 1,
type: 'day',
text: '1j'
}, {
count: 2,
type: 'day',
text: '2j'
}, {
count: 3,
type: 'day',
text: '3j',
}, {
count: 5,
type: 'day',
text: '5j'
}, {
count: 7,
type: 'day',
text: '7j'
}, {
count: 1,
type: 'month',
text: '1m'
}, {
type: 'all',
text: 'All'
}],
selected: 5
},
yAxis: {
title: {
text: 'Puissance (W)'
}
},
xAxis: {
type: 'datetime'
},
scrollbar : {
enabled : false
},
legend : {
enabled : true,
layout: 'vertical',
verticalAlign : 'middle',
align : 'right'
},
series : [ {{.dataPower}} ]
});
});
Highcharts.setOptions({
global: {
useUTC: false
}
});
</script>
{{end}} {{end}}
{{define "extrajs"}} {{define "extrajs"}}
<script type="text/javascript">
var lastTimeStamps = {{.lastHorodate}};
var initData = function(series) {
//loadNewData(series);
setInterval(function() {
loadNewData(series);
}, 30000);
};
var loadNewData = function(series) {
$.ajax({
type: "POST",
data: {
currentTime: lastTimeStamps
},
success: function(json) {
console.log(json);
var obj = JSON.parse(json);
for (var i=0; i<obj.values.length; i++) {
var arrayVal = [obj.values[i][0], obj.values[i][1]];
console.log(arrayVal);
series.addPoint(arrayVal, true, false);
if (i == obj.values.length-1) {
lastTimeStamps = obj.values[i][0] + 1;
}
}
}
});
};
$(function () {
Highcharts.setOptions({
global: {
useUTC: false
}
});
// Create the chart
Highcharts.StockChart({
chart: {
renderTo: "graphe",
events: {
load: function () {
// set up the updating of the chart each second
var series = this.series[0];
initData(series);
}
}
},
rangeSelector: {
buttons: [{
count: 3,
type: 'hour',
text: '3h'
},{
count: 6,
type: 'hour',
text: '6h'
},{
count: 12,
type: 'hour',
text: '12h'
}, {
count: 1,
type: 'day',
text: '1j'
}, {
count: 2,
type: 'day',
text: '2j'
}, {
count: 3,
type: 'day',
text: '3j',
}, {
count: 5,
type: 'day',
text: '5j'
}, {
count: 7,
type: 'day',
text: '7j'
}, {
count: 1,
type: 'month',
text: '1m'
}, {
type: 'all',
text: 'All'
}],
selected: 5
},
yAxis: {
title: {
text: 'Puissance (VA)'
}
},
xAxis: {
type: 'datetime'
},
scrollbar : {
enabled : false
},
legend : {
enabled : true,
layout: 'vertical',
verticalAlign : 'middle',
align : 'right'
},
series : [ {{.dataPower}} ]
});
});
</script>
{{end}} {{end}}
{{define "body"}} {{define "body"}}
<div class="row"> <div class="row">