-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
patch: simplify mocktr181 HandleWrp and handle empty payload case #215
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,53 +115,52 @@ | |
|
||
// HandleWrp is called to process a tr181 command | ||
func (h Handler) HandleWrp(msg wrp.Message) error { | ||
payload := new(Tr181Payload) | ||
|
||
err := json.Unmarshal(msg.Payload, &payload) | ||
statusCode, payloadResponse, err := h.proccessCommand(msg.Payload) | ||
if err != nil { | ||
return errors.Join(err, wrpkit.ErrNotHandled) | ||
} | ||
|
||
var payloadResponse []byte | ||
var statusCode int64 | ||
|
||
command := payload.Command | ||
|
||
switch command { | ||
case "GET": | ||
statusCode, payloadResponse, err = h.get(payload) | ||
if err != nil { | ||
return errors.Join(err, wrpkit.ErrNotHandled) | ||
} | ||
|
||
case "SET": | ||
statusCode, payloadResponse, err = h.set(payload) | ||
if err != nil { | ||
return errors.Join(err, wrpkit.ErrNotHandled) | ||
} | ||
|
||
default: | ||
// currently only get and set are implemented for existing mocktr181 | ||
statusCode = 520 | ||
payloadResponse = []byte(fmt.Sprintf(`{"message": "command '%s' is not supported", "statusCode": %d}`, command, statusCode)) | ||
} | ||
|
||
response := msg | ||
response.Destination = msg.Source | ||
response.Source = h.source | ||
response.ContentType = "application/json" | ||
response.Payload = payloadResponse | ||
|
||
response.Status = &statusCode | ||
|
||
err = h.egress.HandleWrp(response) | ||
if err != nil { | ||
if err = h.egress.HandleWrp(response); err != nil { | ||
return errors.Join(err, wrpkit.ErrNotHandled) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (h Handler) proccessCommand(wrpPayload []byte) (int64, []byte, error) { | ||
var ( | ||
err error | ||
payloadResponse []byte | ||
statusCode = int64(520) | ||
) | ||
|
||
if len(wrpPayload) == 0 { | ||
return statusCode, []byte(fmt.Sprintf(`{"message": ""Invalid Input Command"", "statusCode": %d}`, statusCode)), nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we say empty so we know the payload is empty? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we would but that's the expected behavior of the old c simulator. we're trying to match the old c simulator as close as possible |
||
} | ||
|
||
payload := new(Tr181Payload) | ||
err = json.Unmarshal(wrpPayload, &payload) | ||
if err != nil { | ||
return statusCode, payloadResponse, err | ||
} | ||
|
||
switch payload.Command { | ||
case "GET": | ||
return h.get(payload) | ||
case "SET": | ||
return h.set(payload) | ||
default: | ||
// currently only get and set are implemented for existing mocktr181 | ||
return statusCode, []byte(fmt.Sprintf(`{"message": "command '%s' is not supported", "statusCode": %d}`, payload.Command, statusCode)), nil | ||
} | ||
} | ||
|
||
func (h Handler) get(tr181 *Tr181Payload) (int64, []byte, error) { | ||
result := Tr181Payload{ | ||
Command: tr181.Command, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
support empty payload case (520 status code)