Skip to content

Commit dcfcd3b

Browse files
author
Michael Andersen
committed
Finalize the stdout mirroring
1 parent e3839d3 commit dcfcd3b

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/algorithm.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"math"
66
"time"
77

8-
"github.com/davecgh/go-spew/spew"
98
l7g "github.com/immesys/chirp-l7g"
109
)
1110

@@ -41,15 +40,19 @@ var states map[string]*algorithmstate
4140

4241
func main() {
4342
//Register and run our algorithm.
44-
l7g.RunDPA([]byte(ourEntity), Initialize, OnNewDataSet,
43+
err := l7g.RunDPA([]byte(ourEntity), Initialize, OnNewDataSet,
4544
//This is the algorithm vendor
4645
"ucberkeley",
4746
//This is the algorithm version
4847
"1.0")
48+
fmt.Printf("fatal error: %v\n", err)
4949
}
5050

5151
func Initialize(emit l7g.Emitter) {
5252
states = make(map[string]*algorithmstate)
53+
//If you want the algorithm output to be on standard out as well
54+
//to allow for local use, do this
55+
emit.MirrorToStandardOutput(true)
5356
}
5457

5558
// OnNewDataSet encapsulates the algorithm. You can store the emitter and
@@ -62,7 +65,7 @@ func Initialize(emit l7g.Emitter) {
6265
// Sometimes the popHdr will be nil for an element even if the data is present
6366
// this indicates a packet that was lost but reconstructed using forward
6467
// error correction codes.
65-
func OnNewDataSet(info l7g.SetInfo, popHdr []*l7g.L7GHeader, data []*l7g.ChirpHeader, emit l7g.Emitter) {
68+
func OnNewDataSet(info *l7g.SetInfo, popHdr []*l7g.L7GHeader, data []*l7g.ChirpHeader, emit l7g.Emitter) {
6669

6770
//We only want to process complete sets of data
6871
if !info.Complete {
@@ -71,23 +74,23 @@ func OnNewDataSet(info l7g.SetInfo, popHdr []*l7g.L7GHeader, data []*l7g.ChirpHe
7174

7275
//This string is the complete ID of this anemometer
7376
//You can use this as a key into buffers of historic state
74-
idstring := info.Site + "_" + info.MAC
77+
idstring := info.MAC
7578
state, found := states[idstring]
7679
if !found {
7780
//Initialize new algorithm state here
7881
//perhaps make it calibrate or something
7982
state = &algorithmstate{
8083
AnemometerID: idstring,
81-
IsDuct: info.IsDuct(),
84+
IsDuct: info.IsDuct,
8285
}
8386
states[idstring] = state
8487
}
8588

8689
//Initialize the output data object
8790
//We will build up the data inside this as we process the input data
8891
outputdata := l7g.OutputData{
89-
Timestamp: info.TimeOfFirst,
90-
Sensor: idstring,
92+
Timestamp: info.TimeOfFirst.UnixNano(),
93+
Sensor: info.MAC,
9194
}
9295

9396
//Over all paths
@@ -144,14 +147,12 @@ func OnNewDataSet(info l7g.SetInfo, popHdr []*l7g.L7GHeader, data []*l7g.ChirpHe
144147

145148
//If you have other information output from the algorithm you can
146149
//add that in here
147-
outputdata.Extradata = append(outputdata.Extradata, "we are busy calibrating")
150+
outputdata.Extradata = append(outputdata.Extradata, "the algorithm has not been filled in yet")
148151

149152
//Note that you can also use print statements. These are not visible to
150153
//consumers of the data, but you can see them using `make watch`
151-
fmt.Printf("About to emit data\n")
152-
spew.Dump(outputdata) //this prints the whole object nicely
153154

154-
//Emit the data on the data bus
155+
//Emit the data on the data bus (and if DuplicateEmitToStdout is true, also
156+
//to standard output)
155157
emit.Data(outputdata)
156-
157158
}

0 commit comments

Comments
 (0)