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
|
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"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -115,14 +114,6 @@ func makeSatel(satelAddr string) *satel.Satel {
|
||||||
return satel.NewConfig(satelConn, satel.Config{EventsQueueSize: 10})
|
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() {
|
func main() {
|
||||||
var (
|
var (
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
|
@ -146,7 +137,7 @@ func main() {
|
||||||
|
|
||||||
tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate))
|
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(
|
Consume(
|
||||||
SendToTg(
|
SendToTg(
|
||||||
|
@ -156,9 +147,7 @@ func main() {
|
||||||
go CloseSatelOnCtrlC(s)
|
go CloseSatelOnCtrlC(s)
|
||||||
|
|
||||||
for e := range FilterByIndex(FilterByType(
|
for e := range FilterByIndex(FilterByType(
|
||||||
FilterByLastSeen(
|
FilterByLastSeen(s.Events, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
|
||||||
CallWhenClosed(s.Events, func() { logger.Print("Satel disconnected.") }),
|
|
||||||
&dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
|
|
||||||
allowedTypes),
|
allowedTypes),
|
||||||
allowedIndexes) {
|
allowedIndexes) {
|
||||||
logger.Print("Received change from SATEL: ", e)
|
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))
|
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:
|
const TelegramMessageTemplate = `Received following changes:
|
||||||
{{- range .Messages}}
|
{{- range .Messages}}
|
||||||
:: {{.IndexDesc}}: {{.FormatEvent}}
|
:: {{.SatelEvent.Index}}: {{.FormatEvent}}
|
||||||
{{- else -}}
|
{{- else -}}
|
||||||
Huh, no messages - this is a bug
|
Huh, no messages - this is a bug
|
||||||
{{- end}}`
|
{{- end}}`
|
||||||
|
|
Loading…
Reference in New Issue