Example #1
0
func (device *DummyDevice) Validate() error {
	validator := validator.NewValidator()
	validator.SetValidationFunc("validtopic", inidef.ValidMqttPublishTopic)
	if err := validator.Validate(device); err != nil {
		return err
	}
	return nil
}
Example #2
0
// Validate validates Topic is valid for MQTT or not.
func (topic TopicString) Validate() error {
	if err := validator.Validate(&topic); err != nil {
		return err
	}

	if !utf8.ValidString(topic.Str) {
		return errors.New("not a valid UTF8 string")
	}

	if inidef.ReU0.FindString(topic.Str) != "" {
		return errors.New("topic should NOT include \\U0000 character")
	}

	if inidef.ReWild.FindString(topic.Str) != "" {
		return errors.New("should not MQTT pub-topic include wildard character")
	}
	return nil
}
Example #3
0
// Validate validates Topic is valid for MQTT or not.
func (topic TopicString) Validate() error {
	if err := validator.Validate(&topic); err != nil {
		return err
	}

	if !utf8.ValidString(topic.Str) {
		return errors.New("not a valid UTF8 string")
	}
	reu0 := regexp.MustCompile("\u0000")
	if reu0.FindString(topic.Str) != "" {
		return errors.New("topic should NOT include \\U0000 character")
	}
	rewild := regexp.MustCompile("[+#]+")
	if rewild.FindString(topic.Str) != "" {
		return errors.New("should not MQTT pub-topic include wildard character")
	}
	return nil
}
Example #4
0
func (gw *Gateway) Validate() error {
	return validator.Validate(gw)
}
Example #5
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
}
Example #6
0
func (u UserId) isValid() error {
	return validator.Validate(u)
}