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

Golang pubsub.Event类代码示例

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

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



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

示例1: Event

func (self *Thermostat) Event(ev *pubsub.Event) {
	now := ev.Timestamp.Local() // must use Local time, as schedule is in local
	switch ev.Topic {
	case "temp":
		// temperature device update
		device := services.Config.LookupDeviceName(ev)
		if dev, ok := self.Devices[device]; ok {
			temp, _ := ev.Fields["temp"].(float64)
			dev.Update(temp, now)
			self.Check(now)
		}
	case "house":
		// house state update
		state := ev.Fields["state"]
		self.Occupied = (state != "Empty")
		self.Check(now)
	case "command":
		if ev.Target() == "ch" {
			value, ok := ev.Fields["value"].(string)
			if ok {
				err := self.setParty(value, ev.Timestamp)
				if err == nil {
					self.Check(now)
					// TODO - response
				} else {
					// TODO - error response
				}
			}
		}
	}
}
开发者ID:kienhung,项目名称:gohome,代码行数:31,代码来源:main.go


示例2: sendAnswer

func sendAnswer(request *pubsub.Event, source string, answer Answer) {
	fields := pubsub.Fields{
		"source": source,
		"target": request.StringField("source"),
	}
	if answer.Text != "" {
		fields["message"] = answer.Text
	}
	if answer.Json != nil {
		fields["json"] = answer.Json
	}

	remote := request.StringField("remote")
	if remote != "" {
		fields["remote"] = remote
	}

	topic := "alert"
	reply_to := request.StringField("reply_to")
	if reply_to != "" {
		topic = reply_to
	}

	response := pubsub.NewEvent(topic, fields)
	Publisher.Emit(response)
}
开发者ID:kienhung,项目名称:gohome,代码行数:26,代码来源:query.go


示例3: translateCommands

// Translate command messages into rfxtrx packets
func translateCommands(ev *pubsub.Event) (gorfxtrx.OutPacket, error) {
	device := ev.Device()
	command := ev.Command()
	if command != "off" && command != "on" {
		log.Println("Command not recognised:", command)
		return nil, nil
	}

	pids := services.Config.LookupDeviceProtocol(device)
	if len(pids) == 0 {
		log.Println("Device not found for:", device)
		return nil, nil
	}

	switch {
	case pids["homeeasy"] != "":
		return gorfxtrx.NewLightingHE(0x00, pids["homeeasy"], command)
	case pids["x10"] != "":
		return gorfxtrx.NewLightingX10(0x01, pids["x10"], command)
	}
	return nil, nil
}
开发者ID:kienhung,项目名称:gohome,代码行数:23,代码来源:main.go


示例4: writeToLogFile

func writeToLogFile(ev *pubsub.Event) {
	name := ev.Topic
	device := services.Config.LookupDeviceName(ev)
	if device != "" {
		ev.Fields["device"] = device
	}
	p := path.Join(logDir, name)
	ensureDirectory(p)
	p = path.Join(p, "data.log")
	// reopen the log file each time, so that log rotation can happen in the
	// background.
	// TODO: could this be done more smartly by checking inode and only
	// reopening when it changes?
	fio, err := os.OpenFile(p, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
	defer fio.Close()
	if err != nil {
		log.Println("Couldn't write file:", err)
		return
	}

	fio.Write(ev.Bytes())
	fio.WriteString("\n")
}
开发者ID:kienhung,项目名称:gohome,代码行数:23,代码来源:main.go


示例5: LookupDeviceName

// Find the device name
func (self *Config) LookupDeviceName(ev *pubsub.Event) string {
	// silly question: is device set on the event
	if ev.Device() != "" {
		return ev.Device()
	}
	topic := ev.Topic
	source := ev.Source()
	// try: protocol.id
	if device, ok := self.Protocols[topic][source]; ok {
		return device
	}
	// fallback: topic.source
	// ignore dynamic topics (prefix _)
	if topic != "" && source != "" && !strings.HasPrefix(topic, "_") {
		return topic + "." + source
	}
	return ""
}
开发者ID:kienhung,项目名称:gohome,代码行数:19,代码来源:config.go


示例6: Emit

// Emit an event
func (pub *Publisher) Emit(ev *pubsub.Event) {
	// put all topics under gohome/
	topic := "gohome/" + ev.Topic
	r := pub.client.Publish(MQTT.QOS_ONE, topic, ev.Bytes())
	<-r
}
开发者ID:kienhung,项目名称:gohome,代码行数:7,代码来源:publisher.go


示例7: eventCommand

func eventCommand(ev *pubsub.Event) {
	cam, ok := cameras[ev.Device()]
	if !ok {
		return
	}

	p, _ := ev.Fields["preset"].(float64)
	preset := int(p)

	switch ev.Fields["command"] {
	case "position":
		log.Printf("%s going to preset %d", ev.Device(), preset)
		cam.GotoPreset(preset)

	case "snapshot":
		log.Printf("%s taking snapshot", ev.Device())
		go func() {
			if preset != 0 {
				log.Printf("Going to preset: %d", preset)
				cam.GotoPreset(preset)
				time.Sleep(GotoDelay)
			}
			filename, err := cam.Snapshot()
			if err != nil {
				log.Println("Error taking snapshot:", err)
			} else {
				log.Println("Snapshot:", filename)
			}
		}()

	case "video":
		timeout, ok := ev.Fields["timeout"].(float64)
		if !ok {
			timeout = 15
		}
		duration := time.Duration(timeout) * time.Second
		log.Printf("%s recording video for %s", ev.Device(), duration)
		go func() {
			if preset != 0 {
				log.Printf("Going to preset: %d", preset)
				cam.GotoPreset(preset)
				time.Sleep(GotoDelay)
			}
			if ir, ok := ev.Fields["ir"].(bool); ok {
				err := cam.Ir(ir)
				if err != nil {
					log.Println("Error setting ir:", err)
				}
			}
			filename, err := cam.Video(duration)
			if err != nil {
				log.Println("Error taking video:", err)
			} else {
				log.Println("Video:", filename)
			}
		}()

	case "ir":
		on, _ := ev.Fields["on"].(bool)
		log.Printf("%s infra-red turned %s", ev.Device(), on)
		cam.Ir(on)

	case "detection":
		on, _ := ev.Fields["on"].(bool)
		log.Printf("%s detection %s", ev.Device(), on)
		cam.Detect(on)
	}
}
开发者ID:kienhung,项目名称:gohome,代码行数:68,代码来源:main.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang upid.Parse函数代码示例发布时间:2022-05-24
下一篇:
Golang glog.V函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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