-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
143 lines (127 loc) · 6.34 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
"use strict";
/*
* Created with @iobroker/create-adapter v1.31.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");
// Load your modules here, e.g.:
// const fs = require("fs");
class Aquarium extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "aquarium",
});
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
// this.on("objectChange", this.onObjectChange.bind(this));
// this.on("message", this.onMessage.bind(this));
this.on("unload", this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
// The adapters config (in the instance object everything under the attribute "native") is accessible via
// this.config:
this.log.info("config option1: " + this.config.option1);
this.log.info("config option2: " + this.config.option2);
this.log.info("##### CREATE OBJECTS ##### ");
const name = "Nano Cube";
await this.setObjectNotExists(name, { type: "device", common: { name: name }, native: {} });
await this.setObjectNotExists(name+".temperature", { type: "state", common: { name: "temperature", role: "value.temperature", write: true, read: true, type: "number", unit: "°C", min: 5, max: 30}, native: {}});
await this.setObjectNotExists(name+".KH", { type: "state", common: { name: "KH", role: "value", write: true, read: true, type: "number", unit: "°dKH"}, native: {}});
await this.setObjectNotExists(name+".GH", { type: "state", common: { name: "GH", role: "value", write: true, read: true, type: "number", unit: "°dGH"}, native: {}});
await this.setObjectNotExists(name+".Ca", { type: "state", common: { name: "Ca", role: "value", write: true, read: true, type: "number", unit: "mg/l"}, native: {}});
await this.setObjectNotExists(name+".Mg", { type: "state", common: { name: "Mg", role: "value", write: true, read: true, type: "number", unit: "mg/l"}, native: {}});
await this.setObjectNotExists(name+".pH", { type: "state", common: { name: "pH", role: "value", write: true, read: true, type: "number"}, native: {}});
await this.setObjectNotExists(name+".NH4", { type: "state", common: { name: "NH4", role: "value", write: true, read: true, type: "number", unit: "mg/l"}, native: {}});
await this.setObjectNotExists(name+".NO2", { type: "state", common: { name: "NO2", role: "value", write: true, read: true, type: "number", unit: "mg/l"}, native: {}});
await this.setObjectNotExists(name+".NO3", { type: "state", common: { name: "NO3", role: "value", write: true, read: true, type: "number", unit: "mg/l"}, native: {}});
await this.setObjectNotExists(name+".PO4", { type: "state", common: { name: "PO4", role: "value", write: true, read: true, type: "number", unit: "mg/l"}, native: {}});
await this.setObjectNotExists(name+".SiO2", { type: "state", common: { name: "SiO2", role: "value", write: true, read: true, type: "number", unit: "mg/l"}, native: {}});
await this.setObjectNotExists(name+".FE", { type: "state", common: { name: "FE", role: "value", write: true, read: true, type: "number", unit: "mg/l"}, native: {}});
await this.setObjectNotExists(name+".K", { type: "state", common: { name: "K", role: "value", write: true, read: true, type: "number", unit: "mg/l"}, native: {}});
await this.setObjectNotExists(name+".Cu", { type: "state", common: { name: "Cu", role: "value", write: true, read: true, type: "number", unit: "mg/l"}, native: {}});
await this.setObjectNotExists(name+".O2", { type: "state", common: { name: "O2", role: "value", write: true, read: true, type: "number", unit: "mg/l"}, native: {}});
await this.setObjectNotExists(name+".CO2", { type: "state", common: { name: "CO2", role: "value", write: true, read: true, type: "number", unit: "mg/l"}, native: {}});
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
// Here you must clear all timeouts or intervals that may still be active
// clearTimeout(timeout1);
// clearTimeout(timeout2);
// ...
// clearInterval(interval1);
callback();
} catch (e) {
callback();
}
}
// If you need to react to object changes, uncomment the following block and the corresponding line in the constructor.
// You also need to subscribe to the objects with `this.subscribeObjects`, similar to `this.subscribeStates`.
// /**
// * Is called if a subscribed object changes
// * @param {string} id
// * @param {ioBroker.Object | null | undefined} obj
// */
// onObjectChange(id, obj) {
// if (obj) {
// // The object was changed
// this.log.info(`object ${id} changed: ${JSON.stringify(obj)}`);
// } else {
// // The object was deleted
// this.log.info(`object ${id} deleted`);
// }
// }
/**
* 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.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
} else {
// The state was deleted
this.log.info(`state ${id} deleted`);
}
}
// If you need to accept messages in your adapter, uncomment the following block and the corresponding line in the constructor.
// /**
// * Some message was sent to this instance over message box. Used by email, pushover, text2speech, ...
// * Using this method requires "common.messagebox" property to be set to true in io-package.json
// * @param {ioBroker.Message} obj
// */
// onMessage(obj) {
// if (typeof obj === "object" && obj.message) {
// if (obj.command === "send") {
// // e.g. send email or pushover or whatever
// this.log.info("send command");
// // Send response in callback if required
// if (obj.callback) this.sendTo(obj.from, obj.command, "Message received", obj.callback);
// }
// }
// }
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Aquarium(options);
} else {
// otherwise start the instance directly
new Aquarium();
}