Skip to content

Commit

Permalink
Make all elements optional in xml parsing for video encoder (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
waranlogesh authored Jun 6, 2024
1 parent e119619 commit 0a68201
Show file tree
Hide file tree
Showing 8 changed files with 3,753 additions and 22 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/ci_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: CI Tests

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build:

name: Build and test
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Read .tool-versions
uses: marocchino/tool-versions-action@v1
id: versions

- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
otp-version: ${{ steps.versions.outputs.erlang }}
elixir-version: ${{ steps.versions.outputs.elixir }}
version-type: "strict"

- name: Restore dependencies cache
uses: actions/cache@v4
with:
path: deps
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
restore-keys: ${{ runner.os }}-mix-

- name: Install dependencies
run: mix deps.get

- name: Run tests
run: mix test
88 changes: 88 additions & 0 deletions lib/factory.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
defmodule Onvif.Factory do
def device do
%Onvif.Device{
address: "http://192.168.254.89",
auth_type: :digest_auth,
device_service_path: "/onvif/device_service",
firmware_version: nil,
hardware_id: "V1.0",
manufacturer: "General",
media_ver10_service_path: "/onvif/media_service",
media_ver20_service_path: "/onvif/media2_service",
model: "N864A6",
ntp: "NTP",
password: "admin",
port: 80,
scopes: [
"onvif://www.onvif.org/location/country/China",
"onvif://www.onvif.org/name/General",
"onvif://www.onvif.org/hardware/N864A6",
"onvif://www.onvif.org/Profile/Streaming",
"onvif://www.onvif.org/type/Network_Video_Transmitter",
"onvif://www.onvif.org/extension/unique_identifier/1",
"onvif://www.onvif.org/Profile/G",
"onvif://www.onvif.org/Profile/T"
],
serial_number: "NV012306000836",
services: [
%Onvif.Device.Service{
namespace: "http://www.onvif.org/ver10/replay/wsdl",
version: "17.06",
xaddr: "http://192.168.254.89/onvif/replay_service"
},
%Onvif.Device.Service{
namespace: "http://www.onvif.org/ver10/search/wsdl",
version: "18.12",
xaddr: "http://192.168.254.89/onvif/search_service"
},
%Onvif.Device.Service{
namespace: "http://www.onvif.org/ver10/recording/wsdl",
version: "18.06",
xaddr: "http://192.168.254.89/onvif/recording_service"
},
%Onvif.Device.Service{
namespace: "http://www.onvif.org/ver10/deviceIO/wsdl",
version: "18.12",
xaddr: "http://192.168.254.89/onvif/deviceIO_service"
},
%Onvif.Device.Service{
namespace: "http://www.onvif.org/ver10/events/wsdl",
version: "2.60",
xaddr: "http://192.168.254.89/onvif/event_service"
},
%Onvif.Device.Service{
namespace: "http://www.onvif.org/ver20/ptz/wsdl",
version: "18.12",
xaddr: "http://192.168.254.89/onvif/ptz_service"
},
%Onvif.Device.Service{
namespace: "http://www.onvif.org/ver20/media/wsdl",
version: "18.12",
xaddr: "http://192.168.254.89/onvif/media2_service"
},
%Onvif.Device.Service{
namespace: "http://www.onvif.org/ver10/media/wsdl",
version: "18.06",
xaddr: "http://192.168.254.89/onvif/media_service"
},
%Onvif.Device.Service{
namespace: "http://www.onvif.org/ver20/imaging/wsdl",
version: "18.12",
xaddr: "http://192.168.254.89/onvif/imaging_service"
},
%Onvif.Device.Service{
namespace: "http://www.onvif.org/ver20/analytics/wsdl",
version: "18.12",
xaddr: "http://192.168.254.89/onvif/analytics_service"
},
%Onvif.Device.Service{
namespace: "http://www.onvif.org/ver10/device/wsdl",
version: "18.12",
xaddr: "http://192.168.254.89/onvif/device_service"
}
],
time_diff_from_system_secs: 3597,
username: "admin"
}
end
end
39 changes: 18 additions & 21 deletions lib/media/ver10/profile/video_encoder_configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ defmodule Onvif.Media.Ver10.Profile.VideoEncoderConfiguration do

@primary_key false
@derive Jason.Encoder
@required [:reference_token, :name, :encoding]
@optional [:use_count, :guaranteed_frame_rate, :quality, :session_timeout]
embedded_schema do
field(:reference_token, :string)
field(:name, :string)
Expand Down Expand Up @@ -57,23 +59,24 @@ defmodule Onvif.Media.Ver10.Profile.VideoEncoderConfiguration do
def parse(doc) do
xmap(
doc,
reference_token: ~x"./@token"s,
name: ~x"./tt:Name/text()"s,
use_count: ~x"./tt:UseCount/text()"i,
guaranteed_frame_rate: ~x"./tt:GuaranteedFrameRate/text()"s,
encoding: ~x"./tt:Encoding/text()"s,
session_timeout: ~x"./tt:SessionTimeout/text()"s,
quality: ~x"./tt:Quality/text()"f,
resolution: ~x"./tt:Resolution"e |> transform_by(&parse_resolution/1),
rate_control: ~x"./tt:RateControl"e |> transform_by(&parse_rate_control/1),
mpeg4_configuration: ~x"./tt:Mpeg4"e |> transform_by(&parse_mpeg4_configuration/1),
h264_configuration: ~x"./tt:H264"e |> transform_by(&parse_h264_configuration/1),
reference_token: ~x"./@token"so,
name: ~x"./tt:Name/text()"so,
use_count: ~x"./tt:UseCount/text()"io,
guaranteed_frame_rate: ~x"./tt:GuaranteedFrameRate/text()"so,
encoding: ~x"./tt:Encoding/text()"so,
session_timeout: ~x"./tt:SessionTimeout/text()"so,
quality: ~x"./tt:Quality/text()"fo,
resolution: ~x"./tt:Resolution"eo |> transform_by(&parse_resolution/1),
rate_control: ~x"./tt:RateControl"eo |> transform_by(&parse_rate_control/1),
mpeg4_configuration: ~x"./tt:Mpeg4"eo |> transform_by(&parse_mpeg4_configuration/1),
h264_configuration: ~x"./tt:H264"eo |> transform_by(&parse_h264_configuration/1),
multicast_configuration:
~x"./tt:Multicast"e |> transform_by(&MulticastConfiguration.parse/1)
~x"./tt:Multicast"eo |> transform_by(&MulticastConfiguration.parse/1)
)
end

defp parse_resolution([]), do: nil
defp parse_resolution(nil), do: nil

defp parse_resolution(doc) do
xmap(
Expand All @@ -84,6 +87,7 @@ defmodule Onvif.Media.Ver10.Profile.VideoEncoderConfiguration do
end

defp parse_rate_control([]), do: nil
defp parse_rate_control(nil), do: nil

defp parse_rate_control(doc) do
xmap(
Expand Down Expand Up @@ -136,15 +140,8 @@ defmodule Onvif.Media.Ver10.Profile.VideoEncoderConfiguration do

def changeset(module, attrs) do
module
|> cast(attrs, [
:reference_token,
:name,
:use_count,
:guaranteed_frame_rate,
:encoding,
:quality,
:session_timeout
])
|> cast(attrs, @required ++ @optional)
|> validate_required(@required)
|> cast_embed(:resolution, with: &resolution_changeset/2)
|> cast_embed(:rate_control, with: &rate_control_changeset/2)
|> cast_embed(:mpeg4_configuration, with: &mpeg4_configuration_changeset/2)
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ defmodule Onvif.MixProject do
{:sweet_xml, "~> 0.7"},
{:tesla, "~> 1.7"},
{:xml_builder, "~> 2.1"},
{:jason, "~> 1.4"}
{:jason, "~> 1.4"},
{:mimic, "~> 1.7", only: :test}
]
end
end
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"},
"mimic": {:hex, :mimic, "1.7.4", "cd2772ffbc9edefe964bc668bfd4059487fa639a5b7f1cbdf4fd22946505aa4f", [:mix], [], "hexpm", "437c61041ecf8a7fae35763ce89859e4973bb0666e6ce76d75efc789204447c3"},
"mint": {:hex, :mint, "1.5.1", "8db5239e56738552d85af398798c80648db0e90f343c8469f6c6d8898944fb6f", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "4a63e1e76a7c3956abd2c72f370a0d0aecddc3976dea5c27eccbecfa5e7d5b1e"},
"nimble_options": {:hex, :nimble_options, "1.0.2", "92098a74df0072ff37d0c12ace58574d26880e522c22801437151a159392270e", [:mix], [], "hexpm", "fd12a8db2021036ce12a309f26f564ec367373265b53e25403f0ee697380f1b8"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
Expand Down
Loading

0 comments on commit 0a68201

Please sign in to comment.