-
Notifications
You must be signed in to change notification settings - Fork 10
/
view.php
283 lines (243 loc) · 8 KB
/
view.php
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
global $path;
$level = "DEBUG";
$log_levels = array(
1=>"DEBUG",
2=>"INFO",
3=>"WARNING",
4=>"ERROR",
5=>"CRITICAL"
);
?>
<style>
section {
position: relative;
}
.dropdown-menu-right {
right: 0 !important;
left: initial;
}
.log { padding:20px}
</style>
<?php if(!empty($tabs)) echo $tabs ?>
<h2>EmonHub</h2>
Emonhub provides the interface between hardware and emoncms inputs. Read from serial, spi, modbus, mbus and more.
<br><br>
<div class="input-prepend input-append" style="float:right">
<button class="btn btn-info" id="show-emonhublogview">View log</button>
<button class="btn btn-danger" id="show-editor">Edit config</button>
<button class="btn btn-warning" id="restart">Restart</button>
</div>
<div id="editor">
<h4>Config:</h4>
<textarea id="configtextarea" style="width:100%; height:400px"></textarea><br>
<button class="btn btn-warning" id="save">Save</button><br><br>
<a href="https://github.com/openenergymonitor/emonhub/blob/emon-pi/configuration.md" target="_blank">EmonHub Config Documentation</a>
</div>
<div id="emonhublogview" style="display:none">
<div class="input-prepend input-append">
<span class="add-on">Auto update</span>
<button class="btn auto-update-toggle btn-success">ON</button>
</div>
<div class="input-prepend input-append">
<span class="add-on">Auto scroll</span>
<button class="btn auto-scroll-toggle btn-success">ON</button>
</div>
<section>
<h4>Log:</h4>
<pre id="emonhub-console-log" class="log" style="height:600px"></pre>
<div id="log-level" class="dropup dropdown">
<a class="btn btn-small dropdown-toggle btn-inverse text-uppercase" data-toggle="dropdown" href="#" title="Change the logging level">
<span class="log-level-name">Log Level: <?php echo $level ?></span>
<span class="caret"></span>
</a>
<ul class="dropdown-menu dropdown-menu-right">
<?php
if(!empty($log_levels)): foreach($log_levels as $_level=>$name):
$active = $level == $name ? ' active': '';
printf('<li><a href="#" data-key="%s" class="btn%s">%s</a></li>', $_level, $active, $name);
endforeach; endif;
?>
</ul>
</div>
</section>
<br>
<div style="float: right;">
<a href="<?php echo $path; ?>config/downloadlog" class="btn btn-info">Download Log</a>
</div>
<a href="https://github.com/openenergymonitor/emonhub" target="_blank">EmonHub Documentation</a>
</div>
<script>
var config = "";
var emonhublog_updater = false;
var logfile_position = 0;
const logdiv = document.getElementById("emonhub-console-log")
var autoupdate = true;
var auto_scroll = true;
var log_level = "DEBUG";
var log_levels = <?php echo json_encode($log_levels); ?>;
var downloaded_log = "";
$("#emonhublogview").show();
$("#editor").hide();
emonhublog_refresh();
enable_autoupdate();
$.ajax({
url: path+"config/get",
dataType: 'text', async: false,
success: function(data) {
config = data;
}
});
$("#configtextarea").val(config);
$("#save").click(function(){
config = $("#configtextarea").val();
$.ajax({ type: "POST", url: path+"config/set", data: "config="+config, async: false, success: function(data){
console.log(data);
alert(data);
}});
});
$("#restart").click(function(){
alert("Restarting EmonHub...");
$.ajax({ url: path+"admin/service/restart?name=emonhub", dataType: 'text', async: false});
});
$(".auto-update-toggle").click(function(){
if (autoupdate==true) {
disable_autoupdate();
} else {
enable_autoupdate();
}
auto_update_button(autoupdate);
});
$(".auto-scroll-toggle").click(function(){
if (auto_scroll==true) {
auto_scroll = false;
} else {
auto_scroll = true;
}
auto_scroll_button(auto_scroll);
});
$("#show-editor").click(function(){
$("#editor").show();
$("#emonhublogview").hide();
disable_autoupdate();
});
$("#show-emonhublogview").click(function(){
enable_autoupdate();
emonhublog_refresh();
$("#emonhublogview").show();
$("#editor").hide();
});
$("#emonhub-console-log").scroll(function() {
if (auto_scroll) {
if (last_set_height!=logdiv.scrollTop) {
auto_scroll = false;
auto_scroll_button(auto_scroll);
}
} else {
if (((logdiv.scrollHeight-logdiv.scrollTop)-640)<20) {
auto_scroll = true;
auto_scroll_button(auto_scroll);
}
}
});
function enable_autoupdate() {
autoupdate = true;
clearInterval(emonhublog_updater);
emonhublog_updater = setInterval(emonhublog_refresh,1000);
}
function disable_autoupdate() {
autoupdate = false;
clearInterval(emonhublog_updater);
}
function filter_log_level(data_in, level) {
if (level=="DEBUG") return data_in;
var filter = [];
if (level=="INFO") filter = ["INFO","WARNING","ERROR","CRITICAL"];
if (level=="WARNING") filter = ["WARNING","ERROR","CRITICAL"];
if (level=="ERROR") filter = ["ERROR","CRITICAL"];
if (level=="CRITICAL") filter = ["CRITICAL"];
var lines_in = data_in.split("\n");
if (lines_in.length==1 && lines_in[0]=="") return "";
var lines_out = [];
for (var z=0; z<lines_in.length; z++) {
for (var f in filter) {
if (lines_in[z].indexOf(filter[f])!=-1) {
lines_out.push(lines_in[z]);
}
}
}
if (lines_out.length==0) return "";
return lines_out.join("\n")+"\n";
}
function emonhublog_refresh()
{
$.ajax({
url: path+"config/getemonhublog?pos="+logfile_position,
dataType: 'text', async: true,
success: function(data) {
var firstnewline = data.indexOf("\n");
logfile_position = parseInt(data.substr(0, firstnewline));
data = data.substr(firstnewline+1,data.length);
downloaded_log += data;
logdiv.textContent += filter_log_level(data,log_level);
if (auto_scroll) {
logdiv.scrollTop = logdiv.scrollHeight;
last_set_height = logdiv.scrollTop;
}
}
});
}
$(function(){
$('#log-level ul li a').click(function(event){
event.preventDefault();
var $btn = $(this);
var $toggle = $btn.parents('ul').prev('.btn');
var key = $btn.data('key');
$toggle.find('.log-level-name').text(gettext('log level: %s').replace('%s',log_levels[""+key]));
$btn.addClass('active');
$btn.parents('li').siblings().find('a').removeClass('active');
log_level = log_levels[""+key];
logdiv.textContent = filter_log_level(downloaded_log,log_level);
if (auto_scroll) {
logdiv.scrollTop = logdiv.scrollHeight;
last_set_height = logdiv.scrollTop;
}
})
})
/**
* emulate the php gettext function for replacing php strings in js
*/
function gettext(property) {
_strings = typeof translations === 'undefined' ? getTranslations() : translations;
if (_strings.hasOwnProperty(property)) {
return _strings[property];
} else {
return property;
}
}
/**
* return object of gettext translated strings
*
* @return object
*/
function getTranslations(){
return {
'Log level: %s': "<?php echo _('Log level: %s') ?>",
'Error sending data': "<?php echo _('Error sending data') ?>"
}
}
function auto_update_button(state) {
if (state==true) {
$(".auto-update-toggle").html("ON").removeClass('btn-warning').addClass('btn-success');
} else {
$(".auto-update-toggle").html("OFF").removeClass('btn-success').addClass('btn-warning');
}
}
function auto_scroll_button(state) {
if (state==true) {
$(".auto-scroll-toggle").html("ON").removeClass('btn-warning').addClass('btn-success');
} else {
$(".auto-scroll-toggle").html("OFF").removeClass('btn-success').addClass('btn-warning');
}
}
</script>