// Code generated by go-swagger; DO NOT EDIT.

package models

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

import (
	"context"

	"github.com/go-openapi/errors"
	"github.com/go-openapi/strfmt"
	"github.com/go-openapi/swag"
	"github.com/go-openapi/validate"
)

// APIDeviceProfileTemplate api device profile template
//
// swagger:model apiDeviceProfileTemplate
type APIDeviceProfileTemplate struct {

	// RX1 delay (for ABP).
	AbpRx1Delay int64 `json:"abpRx1Delay,omitempty"`

	// RX1 DR offset (for ABP).
	AbpRx1DrOffset int64 `json:"abpRx1DrOffset,omitempty"`

	// RX2 DR (for ABP).
	AbpRx2Dr int64 `json:"abpRx2Dr,omitempty"`

	// RX2 frequency (for ABP, Hz).
	AbpRx2Freq int64 `json:"abpRx2Freq,omitempty"`

	// ADR algorithm ID.
	AdrAlgorithmID string `json:"adrAlgorithmId,omitempty"`

	// Auto-detect measurements.
	// If set to true, measurements will be automatically added based on the
	// keys of the decoded payload. In cases where the decoded payload contains
	// random keys in the data, you want to set this to false.
	AutoDetectMeasurements bool `json:"autoDetectMeasurements,omitempty"`

	// Class-B ping-slot DR.
	ClassBPingSlotDr int64 `json:"classBPingSlotDr,omitempty"`

	// Class-B ping-slot freq (Hz).
	ClassBPingSlotFreq int64 `json:"classBPingSlotFreq,omitempty"`

	// Class-B ping-slot periodicity (only for Class-B).
	// Valid options are: 0 - 7.
	//
	// Number of ping-slots per beacon-period:
	// pingNb = 2^(7-periodicity)
	//
	// Periodicity: 0 = 128 ping-slots per beacon period = ~ every 1 sec
	// Periodicity: 7 = 1 ping-slot per beacon period = ~ every 128 sec
	ClassBPingSlotPeriodicity int64 `json:"classBPingSlotPeriodicity,omitempty"`

	// Class-B timeout (seconds).
	// This is the maximum time ChirpStack will wait to receive an acknowledgement from the device (if requested).
	ClassBTimeout int64 `json:"classBTimeout,omitempty"`

	// Class-C timeout (seconds).
	// This is the maximum time ChirpStack will wait to receive an acknowledgement from the device (if requested).
	ClassCTimeout int64 `json:"classCTimeout,omitempty"`

	// Description.
	Description string `json:"description,omitempty"`

	// Device-status request interval (times / day).
	// This defines the times per day that ChirpStack will request the device-status
	// from the device.
	DeviceStatusReqInterval int64 `json:"deviceStatusReqInterval,omitempty"`

	// Firmware.
	Firmware string `json:"firmware,omitempty"`

	// Flush queue on device activation.
	FlushQueueOnActivate bool `json:"flushQueueOnActivate,omitempty"`

	// Device-profile template ID.
	ID string `json:"id,omitempty"`

	// LoRaWAN mac-version.
	MacVersion *CommonMacVersion `json:"macVersion,omitempty"`

	// Measurements.
	// If defined, ChirpStack will visualize these metrics in the web-interface.
	Measurements map[string]APIMeasurement `json:"measurements,omitempty"`

	// Name.
	Name string `json:"name,omitempty"`

	// Payload codec runtime.
	PayloadCodecRuntime *APICodecRuntime `json:"payloadCodecRuntime,omitempty"`

	// Payload codec script.
	PayloadCodecScript string `json:"payloadCodecScript,omitempty"`

	// Regional parameters revision.
	RegParamsRevision *CommonRegParamsRevision `json:"regParamsRevision,omitempty"`

	// Region.
	Region *CommonRegion `json:"region,omitempty"`

	// Supports Class B.
	SupportsClassB bool `json:"supportsClassB,omitempty"`

	// Supports Class-C.
	SupportsClassC bool `json:"supportsClassC,omitempty"`

	// Supports OTAA.
	SupportsOtaa bool `json:"supportsOtaa,omitempty"`

	// User defined tags.
	Tags map[string]string `json:"tags,omitempty"`

	// Uplink interval (seconds).
	// This defines the expected uplink interval which the device uses for
	// communication. When the uplink interval has expired and no uplink has
	// been received, the device is considered inactive.
	UplinkInterval int64 `json:"uplinkInterval,omitempty"`

	// Vendor.
	Vendor string `json:"vendor,omitempty"`
}

