-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
173 lines (151 loc) · 4.65 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"use strict";
var inherits = require("util").inherits;
var debug = require("debug")("homebridge-seasons"),
seasonCalculator = require("date-season");
var Service,
Characteristic,
CustomUUID = {
SeasonService: "ca741310-c62e-454b-a63b-3a1db3ca2c3a",
SeasonCharacteristic: "9382ccde-6cab-42e7-877a-2df98b8d0b66",
SeasonNameCharacteristic: "02e4c0e3-44f9-44b8-8667-98f54b376ce4",
},
SeasonService,
SeasonCharacteristic,
SeasonNameCharacteristic;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-seasons", "Seasons", SeasonsPlatform);
SeasonService = function(displayName, subtype) {
Service.call(this, displayName, CustomUUID.SeasonService, subtype);
};
inherits(SeasonService, Service);
SeasonCharacteristic = function() {
Characteristic.call(this, "Season", CustomUUID.SeasonCharacteristic);
this.setProps({
format: Characteristic.Formats.UINT8,
maxValue: 3,
minValue: 0,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(SeasonCharacteristic, Characteristic);
SeasonNameCharacteristic = function() {
Characteristic.call(this, "Season Name", CustomUUID.SeasonNameCharacteristic);
this.setProps({
format: Characteristic.Formats.STRING,
perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(SeasonNameCharacteristic, Characteristic);
}
function SeasonsPlatform(log, config) {
this.log = log;
this.config = config;
}
SeasonsPlatform.prototype = {
accessories: function(callback) {
this.accessories = [];
this.accessories.push(new SeasonsAccessory(this));
callback(this.accessories);
}
}
function SeasonsAccessory(platform) {
this.log = platform.log;
this.config = platform.config;
this.name = "Season";
this.informationService = new Service.AccessoryInformation();
this.informationService.setCharacteristic(Characteristic.Manufacturer, "Homebridge Seasons");
this.informationService.setCharacteristic(Characteristic.Model, "github.com/naofireblade/homebridge-seasons");
// Get calendar type from config
this.calendar = ("calendar" in this.config ? this.config["calendar"] : "meteorologic");
// Get hemisphere from config
this.hemisphere = ("hemisphere" in this.config ? this.config["hemisphere"] : "north");
// Create service and add characteristics depending on config
this.seasonService = new SeasonService(this.name);
if (this.config["display"] === "both" || this.config["display"] === "number") {
this.seasonService.addCharacteristic(SeasonCharacteristic);
this.seasonService.getCharacteristic(SeasonCharacteristic).on("get", this.getCurrentSeason.bind(this));
}
if (this.config["display"] === "both" || this.config["display"] === "name") {
this.seasonService.addCharacteristic(SeasonNameCharacteristic);
this.seasonService.getCharacteristic(SeasonNameCharacteristic).on("get", this.getCurrentSeasonName.bind(this));
}
}
SeasonsAccessory.prototype = {
identify: function (callback) {
debug("Identify requested");
callback();
},
getServices: function () {
return [this.informationService, this.seasonService];
},
getCurrentSeason: function(callback) {
let that = this;
this.getCurrentSeasonName(function (error, result) {
let season;
if (result === "Spring") {
season = 0;
}
else if (result === "Summer") {
season = 1;
}
else if (result === "Autumn") {
season = 2;
}
else if (result === "Winter") {
season = 3;
}
else {
that.log.error("Unkown season " + result);
}
callback(false, season);
});
},
getCurrentSeasonName: function(callback) {
let seasonName;
if (this.calendar === "meteorologic") {
debug("Using meteorologic calendar to get current season");
let month = (new Date()).getMonth() + 1;
switch (month) {
case 1:
case 2:
case 12:
seasonName = "Winter";
break;
case 3:
case 4:
case 5:
seasonName = "Spring";
break;
case 6:
case 7:
case 8:
seasonName = "Summer";
break;
case 9:
case 10:
case 11:
seasonName = "Autumn";
break;
}
}
else {
debug("Using astronomic calendar to get current season");
let northernHemisphere = this.hemisphere === "north";
if (northernHemisphere) {
debug("Hemisphere is north");
}
else {
debug("Hemisphere is south");
}
let astronomicCalendar = seasonCalculator({ north: northernHemisphere, autumn: true });
seasonName = astronomicCalendar(new Date());
}
debug("Current season is " + seasonName);
callback(false, seasonName);
}
}