diff --git a/lib/search/get_recording_summary.ex b/lib/search/get_recording_summary.ex new file mode 100644 index 0000000..e818814 --- /dev/null +++ b/lib/search/get_recording_summary.ex @@ -0,0 +1,35 @@ +defmodule Onvif.Search.GetRecordingSummary do + import SweetXml + import XmlBuilder + require Logger + + def soap_action, do: "http://www.onvif.org/ver10/search/wsdl/GetRecordingSummary" + + def request(device) do + Onvif.Search.request(device, __MODULE__) + end + + def request_body() do + element(:"s:Body", [ + element(:"tse:GetRecordingSummary") + ]) + end + + def response(xml_response_body) do + doc = parse(xml_response_body, namespace_conformant: true, quiet: true) + + parsed_result = + xpath( + doc, + ~x"//s:Envelope/s:Body/tse:GetRecordingSummaryResponse/tse:Summary" + |> add_namespace("s", "http://www.w3.org/2003/05/soap-envelope") + |> add_namespace("tse", "http://www.onvif.org/ver10/search/wsdl") + |> add_namespace("tt", "http://www.onvif.org/ver10/schema"), + data_from: ~x"//tt:DataFrom/text()"so, + data_until: ~x"//tt:DataUntil/text()"so, + number_recordings: ~x"//tt:NumberRecordings/text()"so + ) + + {:ok, parsed_result} + end +end diff --git a/test/search/fixtures/get_recording_summary__success.xml b/test/search/fixtures/get_recording_summary__success.xml new file mode 100644 index 0000000..8130e42 --- /dev/null +++ b/test/search/fixtures/get_recording_summary__success.xml @@ -0,0 +1,57 @@ + + + + + + + 1970-01-01T00:00:00Z + 2024-12-13T08:08:49Z + 8 + + + + diff --git a/test/search/get_recording_summary_test.exs b/test/search/get_recording_summary_test.exs new file mode 100644 index 0000000..30f466d --- /dev/null +++ b/test/search/get_recording_summary_test.exs @@ -0,0 +1,25 @@ +defmodule Onvif.Search.GetRecordingSummaryTest do + use ExUnit.Case, async: true + + @moduletag capture_log: true + + describe "GetRecordingSummary/0" do + test "get an event search token" do + xml_response = File.read!("test/search/fixtures/get_recording_summary__success.xml") + + device = Onvif.Factory.device() + + Mimic.expect(Tesla, :request, fn _client, _opts -> + {:ok, %{status: 200, body: xml_response}} + end) + + {:ok, response} = + Onvif.Search.GetRecordingSummary.request(device) + + assert response == %{ + data_from: "1970-01-01T00:00:00Z", + data_until: "2024-12-13T08:08:49Z", + number_recordings: "8"} + end + end +end