Example #1
0
func Handler(in <-chan nog.Message, out chan<- nog.Message) {
	out <- nog.Message{What: "started"}
	a := &Activity{}

	go func() {
		out <- nog.Template("activity")
	}()

	for m := range in {
		if m.Why == "statechanged" {
			dec := json.NewDecoder(strings.NewReader(m.What))
			if err := dec.Decode(a); err != nil {
				log.Println("activity decode err:", err)
			}
		} else {
			const IAM = "I am "
			what := m.What
			if strings.HasPrefix(m.What, IAM) {
				what = m.What[len(IAM):]
				a.UpdateActivity(what)
				if what, err := json.Marshal(a); err == nil {
					out <- nog.Message{What: string(what), Why: "statechanged"}
				} else {
					log.Println("StateChanged err:", err)
				}

				out <- nog.Message{What: "do " + what}
			}
		}
	}
	out <- nog.Message{What: "stopped"}
	close(out)

}
Example #2
0
func Handler(in <-chan nog.Message, out chan<- nog.Message) {
	out <- nog.Message{What: "started"}
	s := &Motion{}

	go func() {
		out <- nog.Template("motion")
	}()

	var motionTimer *time.Timer
	var motionTimeout <-chan time.Time

	if c, err := gpio.GPIOInterrupt(7); err == nil {
		s.motionChannel = c
	} else {
		log.Println("Warning: Motion sensor off:", err)
		out <- nog.Message{What: "no motion sensor found"}
		goto done
	}

	for {
		select {
		case m, ok := <-in:
			if !ok {
				goto done
			}
			if m.Why == "statechanged" {
				dec := json.NewDecoder(strings.NewReader(m.What))
				if err := dec.Decode(s); err != nil {
					log.Println("motion decode err:", err)
				}
			}
		case motion := <-s.motionChannel:
			if motion {
				out <- nog.Message{What: "motion detected"}
				const duration = 60 * time.Second
				if motionTimer == nil {
					s.Motion = true
					motionTimer = time.NewTimer(duration)
					motionTimeout = motionTimer.C // enable motionTimeout case
				} else {
					motionTimer.Reset(duration)
				}
			}
		case <-motionTimeout:
			s.Motion = false
			motionTimer = nil
			motionTimeout = nil
			out <- nog.Message{What: "motion detected timeout"}
		}
	}
done:
	out <- nog.Message{What: "stopped"}
	close(out)

}
Example #3
0
func Handler(in <-chan nog.Message, out chan<- nog.Message) {
	out <- nog.Message{What: "started"}
	s := &Presence{}
	var presenceChannel chan presence.Presence

	go func() {
		out <- nog.Template("presence")
	}()

	for {
		select {
		case m, ok := <-in:
			if !ok {
				goto done
			}
			if m.Why == "statechanged" {
				dec := json.NewDecoder(strings.NewReader(m.What))
				if err := dec.Decode(s); err != nil {
					log.Println("presence decode err:", err)
				}
				if s.Present == nil {
					s.Present = make(map[string]bool)
				}
				if presenceChannel == nil {
					presenceChannel = presence.Listen(s.Present)
				}
			}
		case p := <-presenceChannel:
			if s.Present[p.Name] != p.Status {
				s.Present[p.Name] = p.Status
				var status string
				if p.Status {
					status = "home"
				} else {
					status = "away"
				}
				out <- nog.Message{What: p.Name + " is " + status}

				if what, err := json.Marshal(s); err == nil {
					out <- nog.Message{What: string(what), Why: "statechanged"}
				} else {
					log.Println("StateChanged err:", err)
				}
			}
		}
	}
done:
	out <- nog.Message{What: "stopped"}
	close(out)

}
Example #4
0
func Handler(in <-chan nog.Message, out chan<- nog.Message) {
	out <- nog.Message{What: "started"}

	go func() {
		out <- nog.Template("daylights")
	}()

	for m := range in {
		if m.What == "it is light" {
			out <- nog.Message{What: "do daylights off"}
		}
		if m.What == "it is dark" {
			out <- nog.Message{What: "do daylights on"}
		}
	}

	out <- nog.Message{What: "stopped"}
	close(out)
}
Example #5
0
func Handler(in <-chan nog.Message, out chan<- nog.Message) {
	out <- nog.Message{What: "started"}
	a := &Nouns{}

	go func() {
		out <- nog.Template("nouns")
	}()

	for m := range in {
		if m.Why == "statechanged" {
			dec := json.NewDecoder(strings.NewReader(m.What))
			if err := dec.Decode(a); err != nil {
				log.Println("nouns decode error:", err)
			}
		}
	}
	out <- nog.Message{What: "stopped"}
	close(out)

}
Example #6
0
func Handler(in <-chan nog.Message, out chan<- nog.Message) {
	out <- nog.Message{What: "started"}
	s := &Schedule{}
	var scheduledEventsChannel <-chan scheduler.Event

	go func() {
		out <- nog.Template("schedule")
	}()

	for {
		select {
		case e := <-scheduledEventsChannel:
			out <- nog.Message{What: e.What}
		case m, ok := <-in:
			if !ok {
				goto done
			}
			if m.Why == "statechanged" {
				dec := json.NewDecoder(strings.NewReader(m.What))
				if err := dec.Decode(s); err != nil {
					log.Println("schedule decode err:", err)
				}
				if scheduledEventsChannel == nil {
					if c, err := s.Schedule.Run(); err == nil {
						scheduledEventsChannel = c
					} else {
						log.Println("Warning: Scheduled events off:", err)
					}
				}
			}
		}
	}
done:
	out <- nog.Message{What: "stopped"}
	close(out)
}
Example #7
0
func Handler(in <-chan nog.Message, out chan<- nog.Message) {
	h := &Hue{}
	var createUserChan <-chan time.Time

	out <- nog.Message{What: "started"}

	go func() {
		out <- nog.Template("hue")
	}()

	for {
		select {
		case <-createUserChan:
			if err := h.Hue.CreateUser(h.Hue.Username, "Marvin"); err == nil {
				createUserChan = nil
			} else {
				out <- nog.Message{What: fmt.Sprintf("%s: press hue link button to authenticate", err)}
			}
		case m, ok := <-in:
			if !ok {
				goto done
			}
			if m.Why == "statechanged" {
				dec := json.NewDecoder(strings.NewReader(m.What))
				if err := dec.Decode(h); err != nil {
					log.Println("hue decode err:", err)
				}
				if err := h.Hue.GetState(); err != nil {
					createUserChan = time.NewTicker(1 * time.Second).C
				} else {
					createUserChan = nil
				}
				continue
			}
			const SETHUE = "set hue address "
			if strings.HasPrefix(m.What, SETHUE) {
				words := strings.Split(m.What[len(SETHUE):], " ")
				if len(words) == 3 {
					address := words[0]
					state := words[2]
					var s interface{}
					dec := json.NewDecoder(strings.NewReader(state))
					if err := dec.Decode(&s); err != nil {
						log.Println("json decode err:", err)
					} else {
						h.Hue.Set(address, s)
						err := h.Hue.GetState()
						if err != nil {
							log.Println("ERROR:", err)
						}
						if what, err := json.Marshal(h); err == nil {
							out <- nog.Message{What: string(what), Why: "statechanged"}
						} else {
							log.Println("StateChanged err:", err)
						}
					}
				} else {
					log.Println("unexpected number of words in:", m)
				}
			}
			const SET = "set light "
			if strings.HasPrefix(m.What, SET) {
				e := strings.SplitN(m.What[len(SET):], " to ", 2)
				if len(e) == 2 {
					address := h.Nouns[e[0]]
					state := h.States[e[1]]
					if strings.Contains(address, "/light") {
						address += "/state"
					} else {
						address += "/action"
					}
					h.Hue.Set(address, state)
					err := h.Hue.GetState()
					if err != nil {
						log.Println("ERROR:", err)
					}
					if what, err := json.Marshal(h); err == nil {
						out <- nog.Message{What: string(what), Why: "statechanged"}
					} else {
						log.Println("StateChanged err:", err)
					}
				} else {
					log.Println("unexpected number of words in:", m)
				}
			}
		}
	}
done:
	out <- nog.Message{What: "stopped"}
	close(out)

}