Пример #1
0
func isOccupied() bool {
	// get presence from store
	value, err := services.Stor.Get("gohome/state/events/state/house.presence")
	if err != nil {
		log.Println("Couldn't get current presence:", err)
		return true
	}
	event := pubsub.Parse(value)
	return (event.Fields["state"] != "Empty")
}
Пример #2
0
func ExampleTranslateCommand() {
	ev := pubsub.Parse(`{"device": "light.glowworm", "timestamp": "2014-03-13 19:40:58.368298", "command": "on", "topic": "command"}`)
	services.Config = config.ExampleConfig
	pkt, err := translateCommands(ev)
	fmt.Println(err)
	fmt.Printf("%+v\n", pkt)
	// Output:
	// <nil>
	// &{typeId:0 SequenceNumber:0 HouseCode:74565 UnitCode:3 command:1 Level:0}
}
Пример #3
0
func ParseMsg(msg string) *pubsub.Event {
	// format: topic\0data
	//
	// topic is only used for subscription matching, present
	// in data so ignored otherwise)
	p := strings.Split(msg, "\000")
	if len(p) != 2 {
		return nil
	}
	return pubsub.Parse(p[1])
}
Пример #4
0
func (self *Subscriber) publishHandler(client *MQTT.MqttClient, msg MQTT.Message) {
	body := string(msg.Payload())
	event := pubsub.Parse(body)
	if event == nil {
		return
	}
	self.channelsLock.Lock()
	// fmt.Printf("Event: %+v\n", event)
	for _, ch := range self.channels {
		if ch.filter(event) {
			// fmt.Printf("Sending to: %+v\n", ch.topics)
			ch.C <- event
		}
	}
	self.channelsLock.Unlock()
}
Пример #5
0
func (self *Service) Run() error {
	b := bufio.NewScanner(os.Stdin)
	for b.Scan() {
		ev := pubsub.Parse(b.Text())
		if ev != nil {
			fmt.Println(ev)
			services.Publisher.Emit(ev)
		} else {
			fmt.Println("Parse failed")
		}
	}

	// give it time to send
	time.Sleep(time.Duration(500) * time.Millisecond)
	return nil
}
Пример #6
0
func request(path string, params url.Values) {
	if os.Getenv("GOHOME_API") == "" {
		fmtFatalf("Set GOHOME_API to the gohome api url.")
	}
	// add http auth
	api := os.Getenv("GOHOME_API")
	uri := fmt.Sprintf("%s/%s", api, path)
	if len(params) > 0 {
		uri += "?" + params.Encode()
	}
	client := httpClient()
	resp, err := client.Get(uri)
	if err != nil {
		if strings.HasSuffix(err.Error(), " EOF") { // yuck
			fmtFatalf("Server disconnected\n")
		} else {
			fmtFatalf("error: %s\n", err)
		}
	}
	defer resp.Body.Close()
	scanner := bufio.NewScanner(resp.Body)

	n := 0
	for scanner.Scan() {
		ev := pubsub.Parse(scanner.Text())
		if ev == nil {
			continue
		}
		source := ev.Source()
		message := ev.StringField("message")

		if strings.Contains(message, "\n") {
			fmt.Printf("\x1b[32;1m%s\x1b[0m\n%s\n", source, message)
		} else {
			fmt.Printf("\x1b[32;1m%s\x1b[0m %s\n", source, message)
		}
		n += 1
	}
	if n == 0 {
		fmt.Println("No response")
	}
}
Пример #7
0
func getDevicesEvents() map[string]map[string]interface{} {
	// Get state from store
	ret := make(map[string]map[string]interface{})
	// gohome/state/events/<topic>/<device>
	nodes, _ := services.Stor.GetRecursive("gohome/state/events")
	for _, node := range nodes {
		ks := strings.Split(node.Key, "/")
		if len(ks) != 5 {
			continue
		}
		ev := pubsub.Parse(node.Value)
		topic := ks[3]
		device := ks[4]
		if _, ok := ret[device]; !ok {
			ret[device] = make(map[string]interface{})
		}
		ret[device][topic] = ev.Map()
	}
	// returns {device: {topic: {event}}}
	return ret
}