Skip to content

Commit fbb0fa8

Browse files
committed
float数据类型入库处理
1 parent 12d9c3a commit fbb0fa8

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

rebar.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
%% Release profiles
2929
%% To create a release just run
3030
%% rebar3 as prod release
31-
{relx, [{release, {eadm, "0.1.42"},
31+
{relx, [{release, {eadm, "0.1.43"},
3232
[eadm, nova, epgsql, poolboy, lager, sasl, inets, observer, runtime_tools]},
3333
{mode, minimal},
3434
{extended_start_script, true},

src/apis/api_watch.erl

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ index(#{params := Params}) ->
129129
<<"12">> ->
130130
% 体温数据
131131
try
132-
BodyTemperature = maps:get(<<"bodyTemperature">>, Params, <<"0">>),
132+
BodyTemperature = bin_to_float(maps:get(<<"bodyTemperature">>, Params, <<"0">>)),
133133
BTUtcTime = eadm_utils:parse_date_time(maps:get(<<"BTUtcTime">>, Params, <<"1970/1/1 00:00:00">>)),
134134
eadm_pgpool:equery(pool_pg, "insert into lc_watchbt(ptime, bodytemperature)
135135
values($1, $2::real) on conflict (ptime)
@@ -142,8 +142,8 @@ index(#{params := Params}) ->
142142
<<"14">> ->
143143
% 体温数据
144144
try
145-
BodyTemperature = maps:get(<<"bodyTemperature">>, Params, <<"0">>),
146-
WristTemperature = maps:get(<<"wristTemperature">>, Params, <<"0">>),
145+
BodyTemperature = bin_to_float(maps:get(<<"bodyTemperature">>, Params, <<"0">>)),
146+
WristTemperature = bin_to_float(maps:get(<<"wristTemperature">>, Params, <<"0">>)),
147147
BTUtcTime = eadm_utils:parse_date_time(maps:get(<<"BTUtcTime">>, Params, <<"1970/1/1 00:00:00">>)),
148148
eadm_pgpool:equery(pool_pg, "insert into lc_watchbt(ptime, bodytemperature, wristtemperature)
149149
values($1, $2::real, $3::real) on conflict (ptime)
@@ -463,3 +463,33 @@ index(#{params := Params}) ->
463463
lager:info("编码未定义: ~p~n", [Params]),
464464
#{<<"success">> => false}
465465
end.
466+
467+
468+
%%====================================================================
469+
%% 内部函数
470+
%%====================================================================
471+
%% @doc
472+
%% 将二进制表示的数字转换为浮点数
473+
%% @end
474+
bin_to_float(Binary) ->
475+
case binary_to_list(Binary) of
476+
List when erlang:is_list(List) ->
477+
try
478+
Float = list_to_float(add_decimal_point(List)),
479+
Float
480+
catch
481+
error:badarg ->
482+
error({invalid_binary, Binary})
483+
end;
484+
_ ->
485+
error({invalid_binary, Binary})
486+
end.
487+
488+
%% @doc
489+
%% 添加小数点以确保有效的浮点数表示
490+
%% @end
491+
add_decimal_point(List) when erlang:is_list(List) ->
492+
case lists:last(List) of
493+
X when X >= $0, X =< $9 -> lists:append(List, ".0");
494+
_ -> List
495+
end.

src/eadm.app.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
22
{application, eadm,
33
[{description, "RG管理平台"},
4-
{vsn, "0.1.42"},
4+
{vsn, "0.1.43"},
55
{registered, []},
66
{mod, {eadm_app, []}},
77
{included_applications, []},

0 commit comments

Comments
 (0)