-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (139 loc) · 6.26 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
/*** Astronomy Z-Way HA module *******************************************
Version: 1.10
(c) Maroš Kollár, 2015-2017
-----------------------------------------------------------------------------
Author: Maroš Kollár <maros@k-1.com>
Description:
This module checks weather updates via weatherundergound.com
******************************************************************************/
function Astronomy (id, controller) {
// Call superconstructor first (AutomationModule)
Astronomy.super_.call(this, id, controller);
this.latitude = undefined;
this.longitude = undefined;
this.vDev = {};
this.timer = undefined;
}
inherits(Astronomy, AutomationModule);
_module = Astronomy;
// Helper function
function round(number) {
var factor = Math.pow(10, 2);
return Math.round(number * factor) / factor;
}
// ----------------------------------------------------------------------------
// --- Module instance initialized
// ----------------------------------------------------------------------------
Astronomy.prototype.events = [
'sunrise', // sunrise (top edge of the sun appears on the horizon)
'sunriseEnd', // sunrise ends (bottom edge of the sun touches the horizon)
'goldenHourEnd', // morning golden hour (soft light, best time for photography) ends
'solarNoon', // solar noon (sun is in the highest position)
'goldenHour', // evening golden hour starts
'sunsetStart', // sunset starts (bottom edge of the sun touches the horizon)
'sunset', // sunset (sun disappears below the horizon, evening civil twilight starts)
'dusk', // dusk (evening nautical twilight starts)
'nauticalDusk', // nautical dusk (evening astronomical twilight starts)
'night', // night starts (dark enough for astronomical observations)
'nadir', // nadir (darkest moment of the night, sun is in the lowest position)
'nightEnd', // night ends (morning astronomical twilight starts)
'nauticalDawn', // nautical dawn (morning nautical twilight starts)
'dawn' // dawn (morning nautical twilight ends, morning civil twilight starts)
];
Astronomy.prototype.init = function (config) {
Astronomy.super_.prototype.init.call(this, config);
var self = this;
// See https://github.com/mourner/suncalc
executeFile(self.moduleBasePath()+"/suncalc.js");
self.latitude = config.latitude.toString();
self.longitude = config.longitude.toString();
config.createAltitudeDevice = true;
var langFile = self.controller.loadModuleLang("Astronomy");
_.each(self.events,function(event) {
self[event+'Timer'] = undefined;
});
_.each(['altitude','azimuth'],function(type) {
var configKey = 'create' + type.charAt(0).toUpperCase() + type.substring(1) + 'Device';
var configDevice = config[configKey];
if (configDevice === true
|| _.isUndefined(configDevice)) {
self.vDev[type]= self.controller.devices.create({
deviceId: "Astronomy_"+self.id+"_"+type,
defaults: {
deviceType: "sensorMultilevel",
metrics: {
icon: 'icon.png',
title: langFile[type+'_device']
}
},
overlay: {
probeType: 'astronomy_sun_'+type,
metrics: {
scaleTitle: "°"
}
},
moduleId: self.id
});
}
});
self.interval = setInterval(function() {
self.updateCalculation(self);
}, 60*1000);
self.updateCalculation();
};
Astronomy.prototype.stop = function () {
var self = this;
_.each(['altitude','azimuth'],function(type) {
if (typeof(self.vDev[type]) !== 'undefined') {
self.controller.devices.remove(self.vDev[type].id);
self.vDev[type] = undefined;
}
});
clearInterval(self.interval);
self.timer = undefined;
Astronomy.super_.prototype.stop.call(self);
};
// ----------------------------------------------------------------------------
// --- Module methods
// ----------------------------------------------------------------------------
Astronomy.prototype.updateCalculation = function () {
var self = this;
//var langFile = self.controller.loadModuleLang("Astronomy");
var now = new Date();
var position = SunCalc.getPosition(now, self.config.latitude, self.config.longitude);
var times = SunCalc.getTimes(now, self.config.latitude, self.config.longitude);
var azimuth = round(position.azimuth * 180 / Math.PI + 180);
var altitude = round(position.altitude * 180 / Math.PI);
var previous = parseFloat(self.vDev.altitude.get("metrics:level") || altitude);
var mode;
console.log("[Astronomy] Calculate");
if (altitude < -2) {
mode = 'night';
} else {
mode = 'day';
}
if (! _.isUndefined(self.vDev.altitude)) {
self.vDev.altitude.set("metrics:icon", "/ZAutomation/api/v1/load/modulemedia/Astronomy/altitude_"+mode+".png");
self.vDev.altitude.set("metrics:level",altitude);
self.vDev.altitude.set("metrics:azimuth",azimuth);
self.vDev.altitude.set("metrics:altitude",altitude);
self.vDev.altitude.set("metrics:trend",(previous <= altitude) ? 'rise':'set');
}
if (! _.isUndefined(self.vDev.azimuth)) {
self.vDev.azimuth.set("metrics:icon", "/ZAutomation/api/v1/load/modulemedia/Astronomy/azimuth_"+mode+".png");
self.vDev.azimuth.set("metrics:level",azimuth);
self.vDev.azimuth.set("metrics:azimuth",azimuth);
self.vDev.azimuth.set("metrics:altitude",altitude);
}
_.each(self.events,function(event) {
if (! _.isUndefined(self.vDev.altitude)) {
self.vDev.altitude.set("metrics:"+event,times[event]);
}
if (times[event].getHours() === now.getHours()
&& times[event].getMinutes() === now.getMinutes()
&& times[event].getDate() === now.getDate()) {
console.log("[Astronomy] Event "+event);
self.controller.emit("astronomy."+event);
}
});
};