-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
229 lines (209 loc) · 7.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
'use strict'
/*
* Created with @iobroker/create-adapter v1.9.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')
// Time Modules
const cron = require('node-cron') // Cron Schedulervar
class Timecounter extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
constructor (options) {
super({
...options,
name: 'timecounter'
})
this.dicCountIfGreaterOrEqual = {}
this.on('ready', this.onReady.bind(this))
this.on('objectChange', this.onObjectChange.bind(this))
this.on('stateChange', this.onStateChange.bind(this))
// this.on("message", this.onMessage);
this.on('unload', this.onUnload.bind(this))
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady () {
await this.initialObjects()
this.subscribeForeignObjects('*')
// repeat evey minute the calculation of the totalEnergy
cron.schedule('* * * * *', async () => {
this.log.debug('cron started')
for (let idobject in this.dicCountIfGreaterOrEqual) {
await this.calcNewTimeinCounter(idobject)
}
})
}
/**
* Is called if a subscribed object changes
* @param {string} id
* @param {ioBroker.Object | null | undefined} obj
*/
async onObjectChange (id, obj) {
let settingsforme = (obj && obj.common && obj.common.custom && obj.common.custom[this.namespace])
let oldsettingsexist = (id in this.dicCountIfGreaterOrEqual)
if (settingsforme || oldsettingsexist) { await this.initialObjects() }
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
async onStateChange (id, state) {
if (state && (id in this.dicCountIfGreaterOrEqual)) {
this.log.info(id + ' state changed')
await this.calcNewTimeinCounter(id)
// @ts-ignore
this.dicIdLastValue[id] = await this.getForeignStateAsync(id)
}
}
/**
* create for every enabled object the needed stats and set it to initial it
*/
async initialObjects () {
this.log.info('inital all Objects')
// all unsubscripe to begin completly new
this.unsubscribeForeignStates('*')
// delete all dics
this.dicCountIfGreaterOrEqual = {}
this.dicIdTotalTime = {}
this.dicIdLastValue = {}
this.dicIdUnit = {}
// read out all Objects
let objects = await this.getForeignObjectsAsync('')
for (let idobject in objects) {
let iobrokerObject = objects[idobject]
// only do something when enabled and MaxPowerset
if (iobrokerObject && iobrokerObject.common && iobrokerObject.common.custom && iobrokerObject.common.custom[this.namespace] && iobrokerObject.common.custom[this.namespace].enabled && iobrokerObject.common.custom[this.namespace].idTotalTime && iobrokerObject.common.custom[this.namespace].countIfGreaterOrEqual && iobrokerObject.common.custom[this.namespace].unit) {
this.log.info('initial (check OK): ' + iobrokerObject._id)
// @ts-ignore
this.dicIdTotalTime[iobrokerObject._id] = iobrokerObject.common.custom[this.namespace].idTotalTime
// @ts-ignore
this.dicCountIfGreaterOrEqual[iobrokerObject._id] = iobrokerObject.common.custom[this.namespace].countIfGreaterOrEqual
// @ts-ignore
this.dicIdLastValue[iobrokerObject._id] = await this.getForeignStateAsync(iobrokerObject._id)
// @ts-ignore
this.dicIdUnit[iobrokerObject._id] = iobrokerObject.common.custom[this.namespace].unit
await this.createObjectsForId(iobrokerObject, iobrokerObject.common.custom[this.namespace].unit)
this.log.debug('subscribeForeignStates ' + iobrokerObject._id)
await this.subscribeForeignStatesAsync(iobrokerObject._id)
await this.calcNewTimeinCounter(iobrokerObject._id)
this.log.debug('initial done ' + iobrokerObject._id)
}
}
this.log.info('initial completed')
}
/**
* calc the Total Energy size the last Change and add it
* @param {string} id
*/
async calcNewTimeinCounter (id) {
// Die Aktuelle Power auslesen
// EnergyTotal auslesen, timestamp und aktueller wert wird benötigt
let objTotalTime = await this.getForeignStateAsync(this.getIdTotalTime(id))
let toAddTime = 0
let newTotalTime = 0
// Wenn Datenpunkt noch keinen Wert hat nichts berechnen
if (objTotalTime) {
if (!objTotalTime.val) {
objTotalTime.val = 0
}
// @ts-ignore
let lastValue = this.dicIdLastValue[id]
// @ts-ignore
let ifGreaterOrEquan = this.dicCountIfGreaterOrEqual[id]
if (lastValue && lastValue.val && lastValue.val >= ifGreaterOrEquan) {
// berechnen wieviel Minuten dazukommen (alles auf 2 nachkomma runden)
// @ts-ignore
let unit = this.dicIdUnit[id]
let Multi = 1
let round = 10000
if (unit === 'min') {
Multi = 60
round = 100
} else if (unit === 'sec') {
Multi = 3600
round = 10
}
toAddTime = Math.round(Multi * (((new Date().getTime()) - objTotalTime.ts) / 3600000) * round) / round
newTotalTime = Math.round((objTotalTime.val + toAddTime) * round) / round
} else {
toAddTime = -1
}
}
if (toAddTime >= 0) {
// neuen wert setzen
this.log.debug(this.getIdTotalTime(id) + ' set ' + newTotalTime + ' (added:' + toAddTime + ')')
await this.setForeignStateAsync(this.getIdTotalTime(id), { val: newTotalTime, ack: true })
} else {
this.log.debug(id + ' update timestamp')
await this.setForeignStateAsync(this.getIdTotalTime(id), { ts: new Date().getTime(), ack: true })
}
}
/**
* create Datapoints needed for a datapoint
* @param {ioBroker.Object} iobrokerObject
* @param {string} unit
*/
async createObjectsForId (iobrokerObject, unit) {
this.log.debug('create Datapoints for ' + iobrokerObject._id + ' if not exists')
let idob = this.getIdTotalTime(iobrokerObject._id)
this.log.debug('create ' + idob + ' if not exists')
await this.extendForeignObject(idob, {
type: 'state',
common: {
// @ts-ignore
name: iobrokerObject.common.name + this.dicIdTotalTime[iobrokerObject._id],
role: 'value.interval',
type: 'number',
desc: 'Created by timecounter',
unit: unit,
read: true,
write: true,
def: 0
},
native: {}
})
}
/**
* gibt die id für Virtual_Energy_Total zurück
* @param {string} id
*/
getIdParent (id) {
return id.substr(0, id.lastIndexOf('.') + 1)
}
/**
* Gibt die ID für Virtual_Energy_Power zurück
* @param {string} id
*/
getIdTotalTime (id) {
// @ts-ignore
return this.getIdParent(id) + this.dicIdTotalTime[id]
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
async onUnload (callback) {
try {
this.log.info('cleaned everything up...')
callback()
} catch (e) {
callback()
}
}
}
// @ts-ignore
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Timecounter(options)
} else {
// otherwise start the instance directly
new Timecounter()
}