diff --git a/internal/wrphandlers/mocktr181/handler.go b/internal/wrphandlers/mocktr181/handler.go index b05cb56..4a576d7 100644 --- a/internal/wrphandlers/mocktr181/handler.go +++ b/internal/wrphandlers/mocktr181/handler.go @@ -60,6 +60,7 @@ type Tr181Payload struct { Command string `json:"command"` Names []string `json:"names"` Parameters []Parameter `json:"parameters"` + StatusCode int `json:"statusCode"` } type Parameters struct { @@ -127,13 +128,13 @@ func (h Handler) HandleWrp(msg wrp.Message) error { switch command { case "GET": - statusCode, payloadResponse, err = h.get(payload.Names) + statusCode, payloadResponse, err = h.get(payload) if err != nil { return err } case "SET": - statusCode, payloadResponse, err = h.set(payload.Parameters) + statusCode, payloadResponse, err = h.set(payload) if err != nil { return err } @@ -156,11 +157,14 @@ func (h Handler) HandleWrp(msg wrp.Message) error { return err } -func (h Handler) get(names []string) (int64, []byte, error) { - result := Tr181Payload{} - statusCode := int64(http.StatusOK) +func (h Handler) get(tr181 *Tr181Payload) (int64, []byte, error) { + result := Tr181Payload{ + Command: tr181.Command, + Names: tr181.Names, + StatusCode: http.StatusOK, + } - for _, name := range names { + for _, name := range tr181.Names { for _, mockParameter := range h.parameters { if !strings.HasPrefix(mockParameter.Name, name) { continue @@ -173,32 +177,36 @@ func (h Handler) get(names []string) (int64, []byte, error) { Value: mockParameter.Value, DataType: mockParameter.DataType, Attributes: mockParameter.Attributes, + Message: "Success", }) default: result.Parameters = append(result.Parameters, Parameter{ Message: fmt.Sprintf("Invalid parameter name: %s", mockParameter.Name), }) - statusCode = 520 + result.StatusCode = 520 } } } + if len(result.Parameters) == 0 { + result.StatusCode = 520 + } + payload, err := json.Marshal(result) if err != nil { return http.StatusInternalServerError, payload, errors.Join(ErrInvalidResponsePayload, err) } - if len(result.Parameters) == 0 { - statusCode = int64(520) - } - - return statusCode, payload, nil + return int64(result.StatusCode), payload, nil } -func (h Handler) set(parameters []Parameter) (int64, []byte, error) { - statusCode := http.StatusAccepted - result := Tr181Payload{} - for _, parameter := range parameters { +func (h Handler) set(tr181 *Tr181Payload) (int64, []byte, error) { + result := Tr181Payload{ + Command: tr181.Command, + Names: tr181.Names, + StatusCode: http.StatusOK, + } + for _, parameter := range tr181.Parameters { for i := range h.parameters { mockParameter := &h.parameters[i] if mockParameter.Name != parameter.Name { @@ -210,19 +218,19 @@ func (h Handler) set(parameters []Parameter) (int64, []byte, error) { mockParameter.Value = parameter.Value mockParameter.DataType = parameter.DataType mockParameter.Attributes = parameter.Attributes - result.Parameters = append(result.Parameters, Parameter{ Name: mockParameter.Name, Value: mockParameter.Value, DataType: mockParameter.DataType, Attributes: mockParameter.Attributes, + Message: "Success", }) default: result.Parameters = append(result.Parameters, Parameter{ Name: mockParameter.Name, Message: "Parameter is not writable", }) - statusCode = 520 + result.StatusCode = 520 } } } @@ -232,7 +240,7 @@ func (h Handler) set(parameters []Parameter) (int64, []byte, error) { return http.StatusInternalServerError, payload, errors.Join(ErrInvalidResponsePayload, err) } - return int64(statusCode), payload, nil + return int64(result.StatusCode), payload, nil } func (h Handler) loadFile() ([]MockParameter, error) {