-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.go
64 lines (52 loc) · 1.29 KB
/
new.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package sensor
import (
"github.com/cskr/pubsub"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/edgefarm/edgefarm/examples/basic/producer/internal/export"
)
type Configuration struct {
SampleRate float64
RingBufEntries int32
}
type sampler struct {
rb *samplesRingbuf
}
// Unit is the instance of the SensorUnit
type Unit struct {
cfg *Configuration
logger zerolog.Logger
ps *pubsub.PubSub
sampler *sampler
export export.Exporter
}
// New creates a new instance of the MetricsUnit
func New(ps *pubsub.PubSub, export export.Exporter, cfg *Configuration) *Unit {
t := &Unit{
cfg: cfg,
ps: ps,
logger: log.With().Str("component", "sensor").Logger(),
export: export,
}
return t
}
// Run starts the sensor unit
// It starts the sampler and the publisher with the given subject to publish to
// It returns as soon as all go routines are started
func (s *Unit) Run(subject string) error {
s.logger.Info().Msg("sensorunit starting")
// start sensor sampling
var rb *samplesRingbuf
// start simulation
rb, err := s.simulatedSampling(s.cfg.SampleRate, s.cfg.RingBufEntries)
if err != nil {
return err
}
s.sampler = &sampler{
rb: rb,
}
// start publisher
s.logger.Info().Msg("about to start publisher")
s.publisher(subject)
return nil
}