Skip to content

Commit

Permalink
FindEvents (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloo authored Dec 13, 2024
1 parent 2df3784 commit 839fb93
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 9 deletions.
2 changes: 0 additions & 2 deletions lib/device.ex
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,4 @@ defmodule Onvif.Device do
%Onvif.Device.Service{} = service -> service.xaddr |> URI.parse() |> Map.get(:path)
end
end


end
3 changes: 2 additions & 1 deletion lib/recording/create_recording.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ defmodule Onvif.Recording.CreateRecording do

def gen_maximum_retention_time(nil), do: []
def gen_maximum_retention_time(""), do: []
def gen_maximum_retention_time(maximum_retention_time), do: element(:"tt:MaximumRetentionTime", maximum_retention_time)

def gen_maximum_retention_time(maximum_retention_time),
do: element(:"tt:MaximumRetentionTime", maximum_retention_time)

def response(xml_response_body) do
recording_token =
Expand Down
1 change: 1 addition & 0 deletions lib/recording/create_recording_job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ defmodule Onvif.Recording.CreateRecordingJob do
|> add_namespace("trc", "http://www.onvif.org/ver10/recording/wsdl")
|> add_namespace("tt", "http://www.onvif.org/ver10/schema")
)

{:ok, parsed_result}
end
end
39 changes: 39 additions & 0 deletions lib/search/find_events.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule Onvif.Search.FindEvents do
import SweetXml
import XmlBuilder
require Logger

def soap_action, do: "http://www.onvif.org/ver10/search/wsdl/FindEvents"

def request(device, args) do
Onvif.Search.request(device, args, __MODULE__)
end

def request_body([included_recordings, start_point, end_point, search_filter, keep_alive_time]) do
element(:"s:Body", [
element(:"tse:FindEvents", [
element(:"tse:StartPoint", start_point),
element(:"tse:EndPoint", end_point),
element(:"tse:scope", [
Enum.map(included_recordings, fn ir -> element(:"tt:IncludedRecordings", [ir]) end)
]),
element(:"tt:SearchFilter", [search_filter]),
element(:"tse:IncludeStartState", false),
element(:"tse:KeepAliveTime", keep_alive_time)
])
])
end

def response(xml_response_body) do
parsed_result =
xml_response_body
|> parse(namespace_conformant: true, quiet: true)
|> xpath(
~x"//tse:SearchToken/text()"s
|> add_namespace("s", "http://www.w3.org/2003/05/soap-envelope")
|> add_namespace("tse", "http://www.onvif.org/ver10/search/wsdl")
)

{:ok, parsed_result}
end
end
7 changes: 6 additions & 1 deletion test/recording/create_recording_job_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ defmodule Onvif.Recording.CreateRecordingJobTest do
{:ok, %{status: 200, body: xml_response}}
end)

{:ok, response} = Onvif.Recording.CreateRecordingJob.request(device, ["SD_DISK_20241120_211729_9C896594", "9", "Active"])
{:ok, response} =
Onvif.Recording.CreateRecordingJob.request(device, [
"SD_DISK_20241120_211729_9C896594",
"9",
"Active"
])

assert response == "SD_DISK_20241120_211729_9C896594"
end
Expand Down
12 changes: 7 additions & 5 deletions test/recording/create_recording_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ defmodule Onvif.Recording.CreateRecordingTest do
{:ok, %{status: 200, body: xml_response}}
end)

{:ok, response_uri} = Onvif.Recording.CreateRecording.request(
device,
config: %Onvif.Recording.Recording.Configuration{
{:ok, response_uri} =
Onvif.Recording.CreateRecording.request(
device,
config: %Onvif.Recording.Recording.Configuration{
content: "test",
maximum_retention_time: "PT1H",
source: %Onvif.Recording.Recording.Configuration.Source{
name: "test",
name: "test"
}
})
}
)

assert response_uri == "SD_DISK_20200422_123501_A2388AB3"
end
Expand Down
28 changes: 28 additions & 0 deletions test/search/find_events_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Onvif.Search.FindEventsTest do
use ExUnit.Case, async: true

