dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/requests/event.go (about)

     1  //
     2  // Copyright (C) 2020-2021 IOTech Ltd
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  
     6  package requests
     7  
     8  import (
     9  	"encoding/json"
    10  	"os"
    11  
    12  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
    13  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos"
    14  	dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common"
    15  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors"
    16  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models"
    17  
    18  	"github.com/fxamacker/cbor/v2"
    19  )
    20  
    21  // AddEventRequest defines the Request Content for POST event DTO.
    22  type AddEventRequest struct {
    23  	dtoCommon.BaseRequest `json:",inline"`
    24  	Event                 dtos.Event `json:"event" validate:"required"`
    25  }
    26  
    27  // NewAddEventRequest creates, initializes and returns an AddEventRequests
    28  func NewAddEventRequest(event dtos.Event) AddEventRequest {
    29  	return AddEventRequest{
    30  		BaseRequest: dtoCommon.NewBaseRequest(),
    31  		Event:       event,
    32  	}
    33  }
    34  
    35  // Validate satisfies the Validator interface
    36  func (a AddEventRequest) Validate() error {
    37  	if err := common.Validate(a); err != nil {
    38  		return err
    39  	}
    40  
    41  	// BaseReading has the skip("-") validation annotation for BinaryReading and SimpleReading
    42  	// Otherwise error will occur as only one of them exists
    43  	// Therefore, need to validate the nested BinaryReading and SimpleReading struct here
    44  	for _, r := range a.Event.Readings {
    45  		if err := r.Validate(); err != nil {
    46  			return err
    47  		}
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  type unmarshal func([]byte, interface{}) error
    54  
    55  func (a *AddEventRequest) UnmarshalJSON(b []byte) error {
    56  	return a.Unmarshal(b, json.Unmarshal)
    57  }
    58  
    59  func (a *AddEventRequest) UnmarshalCBOR(b []byte) error {
    60  	return a.Unmarshal(b, cbor.Unmarshal)
    61  }
    62  
    63  func (a *AddEventRequest) Unmarshal(b []byte, f unmarshal) error {
    64  	// To avoid recursively invoke unmarshaler interface, intentionally create a struct to represent AddEventRequest DTO
    65  	var addEvent struct {
    66  		dtoCommon.BaseRequest
    67  		Event dtos.Event
    68  	}
    69  	if err := f(b, &addEvent); err != nil {
    70  		return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal the byte array.", err)
    71  	}
    72  
    73  	*a = AddEventRequest(addEvent)
    74  
    75  	// validate AddEventRequest DTO
    76  	if err := a.Validate(); err != nil {
    77  		return err
    78  	}
    79  
    80  	// Normalize reading's value type
    81  	for i, r := range a.Event.Readings {
    82  		valueType, err := common.NormalizeValueType(r.ValueType)
    83  		if err != nil {
    84  			return errors.NewCommonEdgeXWrapper(err)
    85  		}
    86  		a.Event.Readings[i].ValueType = valueType
    87  	}
    88  	return nil
    89  }
    90  
    91  func (a *AddEventRequest) Encode() ([]byte, string, error) {
    92  	var encoding = common.ContentTypeJSON
    93  
    94  	for _, r := range a.Event.Readings {
    95  		if r.ValueType == common.ValueTypeBinary {
    96  			encoding = common.ContentTypeCBOR
    97  			break
    98  		}
    99  	}
   100  	if v := os.Getenv(common.EnvEncodeAllEvents); v == common.ValueTrue {
   101  		encoding = common.ContentTypeCBOR
   102  	}
   103  
   104  	var err error
   105  	var encodedData []byte
   106  	switch encoding {
   107  	case common.ContentTypeCBOR:
   108  		encodedData, err = cbor.Marshal(a)
   109  		if err != nil {
   110  			return nil, "", errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to encode AddEventRequest to CBOR", err)
   111  		}
   112  	case common.ContentTypeJSON:
   113  		encodedData, err = json.Marshal(a)
   114  		if err != nil {
   115  			return nil, "", errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to encode AddEventRequest to JSON", err)
   116  		}
   117  	}
   118  
   119  	return encodedData, encoding, nil
   120  }
   121  
   122  // AddEventReqToEventModel transforms the AddEventRequest DTO to the Event model
   123  func AddEventReqToEventModel(addEventReq AddEventRequest) (event models.Event) {
   124  	readings := make([]models.Reading, len(addEventReq.Event.Readings))
   125  	for i, r := range addEventReq.Event.Readings {
   126  		readings[i] = dtos.ToReadingModel(r)
   127  	}
   128  
   129  	tags := make(map[string]interface{})
   130  	for tag, value := range addEventReq.Event.Tags {
   131  		tags[tag] = value
   132  	}
   133  
   134  	return models.Event{
   135  		Id:          addEventReq.Event.Id,
   136  		DeviceName:  addEventReq.Event.DeviceName,
   137  		ProfileName: addEventReq.Event.ProfileName,
   138  		SourceName:  addEventReq.Event.SourceName,
   139  		Origin:      addEventReq.Event.Origin,
   140  		Readings:    readings,
   141  		Tags:        tags,
   142  	}
   143  }