nvi-inc / gromet

Go MET server
MIT License
0 stars 1 forks source link

specify parameter positions for wind data parsing #5

Open wehimwich opened 3 years ago

wehimwich commented 3 years ago

Some wind sensors are configured to have the speed and direction data in different positions in the device response. There is not an urgent need to do this, but would it be easy to add (optionally?) specifying in gromet.yml which parameters are which?

For example, for the NMEA (0183 V2.20 :):) ) wind data format, the code had to be modified thusly:

diff --git a/main.go b/main.go
index 29bbb1d..9c1c88c 100644
--- a/main.go
+++ b/main.go
@@ -107,17 +107,17 @@ func openWindConn(addr string) <-chan windstate {

                                fields := strings.FieldsFunc(resp[1:], func(r rune) bool { return r == ',' })

-                               if len(fields) < 2 {
+                               if len(fields) < 3 {
                                        log.Printf("error reading from wind: unexpected response %q", resp)
                                        conn.Close()
                                        continue ConnLoop
                                }

                                s.t = time.Now()
-                               s.speed, err = strconv.ParseFloat(fields[0], 64)
+                               s.speed, err = strconv.ParseFloat(fields[3], 64)
                                if err != nil {
                                        s.speed = math.NaN()
-                                       log.Printf("error decoding message wind device: wind speed given as %q", fields[0])
+                                       log.Printf("error decoding message wind device: wind speed given as %q", fields[3])
                                }

                                s.direction, err = strconv.ParseFloat(fields[1], 64)

Of course in general the direction could be in a different field too. As the example shows, the number of required fields is also dependent.

This has not been an issue for the MET4 data so far.

wehimwich commented 5 months ago

Actually:

-                               if len(fields) < 2 {
+                               if len(fields) < 3 {

needs to be:

-                               if len(fields) < 2 {
+                               if len(fields) < 4 {

D'oh. Need to learn go.