Skip to content

Commit

Permalink
add verbose logging
Browse files Browse the repository at this point in the history
  • Loading branch information
groupsky committed Apr 21, 2024
1 parent 42cac82 commit 8ec881b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docker/automations/bots/irrigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = (name, {
valveControlTemplate = (state) => ({state}),
schedule,
duration,
verbose
}) => ({
start: ({mqtt}) => {
const interval = parser.parseExpression(schedule);
Expand All @@ -31,6 +32,10 @@ module.exports = (name, {

const status = computeWantedStatus()

if (verbose) {
console.log(`[${name}] state`, status.state, 'for', status.timeout / 60000, 'minutes')
}

mqtt.publish(valveControlTopic, valveControlTemplate(status.state))

setTimeout(update, status.timeout)
Expand Down
8 changes: 4 additions & 4 deletions docker/automations/bots/solar-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = (name, {
if (status == null || wantedStatus == null) return
if (status !== wantedStatus) {
if (verbose) {
console.log('state different than wanted, publishing', commandTopic, commandTemplate(wantedStatus))
console.log(`[${name}] state different than wanted, publishing`, commandTopic, commandTemplate(wantedStatus))
}
mqtt.publish(commandTopic, commandTemplate(wantedStatus))
}
Expand All @@ -42,8 +42,8 @@ module.exports = (name, {
const current = states.reduce((best, current) => best.eta > 0 || current.eta <= 0 && best.eta < current.eta ? current : best)
const next = states.reduce((best, current) => best.eta <= 0 || current.eta > 0 && best.eta > current.eta ? current : best)
if (verbose) {
console.log('current state', current.state, 'since', -Math.round(current.eta / 60000), 'm')
console.log('next state', next.state, 'in', Math.round(next.eta / 60000), 'm')
console.log(`[${name}] current state`, current.state, 'since', -Math.round(current.eta / 60000), 'm')
console.log(`[${name}] next state`, next.state, 'in', Math.round(next.eta / 60000), 'm')
}
wantedStatus = solarTimeStates[current.state]
setTimeout(computeWantedStatus, next.eta)
Expand All @@ -53,7 +53,7 @@ module.exports = (name, {
mqtt.subscribe(statusTopic, (payload) => {
status = Boolean(stateParser(payload))
if (verbose) {
console.log('status updated to', status)
console.log(`[${name}] status updated to`, status)
}
update()
})
Expand Down
9 changes: 8 additions & 1 deletion docker/automations/bots/timeout-emit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@ module.exports = (name, {
listenFilter = () => true,
timeout,
emitTopic,
emitValue
emitValue,
verbose
}) => ({
start: ({ mqtt }) => {
let timer = null

mqtt.subscribe(listenTopic, (payload) => {
if (listenFilter(payload)) {
if (verbose) {
console.log(`[${name}] received`, payload, timer ? 'timer already started': `starting timer for ${timeout/60000} minutes`)
}
if (!timer) {
timer = setTimeout(() => {
mqtt.publish(emitTopic, emitValue instanceof Function ? emitValue(payload) : emitValue)
}, timeout)
}
} else {
if (verbose) {
console.log(`[${name}] received`, payload, timer ? 'stopping timer': 'no pending timer')
}
if (timer) {
clearTimeout(timer)
timer = null
Expand Down

0 comments on commit 8ec881b

Please sign in to comment.