MCPcopy Create free account
hub / github.com/WebThingsIO/webthing-node / constructor

Method constructor

example/platform/adc/adc-property.js:27–62  ·  view source on GitHub ↗
(thing, name, value, metadata, config)

Source from the content-addressed store, hash-verified

25
26class AdcInProperty extends Property {
27 constructor(thing, name, value, metadata, config) {
28 super(thing, name, new Value(Number(value)),
29 {
30 '@type': 'LevelProperty',
31 title: (metadata && metadata.title) || `Level: ${name}`,
32 type: 'number',
33 readOnly: true,
34 description:
35 (metadata && metadata.description) ||
36 (`ADC Sensor on pin=${config.pin}`),
37 });
38 const self = this;
39 config.frequency = config.frequency || 1;
40 config.range = config.range || 0xFFF;
41 this.period = 1000.0 / config.frequency;
42 this.config = config;
43 this.port = adc.open(config, (err) => {
44 log(`log: ADC: ${self.getName()}: open: ${err} (null expected)`);
45 if (err) {
46 console.error(`error: ADC: ${self.getName()}: Fail to open:\
47 ${config.pin}`);
48 return null;
49 }
50 self.inverval = setInterval(() => {
51 let value = Number(self.port.readSync());
52 verbose(`log: ADC:\
53 ${self.getName()}: update: 0x${value.toString(0xF)}`);
54 value = Number(Math.floor(100.0 * value / self.config.range));
55 if (value !== self.lastValue) {
56 log(`log: ADC: ${self.getName()}: change: ${value}%`);
57 self.value.notifyOfExternalUpdate(value);
58 self.lastValue = value;
59 }
60 }, self.period);
61 });
62 }
63
64 close() {
65 try {

Callers

nothing calls this directly

Calls 3

readSyncMethod · 0.80
getNameMethod · 0.45

Tested by

no test coverage detected