-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
170 lines (146 loc) · 5.13 KB
/
main.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
'use strict';
/*
* Created with @iobroker/create-adapter v2.3.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
const { Zones, Zone } = require('./lib/zone.js');
const { Programs, Program } = require('./lib/program.js');
const { Schedules, Schedule } = require('./lib/schedule.js');
const Weather = require('./lib/weather.js');
/*
Zone:
Nummer
Name
STATE Device
ON_TIME Device
Standardmäßige Bewässerungszeit
Programm:
Name
Zonenliste
Liste Bewässerungszeit
Parallel/Sequentiell
enable/disable
program läuft
Schedule:
wochentag Uhrzeit, ....
Zonenliste:
program Nummer
Zonennummer
Order
mit/nach vorhergegangenen
Bewässerungszeit
Wetterabhängig
Wetterbeeinflussung:
Temperatur
Wind
Regen
Vorhersage
*/
class IrrigationControl extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'irrigation-control',
});
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
// this.on('objectChange', this.onObjectChange.bind(this));
// this.on('message', this.onMessage.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
// Reset the connection indicator during startup
this.setState('info.connection', false, true);
this.log.debug('Config data: ' + JSON.stringify(this.config));
this.zones = Zones.getInstance(this);
this.progs = Programs.getInstance(this);
this.schedules = Schedules.getInstance(this);
this.weather = Weather.getInstance(this);
await Zones.create_config(this.config.zones);
await Programs.create_config(this.config.programs);
await Schedules.create_config(this.config.schedules);
await this.zones.initialize();
await this.progs.initialize();
await this.schedules.initialize();
await this.weather.initialize();
//Zones.create_zone(this, new Zone('Rasen hinten links', 'statedev', 'ontimedev', 60, true));
//Zones.create_zone(this, new Zone('Rasen hinten rechts', 'statedev', 'ontimedev', 60, true));
//Programs.create_program(this, new Program('Rasenflächen', [{ 'name': 'Rasen hinten links', 'duration' : 0 }, { 'name': 'Rasen hinten rechts', 'duration': 100} ], false, true));
//Programs.create_program(this, new Program('Beete', [{ 'name': 'Rasen hinten links', 'duration' : 5 }, { 'name': 'Rasen hinten rechts', 'duration': 8} ], false, true));
//Schedules.create_schedule(this, new Schedule('Test 1', '* * * * 0,10,20,30,40,50', 'Rasenflächen'));
this.setState('info.connection', true, true);
this.setState('info.active', 0, true);
this.log.info('IrrigationControl adapter started');
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
if (this.zones) this.zones.stopAll();
callback();
} catch (e) {
callback();
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
if (state) {
// The state was changed
this.log.debug(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
} else {
// The state was deleted
this.log.debug(`state ${id} deleted`);
}
if (!this.zones || !this.progs) return;
if (id.startsWith(this.namespace)) {
const comp = id.split('.');
const i = id.substring(this.namespace.length+1);
if (comp[2] == 'zones' && this.zones) {
this.zones.stateChange(i, state);
if (comp.at(-1) == 'state') this.progs.zoneChange(i, state);
}
if (comp[2] == 'programs' && this.progs) this.progs.stateChange(i, state);
if (comp[2] == 'schedules' && this.schedules) this.schedules.stateChange(i, state);
} else {
if (id == this.config.rainSensor) {
this.weather.stateChange(id, state);
} else {
this.zones.stateChange(id, state);
}
}
}
onObjectChange(id, obj) {
if (obj) {
this.log.debug(`object ${id} changed: (${JSON.stringify(obj)})`);
// Objekt geändert
} else {
this.log.debug(`object ${id} deleted`);
// Objekt gelöscht
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new IrrigationControl(options);
} else {
// otherwise start the instance directly
new IrrigationControl();
}