1
0
Fork 0

Retrieve partition names from Satel

This commit is contained in:
Michał Rudowicz 2024-03-04 21:11:34 +01:00
parent d8e67643f5
commit 2e513fb4da
11 changed files with 115 additions and 26 deletions

View File

@ -8,7 +8,7 @@ import (
"os" "os"
"sync" "sync"
"github.com/probakowski/go-satel" "git.sr.ht/~michalr/go-satel"
) )
var EXPECTED_PERSISTENCE_FILE_MAGIC = [...]byte{'H', 'S', 'W', 'R', 'O', 'A', 'L', 'A', 'R', 'M', 'B', 'O', 'T'} var EXPECTED_PERSISTENCE_FILE_MAGIC = [...]byte{'H', 'S', 'W', 'R', 'O', 'A', 'L', 'A', 'R', 'M', 'B', 'O', 'T'}

View File

@ -3,7 +3,7 @@ package main
import ( import (
"log" "log"
"github.com/probakowski/go-satel" "git.sr.ht/~michalr/go-satel"
) )
func FilterByType(ev <-chan satel.Event, allowedTypes []satel.ChangeType) <-chan satel.Event { func FilterByType(ev <-chan satel.Event, allowedTypes []satel.ChangeType) <-chan satel.Event {

View File

@ -7,7 +7,7 @@ import (
"sync" "sync"
"testing" "testing"
"github.com/probakowski/go-satel" "git.sr.ht/~michalr/go-satel"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )

3
go.mod
View File

@ -3,14 +3,15 @@ module alarm_bot
go 1.19 go 1.19
require ( require (
git.sr.ht/~michalr/go-satel v0.0.0-20240304192450-999413ca9994
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/probakowski/go-satel v0.0.0-20211120120346-bed9818777ce
github.com/stretchr/testify v1.8.4 github.com/stretchr/testify v1.8.4
) )
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

44
go.sum
View File

@ -1,6 +1,7 @@
git.sr.ht/~michalr/go-satel v0.0.0-20211120120346-bed9818777ce h1:6navIHdH9NaXuAd6R2rACpqz2Das2Gu31E1JxT3Tv8w= git.sr.ht/~michalr/go-satel v0.0.0-20240303214713-fcbf3bd29f6f h1:+jaUKR+4mKlJASOlLBfHxFr4qXfhubYbBM37fepIJug=
git.sr.ht/~michalr/go-satel v0.0.0-20211120120346-bed9818777ce/go.mod h1:q3DquDWRcoFWZ61dGZFg3snucolljixMoAzJIiCjWoY= git.sr.ht/~michalr/go-satel v0.0.0-20240303214713-fcbf3bd29f6f/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= git.sr.ht/~michalr/go-satel v0.0.0-20240304192450-999413ca9994 h1:uIDVu4eIJKT4AE0cckgId143xR8vlKFKSVBfQFKPHRg=
git.sr.ht/~michalr/go-satel v0.0.0-20240304192450-999413ca9994/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -8,15 +9,44 @@ github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGi
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/probakowski/go-satel v0.0.0-20211120120346-bed9818777ce h1:V21PmRMDowz+5pA7zn0YTVLnoGEEFqj14UN6/f3zRiY=
github.com/probakowski/go-satel v0.0.0-20211120120346-bed9818777ce/go.mod h1:q3DquDWRcoFWZ61dGZFg3snucolljixMoAzJIiCjWoY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

10
main.go
View File

@ -13,8 +13,8 @@ import (
"sync" "sync"
"time" "time"
"git.sr.ht/~michalr/go-satel"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/probakowski/go-satel"
) )
const ( const (
@ -23,7 +23,9 @@ const (
) )
type TgSender struct { type TgSender struct {
bot *tgbotapi.BotAPI bot *tgbotapi.BotAPI
s SatelNameGetter
logger *log.Logger
} }
func (self TgSender) Send(msg GenericMessage, tpl *template.Template) error { func (self TgSender) Send(msg GenericMessage, tpl *template.Template) error {
@ -31,7 +33,7 @@ func (self TgSender) Send(msg GenericMessage, tpl *template.Template) error {
if chatIds == nil { if chatIds == nil {
return nil return nil
} }
message := msg.Format(tpl) message := msg.Format(tpl, self.s, self.logger)
for _, chatId := range *chatIds { for _, chatId := range *chatIds {
toSend := tgbotapi.NewMessage(chatId, message) toSend := tgbotapi.NewMessage(chatId, message)
toSend.ParseMode = "HTML" toSend.ParseMode = "HTML"
@ -142,7 +144,7 @@ func main() {
} }
logger.Print("Created Telegram Bot API client") logger.Print("Created Telegram Bot API client")
tgSender := TgSender{bot} tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds)}
tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate)) tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate))

View File

