-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS18B20-M16-P32_accessory.ts
89 lines (79 loc) · 2.85 KB
/
DS18B20-M16-P32_accessory.ts
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
// here's a DS18B20 (negative values) temperature sensor device that we'll expose to HomeKit
import {
Accessory,
Categories,
Characteristic,
CharacteristicEventTypes,
CharacteristicValue,
CharacteristicGetCallback,
Service,
uuid
} from '../';
import http from "http";
type Options = {
host: string,
path: string
};
const opts : Options = {
host: '192.168.0.16',
path: '/sec/?pt=32&cmd=get'
};
class SensorClass {
currentTemperature: CharacteristicValue = 50;
name: CharacteristicValue = 'Гараж'; //name of accessory
getTemperature () {
let temp : any[] = [];
const req = http.request(opts, (res) => {
let data : string = '';
res.on('data', (chunk: string) => {
data += chunk;
});
res.on('end', () => {
temp = data.split(":");
SENS.currentTemperature = parseFloat(temp[1]);
SensorAccessory
.getService(Service.TemperatureSensor)!.getCharacteristic(Characteristic.CurrentTemperature).updateValue(SENS.currentTemperature);
});
});
req.on("error", (e) => {
console.error(e);
});
req.end();
return this.currentTemperature;
}
}
const SENS = new SensorClass();
// Generate a consistent UUID for our Temperature Sensor Accessory that will remain the same
// even when restarting our server. We use the `uuid.generate` helper function to create
// a deterministic UUID based on an arbitrary "namespace" and the string "temperature-sensor".
const sensorUUID = uuid.generate('hap-nodejs:accessories:temperature-sensor' + SENS.name);
// This is the Accessory that we'll return to HAP-NodeJS that represents our light.
var SensorAccessory = exports.accessory = new Accessory(SENS.name as string, sensorUUID);
// Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
// @ts-ignore
SensorAccessory.username = "C1:5D:3A:AE:5E:FA";
// @ts-ignore
SensorAccessory.pincode = "031-45-154";
// @ts-ignore
SensorAccessory.category = Categories.SENSOR;
SensorAccessory
.getService(Service.AccessoryInformation)!
.setCharacteristic(Characteristic.Manufacturer, "[KONTUR-HOME]")
.setCharacteristic(Characteristic.Model, "DS18B20")
.setCharacteristic(Characteristic.SerialNumber, "170221");
// Add the actual TemperatureSensor Service.
// We can see the complete list of Services and Characteristics in `lib/gen/HomeKit.ts`
SensorAccessory
.addService(Service.TemperatureSensor)!
.getCharacteristic(Characteristic.CurrentTemperature)!
.setProps({
minValue: -50,
maxValue: 150
})
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
callback(null, SENS.getTemperature());
});
// our temperature reading every 60 seconds
//setInterval(function() {
// SENSOR.getTemperature();
//}, 60000);