diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 560ba57..add9f7a 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -257,14 +257,10 @@ handle_event(cast, {handle, {data, Data}}, session, State = #state{uuid = UUID, end, %% 数据写入influxdb - NTags = case maps:is_key(<<"device_uuid">>, Info) of - true -> - Tags#{<<"uuid">> => UUID, <<"service_name">> => ServiceName, <<"device_uuid">> => maps:get(<<"device_uuid">>, Info)}; - false -> - Tags#{<<"uuid">> => UUID, <<"service_name">> => ServiceName} - end, - Measurement = <<"metric">>, - Points = lists:map(fun(Fields) -> influx_point:new(Measurement, NTags, Fields, Timestamp) end, FieldsList), + 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), Precision = influx_client:get_precision(Timestamp), poolboy:transaction(influx_pool, fun(Pid) -> influx_client:write(Pid, <<"iot">>, <<"iot">>, Precision, Points) end); @@ -387,3 +383,9 @@ router_uuid(#{<<"device_uuid">> := DeviceUUID}, _) when is_binary(DeviceUUID), D DeviceUUID; router_uuid(_, UUID) -> UUID. + +-spec with_device_uuid(Tags :: #{}, Info :: #{}) -> #{}. +with_device_uuid(Tags, #{<<"device_uuid">> := DeviceUUID}) when DeviceUUID /= <<>> -> + Tags#{<<"device_uuid">> => DeviceUUID}; +with_device_uuid(Tags, _) -> + Tags.