Compare commits
No commits in common. "d8e67643f5939ca9d0842319100a697c84320f58" and "0cb91aed62912cc45ec7e39e0c87d826b9503fd2" have entirely different histories.
d8e67643f5
...
0cb91aed62
14
filters.go
14
filters.go
|
@ -79,17 +79,3 @@ 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,7 +7,6 @@ import (
|
|||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
@ -115,14 +114,6 @@ 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
|
||||
|
@ -146,7 +137,7 @@ func main() {
|
|||
|
||||
tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate))
|
||||
|
||||
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath())
|
||||
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), PersistenceFilename)
|
||||
|
||||
Consume(
|
||||
SendToTg(
|
||||
|
@ -156,9 +147,7 @@ func main() {
|
|||
go CloseSatelOnCtrlC(s)
|
||||
|
||||
for e := range FilterByIndex(FilterByType(
|
||||
FilterByLastSeen(
|
||||
CallWhenClosed(s.Events, func() { logger.Print("Satel disconnected.") }),
|
||||
&dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
|
||||
FilterByLastSeen(s.Events, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
|
||||
allowedTypes),
|
||||
allowedIndexes) {
|
||||
logger.Print("Received change from SATEL: ", e)
|
||||
|
|
18
main_test.go
18
main_test.go
|
@ -1,18 +0,0 @@
|
|||
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,57 +122,3 @@ 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}}
|
||||
:: {{.IndexDesc}}: {{.FormatEvent}}
|
||||
:: {{.SatelEvent.Index}}: {{.FormatEvent}}
|
||||
{{- else -}}
|
||||
Huh, no messages - this is a bug
|
||||
{{- end}}`
|
||||
|
|
Loading…
Reference in New Issue