Example #1
0
func main() {
	cfgFile := flag.String("config", "config.yaml", "Path to config file (default: ./config.yaml)")
	flag.Parse()

	// reportChan is used to send wx reports from the reader to the DB recorder
	reportChan := make(chan WxReport)

	// Get a new DavisSi1000 object
	d := NewDavisSi1000()

	// Read our server configuration from our YAML config file
	filename, _ := filepath.Abs(*cfgFile)
	cfg, err := config.New(filename)
	if err != nil {
		log.Fatalln("Error reading config file.  Did you pass the -config flag?  Run with -h for help.\n", err)
	}
	d.config = cfg

	// Connect to influxdb
	u, _ := url.Parse(d.config.InfluxDB.InfluxURL)
	ic := influx.NewClient(influx.Config{
		URL:      u,
		Username: d.config.InfluxDB.InfluxUser,
		Password: d.config.InfluxDB.InfluxPass,
	})

	// Connect to the Si1000.  Subsequent re-connects are handled within readReports()
	d.connectToSerialSi1000()

	go d.storeReports(reportChan, ic)
	d.readReports(reportChan)
}
func main() {
	flag.Parse()

	addr, err := net.ResolveUDPAddr("udp4", *inAddr)
	if err != nil {
		log.Fatalf("Can't resolve address: '%v'", err)
	}

	sock, err := net.ListenUDP("udp4", addr)
	if err != nil {
		log.Fatalf("Can't open UDP socket: '%v'", err)
	}

	log.Printf("Start listening on udp://%v\n", *inAddr)

	defer sock.Close()

	u, _ := url.Parse("http://localhost:8086")
	c := client.NewClient(client.Config{
		URL:      u,
		Username: "******",
		Password: "******",
	})

	sinkChan := make(chan Pinba.Request)
	bpc := client.BatchPointsConfig{
		Precision: "us",
		Database:  "pinba",
	}
	sink := NewInfluxDBSink(100, sinkChan, c, bpc)
	go sink.run()
	for {
		var buf = make([]byte, 65536)
		rlen, _, err := sock.ReadFromUDP(buf)
		if err != nil {
			log.Fatalf("Error on sock.ReadFrom, %v", err)
		}
		if rlen == 0 {
			continue
		}

		request := &Pinba.Request{}
		proto.Unmarshal(buf[0:rlen], request)
		log.Printf("%v", request)
		sinkChan <- *request
	}
}