JavaScript beginner here, I'm trying to get something going in Nodejs using hap-nodejs module.
My Main file looks like this:
const config = require('../config');
const Syno = require('synology');
const docker = new Docker(config.dockerode);
const storage = require('node-persist');
const os = require('os');
storage.initSync();
const {Bridge, uuid, Categories} = require('hap-nodejs');
const TemperatureSensorAccessory = require('./TempGageAccessory');
const name = config.bridge.deviceName.replace('$hostname', os.hostname()).replace(/\./, ' ');
const bridge = new Bridge(name, uuid.generate('Remote Bridge'));
bridge.publish({
username: config.bridge.mac,
port: config.bridge.port,
pincode: config.bridge.pincode,
category: Categories.BRIDGE
});
async function publishSynology()
{
const dev = new TemperatureSensorAccessory('NAS_SYS_TEMP');
bridge.addBridgedAccessories({
accessory: dev.getAccessory()
});
}
publishSynology();
and my /TempGageAccessory.js file where the problem is this one here:
const {Accessory, Service, Characteristic, uuid, Categories} = require('hap-nodejs');
const Syno = require('synology');
const {emitter, docker} = require('./DockerKit');
module.exports = class TemperatureSensorAccessory {
constructor(name) {
console.log('Constructing ' + name);
this.TemperatureSensor = {
name,
model: 'Synology NAS Temperature Sensor',
category: Categories.SENSOR,
description: name,
setTemperature: function () {
let syno = new Syno({
host : '10.0.0.1',
port : '5656',
secure : 'true',
user : 'user',
password: 'password'
});
syno.query('/webapi/entry.cgi', {
api : 'SYNO.Core.System',
version: 1,
method : 'info'
}, function(err, data) {
if (err) return console.error(err);
console.log(data['data']['sys_temp']);
this.temperature = parseFloat(data['data']['sys_temp']);
return parseFloat(data['data']['sys_temp']);
});
},
temperature: 0.0,
getTemperature: async function () {
console.log('[' + name + '] Getting Temperature' + this.temperature);
return this.temperature;
},
uuid: uuid.generate('hap-nodejs:accessoires:temperaturesensor:synology:' + name),
accessory: null
};
const that = this;
that.TemperatureSensor.setTemperature();
that.getAccessory().getService(Service.TemperatureSensor).getCharacteristic(Characteristic.On).updateValue(that.TemperatureSensor.temperature);
}
getAccessory() {
if (!this.TemperatureSensor.accessory) {
let acc;
acc = new Accessory(this.TemperatureSensor.name, this.TemperatureSensor.uuid);
acc.username = '8a:bc:80:74:de:16';
acc.pincode = '123-45-678';
acc
.addService(Service.TemperatureSensor, this.TemperatureSensor.name)
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', async(cb) => {
// return our current value
cb(null, await this.TemperatureSensor.getTemperature());
});
this.TemperatureSensor.accessory = acc;
return acc;
} else return this.TemperatureSensor.accessory;
}
};
I got everything almost there, my rest call is working fine and I'm returning my data. But when I try to assign the result of my query to property temperature I get the following error:
TypeError: Cannot set property 'temperature' of undefined
Please, can someone explain what I'm doing wrong ? I'm no Javascript Developer, first time playing with it coming from C#/Python.