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

GetNTP #73

Merged
merged 21 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 18 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
39 changes: 39 additions & 0 deletions lib/devices/get_ntp.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule Onvif.Devices.GetNTP do
import SweetXml
import XmlBuilder
require Logger

alias Onvif.Device

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

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

def request_body do
element(:"s:Body", [element(:"tds:GetNTP")])
end

def response(xml_response_body) do
xml_response_body
|> parse(namespace_conformant: true, quiet: true)
|> xpath(
~x"//s:Envelope/s:Body/tds:GetNTPResponse/tds:NTPInformation"eo
|> 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")
)
|> Onvif.Devices.NTP.parse()
|> Onvif.Devices.NTP.to_struct()
|> case do
{:ok, ntp} ->
{:ok, ntp}

{:error, changeset} ->
Logger.error("Discarding invalid NTPResponse: #{inspect(changeset)}")
{:ok, nil}
end
end
end
90 changes: 90 additions & 0 deletions lib/devices/ntp.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
defmodule Onvif.Devices.NTP do
@moduledoc """
Schema for the NTP configuration to be used with SetNTP and GetNTP operations.
"""

use Ecto.Schema
import Ecto.Changeset
import SweetXml

@primary_key false
@derive Jason.Encoder
@required [:from_dhcp]
@optional []

embedded_schema do
field(:from_dhcp, :boolean)

embeds_one :ntp_manual, NTPManual, primary_key: false, on_replace: :update do
@derive Jason.Encoder
field(:type, Ecto.Enum, values: [ipv4: "IPv4", ipv6: "IPv6", dns: "DNS"])
field(:ipv4_address, :string)
field(:ipv6_address, :string)
field(:dns_name, :string)
end

embeds_one :ntp_from_dhcp, NTPFromDHCP, primary_key: false, on_replace: :update do
@derive Jason.Encoder
field(:type, Ecto.Enum, values: [ipv4: "IPv4", ipv6: "IPv6", dns: "DNS"])
field(:ipv4_address, :string)
field(:ipv6_address, :string)
field(:dns_name, :string)
end
end

def parse(nil), do: %{}
def parse([]), do: %{}

def parse(doc) do
xmap(
doc,
from_dhcp: ~x"./tt:FromDHCP/text()"so,
ntp_from_dhcp: ~x"./tt:NTPFromDHCP"eo |> transform_by(&parse_ntp_data/1),
ntp_manual: ~x"./tt:NTPManual"eo |> transform_by(&parse_ntp_data/1)
)
end

def parse_ntp_data([]), do: nil
def parse_ntp_data(nil), do: nil

def parse_ntp_data(doc) do
xmap(
doc,
type: ~x"./tt:Type/text()"so,
ipv4_address: ~x"./tt:IPv4Address/text()"so,
ipv6_address: ~x"./tt:IPv6Address/text()"so,
dns_name: ~x"./tt:DNSname/text()"so
)
end

def to_struct(parsed) do
%__MODULE__{}
|> changeset(parsed)
|> apply_action(:validate)
end

@spec to_json(%__MODULE__{}) ::
{:error,
%{
:__exception__ => any,
:__struct__ => Jason.EncodeError | Protocol.UndefinedError,
optional(atom) => any
}}
| {:ok, binary}
def to_json(%__MODULE__{} = schema) do
Jason.encode(schema)
end

def changeset(module, attrs) do
module
|> cast(attrs, @required ++ @optional)
|> validate_required(@required)
|> cast_embed(:ntp_from_dhcp, with: &ntp_data_changeset/2)
|> cast_embed(:ntp_manual, with: &ntp_data_changeset/2)
end

def ntp_data_changeset(module, attrs) do
module
|> cast(attrs, [:type, :ipv4_address, :ipv6_address, :dns_name])
paoloo marked this conversation as resolved.
Show resolved Hide resolved
end
end
51 changes: 51 additions & 0 deletions test/devices/fixtures/get_ntp_response.xml
paoloo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<env:Envelope
xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:soapenc="http://www.w3.org/2003/05/soap-encoding"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tt="http://www.onvif.org/ver10/schema"
xmlns:tds="http://www.onvif.org/ver10/device/wsdl"
xmlns:trt="http://www.onvif.org/ver10/media/wsdl"
xmlns:timg="http://www.onvif.org/ver20/imaging/wsdl"
xmlns:tev="http://www.onvif.org/ver10/events/wsdl"
xmlns:tptz="http://www.onvif.org/ver20/ptz/wsdl"
xmlns:tan="http://www.onvif.org/ver20/analytics/wsdl"
xmlns:tst="http://www.onvif.org/ver10/storage/wsdl"
xmlns:ter="http://www.onvif.org/ver10/error"
xmlns:dn="http://www.onvif.org/ver10/network/wsdl"
xmlns:tns1="http://www.onvif.org/ver10/topics"
xmlns:tmd="http://www.onvif.org/ver10/deviceIO/wsdl"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl"
xmlns:wsoap12="http://schemas.xmlsoap.org/wsdl/soap12"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http"
xmlns:d="http://schemas.xmlsoap.org/ws/2005/04/discovery"
xmlns:wsadis="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2"
xmlns:wsa="http://www.w3.org/2005/08/addressing"
xmlns:wstop="http://docs.oasis-open.org/wsn/t-1"
xmlns:wsrf-bf="http://docs.oasis-open.org/wsrf/bf-2"
xmlns:wsntw="http://docs.oasis-open.org/wsn/bw-2"
xmlns:wsrf-rw="http://docs.oasis-open.org/wsrf/rw-2"
xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"
xmlns:wsrf-r="http://docs.oasis-open.org/wsrf/r-2"
xmlns:trc="http://www.onvif.org/ver10/recording/wsdl"
xmlns:tse="http://www.onvif.org/ver10/search/wsdl"
xmlns:trp="http://www.onvif.org/ver10/replay/wsdl"
xmlns:tnshik="http://www.hikvision.com/2011/event/topics"
xmlns:hikwsd="http://www.onvifext.com/onvif/ext/ver10/wsdl"
xmlns:hikxsd="http://www.onvifext.com/onvif/ext/ver10/schema"
xmlns:tas="http://www.onvif.org/ver10/advancedsecurity/wsdl"
xmlns:tr2="http://www.onvif.org/ver20/media/wsdl"
xmlns:axt="http://www.onvif.org/ver20/analytics">
<env:Body>
<tds:GetNTPResponse>
<tds:NTPInformation>
<tt:FromDHCP>false</tt:FromDHCP>
<tt:NTPManual>
<tt:Type>DNS</tt:Type>
<tt:DNSname>time.windows.com</tt:DNSname>
</tt:NTPManual>
</tds:NTPInformation>
</tds:GetNTPResponse>
</env:Body>
</env:Envelope>
30 changes: 30 additions & 0 deletions test/devices/get_ntp.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Onvif.Devices.GetNTPTest do
use ExUnit.Case, async: true

@moduletag capture_log: true

describe "GetNTP/1" do
test "check ntp configuration" do
xml_response = File.read!("test/devices/fixtures/get_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.GetNTP.request(device)

assert service_capabilities == %Onvif.Devices.NTP{
from_dhcp: false,
ntp_from_dhcp: nil,
ntp_manual: %Onvif.Devices.NTP.NTPManual{
dns_name: "time.windows.com",
ipv4_address: nil,
ipv6_address: nil,
type: :dns
}
}
end
end
end