Skip to content

Commit

Permalink
ref
Browse files Browse the repository at this point in the history
  • Loading branch information
m-pavel committed Dec 18, 2019
1 parent 77a7246 commit 62619b1
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions mqtt/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type TionService struct {
debug bool
fake *bool
keepbt *bool
ss ghm.SendState
ctx *ghm.ServiceContext
}

// PrepareCommandLineParams for TionService
Expand All @@ -43,25 +43,26 @@ func (ts *TionService) PrepareCommandLineParams() {
// Name of TionService
func (ts TionService) Name() string { return "tion" }

func (ts *TionService) OnConnect(client MQTT.Client, topic, topicc, topica string) {
if token := client.Subscribe(topicc, 2, ts.control); token.WaitTimeout(timeout) && token.Error() != nil {
log.Println(token.Error())
}
}

// Init TionService
func (ts *TionService) Init(client MQTT.Client, topic, topicc, topica string, debug bool, ss ghm.SendState) error {
func (ts *TionService) Init(ctx *ghm.ServiceContext) error {
go func() {
log.Println(http.ListenAndServe(":7070", nil))
}()
if *ts.fake {
log.Println("Using fake device.")
ts.t = fake.NewFake()
} else {
ts.t = tionimpl.New(*ts.bt, debug)
}

ts.debug = debug
ts.ss = ss

if token := client.Subscribe(topicc, 2, ts.control); token.WaitTimeout(timeout) && token.Error() != nil {
return token.Error()
ts.t = tionimpl.New(*ts.bt, ctx.Debug())
}

ts.debug = ctx.Debug()
ts.ctx = ctx
if *ts.keepbt {
return ts.t.Connect(timeout)
}
Expand Down Expand Up @@ -185,7 +186,7 @@ func (ts *TionService) control(cli MQTT.Client, msg MQTT.Message) {
if err = ts.t.Update(cs, timeout); err != nil {
log.Println(err)
} else {
if err := ts.ss(); err != nil {
if err := ts.ctx.SendState(); err != nil {
log.Println(err)
} else {
log.Println("Made update by MQTT request")
Expand Down

0 comments on commit 62619b1

Please sign in to comment.