Skip to content

Commit

Permalink
chore: add mqtt logs
Browse files Browse the repository at this point in the history
  • Loading branch information
saitofun committed Jul 18, 2024
1 parent 446354d commit c4e40c8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 57 deletions.
37 changes: 19 additions & 18 deletions pkg/modules/event/event_device_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,24 @@ func (e *DeviceQuery) Handle(ctx context.Context) (err error) {
}

meta, _ := contexts.AppMetaFromContext(ctx)
err = PublicMqttMessage(ctx,
"device_query", "backend/"+e.imei+"/status",
&struct {
Status int32 `json:"status"`
Proposer string `json:"proposer,omitempty"`
Firmware string `json:"firmware,omitempty"`
URI string `json:"uri,omitempty"`
Version string `json:"version,omitempty"`
ServerMeta string `json:"server_meta"`
}{
Status: dev.Status,
Proposer: dev.Proposer,
Firmware: firmware,
URI: uri,
Version: version,
ServerMeta: meta.String(),
},
pubType := "DeviceQuery"
pubData := &struct {
Status int32 `json:"status"`
Proposer string `json:"proposer,omitempty"`
Firmware string `json:"firmware,omitempty"`
URI string `json:"uri,omitempty"`
Version string `json:"version,omitempty"`
ServerMeta string `json:"server_meta"`
}{
Status: dev.Status,
Proposer: dev.Proposer,
Firmware: firmware,
URI: uri,
Version: version,
ServerMeta: meta.String(),
}
return errors.Wrapf(
PublicMqttMessage(ctx, pubType, "backend/"+e.imei+"/status", pubData),
"failed to publish %s", pubType,
)
return errors.Wrapf(err, "failed to publish device_query response")
}
33 changes: 17 additions & 16 deletions pkg/modules/event/event_firmware_updated.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,22 @@ func (e *FirmwareUpdated) Handle(ctx context.Context) (err error) {
}

meta, _ := contexts.AppMetaFromContext(ctx)
err = PublicMqttMessage(ctx,
"firmware_updated", "device/app_update/"+app.ID,
&struct {
Name string `json:"name"`
Version string `json:"version"`
Uri string `json:"uri"`
Avatar string `json:"avatar"`
ServerMeta string `json:"meta"`
}{
Name: app.ID,
Version: app.Version,
Uri: app.Uri,
Avatar: app.Avatar,
ServerMeta: meta.String(),
},
pubType := "FirmwareUpdated"
pubData := &struct {
Name string `json:"name"`
Version string `json:"version"`
Uri string `json:"uri"`
Avatar string `json:"avatar"`
ServerMeta string `json:"meta"`
}{
Name: app.ID,
Version: app.Version,
Uri: app.Uri,
Avatar: app.Avatar,
ServerMeta: meta.String(),
}
return errors.Wrapf(
PublicMqttMessage(ctx, pubType, "device/app_update/"+app.ID, pubData),
"failed to publish %s", pubType,
)
return errors.Wrap(err, "failed to publish firmware_updated response")
}
10 changes: 5 additions & 5 deletions pkg/modules/event/event_pebble_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ func (e *PebbleConfig) Handle(ctx context.Context) (err error) {
return errors.Wrapf(err, "failed to fetch app_v2: %s", app.ID)
}

err = PublicMqttMessage(ctx,
"pebble_config",
"backend/"+e.Imei+"/config",
app.Data,
pubType := "PebbleConfig"
pubData := app.Data
return errors.Wrapf(
PublicMqttMessage(ctx, pubType, "backend/"+e.Imei+"/config", pubData),
"failed to publish %s", pubType,
)
return errors.Wrap(err, "failed to publish pebble_config response")
}
30 changes: 16 additions & 14 deletions pkg/modules/event/event_pebble_firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,21 @@ func (e *PebbleFirmware) Handle(ctx context.Context) (err error) {
}

meta, _ := contexts.AppMetaFromContext(ctx)
err = PublicMqttMessage(ctx,
"pebble_firmware", "backend/"+e.Imei+"/firmware",
&struct {
Firmware string `json:"firmware"`
Uri string `json:"uri"`
Version string `json:"version"`
ServerMeta string `json:"server_meta"`
}{
Firmware: e.App,
Uri: app.Uri,
Version: app.Version,
ServerMeta: meta.String(),
},
pubType := "PebbleFirmware"
pubData := &struct {
Firmware string `json:"firmware"`
Uri string `json:"uri"`
Version string `json:"version"`
ServerMeta string `json:"server_meta"`
}{
Firmware: e.App,
Uri: app.Uri,
Version: app.Version,
ServerMeta: meta.String(),
}

return errors.Wrapf(
PublicMqttMessage(ctx, pubType, "backend/"+e.Imei+"/firmware", pubData),
"failed to publish %s", pubType,
)
return errors.Wrap(err, "failed to publish pebble_firmware response")
}
7 changes: 3 additions & 4 deletions pkg/modules/event/util_mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/machinefi/sprout-pebble-sequencer/pkg/contexts"
)

func PublicMqttMessage(ctx context.Context, id, topic string, v any) error {
func PublicMqttMessage(ctx context.Context, tpe, topic string, v any) error {
mq := must.BeTrueV(contexts.MqttBrokerFromContext(ctx))
l := must.BeTrueV(contexts.LoggerFromContext(ctx))
cli, err := mq.NewClient(uuid.NewString(), topic)
Expand All @@ -21,7 +21,7 @@ func PublicMqttMessage(ctx context.Context, id, topic string, v any) error {
defer mq.Close(cli)

var data any
switch data.(type) {
switch v.(type) {
case string:
data = v
case []byte:
Expand All @@ -34,8 +34,7 @@ func PublicMqttMessage(ctx context.Context, id, topic string, v any) error {
}
if err = cli.Publish(data); err != nil {
err = errors.Wrap(err, "failed to publish mqtt")
l.Error(err, "client_id", id, "topic", topic, "data", v)
return err
}
l.Info("mqtt published", "type", tpe, "topic", topic, "data", v, "result", err)
return nil
}

0 comments on commit c4e40c8

Please sign in to comment.