@ -3,23 +3,64 @@ package main
import ( import (
"fmt" "fmt"
"html/template" "html/template"
"log"
"strings" "strings"
"github.com/probakowski/go-satel" "git.sr.ht/~michalr/go-satel"
) )
type SatelNameGetter interface {
GetName(devType satel.DeviceType, index byte) (*satel.NameEvent, error)
}
type MsgContent struct { type MsgContent struct {
SatelEvent satel.Event SatelEvent satel.Event
} }
type SatelMsgContent struct {
SatelEvent satel.Event
s SatelNameGetter
logger *log.Logger
}
type GenericMessage struct { type GenericMessage struct {
ChatIds ChatId ChatIds ChatId
Messages []MsgContent Messages []MsgContent
} }
func (self GenericMessage) Format(template *template.Template) string { type SatelMessage struct {
ChatIds ChatId
Messages []SatelMsgContent
}
func getSatelMessageContent(messages []MsgContent, s SatelNameGetter, logger *log.Logger) []SatelMsgContent {
retval := make([]SatelMsgContent, len(messages))
for i, v := range messages {
retval[i] = SatelMsgContent{v.SatelEvent, s, logger}
}
return retval
}
func (self SatelMsgContent) GetName() string {
if self.SatelEvent.Type == satel.ArmedPartition {
// Satel has 1-based indexes of partitions, go-satel has 0-based
name, err := self.s.GetName(satel.DeviceType_Partition, byte(self.SatelEvent.Index+1))
if err != nil {
self.logger.Print("Could not get partition name: ", err)
return fmt.Sprint("Partition#", self.SatelEvent.Index)
}
return name.DevName
} else {
return self.IndexDesc()
}
}
func (self GenericMessage) Format(template *template.Template, s SatelNameGetter, logger *log.Logger) string {
b := strings.Builder{} b := strings.Builder{}
template.Execute(&b, self) template.Execute(&b, SatelMessage{
self.ChatIds,
getSatelMessageContent(self.Messages, s, logger),
})
return b.String() return b.String()
} }
@ -39,7 +80,7 @@ func getEmojiWhenTrueIsBad(v bool) string {
} }
} }
func (self MsgContent) FormatEvent() string { func (self SatelMsgContent) FormatEvent() string {
switch self.SatelEvent.Type { switch self.SatelEvent.Type {
case satel.ZoneViolation: case satel.ZoneViolation:
return fmt.Sprintf("%s: %s", self.SatelEvent.Type.String(), getEmojiWhenTrueIsBad(self.SatelEvent.Value)) return fmt.Sprintf("%s: %s", self.SatelEvent.Type.String(), getEmojiWhenTrueIsBad(self.SatelEvent.Value))
@ -123,7 +164,7 @@ 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 { func (self SatelMsgContent) IndexDesc() string {
switch self.SatelEvent.Type { switch self.SatelEvent.Type {
case satel.ZoneViolation: case satel.ZoneViolation:
return fmt.Sprintf("Zone %d", self.SatelEvent.Index) return fmt.Sprintf("Zone %d", self.SatelEvent.Index)

View File

@ -6,7 +6,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"github.com/probakowski/go-satel" "git.sr.ht/~michalr/go-satel"
) )
var SUPPORTED_CHANGE_TYPES = [...]satel.ChangeType{ var SUPPORTED_CHANGE_TYPES = [...]satel.ChangeType{

View File

@ -7,7 +7,7 @@ import (
"sync" "sync"
"testing" "testing"
"github.com/probakowski/go-satel" "git.sr.ht/~michalr/go-satel"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )

View File

@ -2,7 +2,7 @@ package main
const TelegramMessageTemplate = `Received following changes: const TelegramMessageTemplate = `Received following changes:
{{- range .Messages}} {{- range .Messages}}
:: {{.IndexDesc}}: {{.FormatEvent}} :: {{.GetName}}: {{.FormatEvent}}
{{- else -}} {{- else -}}
Huh, no messages - this is a bug Huh, no messages - this is a bug
{{- end}}` {{- end}}`

View File

@ -7,19 +7,34 @@ import (
"sync" "sync"
"testing" "testing"
"github.com/probakowski/go-satel" "git.sr.ht/~michalr/go-satel"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
type MockTemplateSender struct { type MockTemplateSender struct {
message string message string
s SatelNameGetter
} }
func (self *MockTemplateSender) Send(msg GenericMessage, tpl *template.Template) error { func (self *MockTemplateSender) Send(msg GenericMessage, tpl *template.Template) error {
self.message = msg.Format(tpl) self.message = msg.Format(tpl, self.s, log.New(io.Discard, "", log.Ltime))
return nil return nil
} }
type MockSatelNameGetter struct {
name string
}
func (self MockSatelNameGetter) GetName(devType satel.DeviceType, index byte) (*satel.NameEvent, error) {
retval := satel.NameEvent{
DevType: devType,
DevNumber: index,
DevTypeFunction: 0,
DevName: self.name,
}
return &retval, nil
}
var ( var (
tplMessageTest1 = satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true} tplMessageTest1 = satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true}
tplMessageTest2 = satel.Event{Type: satel.ZoneViolation, Index: 2, Value: true} tplMessageTest2 = satel.Event{Type: satel.ZoneViolation, Index: 2, Value: true}
@ -28,7 +43,7 @@ var (
func TestTelegramTemplate(t *testing.T) { func TestTelegramTemplate(t *testing.T) {
testEvents := make(chan GenericMessage) testEvents := make(chan GenericMessage)
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
mockSender := MockTemplateSender{} mockSender := MockTemplateSender{s: MockSatelNameGetter{"mockPart"}}
tpl, err := template.New("TestTemplate").Parse(TelegramMessageTemplate) tpl, err := template.New("TestTemplate").Parse(TelegramMessageTemplate)
assert.NoError(t, err) assert.NoError(t, err)
Consume(SendToTg(testEvents, &mockSender, &wg, log.New(io.Discard, "", log.Ltime), tpl)) Consume(SendToTg(testEvents, &mockSender, &wg, log.New(io.Discard, "", log.Ltime), tpl))