diff --git a/apps/iot/src/endpoint/iot_jinzhi_endpoint.erl b/apps/iot/src/endpoint/iot_jinzhi_endpoint.erl index b187ef4..0f8fc4f 100644 --- a/apps/iot/src/endpoint/iot_jinzhi_endpoint.erl +++ b/apps/iot/src/endpoint/iot_jinzhi_endpoint.erl @@ -104,7 +104,7 @@ handle_event(info, fetch_next, _, State = #state{queue = Q, flight_num = FlightN case queue:out(Q) of {{value, EventData = #event_data{id = Id}}, Q1} -> lager:debug("[iot_http_endpoint] fetch_next success, event data is: ~p", [EventData]), - do_post(EventData, State), + catch do_post(EventData, State), TimerRef = erlang:start_timer(?RETRY_INTERVAL, self(), {repost_ticker, EventData}), {keep_state, State#state{timer_map = maps:put(Id, TimerRef, TimerMap), queue = Q1, flight_num = FlightNum + 1}}; @@ -125,7 +125,7 @@ handle_event(info, {ack, Id}, _, State = #state{timer_map = TimerMap, acc_num = %% 收到重发过期请求 handle_event(info, {timeout, _, {repost_ticker, EventData = #event_data{id = Id}}}, _, State = #state{timer_map = TimerMap}) -> lager:debug("[iot_jinzhi_endpoint] repost data: ~p", [EventData]), - do_post(EventData, State), + catch do_post(EventData, State), TimerRef = erlang:start_timer(?RETRY_INTERVAL, self(), {repost_ticker, EventData}), {keep_state, State#state{timer_map = maps:put(Id, TimerRef, TimerMap)}}; @@ -174,26 +174,26 @@ code_change(_OldVsn, StateName, State = #state{}, _Extra) -> %%%=================================================================== -spec do_post(EventData :: #event_data{}, State :: #state{}) -> no_return(). -do_post(#event_data{id = Id, location_code = LocationCode, event_type = EventType, params = Params}, +do_post(#event_data{id = Id, location_code = LocationCode, event_type = EventType, + params = Params = #{<<"event_code">> := EventCode, <<"description">> := Description, <<"datetime">> := Datetime, <<"attachments">> := Attachments0}}, #state{pri_key = PriKey, url = Url, logger_pid = LoggerPid}) -> - lager:debug("[iot_jinzhi_endpoint] do_post, event_type: ~p, params: ~p, location_code: ~p", [EventType, Params, LocationCode]), - <> = LocationCode, + + % <<"occurrenceTime">> => <<"2023-06-10 12:00:00">>, + + Attachments = lists:map(fun(#{<<"filename">> := Filename, <<"name">> := Name}) -> + {ok, FileUrl} = iot_util:file_uri(Filename), + #{<<"name">> => Name, <<"url">> => FileUrl} + end, Attachments0), + + <> = LocationCode, DeviceInfo = #{ <<"location">> => Loc, - <<"category">> => Category, - <<"description">> => <<"校门口花坛损坏"/utf8>>, - <<"occurrenceTime">> => <<"2023-06-10 12:00:00">>, - <<"attachments">> => [ - #{ - <<"name">> => <<"损坏图片.jpg"/utf8>>, - <<"url">> => <<"http://www.baidu.com">> - }, - #{ - <<"name">> => <<"损坏图片.jpg"/utf8>>, - <<"url">> => <<"http://www.baidu.com">> - } - ] + <<"category">> => EventCode, + <<"device_location">> => LocationCode, + <<"description">> => Description, + <<"occurrenceTime">> => Datetime, + <<"attachments">> => Attachments }, ReqData = #{ @@ -203,6 +203,8 @@ do_post(#event_data{id = Id, location_code = LocationCode, event_type = EventTyp }, Body = iolist_to_binary(jiffy:encode(ReqData, [force_utf8])), + lager:debug("[iot_jinzhi_endpoint] do_post url: ~p, event_type: ~p, params: ~p, location_code: ~p, body: ~p", [Url, EventType, Params, LocationCode, Body]), + ReceiverPid = self(), %% 异步提交任务 spawn_monitor(fun() -> diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 646a48d..a7fa6a4 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -410,25 +410,18 @@ handle_event(cast, {handle, {ai_event, Event0}}, ?STATE_ACTIVATED, State = #stat EventText = iot_cipher_aes:decrypt(AES, Event0), lager:debug("[iot_host] uuid: ~p, get ai_event: ~p", [UUID, EventText]), case catch jiffy:decode(EventText, [return_maps]) of - #{<<"event_type">> := EventType, <<"params">> := Params = #{<<"device_uuid">> := DeviceUUID, <<"filename">> := Filename}} -> + #{<<"event_type">> := EventType, <<"params">> := Params = #{<<"device_uuid">> := DeviceUUID}} -> case iot_device:is_alive(DeviceUUID) of error -> + lager:notice("[iot_host] uuid: ~p, device_uuid: ~p is not alive, get ai_event: ~p", [UUID, EventText]), ok; {ok, DevicePid} -> - case iot_util:file_uri(Filename) of - {ok, FileUri} -> - Params1 = maps:put(<<"url">>, FileUri, Params), - %% 保存数据到mysql - Message = iolist_to_binary(jiffy:encode(Params1, [force_utf8])), - ai_event_logs_bo:insert(UUID, DeviceUUID, EventType, Message), + %% 保存数据到mysql + Message = iolist_to_binary(jiffy:encode(Params, [force_utf8])), + ai_event_logs_bo:insert(UUID, DeviceUUID, EventType, Message), + iot_device:change_status(DevicePid, ?DEVICE_ONLINE), - iot_device:change_status(DevicePid, ?DEVICE_ONLINE); - - %% 路由数据 - %iot_ai_router:route_uuid(DeviceUUID, EventType, Params); - error -> - lager:warning("[iot_host] host: ~p, event: ~p, filename: ~p invalid or device is not activated", [UUID, EventType, Filename]) - end + iot_ai_router:route_uuid(DeviceUUID, EventType, Params) end; Event when is_map(Event) -> lager:warning("[iot_host] host: ~p, event: ~p, not supported", [UUID, Event]); diff --git a/apps/iot/src/iot_util.erl b/apps/iot/src/iot_util.erl index 669cea6..2b3404b 100644 --- a/apps/iot/src/iot_util.erl +++ b/apps/iot/src/iot_util.erl @@ -144,8 +144,7 @@ assert(false, Msg) -> file_uri(Filename) when is_binary(Filename) -> case binary:split(Filename, <<"-">>, [global]) of [Year, Month, Day | _] -> - {ok, <<"/upload/", Year/binary, $/, Month/binary, $/, Day/binary, $/, Filename/binary>>}; + {ok, <<"https://lgsiot.njau.edu.cn/upload/", Year/binary, $/, Month/binary, $/, Day/binary, $/, Filename/binary>>}; _ -> error - end. - + end. \ No newline at end of file diff --git a/docs/websocket.md b/docs/websocket.md index 8054dc9..80cbc55 100644 --- a/docs/websocket.md +++ b/docs/websocket.md @@ -191,4 +191,38 @@ Body: 事件内容,AES加密 } } -``` \ No newline at end of file +``` + +## 主机上传AI事件 + +<<0x01, PacketId:4, 0x08, Body:任意长度>> + +PacketId: 4字节整数, 值为0; +Body: 事件内容,AES加密 + +```text + +设备的离在线状态 + +{ + "event_type": 1, // 事件类型 + "params": { + "device_uuid": "", + "description": "垃圾溢满", + "datetime": "2023-06-10 12:00:00", + "event_code": "事件编码,采集项下发的事件编码: 5位事件编码", //1. 异物占道(异物识别) 20405 2.垃圾溢满 20453 + "attachments": [ + { + "name": "垃圾溢满", + "filename": "2023-12-10-xyz.hdc" + }, + { + "name": "垃圾溢满", + "filename": "2023-12-10-xyz.hdc" + } + ] + } +} + +``` +