func main() { defer log.Flush() logConfig := ` <seelog> <outputs formatid="format1"> <rollingfile type="size" filename="./log/roll.log" maxsize="100" maxrolls="5" /> </outputs> <formats> <format id="format1" format="%Date/%Time [%LEV] %Msg%n"/> </formats> </seelog> ` logger, err := log.LoggerFromConfigAsBytes([]byte(logConfig)) check(err) log.ReplaceLogger(logger) weatherStation.UseLogger(logger) defer weatherStation.FlushLog() connection, err := weatherStation.Connect() check(err) weatherStation.WakeUp(connection) mqttClient, opts, err := StartMqttConnection() check(err) for { var wg sync.WaitGroup var dataChannel chan *weatherStation.WeatherData = make(chan *weatherStation.WeatherData) wg.Add(1) go weatherStation.GetCurrentData(connection, 30, dataChannel, &wg) go PostCurrentData(dataChannel, mqttClient, opts) wg.Wait() mqttClient.Disconnect(100) time.Sleep(time.Second * 1) var connected bool = false var i int = 1 for connected == false { mqttClient, _, err = StartMqttConnection() if err == nil { connected = true } else { time.Sleep(time.Second * time.Duration(i)) log.Warnf("wait for %d second to try to reconnect mqtt broker", i) i++ } } //time.Sleep(time.Second * 5) } log.Info("END") }
func TestGettingDataFromWeatherStation(t *testing.T) { actual := weatherStation.GetUSBPath() if assert.Equal(t, "/dev/ttyUSB0", actual) { connection, err := weatherStation.Connect() assert.Nil(t, err) assert.True(t, weatherStation.WakeUp(connection)) time.Sleep(time.Second * 2) assert.Contains(t, weatherStation.CallTestSequence(connection), "TEST") var wg sync.WaitGroup var dataChannel chan *weatherStation.WeatherData = make(chan *weatherStation.WeatherData) wg.Add(1) go weatherStation.GetCurrentData(connection, 1, dataChannel, &wg) wg.Wait() oneLoop := <-dataChannel assert.Len(t, oneLoop, 40) assert.Equal(t, 12, oneLoop.AvgWindSpeed) //assert.True(t, nbRead == 99) //assert.Equal(t, oneLoop, buffer) } }