Example #1
0
// SendToDevice sends data to address on deviceId.
func (m *Viki) sendToDevice(dev string, address string, data interface{}) error {
	if dev, ok := m.DeviceManager.Devices[devicemanager.DeviceId(dev)]; ok {
		dev.Execute(data, address)
		return nil
	}
	return fmt.Errorf("unknown device %s", dev)
}
Example #2
0
// ReadConfig reads the configuration from configuration file.
func (m *Viki) readConfig(file string) error {
	f, err := os.Open(file)
	if err != nil {
		return err
	}
	defer f.Close()

	scanner := bufio.NewScanner(f)
	c := []string{}
	comment := regexp.MustCompile(`^#|^[\s]*$`) // Ignore comment or blank lines.
	for scanner.Scan() {
		line := scanner.Text()
		if comment.MatchString(line) {
			continue
		}
		c = strings.Split(line, ",")
		for i, _ := range c {
			c[i] = strings.Trim(c[i], " ")
		}

		// Get device if any.
		var (
			ok  bool
			dev devicemanager.Device
		)
		i := 2
		// Ignore device if device not specified or empty.
		if len(c)-1 >= i && len(c[i]) > 0 {
			if dev, ok = m.DeviceManager.Devices[devicemanager.DeviceId(c[i])]; !ok {
				return fmt.Errorf("invalid device \"%s\" specified", c[i])
			}
		}

		// Get tags if any.
		tags, i := []string{}, 3
		if len(c)-1 >= i {
			tags = strings.Split(c[i], "|")
			for j, _ := range tags {
				tags[j] = strings.Trim(tags[j], " ")
			}
		}
		m.Objects[c[1]] = InitObject(c[0], dev, tags)
	}
	return nil
}