summaryrefslogtreecommitdiff
path: root/ebus/webhdf/static/src/ebus.js
blob: 5c4fa16fd4f73fd6fc4fc2715c486803f1d8d27b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
var d = new Object();
d.ms = 1;
d.sec = 1000 * d.ms;
d.min = 60 * d.sec;
d.hour = 60 * d.min;
d.day = 24 * d.hour;
d.week = 7 * d.day;
d.month = 30.5 * d.day;
d.now = new Date().getTime();

function int(n) { return Math.round(n); };

$(document).ready(function(){
    var from = d.now - 1*d.day;
    var fromOverview = d.now - 30*d.day;
    var to = d.now;
    var datasetDetail = []
    var datasetOverview = [];
    var plotOverview = null;
    var plotDetail = null;
    var indexFound = null;
    var sensorConfigList = [{"sensorname":"heizkreisregler9.solarDaten.tempKollektor","show":true, "color":"#f30000"},
                      {"sensorname":"heizkreisregler10.betriebsdatenRegler1.kesselTemperatur","show":true, "color":"#283074"},
		      {"sensorname":"heizkreisregler9.solarDaten.tempWarmwasserSolar","show":false, "color":"#f0ff4c"},
		      {"sensorname":"feuerungsautomat1.betriebsdatenRegler1.aussenTemperatur","show":false, "color":"#84b500"},
	//	      {"sensorname":"de.wettermichel.temperature","show":true, "color":"#24f590"},
		      {"sensorname":"heizkreisregler10.betriebsdatenRegler1.boilerTemperatur","show":true, "color":"#48b4ff"}];

    var pickSensorConfig = function(sensorname) {
        var SensorConfigFound;
	$.each(sensorConfigList, function(i,sensorConfig) {
	    if (sensorConfig.sensorname == sensorname) {
                sensorConfigFound = sensorConfig;
		return false;
	    }
        });
	return sensorConfigFound;
    }
    var replot = function() {
	    plotDetail = $.plot($("#ebusgraph"), 
				datasetDetail, 
				{ 
				    xaxis: { mode: "time", min: tzFix(from), max:tzFix(to) },
				    yaxis: { min: -16, max: 100 },
				    legend: { show : true}
				});
    };
    var replotOverview = function() {
	if (plotOverview == null) {
	plotOverview = $.plot($("#overview"),
			      datasetOverview,
			      { // options
				  series: {
				      lines: { show: true, lineWidth: 1 },
				      shadowSize: 0
				  },
				  xaxis: { mode: "time", min: tzFix(fromOverview), max: tzFix(d.now) },
				  yaxis: { ticks: [], min: -26, max: 100, autoscaleMargin: 0.1 },
				  legend: { show: false },
		   		  selection: { mode: "x" }
	       });
	} else {
	    plotOverview.setData(datasetOverview);
	    plotOverview.draw();
	}
	plotOverview.setSelection({xaxis: {'from': from, 'to': to}}, true);
    };
    var plotSensor = function(sensorConfig) {
	plotSensorDetail(sensorConfig);
	plotSensorOverview(sensorConfig);
    };
    var unplotSensor = function(sensorname) {
	unplotSensorDetail(sensorname);
	unplotSensorOverview(sensorname);
    };
    var tzFix = function(d) { return d - new Date().getTimezoneOffset() * 60 * 1000; }
    var tzUnfix = function(d) { return d + new Date().getTimezoneOffset() * 60 * 1000; }

    var plotSensorDetail = function(sensorConfig) {
	$.getJSON("sensor/"+escape(sensorConfig.sensorname)+"/"+int(from/1000)+"/"+int(to/1000),
		  function(response) {
		      if (!response.error) {
	                  response.data = response.data.map(function(d) {
				  return [ tzFix(d[0]*1000), d[1] ]; 
			  });
			  datasetDetail.push({'data':response['data'],
					'label':sensorConfig.sensorname,
					'color':sensorConfig.color});
			  replot();
		      } else {
			  console.log("Fehler: " + response["error"]);
		      }
		  });
    };

    var unplotSensorDetail = function(sensorname) {
        $.each(datasetDetail, function(i, sensor) {
                if (sensor.label == sensorname) {
                        datasetDetail.splice(i,1);
			replot();
			return false;
                }
        });   
    };
	
    var plotSensorOverview = function(sensorConfig) {
	$.getJSON("avg/"+escape(sensorConfig.sensorname)+"/"+int(fromOverview/1000),
		  function(response) {
		      if (!response.error) {
                          response.data = response.data.map(function(d) { 
				  return [ tzFix(d[0]*1000), d[1] ]; 
			  });
			  datasetOverview.push({'data':response['data'],
						'label':sensorConfig.sensorname,
						'color':sensorConfig.color});
			  replotOverview();
		      } else {
			  alert("Overview Fehler: " + response["error"]);
		      }
		  });
	
    };
    var unplotSensorOverview = function(sensorname) {
        $.each(datasetOverview, function(i, sensor) {
                if (sensor.label == sensorname) {
                	datasetOverview.splice(i,1);
			replotOverview();
			return false;
                }
        });
    }

    $("#overview").bind("plotselected", function (event, ranges) {
        range_from = Math.round(ranges.xaxis.from);
	range_to = Math.round(ranges.xaxis.to);
	// max selection range 
	if (range_to - range_from > d.month) {
	// reset selection
	    plotOverview.setSelection({xaxis: {'from': from, 'to': to}}, true);
	    return;
	} else {
	    from = tzUnfix( range_from );
	    to = tzUnfix( range_to );
	}
	sensors = [];
	for (elem in datasetOverview) {
	    sensor = datasetDetail[elem]["label"];
	    sensors.push(sensor);
	}
	datasetDetail =[];
	for (i in sensors) {
	    plotSensorDetail(pickSensorConfig(sensors[i]));
	}
    });
    
    $.each(sensorConfigList, function(i,sensorConfig) {
    	var pickerDiv = $("<div>").attr("id","pick_"+sensorConfig.sensorname.replace(/\./g,"_"))
				  .addClass("picker")
	                          .appendTo("#sensorpicker");

	var pickerCheckbox = $("<input>").attr("type","checkbox")
				         .appendTo(pickerDiv);
	$(pickerDiv).append(sensorConfig.sensorname);
    	if (sensorConfig.show) {
		//Plot
		plotSensor(sensorConfig);
		$(pickerCheckbox).attr("checked","checked");
	}
    });
    // TODO http://people.iola.dk/olau/flot/examples/annotating.html
    
    
    $('.picker input').click( function() {
    	var sensorname = $(this).parent().attr("id").replace("pick_","").replace(/_/g,".");
    	if ($(this).is(":checked")) {
		plotSensor(pickSensorConfig(sensorname));
	} else {
		unplotSensor(sensorname);
	}
    });
});