Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. defmodule Longpoll.AsyncHelper do
  2.  
  3. @port Application.get_env(:gis, Gis.Longpoll.Worker)[:port]
  4. @location Application.get_env(:gis, Gis.Longpoll.Worker)[:location]
  5. @user Application.get_env(:gis, Gis.Longpoll.Worker)[:user]
  6. @pass Application.get_env(:gis, Gis.Longpoll.Worker)[:pass]
  7. @clientip Application.get_env(:gis, Gis.Longpoll.Worker)[:clientip]
  8. @sessionkey Application.get_env(:gis, Gis.Longpoll.Worker)[:sessionkey]
  9. @request_username Application.get_env(:gis, Gis.Longpoll.Worker)[:request_username]
  10.  
  11. def collect_response(id, par, data) do
  12. receive do
  13. %HTTPoison.AsyncStatus{id: ^id, code: 200} ->
  14. collect_response(id, par, data)
  15. %HTTPoison.AsyncHeaders{id: ^id, headers: headers} ->
  16. collect_response(id, par, data)
  17. %HTTPoison.AsyncChunk{id: ^id, chunk: chunk} ->
  18. collect_response(id, par, data <> chunk)
  19. %HTTPoison.AsyncEnd{id: ^id} ->
  20. send par, handle_response({:ok, %{status_code: 200, body: data}})
  21. end
  22. end
  23.  
  24. @doc """
  25. Function for handle response from async connection and cast it to elixir maps
  26. """
  27. def handle_response({:ok, %{status_code: 200, body: data}}) do
  28. # IO.inspect("RESPONSE")
  29. # IO.inspect(data)
  30. devices = []
  31. {status, response} = Poison.decode(data)
  32. if status == :ok do
  33. cmd_response = Map.get(response, "CmdResponse")
  34. call_result = Map.get(cmd_response, "CallResult")
  35. if call_result == "X_OK" do
  36. sts = Map.get(cmd_response, "StsAggregate") |>
  37. Map.get("StsBatchCheckRight")
  38. rights = Map.get(sts, "Rights")
  39. devices = Map.get(sts, "Devices")
  40. end
  41. end
  42. # IO.inspect(length(devices))
  43. devices
  44. end
  45.  
  46. @doc """
  47. Function fos asynchronous post for user Auth
  48. """
  49. @spec post_async(String.t, String.t) :: none
  50. def post_async(user_req, rights) do
  51. port = @port
  52. location = @location
  53. user = @user
  54. pass = @pass
  55. clientip = @clientip
  56. sessionkey = @sessionkey
  57.  
  58. options = [hackney: [basic_auth: {user, pass}, pool: :rights_pool],
  59. connect_timeout: :infinity,
  60. recv_timeout: :infinity,
  61. timeout: :infinity,
  62. stream_to: self
  63. ]
  64.  
  65. headers = [{"Content-Type", "application/x-www-form-urlencoded"},
  66. {"Connection", "Keep-Alive"}]
  67.  
  68. data = %{"sessionkey" => sessionkey,
  69. "username" => @request_username,
  70. "clientip" => clientip,
  71. "jsondata" => nil}
  72.  
  73. # Those information should be in GisObjectCacheAgent
  74. # devices_sliced = Enum.split(Longpoll.Dbworker.Dbreader.get_objects().gis_objects, 1000)
  75.  
  76. command_json = "proxy.json"
  77. url = "http://#{location}:#{port}/"
  78. url_full = url <> command_json
  79.  
  80. params = %{url: url_full, data: data, headers: headers, options: options, rights: rights}
  81.  
  82. # pack it to another function
  83. # Generate devices for check right
  84. send_devices(user_req, params, 0)
  85.  
  86. end
  87.  
  88. #Function for generating cookie
  89. @spec gen_cookie(String.t) :: number
  90. defp gen_cookie(name) do
  91. name |>
  92. to_char_list |>
  93. Enum.map(fn x -> to_string x end) |>
  94. Enum.join("")|>
  95. String.to_integer()
  96. end
  97.  
  98. #Function for getting and broadcasting authorization information from VMX server
  99. defp send_devices(user, params, nr) do
  100. devices_sliced = Longpoll.Agents.GisobjectCacheAgent.get_element(nr)
  101. rights = Map.get(params, :rights)
  102. if devices_sliced != nil do
  103. devices = for x <- devices_sliced, do: %{"DeviceIdentity" => %{"Id" => Map.get(x, "Id"), "@Type" => Map.get(x, "Type")}}
  104. rights_list = cond do
  105. is_list(rights) -> for x <- rights, do: %{"AccessRightType" => x}
  106. true -> [%{"AccessRightType" => rights}]
  107. end
  108. {:ok, cmd_bachcheckrights} = %{"Msg" => %{
  109. "CmdBatchCheckRight" => %{
  110. "TransactionNo" => Longpoll.Agents.TransactionAgent.new_number(),
  111. "User" => %{"Name" => user},
  112. "Cookie" => gen_cookie(user),
  113. "Rights" => rights_list,
  114. "Devices" => devices
  115. }
  116. }
  117. } |> Poison.encode
  118. data = Map.get(params, :data) |>
  119. Map.put(:jsondata, cmd_bachcheckrights) |>
  120. URI.encode_query
  121. %HTTPoison.AsyncResponse{id: id} = HTTPoison.post!(Map.get(params, :url), data,
  122. Map.get(params, :headers), Map.get(params, :options))
  123. devices_recv = collect_response(id, self, <<>>)
  124. # IO.inspect(devices_recv)
  125. Longpoll.Dbworker.Dbwriter.cache_rights(user, devices_recv, "ObjView")
  126. send_devices(user, params, nr + 1)
  127. else
  128. Gis.Endpoint.broadcast("polling:" <> user, "start", %{status: "get_ready_for_rumble"})
  129. IO.inspect("END")
  130. end
  131. end
  132. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement