-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
136 lines (111 loc) · 5.51 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var Service, Characteristic;
import {nordpool} from 'nordpool'
import {schedule} from 'node-cron'
const prices = new nordpool.Prices()
export default (homebridge) => {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-nordpool", "Nordpool", Hb_Nordpool);
}
function Hb_Nordpool(log, config) {
this.log = log;
// url info
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "@ttopholm";
this.model = config["model"] || "Model not available";
this.serial = config["serial"] || "Non-defined serial";
this.VAT = config['VAT'] || 25;
this.area = config['area'] || 'DK1'
this.currency = config['currency'] || 'DKK'
this._currentPrice = 0;
this._maxHourPrice = 0;
this._minHourPrice = 0;
this._day_prices = [];
const hourlyJob = schedule('0 1-23 * * * * ', () => {
this.getCurrentPrice()
});
const dailyJob = schedule('0 0 * * *', () => {
this.getDailyPrices(Date.now()).then(() => {
this.getCurrentPrice();
});
});
}
Hb_Nordpool.prototype = {
getPrice: function (callback) {
callback(null, this._currentPrice);
},
getOccupancyLowState: function(callback) {
const currentHour = new Date().getHours()
if (currentHour == this._minHourPrice) {
return Characteristic.OccupancyDetected.OCCUPANCY_DETECTED;
}
return Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED;
},
getOccupancyHighState: function(callback) {
const currentHour = new Date().getHours()
if (currentHour == this._maxHourPrice) {
return Characteristic.OccupancyDetected.OCCUPANCY_DETECTED;
}
return Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED;
},
identify: function (callback) {
this.log("Identify requested!");
callback(); // success
},
getDailyPrices: function() {
return new Promise((resolve, reject) => {
prices.hourly({area:this.area, currency:this.currency, date: Date.now()}).then(results => {
this._day_prices = [];
results.forEach(data => this._day_prices.push(Math.round(data.value * ((100+this.VAT)/100))))
results.sort(function(a,b) {return a.value - b.value})
this._maxHourPrice = new Date(results.at(-1).date).getHours()
this._minHourPrice = new Date(results.at(0).date).getHours()
resolve()
})
})
},
getCurrentPrice: function() {
const price = this._day_prices[parseInt(new Date().getHours())]
this._currentPrice = price
if (this.lightSensorService) {
this.lightSensorService.setCharacteristic(Characteristic.CurrentAmbientLightLevel, price);
}
const currentHour = new Date().getHours()
if (currentHour == this._minHourPrice && this.occupancyServiceLow && this.occupancyServiceHigh) {
this.occupancyServiceLow.getCharacteristic(Characteristic.OccupancyDetected).updateValue(Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED);
this.occupancyServiceHigh.getCharacteristic(Characteristic.OccupancyDetected).updateValue(Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED);
this.occupancyServiceLow.getCharacteristic(Characteristic.OccupancyDetected).updateValue(Characteristic.OccupancyDetected.OCCUPANCY_DETECTED);
} else if (currentHour == this._maxHourPrice && this.occupancyServiceLow && this.occupancyServiceHigh) {
this.occupancyServiceLow.getCharacteristic(Characteristic.OccupancyDetected).updateValue(Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED);
this.occupancyServiceHigh.getCharacteristic(Characteristic.OccupancyDetected).updateValue(Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED);
this.occupancyServiceHigh.getCharacteristic(Characteristic.OccupancyDetected).updateValue(Characteristic.OccupancyDetected.OCCUPANCY_DETECTED);
}
},
getServices: function () {
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
this.lightSensorService = new Service.LightSensor(this.name);
this.lightSensorService
.getCharacteristic(Characteristic.CurrentAmbientLightLevel)
.on('get', this.getPrice.bind(this));
this.occupancyServiceLow = new Service.OccupancySensor(this.name + "_lowPrice", this.name + "_lowPrice");
this.occupancyServiceLow.setCharacteristic(Characteristic.OccupancyDetected, Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED);
/*this.occupancyServiceLow
.getCharacteristic(Characteristic.OccupancyDetected)
.onGet(this.getOccupancyLowState.bind(this));*/
this.occupancyServiceHigh = new Service.OccupancySensor(this.name + "_highPrice", this.name + "_highPrice");
this.occupancyServiceHigh.setCharacteristic(Characteristic.OccupancyDetected, Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED);
/*this.occupancyServiceHigh
.getCharacteristic(Characteristic.OccupancyDetected)
.onGet(this.getOccupancyHighState.bind(this));
*/
//Get the prices first
this.getDailyPrices(Date.now()).then(() => {
this.getCurrentPrice();
});
return [this.informationService, this.lightSensorService, this.occupancyServiceLow, this.occupancyServiceHigh];
}
};