1
0
Fork 0
hswro-alarm-bot/satel_utils.go

125 lines
4.1 KiB
Go
Raw Normal View History

2024-02-18 17:44:08 +00:00
package main
import (
"errors"
"fmt"
2024-02-18 18:15:56 +00:00
"os"
"os/signal"
2024-02-18 17:44:08 +00:00
"github.com/probakowski/go-satel"
)
var SUPPORTED_CHANGE_TYPES = [...]satel.ChangeType{
satel.ZoneViolation,
satel.ZoneTamper,
satel.ZoneAlarm,
satel.ZoneTamperAlarm,
satel.ZoneAlarmMemory,
satel.ZoneTamperAlarmMemory,
satel.ZoneBypass,
satel.ZoneNoViolationTrouble,
satel.ZoneLongViolationTrouble,
satel.ArmedPartitionSuppressed,
satel.ArmedPartition,
satel.PartitionArmedInMode2,
satel.PartitionArmedInMode3,
satel.PartitionWith1stCodeEntered,
satel.PartitionEntryTime,
satel.PartitionExitTimeOver10s,
satel.PartitionExitTimeUnder10s,
satel.PartitionTemporaryBlocked,
satel.PartitionBlockedForGuardRound,
satel.PartitionAlarm,
satel.PartitionFireAlarm,
satel.PartitionAlarmMemory,
satel.PartitionFireAlarmMemory,
satel.Output,
satel.DoorOpened,
satel.DoorOpenedLong,
satel.StatusBit,
satel.TroublePart1,
satel.TroublePart2,
satel.TroublePart3,
satel.TroublePart4,
satel.TroublePart5,
satel.TroubleMemoryPart1,
satel.TroubleMemoryPart2,
satel.TroubleMemoryPart3,
satel.TroubleMemoryPart4,
satel.TroubleMemoryPart5,
satel.PartitionWithViolatedZones,
satel.ZoneIsolate,
}
var SATEL_STRING_TO_CHANGE_TYPE = map[string]satel.ChangeType{
"zone-violation": satel.ZoneViolation,
"zone-tamper": satel.ZoneTamper,
"zone-alarm": satel.ZoneAlarm,
"zone-tamper-alarm": satel.ZoneTamperAlarm,
"zone-alarm-memory": satel.ZoneAlarmMemory,
"zone-tamper-alarm-memory": satel.ZoneTamperAlarmMemory,
"zone-bypass": satel.ZoneBypass,
"zone-no-violation-trouble": satel.ZoneNoViolationTrouble,
"zone-long-violation-trouble": satel.ZoneLongViolationTrouble,
"armed-partition-suppressed": satel.ArmedPartitionSuppressed,
"armed-partition": satel.ArmedPartition,
"partition-armed-mode-2": satel.PartitionArmedInMode2,
"partition-armed-mode-3": satel.PartitionArmedInMode3,
"partition-with-1st-code-entered": satel.PartitionWith1stCodeEntered,
"partition-entry-time": satel.PartitionEntryTime,
"partition-exit-time-over-10s": satel.PartitionExitTimeOver10s,
"partition-exit-time-under-10s": satel.PartitionExitTimeUnder10s,
"partition-temporary-blocked": satel.PartitionTemporaryBlocked,
"partition-blocked-guard-round": satel.PartitionBlockedForGuardRound,
"partition-alarm": satel.PartitionAlarm,
"partition-fire-alarm": satel.PartitionFireAlarm,
"partition-alarm-memory": satel.PartitionAlarmMemory,
"partition-fire-alarm-memory": satel.PartitionFireAlarmMemory,
"output": satel.Output,
"doors-opened": satel.DoorOpened,
"doors-opened-long": satel.DoorOpenedLong,
"status-bit": satel.StatusBit,
"trouble-part-1": satel.TroublePart1,
"trouble-part-2": satel.TroublePart2,
"trouble-part-3": satel.TroublePart3,
"trouble-part-4": satel.TroublePart4,
"trouble-part-5": satel.TroublePart5,
"trouble-memory-part-1": satel.TroubleMemoryPart1,
"trouble-memory-part-2": satel.TroubleMemoryPart2,
"trouble-memory-part-3": satel.TroubleMemoryPart3,
"trouble-memory-part-4": satel.TroubleMemoryPart4,
"trouble-memory-part-5": satel.TroubleMemoryPart5,
"partition-with-violated-zones": satel.PartitionWithViolatedZones,
"zone-isolate": satel.ZoneIsolate,
}
2024-02-18 18:15:56 +00:00
func CloseChanOnCtrlC(ev <-chan satel.Event) <-chan satel.Event {
returnChan := make(chan satel.Event)
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt)
go func() {
defer close(returnChan)
loop:
for {
select {
case <-exitSignal:
return
case e, ok := <-ev:
if !ok {
break loop
}
returnChan <- e
}
}
}()
return returnChan
}
2024-02-18 17:44:08 +00:00
func StringToSatelChangeType(s string) (satel.ChangeType, error) {
ct, ok := SATEL_STRING_TO_CHANGE_TYPE[s]
if !ok {
return 0, errors.New(fmt.Sprint("Unknown change type: ", s))
}
return ct, nil
}