Compare commits
3 Commits
0cb91aed62
...
d8e67643f5
Author | SHA1 | Date |
---|---|---|
Michał Rudowicz | d8e67643f5 | |
Michał Rudowicz | b19ae59a9c | |
Michał Rudowicz | 765a41f8a1 |
14
filters.go
14
filters.go
|
@ -79,3 +79,17 @@ func FilterByLastSeen(ev <-chan satel.Event, dataStore *DataStore, logger *log.L
|
|||
|
||||
return returnChan
|
||||
}
|
||||
|
||||
func CallWhenClosed(ev <-chan satel.Event, cbk func()) <-chan satel.Event {
|
||||
returnChan := make(chan satel.Event)
|
||||
|
||||
go func() {
|
||||
for e := range ev {
|
||||
returnChan <- e
|
||||
}
|
||||
cbk()
|
||||
close(returnChan)
|
||||
}()
|
||||
|
||||
return returnChan
|
||||
}
|
||||
|
|
15
main.go
15
main.go
|
@ -7,6 +7,7 @@ import (
|
|||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
@ -114,6 +115,14 @@ func makeSatel(satelAddr string) *satel.Satel {
|
|||
return satel.NewConfig(satelConn, satel.Config{EventsQueueSize: 10})
|
||||
}
|
||||
|
||||
func getPersistenceFilePath() string {
|
||||
var stateDir = os.Getenv("STATE_DIRECTORY")
|
||||
if len(stateDir) != 0 {
|
||||
return filepath.Join(stateDir, PersistenceFilename)
|
||||
}
|
||||
return PersistenceFilename
|
||||
}
|
||||
|
||||
func main() {
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
|
@ -137,7 +146,7 @@ func main() {
|
|||
|
||||
tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate))
|
||||
|
||||
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), PersistenceFilename)
|
||||
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath())
|
||||
|
||||
Consume(
|
||||
SendToTg(
|
||||
|
@ -147,7 +156,9 @@ func main() {
|
|||
go CloseSatelOnCtrlC(s)
|
||||
|
||||
for e := range FilterByIndex(FilterByType(
|
||||
FilterByLastSeen(s.Events, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
|
||||
FilterByLastSeen(
|
||||
CallWhenClosed(s.Events, func() { logger.Print("Satel disconnected.") }),
|
||||
&dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
|
||||
allowedTypes),
|
||||
allowedIndexes) {
|
||||
logger.Print("Received change from SATEL: ", e)
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetPersistenceFilename(t *testing.T) {
|
||||
var oldStateDir = os.Getenv("STATE_DIRECTORY")
|
||||
os.Setenv("STATE_DIRECTORY", "test_dir")
|
||||
assert.Equal(t, filepath.Join("test_dir", PersistenceFilename), getPersistenceFilePath())
|
||||
os.Setenv("STATE_DIRECTORY", "")
|
||||
assert.Equal(t, PersistenceFilename, getPersistenceFilePath())
|
||||
os.Setenv("STATE_DIRECTORY", oldStateDir)
|
||||
}
|
|
@ -122,3 +122,57 @@ func (self MsgContent) FormatEvent() string {
|
|||
}
|
||||
panic(fmt.Sprint("Unknown event received: ", self.SatelEvent))
|
||||
}
|
||||
|
||||
func (self MsgContent) IndexDesc() string {
|
||||
switch self.SatelEvent.Type {
|
||||
case satel.ZoneViolation:
|
||||
return fmt.Sprintf("Zone %d", self.SatelEvent.Index)
|
||||
case satel.ZoneTamper:
|
||||
return fmt.Sprintf("Zone %d", self.SatelEvent.Index)
|
||||
case satel.ZoneAlarm:
|
||||
return fmt.Sprintf("Zone %d", self.SatelEvent.Index)
|
||||
case satel.ZoneTamperAlarm:
|
||||
return fmt.Sprintf("Zone %d", self.SatelEvent.Index)
|
||||
case satel.ZoneAlarmMemory:
|
||||
return fmt.Sprintf("Zone %d", self.SatelEvent.Index)
|
||||
case satel.ZoneTamperAlarmMemory:
|
||||
return fmt.Sprintf("Zone %d", self.SatelEvent.Index)
|
||||
case satel.ZoneBypass:
|
||||
return fmt.Sprintf("Zone %d", self.SatelEvent.Index)
|
||||
case satel.ZoneNoViolationTrouble:
|
||||
return fmt.Sprintf("Zone %d", self.SatelEvent.Index)
|
||||
case satel.ZoneLongViolationTrouble:
|
||||
return fmt.Sprintf("Zone %d", self.SatelEvent.Index)
|
||||
case satel.ArmedPartitionSuppressed:
|
||||
return fmt.Sprintf("Partition %d", self.SatelEvent.Index)
|
||||
case satel.ArmedPartition:
|
||||
return fmt.Sprintf("Partition %d", self.SatelEvent.Index)
|
||||
case satel.PartitionArmedInMode2:
|
||||
return fmt.Sprintf("Partition %d", self.SatelEvent.Index)
|
||||
case satel.PartitionArmedInMode3:
|
||||
return fmt.Sprintf("Partition %d", self.SatelEvent.Index)
|
||||
case satel.PartitionWith1stCodeEntered:
|
||||
return fmt.Sprintf("Partition %d", self.SatelEvent.Index)
|
||||
case satel.PartitionEntryTime:
|
||||
return fmt.Sprintf("Partition %d", self.SatelEvent.Index)
|
||||
case satel.PartitionExitTimeOver10s:
|
||||
return fmt.Sprintf("Partition %d", self.SatelEvent.Index)
|
||||
case satel.PartitionExitTimeUnder10s:
|
||||
return fmt.Sprintf("Partition %d", self.SatelEvent.Index)
|
||||
case satel.PartitionTemporaryBlocked:
|
||||
return fmt.Sprintf("Partition %d", self.SatelEvent.Index)
|
||||
case satel.PartitionBlockedForGuardRound:
|
||||
return fmt.Sprintf("Partition %d", self.SatelEvent.Index)
|
||||
case satel.PartitionAlarm:
|
||||
return fmt.Sprintf("Partition %d", self.SatelEvent.Index)
|
||||
case satel.PartitionFireAlarm:
|
||||
return fmt.Sprintf("Partition %d", self.SatelEvent.Index)
|
||||
case satel.PartitionAlarmMemory:
|
||||
return fmt.Sprintf("Partition %d", self.SatelEvent.Index)
|
||||
case satel.PartitionFireAlarmMemory:
|
||||
return fmt.Sprintf("Partition %d", self.SatelEvent.Index)
|
||||
case satel.ZoneIsolate:
|
||||
return fmt.Sprintf("Zone %d", self.SatelEvent.Index)
|
||||
}
|
||||
return fmt.Sprintf("Unknown index: %d", self.SatelEvent.Index)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package main
|
|||
|
||||
const TelegramMessageTemplate = `Received following changes:
|
||||
{{- range .Messages}}
|
||||
:: {{.SatelEvent.Index}}: {{.FormatEvent}}
|
||||
:: {{.IndexDesc}}: {{.FormatEvent}}
|
||||
{{- else -}}
|
||||
Huh, no messages - this is a bug
|
||||
{{- end}}`
|
||||
|
|
Loading…
Reference in New Issue