Example #1
0
func main() {
	viper.SetEnvPrefix("SENSOR")
	viper.SetDefault("listen_address", ss.DefaultRadWSAddr)
	viper.SetDefault("flare_address", "localhost"+ss.DefaultFlareWSAddr)
	viper.AutomaticEnv()

	rand.Seed(time.Now().UTC().UnixNano())
	reading := &types.RadiationReading{
		Radiation: ss.InitRadiation,
	}

	flareExit := make(chan bool)
	flareAddr := viper.GetString("flare_address")
	go types.FlareUpdateRoutine(reading, &flareAddr, flareExit)

	go radiationRoutine(reading)

	addr := viper.GetString("listen_address")
	blaster := wsblaster.GetBlaster(&addr, false)
	go blaster.Run()
	ticker := time.NewTicker(1 * time.Second)
	for {
		select {
		case <-ticker.C:
			reading.RLock()
			m, _ := json.Marshal(reading)
			reading.RUnlock()
			blaster.Write(m)
		case <-flareExit:
			log.Fatal("Unable to connect to Solar Flare source")
		}
	}
}
Example #2
0
func main() {
	viper.SetDefault("listen_address", ss.DefaultAggWSAddr)
	viper.SetDefault("sensor_flare_address", "localhost"+ss.DefaultFlareWSAddr)
	viper.SetDefault("gc_address", "localhost:8080")
	viper.SetDefault("post_gc", true)
	viper.SetDefault("admin_token", "1234")
	viper.AutomaticEnv()

	addr := viper.GetString("listen_address")
	blaster := wsblaster.GetBlaster(&addr, true)
	go blaster.Run()

	reading := &Message{}

	flareExit := make(chan bool)
	flareAddr := viper.GetString("sensor_flare_address")
	go types.FlareUpdateRoutine(reading, &flareAddr, flareExit)

	doPost := viper.GetBool("post_gc")
	gcAddr := "http://" + viper.GetString("gc_address") + "/api/readings"
	gcToken := viper.GetString("admin_token")

	ticker := time.NewTicker(1 * time.Second)
	for {
		select {
		case <-ticker.C:
			ok := averageReadings(reading, blaster)
			if !ok {
				break
			}
			reading.RLock()
			m, _ := json.Marshal(reading)
			reading.RUnlock()
			blaster.Write(m)
			if doPost {
				go postReading(m, &gcAddr, &gcToken)
			}
			// Go read all the sensor values and average them
			// POST values to Game Controller
		case <-flareExit:
			log.Fatal("Unable to connect to Solar Flare source")
		}
	}
}