aboutsummaryrefslogtreecommitdiff
path: root/services/data-logger.js
blob: 9c23de17ebf84a9226a5e6cbe82723b29d6fa5e5 (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
const fs = require('fs');
const dataBroadcaster = require('./data-broadcaster');

let logIntervalMs;
let maxLogEntries;

loadLogConfig();

function loadLogConfig() {
  try {
    let config  = JSON.parse(fs.readFileSync('logger-config.json', 'utf8'));
    logIntervalMs = (config.logIntervalSeconds * 1000);
    maxLogEntries = config.maxLogEntries;

  }
  catch (err) {
    console.warn('Error reading logger config. Reverting to defaults.', err);
    logIntervalMs = 60000 // 1 min
    maxLogEntries = 1440  // 24 hrs at 1/min
 }  
}

function startLogging(device) {
  setInterval(() => { log(device); }, logIntervalMs);
  console.log('Logging started for ' + device.alias + ' [' + device.deviceId + '] every ' + (logIntervalMs/1000) + ' seconds');
}

function writeLog(filePath, log) {
  fs.writeFile(filePath, JSON.stringify(log), { flag: 'w' }, (err) => {
    if (err) {
      console.warn('Error writing log for ' + device.alias + ' [' + device.deviceId + ']', err);
    }
  });
}

function getLogEntries(filePath, callback) {
  
  fs.access(filePath, fs.constants.F_OK, (err) => {
    if(err) {
      // No log file, init empty one
      writeLog(filePath, []);
      callback([]);
    }
    else {
      fs.readFile(filePath, 'utf8', (err, data) => {
        if (err) {
          console.warn('Error reading usage log ' + filePath, err);
          callback([]);
        }
        else {
          callback(JSON.parse(data));
        }
      });
    }
  });
}

function log(device) {

  device.emeter.getRealtime().then(response => {

    let logEntry = {
      ts: Date.now(),
      pw: (('power_mw' in response) ? (response.power_mw / 1000) : response.power)
    }

    let filePath = getLogPath(device.deviceId);

    getLogEntries(filePath, (entries) => {
      entries.push(logEntry)
      
      // Remove old entries
      entries.splice(0, entries.length - maxLogEntries);

      writeLog(filePath, entries);
      dataBroadcaster.broadcastNewLogEntry(device.deviceId, logEntry);
    })

  });
}

function getLogPath(deviceId) {
  return deviceId + '-log.json';
}

function getLogEntriesForDevice(deviceId, callback) {
  return getLogEntries(getLogPath(deviceId), callback);
}

module.exports = {
  startLogging: startLogging,
  log: log,
  getLogEntriesForDevice: getLogEntriesForDevice
}