simple.go 867 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package parser
  2. import (
  3. "bufio"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. )
  8. // SimpleParser reads the default output and just scans for temperature
  9. type SimpleParser struct {
  10. Reader *bufio.Reader
  11. }
  12. // Measure returns the temperature. Null if it can't
  13. func (sp SimpleParser) Measure() (*Measurement, error) {
  14. text, err := sp.Reader.ReadString('\n')
  15. if err != nil {
  16. return nil, err
  17. }
  18. fields := strings.Fields(text)
  19. for i := 0; i < len(fields); i++ {
  20. fields[i] = strings.ToLower(fields[i])
  21. }
  22. if len(fields) == 3 {
  23. if strings.Compare(fields[0], "temperature:") == 0 {
  24. temp, _ := strconv.ParseFloat(fields[1], 64)
  25. unit := fields[2]
  26. if strings.Compare(unit, "f") == 0 {
  27. temp = (temp - 32) * (5.0 / 9.0)
  28. }
  29. fmt.Println("temp is " + strconv.FormatFloat(temp, 'f', 6, 64))
  30. return &Measurement{ID: 0, Temperature: temp}, nil
  31. }
  32. }
  33. return nil, nil
  34. }