Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule Longpoll.AsyncHelper do
- @port Application.get_env(:gis, Gis.Longpoll.Worker)[:port]
- @location Application.get_env(:gis, Gis.Longpoll.Worker)[:location]
- @user Application.get_env(:gis, Gis.Longpoll.Worker)[:user]
- @pass Application.get_env(:gis, Gis.Longpoll.Worker)[:pass]
- @clientip Application.get_env(:gis, Gis.Longpoll.Worker)[:clientip]
- @sessionkey Application.get_env(:gis, Gis.Longpoll.Worker)[:sessionkey]
- @request_username Application.get_env(:gis, Gis.Longpoll.Worker)[:request_username]
- def collect_response(id, par, data) do
- receive do
- %HTTPoison.AsyncStatus{id: ^id, code: 200} ->
- collect_response(id, par, data)
- %HTTPoison.AsyncHeaders{id: ^id, headers: headers} ->
- collect_response(id, par, data)
- %HTTPoison.AsyncChunk{id: ^id, chunk: chunk} ->
- collect_response(id, par, data <> chunk)
- %HTTPoison.AsyncEnd{id: ^id} ->
- send par, handle_response({:ok, %{status_code: 200, body: data}})
- end
- end
- @doc """
- Function for handle response from async connection and cast it to elixir maps
- """
- def handle_response({:ok, %{status_code: 200, body: data}}) do
- # IO.inspect("RESPONSE")
- # IO.inspect(data)
- devices = []
- {status, response} = Poison.decode(data)
- if status == :ok do
- cmd_response = Map.get(response, "CmdResponse")
- call_result = Map.get(cmd_response, "CallResult")
- if call_result == "X_OK" do
- sts = Map.get(cmd_response, "StsAggregate") |>
- Map.get("StsBatchCheckRight")
- rights = Map.get(sts, "Rights")
- devices = Map.get(sts, "Devices")
- end
- end
- # IO.inspect(length(devices))
- devices
- end
- @doc """
- Function fos asynchronous post for user Auth
- """
- @spec post_async(String.t, String.t) :: none
- def post_async(user_req, rights) do
- port = @port
- location = @location
- user = @user
- pass = @pass
- clientip = @clientip
- sessionkey = @sessionkey
- options = [hackney: [basic_auth: {user, pass}, pool: :rights_pool],
- connect_timeout: :infinity,
- recv_timeout: :infinity,
- timeout: :infinity,
- stream_to: self
- ]
- headers = [{"Content-Type", "application/x-www-form-urlencoded"},
- {"Connection", "Keep-Alive"}]
- data = %{"sessionkey" => sessionkey,
- "username" => @request_username,
- "clientip" => clientip,
- "jsondata" => nil}
- # Those information should be in GisObjectCacheAgent
- # devices_sliced = Enum.split(Longpoll.Dbworker.Dbreader.get_objects().gis_objects, 1000)
- command_json = "proxy.json"
- url = "http://#{location}:#{port}/"
- url_full = url <> command_json
- params = %{url: url_full, data: data, headers: headers, options: options, rights: rights}
- # pack it to another function
- # Generate devices for check right
- send_devices(user_req, params, 0)
- end
- #Function for generating cookie
- @spec gen_cookie(String.t) :: number
- defp gen_cookie(name) do
- name |>
- to_char_list |>
- Enum.map(fn x -> to_string x end) |>
- Enum.join("")|>
- String.to_integer()
- end
- #Function for getting and broadcasting authorization information from VMX server
- defp send_devices(user, params, nr) do
- devices_sliced = Longpoll.Agents.GisobjectCacheAgent.get_element(nr)
- rights = Map.get(params, :rights)
- if devices_sliced != nil do
- devices = for x <- devices_sliced, do: %{"DeviceIdentity" => %{"Id" => Map.get(x, "Id"), "@Type" => Map.get(x, "Type")}}
- rights_list = cond do
- is_list(rights) -> for x <- rights, do: %{"AccessRightType" => x}
- true -> [%{"AccessRightType" => rights}]
- end
- {:ok, cmd_bachcheckrights} = %{"Msg" => %{
- "CmdBatchCheckRight" => %{
- "TransactionNo" => Longpoll.Agents.TransactionAgent.new_number(),
- "User" => %{"Name" => user},
- "Cookie" => gen_cookie(user),
- "Rights" => rights_list,
- "Devices" => devices
- }
- }
- } |> Poison.encode
- data = Map.get(params, :data) |>
- Map.put(:jsondata, cmd_bachcheckrights) |>
- URI.encode_query
- %HTTPoison.AsyncResponse{id: id} = HTTPoison.post!(Map.get(params, :url), data,
- Map.get(params, :headers), Map.get(params, :options))
- devices_recv = collect_response(id, self, <<>>)
- # IO.inspect(devices_recv)
- Longpoll.Dbworker.Dbwriter.cache_rights(user, devices_recv, "ObjView")
- send_devices(user, params, nr + 1)
- else
- Gis.Endpoint.broadcast("polling:" <> user, "start", %{status: "get_ready_for_rumble"})
- IO.inspect("END")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement