Beispiel #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")
		}
	}
}
Beispiel #2
0
func main() {
	viper.SetEnvPrefix("SENSOR")
	viper.SetDefault("listen_address", ss.DefaultFlareWSAddr)
	viper.AutomaticEnv()

	rand.Seed(time.Now().UTC().UnixNano())
	reading := &types.FlareReading{
		SolarFlare: ss.InitSolarFlare,
	}

	go solarFlareRoutine(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)
		}
	}
}
Beispiel #3
0
func main() {
	flag.Parse()

	adminToken := os.Getenv("ADMIN_TOKEN")
	if len(adminToken) <= 0 {
		adminToken = randToken()
		log.Printf("Admin token not defined. Using '%s'", adminToken)
	}

	autoReadings, err := strconv.ParseBool(os.Getenv("AUTO_READINGS"))
	if err != nil {
		autoReadings = true
	}
	game.autoReadings = autoReadings
	log.Printf("Automatic readings set to '%t'", autoReadings)

	blaster := wsblaster.GetBlaster(addr, false)
	blaster.StartHub()
	game.blaster = blaster

	go game.run(adminToken)

	router := NewRouter(blaster)

	err = http.ListenAndServe(*addr, router)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
Beispiel #4
0
func sensorUpdateRoutine(r *types.SensorSuiteReading, c chan bool) {
	tempAddr := viper.GetString("temperature_address")
	radAddr := viper.GetString("radiation_address")
	addr := viper.GetString("listen_address")

	tu := url.URL{Scheme: "ws", Host: tempAddr, Path: "/ws"}
	ru := url.URL{Scheme: "ws", Host: radAddr, Path: "/ws"}
	tuStr := tu.String()
	ruStr := ru.String()
	tempReader, err := wsreader.GetWSReader(&tuStr)
	if err != nil {
		log.Printf("%s", err)
		c <- true
		return
	}
	radReader, err := wsreader.GetWSReader(&ruStr)
	if err != nil {
		log.Printf("%s", err)
		c <- true
		return
	}
	tempReader.Run()
	radReader.Run()

	blaster := wsblaster.GetBlaster(&addr, false)
	go blaster.Run()

	tr := &types.TemperatureReading{}
	rr := &types.RadiationReading{}
	ticker := time.NewTicker(1 * time.Second)
	for {
		select {
		case m := <-tempReader.C:
			json.Unmarshal(m, tr)
			r.Lock()
			r.Temperature = tr.Temperature
			r.Unlock()
		case m := <-radReader.C:
			json.Unmarshal(m, rr)
			r.Lock()
			r.Radiation = rr.Radiation
			r.Unlock()
		case <-ticker.C:
			r.RLock()
			m, _ := json.Marshal(r)
			r.RUnlock()
			blaster.Write(m)
		case <-tempReader.Exit:
			log.Printf("Lost connection to Temperature websocket")
			c <- true
			return
		case <-radReader.Exit:
			log.Printf("Lost connection to Radiation websocket")
			c <- true
			return
		}
	}
}
Beispiel #5
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")
		}
	}
}