コード例 #1
0
ファイル: interactive.go プロジェクト: upwrd/sift
func locations(server *sift.Server) {
	// Get the locations from database
	siftDB, err := server.DB()
	checkErr(err)
	locs := []db.Location{}
	siftDB.Select(&locs, "SELECT * FROM location")

	// Print the prompt
	fmt.Printf("-- Locations --\n")
	for _, loc := range locs {
		fmt.Printf("   %v %v\n", loc.ID, loc.Name)
	}

	fmt.Printf("[enter R to refresh, A to add a location, an ID to edit, or blank to go back]\n")
	bio := bufio.NewReader(os.Stdin)
	lineByte, _, err := bio.ReadLine()
	checkErr(err)
	line := string(lineByte)

	switch {
	case line == "":
		topLevel(server) // return to top level
	case strings.HasPrefix(line, "r"), strings.HasPrefix(line, "R"):
		locations(server) // refresh by recalling the function
	case strings.HasPrefix(line, "a"), strings.HasPrefix(line, "A"):
		addLocation(server)
	default:
		// Try to parse the input as a number
		id, err := strconv.Atoi(string(line))
		if err != nil {
			locations(server)
		}
		editLocation(server, int64(id))
	}
}
コード例 #2
0
ファイル: interactive.go プロジェクト: upwrd/sift
func editLocationName(server *sift.Server, id int64) {
	// Print the prompt
	fmt.Printf("-- Edit name for Location %v--\n", id)
	fmt.Printf("Name? (blank to go back):\n")
	bio := bufio.NewReader(os.Stdin)
	lineByte, _, err := bio.ReadLine()
	checkErr(err)
	line := string(lineByte)

	switch {
	case line == "":
		locations(server) // go back up to locations
	default:
		siftDB, err := server.DB()
		checkErr(err)
		res, err := siftDB.Exec("UPDATE location SET name=? WHERE id=?", line, id)
		checkErr(err)
		n, err := res.RowsAffected()
		checkErr(err)
		if n == 0 {
			fmt.Printf(">> no rows affected, try again?\n")
		} else {
			fmt.Printf(">> location %v is now named %v\n", id, line)
		}
		locations(server)
	}
}
コード例 #3
0
ファイル: interactive.go プロジェクト: upwrd/sift
func editLocation(server *sift.Server, id int64) {
	// Print the prompt
	fmt.Printf("-- Edit Location %v --\n", id)
	fmt.Printf("[enter N to change name, D to delete, or blank to go back]\n")
	bio := bufio.NewReader(os.Stdin)
	lineByte, _, err := bio.ReadLine()
	checkErr(err)
	line := string(lineByte)

	switch {
	case line == "":
		locations(server) // go back up to locations
	case strings.HasPrefix(line, "d"), strings.HasPrefix(line, "D"):
		// perform delete
		siftDB, err := server.DB()
		checkErr(err)
		_, err = siftDB.Exec("DELETE FROM location WHERE id=?", id)
		checkErr(err)
		fmt.Printf(">> deleted location %v\n", id)
		locations(server)
	case strings.HasPrefix(line, "n"), strings.HasPrefix(line, "N"):
		// change the name
		editLocationName(server, id)
	default:
		// if unknown, repeat prompt
		editLocation(server, id)
	}
}
コード例 #4
0
ファイル: ghostInTheCircuits.go プロジェクト: upwrd/sift
// This SIFT app will randomly alter any detected lights
func ghostInTheCircuits(server *sift.Server, minMS, maxMS int) {
	for {
		// Wait for a random amount of time
		<-time.After(randTime(minMS, maxMS))

		// Get all lights connected to the system
		lightsQuery := `
			SELECT c.name, c.device_id FROM component c
				JOIN device d ON c.device_id=d.id
				WHERE is_online=1 AND type=?`
		lights := []db.Component{}
		db, err := server.DB()
		if err != nil {
			color.Red("could not open DB: %v", err)
		}
		// run the query
		if err = db.Select(&lights, lightsQuery, types.LightEmitter{}.Type()); err != nil {
			color.Red("could not run query to get light ids: %v", err)
		}

		if len(lights) == 0 {
			color.Red("no lights found for the circuit ghosts to play with...")
		} else {
			// For each light found...
			for _, light := range lights {
				// ...assemble a component ID...
				lightID := types.ComponentID{
					Name:     light.Name,
					DeviceID: types.DeviceID(light.DeviceID),
				}

				// ...generate a random brightness value (0-100)...
				randBrightness := uint8(rand.Intn(100))

				// ...then create and submit an intent
				newLightIntent := types.SetLightEmitterIntent{
					BrightnessInPercent: randBrightness,
				}
				if err := server.EnactIntent(lightID, newLightIntent); err != nil {
					color.Red("could not enact intent: %v", err)
				} else {
					color.Blue("set light %v to %v", lightID, randBrightness)
				}
			}
		}
	}
}
コード例 #5
0
ファイル: repeatToConsole.go プロジェクト: upwrd/sift
// main starts a sift server and listens for updates, printing any to console.
func repeatUpdatesToConsole(server *sift.Server) {
	myToken := server.Login()
	updateChan := server.Listen(myToken) // without specifying filters, this will listen to everything

	fmt.Println("listening to SIFT server and printing updates to console...")
	for {
		update := <-updateChan //TODO: uncaught panic if updateChan is closed
		switch typed := update.(type) {
		case notif.ComponentNotification:
			color.Blue("component %+v %v: %+v\n", typed.ID, typed.Action, typed.Component)
		//case notif.DriverNotification:
		//	fmt.Printf("driver %v: %v", typed.NotificationType, typed.Component)
		default:
			color.Red("unhandled update type from updateChan: %T (%v)\n", update, update)
		}
	}
}
コード例 #6
0
ファイル: interactive.go プロジェクト: upwrd/sift
func editDeviceLocation(server *sift.Server, id int64) {
	// Get available locations from database
	siftDB, err := server.DB()
	checkErr(err)
	locs := []db.Location{}
	siftDB.Select(&locs, "SELECT * FROM location")

	// Print the prompt
	fmt.Printf("-- Set Location for Device %v --\n", id)
	fmt.Printf("Available Locations:\n")
	for _, loc := range locs {
		fmt.Printf("   %v %v\n", loc.ID, loc.Name)
	}
	fmt.Printf("[enter an ID to set Location, R to refresh Locations, or blank to go back]\n")

	bio := bufio.NewReader(os.Stdin)
	lineByte, _, err := bio.ReadLine()
	checkErr(err)
	line := string(lineByte)

	switch {
	case line == "":
		editDevice(server, id) // return to top level
	case strings.HasPrefix(line, "r"), strings.HasPrefix(line, "R"):
		editDeviceLocation(server, id) // refresh by recalling the function
	default:
		// Try to parse the input as a number
		locID, err := strconv.Atoi(string(line))
		if err != nil {
			editDeviceLocation(server, id)
		}
		res, err := siftDB.Exec("UPDATE device SET location_id=? WHERE id=?", locID, id)
		checkErr(err)
		n, err := res.RowsAffected()
		checkErr(err)
		if n == 0 {
			fmt.Printf(">> no rows affected, try again?\n")
		} else {
			fmt.Printf(">> device %v is now in location %v\n", id, locID)
		}
		devices(server)
	}
}
コード例 #7
0
ファイル: interactive.go プロジェクト: upwrd/sift
func addLocation(server *sift.Server) {
	// Print the prompt
	fmt.Printf("-- Add Location --\n")
	fmt.Printf("[Enter name (blank to go back)]:\n")
	bio := bufio.NewReader(os.Stdin)
	lineByte, _, err := bio.ReadLine()
	checkErr(err)
	line := string(lineByte)

	switch {
	case line == "":
		locations(server) // go back up to locations
	default:
		siftDB, err := server.DB()
		checkErr(err)
		res, err := siftDB.Exec("INSERT INTO location (name) VALUES (?)", line)
		checkErr(err)
		id, err := res.LastInsertId()
		checkErr(err)
		fmt.Printf(">> inserted %v as location %v\n", line, id)
		locations(server)
	}
}
コード例 #8
0
ファイル: moviesAndChill.go プロジェクト: upwrd/sift
// This SIFT app will turn the lights down in any room with a running media
// player. If there are no running media players, the lights will be set to
// high.
func moviesAndChill(server *sift.Server) {
	if server == nil { // sanity check
		return
	}
	token := server.Login() // authenticate with SIFT

	// SIFT apps can request notifications from SIFT to determine when they
	// should do something. For this app, we'll want to know when media players
	// change (e.g. if they are added or their states change), and when lights
	// change (e.g. if they are added, moved, or removed)
	mediaFilter := notif.ComponentFilter{Type: types.ComponentTypeMediaPlayer}
	lightsFilter := notif.ComponentFilter{Type: types.ComponentTypeLightEmitter}
	listener := server.Listen(token, lightsFilter, mediaFilter) // this starts listening

	for range listener {
		// Each time the listener receives a notification, this example will
		// recalculate the lighting values for each light.
		//   (A better implementation might look at the updates and only
		//   recalculate those that need to be recalculated)

		// Run a query against the SIFT sqlite database to find each available
		// light and the number of PLAYING media players in the same room.
		lightsQuery := `
			SELECT c1.device_id, c1.name, num_playing_mpls_in_room
			FROM light_emitter_state l1
			JOIN component c1 ON l1.id=c1.id
			JOIN device d1 ON c1.device_id=d1.id
			JOIN (SELECT d.location_id loc,
				SUM (CASE WHEN m.play_state="PLAYING" THEN 1 ELSE 0 END) as num_playing_mpls_in_room
				FROM media_player_state m
				JOIN component c ON m.id=c.id
				JOIN device d ON c.device_id=d.id
				GROUP BY d.location_id)
				ON d1.location_id=loc;`
		type result struct {
			DeviceID                     int64 `db:"device_id"`
			Name                         string
			NumPlayingMediaPlayersInRoom int `db:"num_playing_mpls_in_room"`
		}
		results := []result{}
		db, _ := server.DB()
		// run the query
		if err := db.Select(&results, lightsQuery); err != nil {
			panic(fmt.Sprintf("could not run query to get active media players: %v", err))
		}

		// Check out the results and determine how each light should be set. In
		// SIFT, this is done using Intents.
		for _, result := range results {
			var intent types.SetLightEmitterIntent
			if result.NumPlayingMediaPlayersInRoom > 0 { // a movie is playing in the room ...
				intent.BrightnessInPercent = lightsLow // ...bring the lights down low
			} else { // no movies in this room...
				intent.BrightnessInPercent = lightsHigh // ...bring the lights up
			}
			target := types.ComponentID{
				DeviceID: types.DeviceID(result.DeviceID),
				Name:     result.Name,
			}
			// Send the intent to the SIFT server, which will make it real
			if err := server.EnactIntent(target, intent); err != nil {
				fmt.Printf("warning: could not enact intent: %v\n", err)
			}
		}
	}
}