diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 9f5d333..1a44851 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -260,7 +260,10 @@ handle_event(cast, {handle, {data, Data}}, session, State = #state{uuid = UUID, NTags = with_device_uuid(Tags#{<<"uuid">> => UUID, <<"service_name">> => ServiceName}, Info), %% 按照设备的uuid进行分组 - Points = lists:map(fun(Fields) -> influx_point:new(RouterUUID, NTags, Fields, Timestamp) end, FieldsList), + Points = lists:map(fun(Fields) -> + NFields = convert_fields(Fields), + influx_point:new(RouterUUID, NTags, NFields, Timestamp) + end, FieldsList), Precision = influx_client:get_precision(Timestamp), poolboy:transaction(influx_pool, fun(Pid) -> influx_client:write(Pid, <<"metric">>, <<"nannong">>, Precision, Points) end); @@ -378,6 +381,11 @@ code_change(_OldVsn, StateName, State = #state{}, _Extra) -> %%% Internal functions %%%=================================================================== +convert_fields(#{<<"key">> := Key, <<"value">> := Value, <<"unit">> := Unit}) -> + #{Key => jiffy:encode(#{<<"value">> => Value, <<"unit">> := Unit}, [force_utf8])}; +convert_fields(#{<<"key">> := Key, <<"value">> := Value}) -> + #{Key => Value}. + %% 获取到分发的路由 router_uuid(#{<<"device_uuid">> := DeviceUUID}, _) when is_binary(DeviceUUID), DeviceUUID /= <<>> -> DeviceUUID; @@ -389,3 +397,5 @@ with_device_uuid(Tags, #{<<"device_uuid">> := DeviceUUID}) when DeviceUUID /= << Tags#{<<"device_uuid">> => DeviceUUID}; with_device_uuid(Tags, _) -> Tags. + + diff --git a/config/sys-dev.config b/config/sys-dev.config index a7d2235..ca70363 100644 --- a/config/sys-dev.config +++ b/config/sys-dev.config @@ -46,7 +46,7 @@ ] }, - %% influxdb数据库配置 + %% influxdb数据库配置, 测试环境的: 用户名: iot; password: password1234 {influx_pool, [{size, 100}, {max_overflow, 200}, {worker_module, influx_client}], [