summaryrefslogtreecommitdiff
path: root/ebus/webapp/static/console.html
blob: 71a861c7c79a50f900383b591e839d09caacb173 (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
<html>
<head>
    <script src="lib/d3-v2.6.1/d3.js"></script>
    <script src="lib/d3-v2.6.1/d3.time.js"></script>
    <style>
        path {
            stroke: steelblue;
            stroke-width: 2;
            fill: none;
        }
        line {
            stroke: black;
        }
        div.popup {
            position: absolute;
            border: 2px solid gray;
            background-color: #fefefe;
        }
        div.popup .plot {
            margin: 5px;
        }
        #log {
            height: 300px;
            overflow-y: scroll;
        }
    </style>
</head>
<body>
<div id="image">
</div>
<div id="log">
</div>
<script>
    var mapping = {
        "heizkesselWert" : "heizkreisregler10.betriebsdatenRegler1.kesselTemperatur",
        "tempkollektorWert" : "heizkreisregler9.solarDaten.tempKollektor",
        "boilerWert" : "feuerungsautomat1.betriebsdatenRegler1.boilerTemperatur"
    };
    function process(data) {
        for (var i in data) {
                var row = data[i];
                var caption = "[" + d3.format("02d")(row.timestamp) + "] " + row.name + " Value: " + row.value_real + " - " + row.value_string;

                d3.select("#log").insert("div", "div").text(caption);

                for (var mapping_element_id in mapping) {
                    var mapping_sensor_name = mapping[mapping_element_id];
                    if (row.name == mapping_sensor_name) {
                        d3.select("#" + mapping_element_id).text("" + row.value_real);
                    }
                }
            }
    }

    function reload(time_stop) {
        d3.json("../stream/" + time_stop, function(resp) {
            process(resp.data);
            reload(resp.time_stop);
        });
    }

    d3.xml("draw.svg", "image/svg+xml", function(xml) {
        // Write SVG Data into the DOM
        d3.select("#image")[0][0].appendChild(xml.documentElement);

        // Create Popup
        for (var mapping_element_id in mapping) {
            d3.select("#" + mapping_element_id).on("click", function() {
                var elem = d3.select( this );
                               var sensor_name = mapping[elem.attr("id")];
                var popup = d3.select("body")
                    .append("div")
                    .classed("popup", true)
                    .style("top", d3.event.clientY-5)
                    .style("left", d3.event.clientX-5)
                    .on("click", function() {
                        d3.select(this).remove();
                    });
                popup.append("div")
                    .text(sensor_name);

                var w = 550, h = 200, margin = 30;
                var plot = popup.append("svg:svg")
                            .classed("plot", true)
                            .attr("width", w)
                            .attr("height", h);

                var wait_indicator = plot.append("svg:text")
                    .attr("x", w/2)
                    .attr("y", h/2)
                    .text("Bitte Warten");

                var startdate = (new Date().getTime()/1000) - 7*60*60*24
                var enddate = (new Date().getTime()/1000)
                d3.json("../sensor/"+sensor_name+"/" + startdate + "/" + enddate, function(resp) {
                    var data = resp.data.map(function(d) {
                        return [new Date(d[0]), d[1]];
                    })
                    var x_min = d3.min(data, function(d){return d[0]}),
                        x_max = d3.max(data, function(d){return d[0]}),
                        y_min = d3.min(data, function(d){return d[1]}),
                        y_max = d3.max(data, function(d){return d[1]});

                    var x = d3.time.scale()
                        .domain([x_min, x_max])
                        .range([0+margin, w-margin]);
 
                    var y = d3.scale.linear()
                        .domain([y_min, y_max])
                        .range([0+margin, h]);

                   
                    var g = plot.append("svg:g")
                            .attr("transform", "translate(0, "+h+")");

                    var line = d3.svg.line()
                                .x(function(d,i){ return x(d[0]); })
                                .y(function(d) {return -1*y(d[1]);});

                    g.append("svg:path")
                        .attr("d", line(data));

                    // Axes X-Line
                    g.append("svg:line")
                        .attr("x1", 0)
                        .attr("y1", -1*y(y_min))
                        .attr("x2", x(x_max))
                        .attr("y2", -1 * y(y_min));
                        
                 
                    // Tick Labels
                    var tick_format = d3.time.format("%d.%m");
                    g.selectAll(".xlabel")
                        .data(x.ticks(5)).enter()
                        .append("svg:text")
                        .text(function(d){return tick_format(d) })
                        .attr("x", function(d) { return x(d) })
                        .attr("y", 0)
                        .attr("text-anchor", "middle");

                    g.selectAll(".ylabel")
                        .data(y.ticks(5)).enter()
                        .append("svg:text")
                        .text(String)
                        .attr("x", 0)
                        .attr("y", function(d) {return -1*y(d)})
                        .attr("text-anchor", "right");

                    // X-Lines
                    g.selectAll(".xlines")
                        .data(x.ticks(5)).enter()
                        .append("svg:line")
                        .style("fill", "none")
                        .style("stroke", "#000000")
                        .style("stroke-width", 0.5)
                        .style("stroke-dasharray", "6,1")
                        .attr("x1", function(d){return x(d)})
                        .attr("y1", -1*y(y_min)+(margin/2))
                        .attr("x2", function(d){return x(d)})
                        .attr("y2", -1*y(y_max));

                    //
                    wait_indicator.remove();
                });
            });
        }

        d3.json("../all_values", function(resp) {
            process(resp.data);
            reload(resp.time_stop);
        });
    });
</script>
</body>
</html>