func getAllComponents(tx *sqlx.Tx, exFlags ExpansionFlags) (map[types.ComponentID]types.Component, error) { type result struct { ID int64 DeviceID int64 `db:"device_id"` } // get results for each component in the database results := []result{} err := tx.Select(&results, "SELECT id, device_id FROM component") if err != nil { return nil, fmt.Errorf("error getting component IDs: %v", err) } // Build a set of components from database componentsByKey := make(map[types.ComponentID]types.Component) for _, result := range results { name, comp, err := getComponentTx(tx, result.ID, exFlags) if err != nil { return nil, fmt.Errorf("error getting component with ID %v: %v", result.ID, err) } key := types.ComponentID{ Name: name, DeviceID: types.DeviceID(result.DeviceID), } componentsByKey[key] = comp // put into the destination map } return componentsByKey, nil // all components gathered without error }
func upsertDeviceTx(tx *sqlx.Tx, extID types.ExternalDeviceID, d types.Device) (types.DeviceID, error) { // Try updating q := "UPDATE device SET name=?, is_online=? WHERE manufacturer=? AND external_id=?" res, err := tx.Exec(q, d.Name, d.IsOnline, extID.Manufacturer, extID.ID) if err != nil { return 0, fmt.Errorf("error updating device: %v", err) } // Check the number of rows affected by the update; should be 1 if the // light_emitter_state row existed, and 0 if not if n, err := res.RowsAffected(); err != nil { return 0, fmt.Errorf("error getting row count (required for update): %v", err) } else if n == 0 { // The update failed, do an insert instead q = "INSERT INTO device (manufacturer, external_id, name, is_online) VALUES (?, ?, ?, ?)" res, err := tx.Exec(q, extID.Manufacturer, extID.ID, d.Name, d.IsOnline) if err != nil { return 0, fmt.Errorf("error inserting device: %v", err) } id, err := res.LastInsertId() // Get ID from insert if err != nil || id == 0 { return 0, fmt.Errorf("error or zero-value ID (id: %v, err: %v)", id, err) } Log.Debug("inserted new device", "id", id, "external_id", extID, "device", d, "query", q) return types.DeviceID(id), nil } // Do Select to get the ID var id int64 if err = tx.Get(&id, "SELECT id FROM device WHERE manufacturer=? AND external_id=?", extID.Manufacturer, extID.ID); err != nil { return 0, fmt.Errorf("could not run select to get ID: %v", err) } Log.Debug("updated existing device", "id", id, "external_id", extID, "device", d, "query", q) return types.DeviceID(id), nil }
// 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) } } } } }
func getDevicesTx(tx *sqlx.Tx, exFlags ExpansionFlags) (map[types.DeviceID]types.Device, error) { devIDs := []int64{} if err := tx.Select(&devIDs, "SELECT id FROM device"); err != nil { return nil, fmt.Errorf("error getting device IDs from database: %v", err) } devs := make(map[types.DeviceID]types.Device) for _, id := range devIDs { dev := types.Device{} devID := types.DeviceID(id) if err := getDeviceTx(tx, &dev, devID, exFlags); err != nil { return nil, fmt.Errorf("could not get expanded Device with ID %v: %v", id, err) } devs[devID] = dev } return devs, nil }
// 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) } } } }