Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetNTP #75

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions lib/devices/set_ntp.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule Onvif.Devices.SetNTP do
import SweetXml
import XmlBuilder

alias Onvif.Device
alias Onvif.Devices.NTP

def soap_action, do: "http://www.onvif.org/ver10/device/wsdl/SetNTP"

@spec request(Device.t(), list) :: {:ok, any} | {:error, map()}
def request(device, args) do
Onvif.Devices.request(device, args, __MODULE__)
end

def request_body(config: %NTP{} = ntp) do
element(:"s:Body", [
element(:"tds:SetNTP", [
element(:"tds:FromDHCP", ntp.from_dhcp),
ntp_manual_element(ntp, ntp.from_dhcp) |> List.flatten()
paoloo marked this conversation as resolved.
Show resolved Hide resolved
])
])
end

defp ntp_manual_element(%NTP{} = _ntp, true), do: []
paoloo marked this conversation as resolved.
Show resolved Hide resolved

defp ntp_manual_element(%NTP{} = ntp, false) do
paoloo marked this conversation as resolved.
Show resolved Hide resolved
[element(:"tds:NTPManual", ntp_add_manual_element(ntp.ntp_manual))]
end

defp ntp_add_manual_element(ntp_manual) do
[
element(:"tt:Type", ntp_manual.type),
paoloo marked this conversation as resolved.
Show resolved Hide resolved
ntp_manual_element_data(ntp_manual)
]
end

defp ntp_manual_element_data(ntp_manual) do
paoloo marked this conversation as resolved.
Show resolved Hide resolved
case ntp_manual.type do
"IPv4" -> element(:"tt:IPv4Address", ntp_manual.ipv4_address)
"IPv6" -> element(:"tt:IPv6Address", ntp_manual.ipv6_address)
"DNS" -> element(:"tt:DNSname", ntp_manual.dns_name)
end
end

def response(xml_response_body) do
res =
xml_response_body
|> parse(namespace_conformant: true, quiet: true)
|> xpath(
~x"//s:Envelope/s:Body/tds:SetNTPResponse/text()"s
|> add_namespace("s", "http://www.w3.org/2003/05/soap-envelope")
|> add_namespace("tds", "http://www.onvif.org/ver10/device/wsdl")
|> add_namespace("tt", "http://www.onvif.org/ver10/schema")
)

{:ok, res}
end
end
8 changes: 8 additions & 0 deletions test/devices/fixtures/set_ntp_response.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope
xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
<soapenv:Body>
<tds:SetNTPResponse/>
</soapenv:Body>
</soapenv:Envelope>
85 changes: 85 additions & 0 deletions test/devices/set_ntp.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
defmodule Onvif.Devices.SetNTPTest do
use ExUnit.Case, async: true

@moduletag capture_log: true

describe "SetNTP/2" do
test "check a dhcp-based ntp" do
xml_response = File.read!("test/devices/fixtures/set_ntp_response.xml")

device = Onvif.Factory.device()

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

{:ok, service_capabilities} =
Onvif.Devices.SetNTP.request(device, config: %Onvif.Devices.NTP{from_dhcp: true})

assert service_capabilities == ""
end

test "check a manual ntp with IPv4" do
xml_response = File.read!("test/devices/fixtures/set_ntp_response.xml")

device = Onvif.Factory.device()

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

{:ok, service_capabilities} =
Onvif.Devices.SetNTP.request(device,
config: %Onvif.Devices.NTP{
from_dhcp: false,
ntp_manual: %Onvif.Devices.NTP.NTPManual{type: "IPv4", ipv4_address: "6.6.6.0"}
}
)

assert service_capabilities == ""
end

test "check a manual ntp with IPv6" do
xml_response = File.read!("test/devices/fixtures/set_ntp_response.xml")

device = Onvif.Factory.device()

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

{:ok, service_capabilities} =
Onvif.Devices.SetNTP.request(device,
config: %Onvif.Devices.NTP{
from_dhcp: false,
ntp_manual: %Onvif.Devices.NTP.NTPManual{
type: "IPv6",
ipv6_address: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
}
}
)

assert service_capabilities == ""
end

test "check a manual ntp with DNS" do
xml_response = File.read!("test/devices/fixtures/set_ntp_response.xml")

device = Onvif.Factory.device()

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

{:ok, service_capabilities} =
Onvif.Devices.SetNTP.request(device,
config: %Onvif.Devices.NTP{
from_dhcp: false,
ntp_manual: %Onvif.Devices.NTP.NTPManual{type: "DNS", dns_name: "ntp.example.com"}
}
)

assert service_capabilities == ""
end
end
end