Subversion Repositories ESP32_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 raymond 1
const timezone = new Date().getTimezoneOffset();
2
 
3
let chart = Highcharts.chart('container', {
4
  time: {
5
    timezoneOffset: timezone
6
  },
7
	chart: {
8
		zoomType: 'x'
9
	},
10
	title: {
11
		text: 'Total heap size and max free block of RAM'
12
	},
13
	subtitle: {
14
		text: 'click and drag in the plot area to zoom in'
15
	},
16
	xAxis: {
17
		type: 'datetime'
18
	},
19
	yAxis: {
20
		title: {
21
			text: 'Bytes'
22
		}
23
	},
24
	legend: {
25
		enabled: false
26
	},
27
	plotOptions: {
28
		area: {
29
			fillColor: {
30
				linearGradient: {
31
					x1: 0,
32
					y1: 0,
33
					x2: 0,
34
					y2: 1
35
				},
36
				stops: [
37
					[0, Highcharts.getOptions().colors[0]],
38
					[1, Highcharts.color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
39
				]
40
			},
41
			marker: {
42
				radius: 2
43
			},
44
			lineWidth: 1,
45
			states: {
46
				hover: {
47
					lineWidth: 1
48
				}
49
			},
50
			threshold: null
51
		}
52
	},
53
 
54
	series: [{
55
			type: 'area',
56
			name: 'Total heap size',
57
			data: []
58
		},
59
		{
60
			type: 'area',
61
			name: 'Max contiguos RAM block',
62
			data: []
63
		},
64
	]
65
});
66
 
67
 
68
function addPoint(timestamp, total, max) {
69
	chart.series[0].addPoint([timestamp, total]);
70
	chart.series[1].addPoint([timestamp, max]);
71
}
72
 
73
 
74
// WebSocket handling
75
function ws_connect() {
76
  var ws = new WebSocket('ws://'+document.location.host+'/ws',['arduino']);
77
  ws.onopen = function() { ws.send('Connected - ' + new Date());};
78
  ws.onmessage = function(e) {
79
    parseMessage(e.data);
80
  };
81
  ws.onclose = function(e) {
82
      setTimeout(function() {
83
      ws_connect();
84
      }, 1000);
85
  };
86
  ws.onerror = function(err) {
87
      ws.close();
88
  };
89
}
90
 
91
function parseMessage(msg) {
92
	try {
93
		const obj = JSON.parse(msg);
94
		if (typeof obj === 'object' && obj !== null) {
95
			// Add new point to chart message
96
			if (obj.addPoint !== null) {
97
 
98
			  // Updated the ESP timedate
99
				var date = new Date(obj.timestamp*1000);   
100
				document.getElementById("esp-time").innerHTML = date;
101
 
102
				// Add the new point
103
				addPoint(obj.timestamp*1000, obj.totalHeap, obj.maxBlock);
104
			}
105
		}
106
	} catch {
107
		console.log('Error on parse message ' + msg);
108
	}
109
}
110
 
111
ws_connect();