• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang sift.Server类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/upwrd/sift.Server的典型用法代码示例。如果您正苦于以下问题:Golang Server类的具体用法?Golang Server怎么用?Golang Server使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Server类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: locations

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))
	}
}
开发者ID:upwrd,项目名称:sift,代码行数:35,代码来源:interactive.go


示例2: editLocationName

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)
	}
}
开发者ID:upwrd,项目名称:sift,代码行数:27,代码来源:interactive.go


示例3: editLocation

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)
	}
}
开发者ID:upwrd,项目名称:sift,代码行数:28,代码来源:interactive.go


示例4: ghostInTheCircuits

// 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)
				}
			}
		}
	}
}
开发者ID:upwrd,项目名称:sift,代码行数:48,代码来源:ghostInTheCircuits.go


示例5: repeatUpdatesToConsole

// 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)
		}
	}
}
开发者ID:upwrd,项目名称:sift,代码行数:18,代码来源:repeatToConsole.go


示例6: editDeviceLocation

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)
	}
}
开发者ID:upwrd,项目名称:sift,代码行数:43,代码来源:interactive.go


示例7: addLocation

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)
	}
}
开发者ID:upwrd,项目名称:sift,代码行数:23,代码来源:interactive.go


示例8: moviesAndChill

// 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)
			}
		}
	}
}
开发者ID:upwrd,项目名称:sift,代码行数:69,代码来源:moviesAndChill.go



注:本文中的github.com/upwrd/sift.Server类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang ipv4.ServiceContext类代码示例发布时间:2022-05-28
下一篇:
Golang waf.New函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap