Exemple #1
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)

}
Exemple #2
0
func Run(in <-chan nog.Message, out chan<- nog.Message) {
	out <- nog.Message{What: "started"}
	name := "eddie.html"
	if j, err := os.OpenFile(path.Join(Root, name), os.O_RDONLY, 0666); err == nil {
		if b, err := ioutil.ReadAll(j); err == nil {
			out <- nog.NewMessage("Eddie", string(b), "template")
		} else {
			log.Println("ERROR reading:", err)
		}
	} else {
		log.Println("WARNING: could not open ", name, err)
	}

	ch, err := gpio.GPIOInterrupt(38)
	if err != nil {
		panic(err)
	}

	go func() {
		var pressCount int64
		for {
			select {
			case value := <-ch:
				if value {
					atomic.AddInt64(&pressCount, 1)
					time.AfterFunc(time.Second, func() {
						atomic.AddInt64(&pressCount, -1)
					})
					switch pressCount {
					case 1:
						out <- nog.NewMessage("Eddie", "I am sleeping", "Eddie")
					case 2:
						out <- nog.NewMessage("Eddie", "set light All to nightlight", "Eddie")
					default:
						out <- nog.NewMessage("Eddie", "set light All to chime", "Eddie")
					}
				}
			}
		}
	}()
	for _ = range in {
	}
}
Exemple #3
0
func Handler(in <-chan nog.Message, out chan<- nog.Message) {
	out <- nog.Message{What: "started"}
	s := &Motion{}
	name := "motion.html"
	if j, err := os.OpenFile(path.Join(Root, name), os.O_RDONLY, 0666); err == nil {
		if b, err := ioutil.ReadAll(j); err == nil {
			out <- nog.Message{What: string(b), Why: "template"}
		} else {
			log.Println("ERROR reading:", err)
		}
	} else {
		log.Println("WARNING: could not open ", name, err)
	}

	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)

}