@moduletag capture_log: true

describe "FindEvents/2" do
test "get an event search token" do
xml_response = File.read!("test/search/fixtures/find_events__success.xml")

device = Onvif.Factory.device()

Mimic.expect(Tesla, :request, fn _client, _opts ->
{:ok, %{status: 200, body: xml_response}}
end)

{:ok, response} =
Onvif.Search.FindEvents.request(device, [
["Record_004"],
"2024-12-06T19:00:00.0Z",
"2024-12-06T19:02:00.0Z",
"tns1:RecordingHistory/Track/State",
"PT1M"
])

assert response == "SearchToken[1]"
end
end
end
53 changes: 53 additions & 0 deletions test/search/fixtures/find_events__success.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://tempuri.org/tns.xsd"
xmlns:tns1="http://www.onvif.org/ver10/topics"
xmlns:c14n="http://www.w3.org/2001/10/xml-exc-c14n#"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:wsa5="http://www.w3.org/2005/08/addressing"
xmlns:xmime5="http://tempuri.org/xmime5.xsd"
xmlns:xop="http://www.w3.org/2004/08/xop/include"
xmlns:tplt="http://www.onvif.org/ver10/plus/schema"
xmlns:tt="http://www.onvif.org/ver10/schema"
xmlns:wstop="http://docs.oasis-open.org/wsn/t-1"
xmlns:wsrfr="http://docs.oasis-open.org/wsrf/r-2"
xmlns:wsrfbf="http://docs.oasis-open.org/wsrf/bf-2"
xmlns:tanae="http://www.onvif.org/ver20/analytics/wsdl/AnalyticsEngineBinding"
xmlns:tanre="http://www.onvif.org/ver20/analytics/wsdl/RuleEngineBinding"
xmlns:tan="http://www.onvif.org/ver20/analytics/wsdl"
xmlns:tds="http://www.onvif.org/ver10/device/wsdl"
xmlns:tes-cppb="http://www.onvif.org/ver10/events/wsdl/CreatePullPointBinding"
xmlns:tes-e="http://www.onvif.org/ver10/events/wsdl/EventBinding"
xmlns:tes-nc="http://www.onvif.org/ver10/events/wsdl/NotificationConsumerBinding"
xmlns:tes-np="http://www.onvif.org/ver10/events/wsdl/NotificationProducerBinding"
xmlns:tes-ppb="http://www.onvif.org/ver10/events/wsdl/PullPointBinding"
xmlns:tes-pps="http://www.onvif.org/ver10/events/wsdl/PullPointSubscriptionBinding"
xmlns:tev="http://www.onvif.org/ver10/events/wsdl"
xmlns:tes-psmb="http://www.onvif.org/ver10/events/wsdl/PausableSubscriptionManagerBinding"
xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2"
xmlns:tes-sm="http://www.onvif.org/ver10/events/wsdl/SubscriptionManagerBinding"
xmlns:timg="http://www.onvif.org/ver20/imaging/wsdl"
xmlns:tmd="http://www.onvif.org/ver10/deviceIO/wsdl"
xmlns:tpl="http://www.onvif.org/ver10/plus/wsdl"
xmlns:tptz="http://www.onvif.org/ver20/ptz/wsdl"
xmlns:tr2="http://www.onvif.org/ver20/media/wsdl"
xmlns:trc="http://www.onvif.org/ver10/recording/wsdl"
xmlns:trp="http://www.onvif.org/ver10/replay/wsdl"
xmlns:trt="http://www.onvif.org/ver10/media/wsdl"
xmlns:trv="http://www.onvif.org/ver10/receiver/wsdl"
xmlns:tse="http://www.onvif.org/ver10/search/wsdl"
xmlns:ter="http://www.onvif.org/ver10/error">
<SOAP-ENV:Header></SOAP-ENV:Header>
<SOAP-ENV:Body>
<tse:FindEventsResponse>
<tse:SearchToken>SearchToken[1]</tse:SearchToken>
</tse:FindEventsResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

0 comments on commit 839fb93

Please sign in to comment.