This repository has been archived by the owner on Jul 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (91 loc) · 3.1 KB
/
index.js
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
var Device = require('zetta-device');
var util = require('util');
var extend = require('node.extend');
// HOWTO: connect to live modbus?
// use this node module: https://github.com/Cloud-Automation/node-modbus
var OPTOEMU_SNR_DR1_IMG = 'http://www.opto22.com/images/products/OPTOEMU-SNR-DR1_p_450.png';
function degToRad(x) {
return x * ( Math.PI / 180 );
}
// mock device based on OPTOEMU SENSOR COMMUNICATION GUIDE
// OptoEMU Sensor(TM) Energy Monitoring Units
// http://www.opto22.com/site/pr_details.aspx?cid=8&item=OPTOEMU-SNR-DR1
var IndustrialMonitor = module.exports = function(opts) {
Device.call(this);
// Power is the live measurement value
this.power = 0;
// Quality (Status): 0 = Value has not been read yet. -1 or less = Number of consecutive failed reads. 1 or greater = Number of consecutive successful reads.
this.measurementStatus = 0;
// Quality (Freshness): Number of seconds since last successful read. Modbus inputs are read sequentially If one or more inputs is currently offline, it can affect the freshness of the points that are online.
this.measurementFreshness = 0;
this._opts = opts || {};
this._increment = this._opts['increment'] || 15;
this._timeOut = null;
this._counter = 0;
this._lastReadStatus = 0;
this._lastGoodPowerReading = 0;
this.style = extend(true, this.style, {properties: {
stateImage: {
url: OPTOEMU_SNR_DR1_IMG,
tintMode: 'original'
},
power: {
display: 'billboard',
significantDigits: 2,
symbol: 'kW'
}
}});
};
util.inherits(IndustrialMonitor, Device);
IndustrialMonitor.prototype.init = function(config) {
config
.name('Industrial Monitor')
.type('industrial-monitor')
.state('ready')
.when('ready', {allow: ['make-not-ready']})
.when('not-ready', {allow: ['make-ready']})
.map('make-ready', this.makeReady)
.map('make-not-ready', this.makeNotReady)
.monitor('power')
.monitor('measurementStatus')
.monitor('measurementFreshness');
this._startMockData();
};
IndustrialMonitor.prototype.makeReady = function(cb) {
this.state = 'ready';
this._startMockData();
cb();
}
IndustrialMonitor.prototype.makeNotReady = function(cb) {
this.state = 'not-ready'
this._stopMockData();
cb();
}
IndustrialMonitor.prototype._startMockData = function(cb) {
var self = this;
this._timeOut = setInterval(function() {
self._counter += self._increment;
if (Math.random() > 0.2) {
self.power = self._lastGoodPowerReading = (Math.sin(degToRad(self._counter)) + 2.0);
if (self._lastReadStatus === 1) {
self.measurementStatus += 1;
} else {
self.measurementStatus = 1;
}
self.measurementFreshness = 0;
self._lastReadStatus = 1;
} else {
self.power = self._lastGoodPowerReading;
if (self._lastReadStatus === -1) {
self.measurementStatus -= 1;
} else {
self.measurementStatus = -1;
}
self.measurementFreshness += self._increment / 100;
self._lastReadStatus = -1;
}
}, 100);
}
IndustrialMonitor.prototype._stopMockData = function(cb) {
clearTimeout(this._timeOut);
}