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 all commits
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
62 changes: 62 additions & 0 deletions lib/devices/set_ntp.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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) |> List.flatten()
])
])
end

defp ntp_manual_element(%NTP{from_dhcp: true} = _ntp), do: []

defp ntp_manual_element(%NTP{from_dhcp: false} = ntp) do
[element(:"tds:NTPManual", ntp_add_manual_element(ntp.ntp_manual))]
end

defp ntp_add_manual_element(ntp_manual) do
[
element(
:"tt:Type",
Keyword.fetch!(Ecto.Enum.mappings(ntp_manual.__struct__, :type), ntp_manual.type)
),
ntp_manual_element_data(ntp_manual)
]
end

defp ntp_manual_element_data(%NTP.NTPManual{type: :ipv4} = ntp_manual),
do: element(:"tt:IPv4Address", ntp_manual.ipv4_address)

defp ntp_manual_element_data(%NTP.NTPManual{type: :ipv6} = ntp_manual),
do: element(:"tt:IPv6Address", ntp_manual.ipv6_address)

defp ntp_manual_element_data(%NTP.NTPManual{type: :dns} = ntp_manual),
do: element(:"tt:DNSname", ntp_manual.dns_name)

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