First attempt at manual battery status.

Ylian Saint-Hilaire committed Aug 28, 2020 at 20:01 UTC 0c1dc084b4c11e15ad71c6c8b6a68e83bd0122ed
1 file changed +46 -30
agents/meshcore.js
+46 -30
@@ -251,34 +251,50 @@ function createMeshCore(agent) {
251 return (svc);
252 }
253
254 - if (require('identifiers').isBatteryPowered && require('identifiers').isBatteryPowered())
255 - {
256 - require('MeshAgent')._battLevelChanged = function _battLevelChanged(val)
257 - {
258 - _battLevelChanged.self._currentBatteryLevel = val;
259 - _battLevelChanged.self.SendCommand({ action: 'battery', state: _battLevelChanged.self._currentPowerState, level: val });
260 - };
261 - require('MeshAgent')._battLevelChanged.self = require('MeshAgent');
262 - require('MeshAgent')._powerChanged = function _powerChanged(val)
263 - {
264 - _powerChanged.self._currentPowerState = (val == 'AC' ? 'ac' : 'dc');
265 - _powerChanged.self.SendCommand({ action: 'battery', state: (val == 'AC' ? 'ac' : 'dc'), level: _powerChanged.self._currentBatteryLevel });
266 - };
267 - require('MeshAgent')._powerChanged.self = require('MeshAgent');
268 - require('MeshAgent').on('Connected', function (status)
269 - {
270 - if (status == 0)
271 - {
272 - require('power-monitor').removeListener('acdc', this._powerChanged);
273 - require('power-monitor').removeListener('batteryLevel', this._battLevelChanged);
274 - }
275 - else
276 - {
277 - require('power-monitor').on('acdc', this._powerChanged);
278 - require('power-monitor').on('batteryLevel', this._battLevelChanged);
279 - }
254 + /*
255 + // TODO: Monitor the file 'batterystate.txt' in the agent's folder and sends battery update when this file is changed.
256 + if (require('fs').existsSync('batterystate.txt')) {
257 + // Setup manual battery monitoring
258 + require('MeshAgent')._batteryFileWatcher = require('fs').watch('.', function (a) {
259 + if (require('MeshAgent')._batteryFileTimer != null) return;
260 + require('MeshAgent')._batteryFileTimer = setTimeout(function () {
261 + sendConsoleText('Battery State Changed');
262 + try {
263 + require('MeshAgent')._batteryFileTimer = null;
264 + var data = require('fs').readFileSync('batterystate.txt').toString();
265 + if (data.length < 10) {
266 + data = data.split(',');
267 + if ((data.length == 2) && ((data[0] == 'ac') || (data[0] == 'dc'))) { require('MeshAgent').SendCommand({ action: 'battery', state: data[0], level: parseInt(data[1]) }); }
268 + }
269 + } catch (ex) { }
270 + }, 1000);
271 });
281 - }
272 + } else {
273 + */
274 + // Setup normal battery monitoring
275 + if (require('identifiers').isBatteryPowered && require('identifiers').isBatteryPowered()) {
276 + require('MeshAgent')._battLevelChanged = function _battLevelChanged(val) {
277 + _battLevelChanged.self._currentBatteryLevel = val;
278 + _battLevelChanged.self.SendCommand({ action: 'battery', state: _battLevelChanged.self._currentPowerState, level: val });
279 + };
280 + require('MeshAgent')._battLevelChanged.self = require('MeshAgent');
281 + require('MeshAgent')._powerChanged = function _powerChanged(val) {
282 + _powerChanged.self._currentPowerState = (val == 'AC' ? 'ac' : 'dc');
283 + _powerChanged.self.SendCommand({ action: 'battery', state: (val == 'AC' ? 'ac' : 'dc'), level: _powerChanged.self._currentBatteryLevel });
284 + };
285 + require('MeshAgent')._powerChanged.self = require('MeshAgent');
286 + require('MeshAgent').on('Connected', function (status) {
287 + if (status == 0) {
288 + require('power-monitor').removeListener('acdc', this._powerChanged);
289 + require('power-monitor').removeListener('batteryLevel', this._battLevelChanged);
290 + }
291 + else {
292 + require('power-monitor').on('acdc', this._powerChanged);
293 + require('power-monitor').on('batteryLevel', this._battLevelChanged);
294 + }
295 + });
296 + }
297 + //}
298
299
300 /*
@@ -1325,12 +1341,12 @@ function createMeshCore(agent) {
1341 try { stats = require('fs').statSync(this.httprequest.xoptions.file) } catch (e) { }
1342 try { if (stats) { this.httprequest.downloadFile = fs.createReadStream(this.httprequest.xoptions.file, { flags: 'rbN' }); } } catch (e) { }
1343 if (this.httprequest.downloadFile) {
1328 - sendConsoleText('BasicFileTransfer, ok, ' + this.httprequest.xoptions.file + ', ' + JSON.stringify(stats));
1344 + //sendConsoleText('BasicFileTransfer, ok, ' + this.httprequest.xoptions.file + ', ' + JSON.stringify(stats));
1345 this.write(JSON.stringify({ op: 'ok', size: stats.size }));
1346 this.httprequest.downloadFile.pipe(this);
1347 this.httprequest.downloadFile.end = function () { }
1348 } else {
1333 - sendConsoleText('BasicFileTransfer, cancel, ' + this.httprequest.xoptions.file);
1349 + //sendConsoleText('BasicFileTransfer, cancel, ' + this.httprequest.xoptions.file);
1350 this.write(JSON.stringify({ op: 'cancel' }));
1351 }
1352 }
@@ -3506,7 +3522,7 @@ function createMeshCore(agent) {
3522 // Send a mesh agent console command
3523 function sendConsoleText(text, sessionid) {
3524 if (typeof text == 'object') { text = JSON.stringify(text); }
3509 - mesh.SendCommand({ action: 'msg', type: 'console', value: text, sessionid: sessionid });
3525 + require('MeshAgent').SendCommand({ action: 'msg', type: 'console', value: text, sessionid: sessionid });
3526 }
3527
3528 // Called before the process exits