Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 2.09 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package main
  2.  
  3. import (
  4.         couch "code.google.com/p/couch-go"
  5.         "encoding/json"
  6.         "flag"
  7.         "fmt"
  8.         "log"
  9.         "net"
  10.         "reflect"
  11.         "runtime"
  12.         "strconv"
  13.         "strings"
  14.         "time"
  15. )
  16.  
  17. // 2011/07/17 02:01:33 2011/07/17 02:01:32.953335       10E8C214000000E4        18.93   l=12.00,h=23.00
  18. type reading struct {
  19.         when    time.Time
  20.         sensor  string
  21.         reading float32
  22. }
  23.  
  24. var timeInFormat string = "2006/01/02 15:04:05"
  25. var timeOutFormat string = "2006-01-02T15:04:05"
  26.  
  27. var couchURL = flag.String("couch", "http://localhost:5984/temperature",
  28.         "URL of the CouchDB")
  29.  
  30. func (r reading) TS() string {
  31.         return r.when.Format(timeOutFormat)
  32. }
  33.  
  34. func (r reading) MarshalJSON() ([]byte, error) {
  35.         doc := map[string]interface{}{
  36.                 "type":    reflect.TypeOf(r).Name(),
  37.                 "reading": r.reading,
  38.                 "ts":      r.TS(),
  39.                 "sensor":  r.sensor,
  40.         }
  41.         return json.Marshal(doc)
  42. }
  43.  
  44. func fatal(format string, v ...interface{}) {
  45.         log.Printf(format, v...)
  46.         runtime.Goexit()
  47. }
  48.  
  49. func process(msg reading) {
  50.         id := fmt.Sprintf("%s_%s", msg.TS(), msg.sensor)
  51.         db, err := couch.Connect(*couchURL)
  52.         if err != nil {
  53.                 fatal("Error connecting to DB:  %v", err)
  54.         }
  55.  
  56.         _, _, ierr := db.InsertWith(msg, id)
  57.         if ierr != nil {
  58.                 fatal("Error inserting new item:  %v", ierr)
  59.         }
  60. }
  61.  
  62. func read(s *net.UDPConn) {
  63.         b := make([]byte, 256)
  64.         for {
  65.                 n, e := s.Read(b)
  66.                 if e != nil {
  67.                         log.Fatalf("Error reading from socket:  %s", e)
  68.                 }
  69.                 parts := strings.Split(string(b[0:n]), "\t")
  70.                 f, ferr := strconv.ParseFloat(parts[2], 32)
  71.                 if ferr != nil {
  72.                         log.Printf("Error parsing float:  %s", ferr)
  73.                         continue
  74.                 }
  75.                 t, terr := time.Parse(timeInFormat, strings.Split(parts[0], ".")[0])
  76.                 if terr != nil {
  77.                         log.Printf("Error parsing time: %s", terr)
  78.                         continue
  79.                 }
  80.  
  81.                 // Do this to convert to UTC
  82.                 // t = time.SecondsToUTC(t.Seconds() - int64(time.LocalTime().ZoneOffset))
  83.  
  84.                 record := reading{
  85.                         when:    t,
  86.                         sensor:  parts[1],
  87.                         reading: float32(f),
  88.                 }
  89.                 go process(record)
  90.         }
  91. }
  92.  
  93. func main() {
  94.         flag.Parse()
  95.  
  96.         socket, err := net.ListenMulticastUDP("udp4",
  97.                 nil, &net.UDPAddr{
  98.                         IP:   net.IPv4(225, 0, 0, 37),
  99.                         Port: 6789,
  100.                 })
  101.  
  102.         if err != nil {
  103.                 log.Fatalf("listen %s", err)
  104.         }
  105.  
  106.         defer socket.Close()
  107.         read(socket)
  108. }