Skip to content

Commit

Permalink
Expose alarm summary over HTTP too, remove alarm history
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalle19 committed Aug 15, 2024
1 parent 23c68fa commit 4afa0d5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 45 deletions.
2 changes: 0 additions & 2 deletions app/enervent.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ export const AVAILABLE_SETTINGS = {
'defrostingAllowed': 55,
}

export const ALARM_REGISTERS_START = 385
export const ALARM_REGISTERS_END = 518
export const AVAILABLE_ALARMS = {
// Alarm number
// Name and descr based on Enervent EN EDA Modbus regirsters: 3x0385
Expand Down
8 changes: 6 additions & 2 deletions app/http.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
getSettings,
setMode as modbusSetMode,
setSetting as modbusSetSetting,
getAlarmHistory,
getDeviceState,
getNewestAlarm,
getAlarmSummary,
} from './modbus.mjs'
import { createLogger } from './logger.mjs'

Expand All @@ -20,15 +21,18 @@ const root = async (req, res) => {
const summary = async (modbusClient, req, res) => {
try {
let modeSummary = await getModeSummary(modbusClient)
const newestAlarm = await getNewestAlarm(modbusClient)

const summary = {
// TODO: Remove in next major version
'flags': modeSummary,
'modes': modeSummary,
'readings': await getReadings(modbusClient),
'settings': await getSettings(modbusClient),
'deviceInformation': await getDeviceInformation(modbusClient),
'alarmHistory': await getAlarmHistory(modbusClient),
'deviceState': await getDeviceState(modbusClient),
'alarmSummary': await getAlarmSummary(modbusClient),
'activeAlarm': newestAlarm?.state === 2 ? newestAlarm : null,
}

res.json(summary)
Expand Down
61 changes: 23 additions & 38 deletions app/modbus.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Mutex } from 'async-mutex'
import { createLogger } from './logger.mjs'
import {
ALARM_REGISTERS_END,
ALARM_REGISTERS_START,
AUTOMATION_TYPE_LEGACY_EDA,
AUTOMATION_TYPE_MD,
AVAILABLE_ALARMS,
Expand Down Expand Up @@ -339,53 +337,40 @@ export const getDeviceInformation = async (modbusClient) => {
return deviceInformation
}

export const getAlarmHistory = async (modbusClient) => {
let alarmHistory = []
export const getAlarmSummary = async (modbusClient) => {
let alarmSummary = { ...AVAILABLE_ALARMS }
const newestAlarm = await getNewestAlarm(modbusClient)

const startRegister = ALARM_REGISTERS_START
const endRegister = ALARM_REGISTERS_END
const alarmOffset = 7

for (let register = startRegister; register <= endRegister; register += alarmOffset) {
const result = await mutex.runExclusive(async () =>
tryReadHoldingRegisters(modbusClient, register, alarmOffset)
)
const code = result.data[0]
const state = result.data[1]
for (const type in alarmSummary) {
// Use "off" as the default alarm state, most likely to be true
alarmSummary[type].state = 0

// Skip unset alarm slots and unknown alarm types
if (AVAILABLE_ALARMS[code] === undefined) {
continue
// Use the state from the newest alarm
if (type === newestAlarm.type) {
alarmSummary[type].state = newestAlarm.state
}

let alarm = Object.assign({}, AVAILABLE_ALARMS[code])
alarm.state = state
alarm.date = parseAlarmTimestamp(result)

alarmHistory.push(alarm)
}

return alarmHistory
return alarmSummary
}

export const getAlarmStatuses = async (modbusClient) => {
let alarms = { ...AVAILABLE_ALARMS }
export const getNewestAlarm = async (modbusClient) => {
const result = await mutex.runExclusive(async () => tryReadHoldingRegisters(modbusClient, 385, 7))

// Use the alarm history to determine the state of each alarm
const alarmHistory = await getAlarmHistory(modbusClient)
const type = result.data[0]
const state = result.data[1]
const timestamp = parseAlarmTimestamp(result)

for (const code in alarms) {
// Use "off" as the default alarm state, most likely to be true
alarms[code].state = 0

for (const historicAlarm of alarmHistory) {
if (historicAlarm.name === alarms[code].name && historicAlarm.state > 0) {
alarms[code].state = historicAlarm.state
}
}
if (AVAILABLE_ALARMS[type] === undefined) {
return null
}

return alarms
return {
...AVAILABLE_ALARMS[type],
type,
state,
timestamp,
}
}

export const getDeviceState = async (modbusClient) => {
Expand Down
7 changes: 4 additions & 3 deletions app/mqtt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
setSetting,
getModeSummary,
setMode,
getAlarmStatuses,
getAlarmSummary,
getDeviceState,
} from './modbus.mjs'
import { createLogger } from './logger.mjs'
Expand Down Expand Up @@ -41,9 +41,10 @@ export const publishValues = async (modbusClient, mqttClient) => {
// Publish each setting
await publishSettings(modbusClient, mqttClient)

const alarmStatuses = await getAlarmStatuses(modbusClient)
// Publish alarm summary
const alarmSummary = await getAlarmSummary(modbusClient)

for (const [, alarm] of Object.entries(alarmStatuses)) {
for (const [, alarm] of Object.entries(alarmSummary)) {
const topicName = `${TOPIC_PREFIX_ALARM}/${alarm.name}`

topicMap[topicName] = createBinaryValue(alarm.state === 2)
Expand Down

0 comments on commit 4afa0d5

Please sign in to comment.