Beispiel #1
0
func (self *Service) querySwitch(q services.Question) string {
	if q.Args == "" {
		// return a list of the devices
		devices := []string{}
		for dev, _ := range services.Config.Devices {
			devices = append(devices, dev)
		}
		sort.Strings(devices)
		return strings.Join(devices, ", ")
	}
	args := strings.Split(q.Args, " ")
	name := args[0]
	command := "on"
	if len(args) > 1 {
		command = args[1]
	}
	matches := []string{}
	for dev, _ := range services.Config.Devices {
		if strings.Contains(dev, name) {
			matches = append(matches, dev)
		}
	}

	if len(matches) == 0 {
		return fmt.Sprintf("device %s not found", name)
	}
	if len(matches) > 1 {
		return fmt.Sprintf("device %s is ambiguous", strings.Join(matches, ", "))
	}
	device := matches[0]
	ev := pubsub.NewCommand(device, command, 0)
	services.Publisher.Emit(ev)
	return fmt.Sprintf("Switched %s %s", matches[0], command)
}
Beispiel #2
0
func command(device string, state bool, repeat int) {
	command := "off"
	if state {
		command = "on"
	}
	ev := pubsub.NewCommand(device, command, repeat)
	services.Publisher.Emit(ev)
}
Beispiel #3
0
func (self *Thermostat) Command() {
	command := "off"
	if self.State {
		command = "on"
	}
	ev := pubsub.NewCommand(self.HeatingDevice, command, 0)
	self.Publisher.Emit(ev)
}
Beispiel #4
0
func apiDevicesControl(w http.ResponseWriter, r *http.Request) {
	q := r.URL.Query()
	device := q.Get("id")
	var command string
	if q.Get("control") == "1" {
		command = "on"
	} else {
		command = "off"
	}
	// send command
	ev := pubsub.NewCommand(device, command, 0)
	services.Publisher.Emit(ev)
	jsonResponse(w, true)
}