|
const noble = require('noble') |
|
const serviceUUIDs = ["1809", "180f"] |
|
|
|
let Service, Characteristic |
|
|
|
module.exports = function (homebridge) { |
|
Service = homebridge.hap.Service |
|
Characteristic = homebridge.hap.Characteristic |
|
homebridge.registerAccessory('homebridge-lonometer', 'Lonometer', Lonometer) |
|
} |
|
|
|
class Lonometer { |
|
constructor (log, config) { |
|
if (config.name === undefined) { return log('Name missing from configuration.') } |
|
noble.on('stateChange', state => { |
|
if (state === 'poweredOn') { |
|
console.log('scanning...') |
|
noble.startScanning(serviceUUIDs, true) |
|
} else { |
|
noble.stopScanning() |
|
} |
|
}) |
|
noble.on('discover', peripheral => { |
|
if (peripheral.id === "84dd20ef106e") { |
|
noble.stopScanning() |
|
peripheral.on('connect', () => console.log("connected!")) |
|
let connect = () => { |
|
console.log("connecting...") |
|
peripheral.connect(err => { |
|
peripheral.discoverServices(serviceUUIDs, (err, services) => { |
|
services.forEach(service => { |
|
service.discoverCharacteristics([], (err, characteristics) => { |
|
characteristics.forEach(characteristic => { |
|
if (characteristic.uuid === "2a1c") { getTemperature(characteristic) } |
|
if (characteristic.uuid === "fff1") { } |
|
if (characteristic.uuid === "2a19") { getBatterylevel(characteristic) } |
|
}) |
|
}) |
|
}) |
|
}) |
|
}) |
|
} |
|
peripheral.on('disconnect', () => { |
|
console.log("disconnected!") |
|
setTimeout(connect, 15 * 60 * 1000) |
|
}) |
|
connect() |
|
} else { |
|
console.log('ignoring device with local name: ' + peripheral.advertisement.localName) |
|
} |
|
}) |
|
var informationService = new Service.AccessoryInformation() |
|
informationService |
|
.setCharacteristic(Characteristic.Name, 'lonometer') |
|
.setCharacteristic(Characteristic.Manufacturer, 'moritzmhmk') |
|
.setCharacteristic(Characteristic.Model, 'v0.0.1') |
|
.setCharacteristic(Characteristic.SerialNumber, '0000000001') |
|
|
|
let temperatureService = new Service.TemperatureSensor(config.name) |
|
// temperatureService.setCharacteristic(Characteristic.StatusActive, false) |
|
|
|
let humidityService = new Service.HumiditySensor(config.name) |
|
|
|
// temperatureService.setCharacteristic(Characteristic.StatusActive, true) |
|
// temperatureService.setCharacteristic(Characteristic.CurrentTemperature, temp++) |
|
|
|
let getTemperature = (characteristic) => { |
|
characteristic.subscribe(err => {}) |
|
characteristic.on('data', (data, isNotification) => { |
|
if (data[0] === 0 && data[1] === 1) { |
|
console.log("received data.") |
|
console.log(`${data[3] - 40}°C \t ${data[4]}% humidity`) |
|
temperatureService.setCharacteristic(Characteristic.CurrentTemperature, data[3] - 40) |
|
humidityService.setCharacteristic(Characteristic.CurrentRelativeHumidity, data[4]) |
|
} |
|
}) |
|
} |
|
let getBatterylevel = (characteristic) => {} |
|
|
|
this.services = [ informationService, temperatureService, humidityService ] |
|
} |
|
getServices () { |
|
return this.services |
|
} |
|
} |