// Validate validates this api device profile template
func (m *APIDeviceProfileTemplate) Validate(formats strfmt.Registry) error {
	var res []error

	if err := m.validateMacVersion(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateMeasurements(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validatePayloadCodecRuntime(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateRegParamsRevision(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateRegion(formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (m *APIDeviceProfileTemplate) validateMacVersion(formats strfmt.Registry) error {
	if swag.IsZero(m.MacVersion) { // not required
		return nil
	}

	if m.MacVersion != nil {
		if err := m.MacVersion.Validate(formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("macVersion")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("macVersion")
			}
			return err
		}
	}

	return nil
}

func (m *APIDeviceProfileTemplate) validateMeasurements(formats strfmt.Registry) error {
	if swag.IsZero(m.Measurements) { // not required
		return nil
	}

	for k := range m.Measurements {

		if err := validate.Required("measurements"+"."+k, "body", m.Measurements[k]); err != nil {
			return err
		}
		if val, ok := m.Measurements[k]; ok {
			if err := val.Validate(formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("measurements" + "." + k)
				} else if ce, ok := err.(*errors.CompositeError); ok {
					return ce.ValidateName("measurements" + "." + k)
				}
				return err
			}
		}

	}

	return nil
}

func (m *APIDeviceProfileTemplate) validatePayloadCodecRuntime(formats strfmt.Registry) error {
	if swag.IsZero(m.PayloadCodecRuntime) { // not required
		return nil
	}

	if m.PayloadCodecRuntime != nil {
		if err := m.PayloadCodecRuntime.Validate(formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("payloadCodecRuntime")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("payloadCodecRuntime")
			}
			return err
		}
	}

	return nil
}

func (m *APIDeviceProfileTemplate) validateRegParamsRevision(formats strfmt.Registry) error {
	if swag.IsZero(m.RegParamsRevision) { // not required
		return nil
	}

	if m.RegParamsRevision != nil {
		if err := m.RegParamsRevision.Validate(formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("regParamsRevision")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("regParamsRevision")
			}
			return err
		}
	}

	return nil
}

func (m *APIDeviceProfileTemplate) validateRegion(formats strfmt.Registry) error {
	if swag.IsZero(m.Region) { // not required
		return nil
	}

	if m.Region != nil {
		if err := m.Region.Validate(formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("region")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("region")
			}
			return err
		}
	}

	return nil
}

// ContextValidate validate this api device profile template based on the context it is used
func (m *APIDeviceProfileTemplate) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	var res []error

	if err := m.contextValidateMacVersion(ctx, formats); err != nil {
		res = append(res, err)
	}

	if err := m.contextValidateMeasurements(ctx, formats); err != nil {
		res = append(res, err)
	}

	if err := m.contextValidatePayloadCodecRuntime(ctx, formats); err != nil {
		res = append(res, err)
	}

	if err := m.contextValidateRegParamsRevision(ctx, formats); err != nil {
		res = append(res, err)
	}

	if err := m.contextValidateRegion(ctx, formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (m *APIDeviceProfileTemplate) contextValidateMacVersion(ctx context.Context, formats strfmt.Registry) error {

	if m.MacVersion != nil {

		if swag.IsZero(m.MacVersion) { // not required
			return nil
		}

		if err := m.MacVersion.ContextValidate(ctx, formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("macVersion")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("macVersion")
			}
			return err
		}
	}

	return nil
}

func (m *APIDeviceProfileTemplate) contextValidateMeasurements(ctx context.Context, formats strfmt.Registry) error {

	for k := range m.Measurements {

		if val, ok := m.Measurements[k]; ok {
			if err := val.ContextValidate(ctx, formats); err != nil {
				return err
			}
		}

	}

	return nil
}

func (m *APIDeviceProfileTemplate) contextValidatePayloadCodecRuntime(ctx context.Context, formats strfmt.Registry) error {

	if m.PayloadCodecRuntime != nil {

		if swag.IsZero(m.PayloadCodecRuntime) { // not required
			return nil
		}

		if err := m.PayloadCodecRuntime.ContextValidate(ctx, formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("payloadCodecRuntime")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("payloadCodecRuntime")
			}
			return err
		}
	}

	return nil
}

func (m *APIDeviceProfileTemplate) contextValidateRegParamsRevision(ctx context.Context, formats strfmt.Registry) error {

	if m.RegParamsRevision != nil {

		if swag.IsZero(m.RegParamsRevision) { // not required
			return nil
		}

		if err := m.RegParamsRevision.ContextValidate(ctx, formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("regParamsRevision")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("regParamsRevision")
			}
			return err
		}
	}

	return nil
}

func (m *APIDeviceProfileTemplate) contextValidateRegion(ctx context.Context, formats strfmt.Registry) error {

	if m.Region != nil {

		if swag.IsZero(m.Region) { // not required
			return nil
		}

		if err := m.Region.ContextValidate(ctx, formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("region")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("region")
			}
			return err
		}
	}

	return nil
}

// MarshalBinary interface implementation
func (m *APIDeviceProfileTemplate) MarshalBinary() ([]byte, error) {
	if m == nil {
		return nil, nil
	}
	return swag.WriteJSON(m)
}

// UnmarshalBinary interface implementation
func (m *APIDeviceProfileTemplate) UnmarshalBinary(b []byte) error {
	var res APIDeviceProfileTemplate
	if err := swag.ReadJSON(b, &res); err != nil {
		return err
	}
	*m = res
	return nil
}
