Exemple #1
0
// NewDummyDevice creates dummy device which outputs specified string/binary payload.
func NewDummyDevice(section inidef.ConfigSection, brokers []*broker.Broker, devChan chan message.Message) (DummyDevice, error) {
	ret := DummyDevice{
		Name:       section.Name,
		DeviceChan: devChan,
	}
	values := section.Values
	bname, ok := section.Values["broker"]
	if !ok {
		return ret, fmt.Errorf("broker does not set")
	}

	for _, b := range brokers {
		if b.Name == bname {
			ret.Broker = brokers
		}
	}
	if ret.Broker == nil {
		return ret, fmt.Errorf("broker does not exists: %s", bname)
	}
	ret.BrokerName = bname

	qos, err := strconv.Atoi(values["qos"])
	if err != nil {
		return ret, fmt.Errorf("qos parse failed, %v", err)
	}
	ret.QoS = byte(qos)

	interval, err := strconv.Atoi(values["interval"])
	if err != nil {
		return ret, err
	} else {
		ret.Interval = int(interval)
	}
	ret.Type = values["type"]
	ret.Payload, err = utils.ParsePayload(values["payload"])
	if err != nil {
		log.Warnf("invalid payload, but continue")
	}
	ret.Retain = false
	if values["retain"] == "true" {
		ret.Retain = true
	}

	sub, ok := values["subscribe"]
	if ok && sub == "true" {
		ret.Subscribe = true
	}

	// Validation
	if err := ret.Validate(); err != nil {
		return ret, err
	}
	return ret, nil
}
Exemple #2
0
// NewBrokers returns []*Broker from inidef.Config.
// If validation failes, retrun error.
func NewBrokers(conf inidef.Config, gwChan chan message.Message) (Brokers, error) {
	var brokers Brokers

	for _, section := range conf.Sections {
		if section.Type != "broker" {
			continue
		}
		values := section.Values

		willMsg, err := utils.ParsePayload(values["will_message"])
		if err != nil {
			log.Warnf("will_message, %v", err)
		}
		broker := &Broker{
			GatewayName:   conf.GatewayName,
			Name:          section.Name,
			Host:          values["host"],
			Username:      values["username"],
			Password:      values["password"],
			TopicPrefix:   values["topic_prefix"],
			WillMessage:   willMsg,
			Tls:           false,
			CaCert:        "",
			RetryInterval: int(0),
			Subscribed:    NewSubscribed(),
			GwChan:        gwChan,
		}
		priority := 1
		if section.Arg != "" {
			priority, err = strconv.Atoi(section.Arg)
			if err != nil {
				return nil, fmt.Errorf("broker priority parse failed, %v", section.Arg)
			}
		}
		broker.Priority = int(priority)

		port, err := strconv.Atoi(values["port"])
		if err != nil {
			return nil, fmt.Errorf("broker port parse failed, %v", values["port"])
		}
		broker.Port = int(port)

		// OPTIONAL fields
		if values["retry_interval"] != "" {
			retry_interval, err := strconv.Atoi(values["retry_interval"])
			if err != nil {
				return nil, err
			} else {
				broker.RetryInterval = int(retry_interval)
			}
		}

		if values["tls"] == "true" && values["cacert"] != "" {
			// validate certificate
			broker.Tls = true
			broker.CaCert = values["cacert"]
			broker.TLSConfig, err = NewTLSConfig(broker.CaCert)
			if err != nil {
				return nil, err
			}
		}

		// Validation
		if err := validator.Validate(broker); err != nil {
			return brokers, err
		}
		brokers = append(brokers, broker)
	}

	// sort by Priority
	sort.Sort(brokers)

	return brokers, nil
}