dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/models/address.go (about)

     1  //
     2  // Copyright (C) 2021 IOTech Ltd
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  
     6  package models
     7  
     8  import (
     9  	"encoding/json"
    10  
    11  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
    12  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors"
    13  )
    14  
    15  type Address interface {
    16  	GetBaseAddress() BaseAddress
    17  }
    18  
    19  // instantiateAddress instantiate the interface to the corresponding address type
    20  func instantiateAddress(i interface{}) (address Address, err error) {
    21  	a, err := json.Marshal(i)
    22  	if err != nil {
    23  		return address, errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to marshal address.", err)
    24  	}
    25  	return unmarshalAddress(a)
    26  }
    27  
    28  func unmarshalAddress(b []byte) (address Address, err error) {
    29  	var alias struct {
    30  		Type string
    31  	}
    32  	if err = json.Unmarshal(b, &alias); err != nil {
    33  		return address, errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal address.", err)
    34  	}
    35  	switch alias.Type {
    36  	case common.REST:
    37  		var rest RESTAddress
    38  		if err = json.Unmarshal(b, &rest); err != nil {
    39  			return address, errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal REST address.", err)
    40  		}
    41  		address = rest
    42  	case common.MQTT:
    43  		var mqtt MQTTPubAddress
    44  		if err = json.Unmarshal(b, &mqtt); err != nil {
    45  			return address, errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal MQTT address.", err)
    46  		}
    47  		address = mqtt
    48  	case common.EMAIL:
    49  		var mail EmailAddress
    50  		if err = json.Unmarshal(b, &mail); err != nil {
    51  			return address, errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal Email address.", err)
    52  		}
    53  		address = mail
    54  	default:
    55  		return address, errors.NewCommonEdgeX(errors.KindContractInvalid, "Unsupported address type", err)
    56  	}
    57  	return address, nil
    58  }
    59  
    60  // BaseAddress is a base struct contains the common fields, such as type, host, port, and so on.
    61  type BaseAddress struct {
    62  	// Type is used to identify the Address type, i.e., REST or MQTT
    63  	Type string
    64  
    65  	// Common properties
    66  	Host string
    67  	Port int
    68  }
    69  
    70  // RESTAddress is a REST specific struct
    71  type RESTAddress struct {
    72  	BaseAddress
    73  	Path       string
    74  	HTTPMethod string
    75  }
    76  
    77  func (a RESTAddress) GetBaseAddress() BaseAddress { return a.BaseAddress }
    78  
    79  // MQTTPubAddress is a MQTT specific struct
    80  type MQTTPubAddress struct {
    81  	BaseAddress
    82  	Publisher      string
    83  	Topic          string
    84  	QoS            int
    85  	KeepAlive      int
    86  	Retained       bool
    87  	AutoReconnect  bool
    88  	ConnectTimeout int
    89  }
    90  
    91  func (a MQTTPubAddress) GetBaseAddress() BaseAddress { return a.BaseAddress }
    92  
    93  // EmailAddress is an Email specific struct
    94  type EmailAddress struct {
    95  	BaseAddress
    96  	Recipients []string
    97  }
    98  
    99  func (a EmailAddress) GetBaseAddress() BaseAddress { return a.BaseAddress }