summaryrefslogtreecommitdiffstats
path: root/applications/luci-app-wifihistory/htdocs/luci-static/resources/view/status/wifihistory.js
blob: 076c97ab5ed3fc28b0238fe4e9573e8eb049f2e0 (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
'use strict';
'require view';
'require poll';
'require rpc';
'require ui';

const callGetHistory = rpc.declare({
	object: 'luci.wifihistory',
	method: 'getHistory',
	expect: { history: {} }
});

const callClearHistory = rpc.declare({
	object: 'luci.wifihistory',
	method: 'clearHistory',
	expect: { result: true }
});

function formatDate(epoch) {
	if (!epoch || epoch <= 0)
		return '-';

	return new Date(epoch * 1000).toLocaleString();
}

return view.extend({
	load() {
		return callGetHistory();
	},

	updateTable(table, history) {
		const stations = Object.values(history);

		/* Sort connected stations first, then by most recently seen */
		stations.sort((a, b) => {
			if (a.connected !== b.connected)
				return a.connected ? -1 : 1;

			return (b.last_seen || 0) - (a.last_seen || 0);
		});

		const rows = [];

		for (const s of stations) {
			let hint;
			if (s.hostname && s.ipv4 && s.ipv6)
				hint = '%s (%s, %s)'.format(s.hostname, s.ipv4, s.ipv6);
			else if (s.hostname && (s.ipv4 || s.ipv6))
				hint = '%s (%s)'.format(s.hostname, s.ipv4 || s.ipv6);
			else if (s.ipv4 || s.ipv6)
				hint = s.ipv4 || s.ipv6;
			else
				hint = '-';

			let sig_value = '-';
			let sig_title = '';

			if (s.signal && s.signal !== 0) {
				if (s.noise && s.noise !== 0) {
					sig_value = '%d/%d\xa0%s'.format(s.signal, s.noise, _('dBm'));
					sig_title = '%s: %d %s / %s: %d %s / %s %d'.format(
						_('Signal'), s.signal, _('dBm'),
						_('Noise'), s.noise, _('dBm'),
						_('SNR'), s.signal - s.noise);
				}
				else {
					sig_value = '%d\xa0%s'.format(s.signal, _('dBm'));
					sig_title = '%s: %d %s'.format(_('Signal'), s.signal, _('dBm'));
				}
			}

			let icon;
			if (!s.connected) {
				icon = L.resource('icons/signal-none.svg');
			}
			else {
				/* Estimate signal quality as percentage:
				 * Map dBm range [-110, -40] to [0%, 100%] */
				const q = Math.min((s.signal + 110) / 70 * 100, 100);
				if (q == 0)
					icon = L.resource('icons/signal-000-000.svg');
				else if (q < 25)
					icon = L.resource('icons/signal-000-025.svg');
				else if (q < 50)
					icon = L.resource('icons/signal-025-050.svg');
				else if (q < 75)
					icon = L.resource('icons/signal-050-075.svg');
				else
					icon = L.resource('icons/signal-075-100.svg');
			}

			rows.push([
				E('span', {
					'class': 'ifacebadge',
					'style': s.connected ? '' : 'opacity:0.5',
					'title': s.connected ? _('Connected') : _('Disconnected')
				}, [
					E('img', { 'src': icon, 'style': 'width:16px;height:16px' }),
					E('span', {}, [ ' ', s.connected ? _('Yes') : _('No') ])
				]),
				s.mac,
				hint,
				s.network || '-',
				E('span', { 'title': sig_title }, sig_value),
				formatDate(s.first_seen),
				formatDate(s.last_seen)
			]);
		}

		cbi_update_table(table, rows, E('em', _('No station history available')));
	},

	handleClearHistory(ev) {
		return ui.showModal(_('Clear Station History'), [
			E('p', _('This will permanently delete all recorded station history. Are you sure?')),
			E('div', { 'class': 'right' }, [
				E('button', {
					'class': 'btn',
					'click': ui.hideModal
				}, _('Cancel')), ' ',
				E('button', {
					'class': 'btn cbi-button-negative',
					'click': ui.createHandlerFn(this, function() {
						return callClearHistory().then(L.bind(function() {
							ui.hideModal();
							return callGetHistory().then(L.bind(function(history) {
								this.updateTable('#wifi_history_table', history);
							}, this));
						}, this));
					})
				}, _('Clear'))
			])
		]);
	},

	render(history) {
		const isReadonlyView = !L.hasViewPermission();

		const v = E([], [
			E('h2', _('Station History')),
			E('div', { 'class': 'cbi-map-descr' },
				_('This page displays a history of all WiFi stations that have connected to this device, including currently connected and previously seen devices.')),

			E('div', { 'class': 'cbi-section' }, [
				E('div', { 'class': 'right', 'style': 'margin-bottom:1em' }, [
					E('button', {
						'class': 'btn cbi-button-negative',
						'disabled': isReadonlyView,
						'click': ui.createHandlerFn(this, 'handleClearHistory')
					}, _('Clear History'))
				]),

				E('table', { 'class': 'table', 'id': 'wifi_history_table' }, [
					E('tr', { 'class': 'tr table-titles' }, [
						E('th', { 'class': 'th' }, _('Connected')),
						E('th', { 'class': 'th' }, _('MAC address')),
						E('th', { 'class': 'th' }, _('Host')),
						E('th', { 'class': 'th' }, _('Network')),
						E('th', { 'class': 'th' }, '%s / %s'.format(_('Signal'), _('Noise'))),
						E('th', { 'class': 'th' }, _('First seen')),
						E('th', { 'class': 'th' }, _('Last seen'))
					])
				])
			])
		]);

		this.updateTable(v.querySelector('#wifi_history_table'), history);

		poll.add(L.bind(function() {
			return callGetHistory().then(L.bind(function(history) {
				this.updateTable('#wifi_history_table', history);
			}, this));
		}, this), 5);

		return v;
	},

	handleSaveApply: null,
	handleSave: null,
	handleReset: null
});