• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

EvergenEnergy / sparkplug-host / 6895158860

16 Nov 2023 06:43PM UTC coverage: 84.434% (+0.03%) from 84.405%
6895158860

Pull #13

github

jlordiales
chore: Add example and Usage in README
Pull Request #13: chore: Add example and Usage in README

7 of 11 new or added lines in 3 files covered. (63.64%)

716 of 848 relevant lines covered (84.43%)

34.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.38
/command_publisher.go
1
package sparkplughost
2

3
import (
4
        "fmt"
5
        "log/slog"
6
        "time"
7

8
        "github.com/EvergenEnergy/sparkplughost/protobuf"
9
        mqtt "github.com/eclipse/paho.mqtt.golang"
10
        "google.golang.org/protobuf/proto"
11
)
12

13
const (
14
        nodeRebirthMetricName   = "Node Control/Rebirth"
15
        deviceRebirthMetricName = "Device Control/Rebirth"
16
)
17

18
type commandPublisher struct {
19
        mqttClients map[string]mqtt.Client
20
        logger      *slog.Logger
21
}
22

23
func newCommandPublisher(mqttClients map[string]mqtt.Client, logger *slog.Logger) *commandPublisher {
20✔
24
        return &commandPublisher{mqttClients: mqttClients, logger: logger}
20✔
25
}
20✔
26

27
func (c *commandPublisher) requestNodeRebirth(descriptor EdgeNodeDescriptor) error {
9✔
28
        c.logger.Debug("Requesting Rebirth for edge node",
9✔
29
                "edge_node_descriptor", descriptor.String())
9✔
30

9✔
31
        return c.writeEdgeNodeMetrics(descriptor, []*protobuf.Payload_Metric{
9✔
32
                {
9✔
33
                        Name:     proto.String(nodeRebirthMetricName),
9✔
34
                        Datatype: proto.Uint32(uint32(protobuf.DataType_Boolean.Number())),
9✔
35
                        Value:    &protobuf.Payload_Metric_BooleanValue{BooleanValue: true},
9✔
36
                },
9✔
37
        })
9✔
38
}
9✔
39

40
func (c *commandPublisher) requestDeviceRebirth(descriptor EdgeNodeDescriptor, deviceID string) error {
×
NEW
41
        c.logger.Debug("Requesting Rebirth for device",
×
NEW
42
                "edge_node_descriptor", descriptor.String(),
×
NEW
43
                "device_id", deviceID)
×
NEW
44

×
45
        return c.writeDeviceMetrics(descriptor, deviceID, []*protobuf.Payload_Metric{
×
46
                {
×
47
                        Name:     proto.String(deviceRebirthMetricName),
×
48
                        Datatype: proto.Uint32(uint32(protobuf.DataType_Boolean.Number())),
×
49
                        Value:    &protobuf.Payload_Metric_BooleanValue{BooleanValue: true},
×
50
                },
×
51
        })
×
52
}
×
53

54
func (c *commandPublisher) writeEdgeNodeMetrics(descriptor EdgeNodeDescriptor, metrics []*protobuf.Payload_Metric) error {
9✔
55
        topic := fmt.Sprintf("%s/%s/NCMD/%s", sparkplugbNamespace, descriptor.GroupID, descriptor.EdgeNodeID)
9✔
56
        return c.publish(topic, metrics)
9✔
57
}
9✔
58

59
func (c *commandPublisher) writeDeviceMetrics(descriptor EdgeNodeDescriptor, deviceID string, metrics []*protobuf.Payload_Metric) error {
×
60
        topic := fmt.Sprintf("%s/%s/DCMD/%s/%s", sparkplugbNamespace, descriptor.GroupID, descriptor.EdgeNodeID, deviceID)
×
61
        return c.publish(topic, metrics)
×
62
}
×
63

64
func (c *commandPublisher) publish(topic string, metrics []*protobuf.Payload_Metric) error {
9✔
65
        ts := proto.Uint64(uint64(time.Now().UnixMilli()))
9✔
66

9✔
67
        payload := &protobuf.Payload{
9✔
68
                Timestamp: ts,
9✔
69
                Metrics:   metrics,
9✔
70
        }
9✔
71

9✔
72
        bytes, err := proto.Marshal(payload)
9✔
73
        if err != nil {
9✔
74
                return err
×
75
        }
×
76

77
        for _, client := range c.mqttClients {
18✔
78
                if t := client.Publish(topic, byte(0), false, bytes); t.Wait() && t.Error() != nil {
9✔
79
                        return err
×
80
                }
×
81
        }
82

83
        return nil
9✔
84
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc