fix jsonrpc

This commit is contained in:
anlicheng 2025-09-26 15:29:54 +08:00
parent ef7e2caf4f
commit 09ae0bc8c0
2 changed files with 11 additions and 13 deletions

View File

@ -64,13 +64,11 @@
}). }).
-record(jsonrpc_request, { -record(jsonrpc_request, {
id = 0 :: integer(),
method :: binary(), method :: binary(),
params = <<>> :: any() params = <<>> :: any()
}). }).
-record(jsonrpc_reply, { -record(jsonrpc_reply, {
id :: integer(),
result :: any() | undefined, result :: any() | undefined,
error :: any() | undefined error :: any() | undefined
}). }).

View File

@ -33,11 +33,11 @@ encode0(#auth_reply{code = Code, payload = Payload}) ->
marshal(?I32, Code), marshal(?I32, Code),
marshal(?Bytes, Payload) marshal(?Bytes, Payload)
]); ]);
encode0(#jsonrpc_reply{id = Id, result = Result, error = undefined}) -> encode0(#jsonrpc_reply{result = Result, error = undefined}) ->
ResultBin = jiffy:encode(#{<<"id">> => Id, <<"result">> => Result}, [force_utf8]), ResultBin = jiffy:encode(#{<<"result">> => Result}, [force_utf8]),
iolist_to_binary([marshal(?Bytes, ResultBin)]); iolist_to_binary([marshal(?Bytes, ResultBin)]);
encode0(#jsonrpc_reply{id = Id, result = undefined, error = Error}) -> encode0(#jsonrpc_reply{result = undefined, error = Error}) ->
ResultBin = jiffy:encode(#{<<"id">> => Id, <<"error">> => Error}, [force_utf8]), ResultBin = jiffy:encode(#{<<"error">> => Error}, [force_utf8]),
iolist_to_binary([marshal(?Bytes, ResultBin)]); iolist_to_binary([marshal(?Bytes, ResultBin)]);
encode0(#pub{topic = Topic, content = Content}) -> encode0(#pub{topic = Topic, content = Content}) ->
iolist_to_binary([ iolist_to_binary([
@ -50,8 +50,8 @@ encode0(#command{command_type = CommandType, command = Command}) ->
marshal(?Bytes, Command) marshal(?Bytes, Command)
]); ]);
encode0(#jsonrpc_request{id = Id, method = Method, params = Params}) -> encode0(#jsonrpc_request{method = Method, params = Params}) ->
ReqBody = jiffy:encode(#{<<"id">> => Id, <<"method">> => Method, <<"params">> => Params}, [force_utf8]), ReqBody = jiffy:encode(#{<<"method">> => Method, <<"params">> => Params}, [force_utf8]),
marshal(?Bytes, ReqBody); marshal(?Bytes, ReqBody);
encode0(#data{service_id = ServiceId, device_uuid = DeviceUUID, route_key = RouteKey, metric = Metric}) -> encode0(#data{service_id = ServiceId, device_uuid = DeviceUUID, route_key = RouteKey, metric = Metric}) ->
iolist_to_binary([ iolist_to_binary([
@ -85,10 +85,10 @@ decode0(?MESSAGE_AUTH_REQUEST, [UUID, Username, Salt, Token, Timestamp]) ->
{ok, #auth_request{uuid = UUID, username = Username, salt = Salt, token = Token, timestamp = Timestamp}}; {ok, #auth_request{uuid = UUID, username = Username, salt = Salt, token = Token, timestamp = Timestamp}};
decode0(?MESSAGE_JSONRPC_REPLY, [ReplyBin]) -> decode0(?MESSAGE_JSONRPC_REPLY, [ReplyBin]) ->
case jiffy:decode(ReplyBin, [return_maps]) of case jiffy:decode(ReplyBin, [return_maps]) of
#{<<"id">> := Id, <<"result">> := Result} -> #{<<"result">> := Result} ->
{ok, #jsonrpc_reply{id = Id, result = Result}}; {ok, #jsonrpc_reply{result = Result}};
#{<<"id">> := Id, <<"error">> := Error} -> #{<<"id">> := Id, <<"error">> := Error} ->
{ok, #jsonrpc_reply{id = Id, error = Error}}; {ok, #jsonrpc_reply{error = Error}};
_ -> _ ->
error error
end; end;
@ -99,8 +99,8 @@ decode0(?MESSAGE_COMMAND, [CommandType, Command]) ->
decode0(?MESSAGE_AUTH_REPLY, [Code, Payload]) -> decode0(?MESSAGE_AUTH_REPLY, [Code, Payload]) ->
{ok, #auth_reply{code = Code, payload = Payload}}; {ok, #auth_reply{code = Code, payload = Payload}};
decode0(?MESSAGE_JSONRPC_REQUEST, [ReqBody]) -> decode0(?MESSAGE_JSONRPC_REQUEST, [ReqBody]) ->
#{<<"id">> := Id, <<"method">> := Method, <<"params">> := Params} = jiffy:decode(ReqBody, [return_maps]), #{<<"method">> := Method, <<"params">> := Params} = jiffy:decode(ReqBody, [return_maps]),
{ok, #jsonrpc_request{id = Id, method = Method, params = Params}}; {ok, #jsonrpc_request{method = Method, params = Params}};
decode0(?MESSAGE_DATA, [ServiceId, DeviceUUID, RouteKey, Metric]) -> decode0(?MESSAGE_DATA, [ServiceId, DeviceUUID, RouteKey, Metric]) ->
{ok, #data{service_id = ServiceId, device_uuid = DeviceUUID, route_key = RouteKey, metric = Metric}}; {ok, #data{service_id = ServiceId, device_uuid = DeviceUUID, route_key = RouteKey, metric = Metric}};
decode0(?MESSAGE_EVENT, [ServiceId, EventType, Params]) -> decode0(?MESSAGE_EVENT, [ServiceId, EventType, Params]) ->