Skip to content

Commit

Permalink
add the search service endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloo committed Dec 3, 2024
1 parent e6d3ea3 commit 6a1d629
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/device.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule Onvif.Device do
:media_ver20_service_path,
:recording_ver10_service_path,
:replay_ver10_service_path,
:search_ver10_service_path,
:auth_type,
:time_diff_from_system_secs,
:port,
Expand All @@ -45,6 +46,7 @@ defmodule Onvif.Device do
field(:media_ver20_service_path, :string)
field(:recording_ver10_service_path, :string)
field(:replay_ver10_service_path, :string)
field(:search_ver10_service_path, :string)
embeds_one(:system_date_time, Onvif.Devices.SystemDateAndTime)
embeds_many(:services, Onvif.Device.Service)

Expand Down Expand Up @@ -294,6 +296,7 @@ defmodule Onvif.Device do
|> Map.put(:media_ver20_service_path, get_media_ver20_service_path(device.services))
|> Map.put(:recording_ver10_service_path, get_recoding_ver10_service_path(device.services))
|> Map.put(:replay_ver10_service_path, get_replay_ver10_service_path(device.services))
|> Map.put(:search_ver10_service_path, get_search_ver10_service_path(device.services))
end

defp get_media_ver20_service_path(services) do
Expand Down Expand Up @@ -323,4 +326,13 @@ defmodule Onvif.Device do
%Onvif.Device.Service{} = service -> service.xaddr |> URI.parse() |> Map.get(:path)
end
end

defp get_search_ver10_service_path(services) do
case Enum.find(services, &String.contains?(&1.namespace, "/search")) do
nil -> nil
%Onvif.Device.Service{} = service -> service.xaddr |> URI.parse() |> Map.get(:path)
end
end


end
1 change: 1 addition & 0 deletions lib/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Onvif.Factory do
media_ver20_service_path: "/onvif/media2_service",
replay_ver10_service_path: "/onvif/replay_service",
recording_ver10_service_path: "/onvif/recording_service",
search_ver10_service_path: "/onvif/search_service",
model: "N864A6",
ntp: "NTP",
password: "admin",
Expand Down
56 changes: 56 additions & 0 deletions lib/search.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule Onvif.Search do
@moduledoc """
Interface for making requests to the Onvif search service
https://www.onvif.org/ver10/replay.wsdl
"""
require Logger
alias Onvif.Device

@namespaces [
"xmlns:tse": "http://www.onvif.org/ver10/search/wsdl",
"xmlns:tt": "http://www.onvif.org/ver10/schema"
]

@spec request(Device.t(), module()) :: {:ok, any} | {:error, map()}
def request(%Device{} = device, operation) do
content = generate_content(operation)
do_request(device, operation, content)
end

def request(%Device{} = device, args, operation) do
content = generate_content(operation, args)
do_request(device, operation, content)
end

defp do_request(device, operation, content) do
device
|> Onvif.API.client(service_path: :search_ver10_service_path)
|> Tesla.request(
method: :post,
headers: [{"Content-Type", "application/soap+xml"}, {"SOAPAction", operation.soap_action()}],
body: %Onvif.Request{content: content, namespaces: @namespaces}
)
|> parse_response(operation)
end

defp generate_content(operation), do: operation.request_body()
defp generate_content(operation, args), do: operation.request_body(args)

defp parse_response({:ok, %{status: 200, body: body}}, operation) do
operation.response(body)
end

defp parse_response({:ok, %{status: status_code, body: body}}, operation)
when status_code >= 400,
do:
{:error,
%{
status: status_code,
reason: "Received #{status_code} from #{operation}",
response: body
}}

defp parse_response({:error, response}, operation) do
{:error, %{status: nil, reason: "Error performing #{operation}", response: response}}
end
end

0 comments on commit 6a1d629

Please sign in to comment.