Skip to content

Commit

Permalink
Merge branch 'main' into po/get_replay_uri
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloo authored Dec 3, 2024

Verified

This commit was signed with the committer’s verified signature.
mpbrown Mike Brown
2 parents edd5ca7 + e6d3ea3 commit e2db86a
Showing 3 changed files with 171 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/recording/create_recording.ex
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ defmodule Onvif.Recording.CreateRecording do
Onvif.Recording.request(device, args, __MODULE__)
end

def request_body(config: %Onvif.Recording.Recordings.Configuration{} = config) do
def request_body(config: %Onvif.Recording.Recording.Configuration{} = config) do
element(:"s:Body", [
element(:"trc:CreateRecording", [
element(:"trc:RecordingConfiguration", [
168 changes: 168 additions & 0 deletions lib/recording/recording.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
defmodule Onvif.Recording.Recording do
@moduledoc """
Onvif.Recording.Recordings schema.
"""

use Ecto.Schema
import Ecto.Changeset
import SweetXml

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

embedded_schema do
field(:recording_token, :string)

embeds_one :configuration, Configuration, primary_key: false, on_replace: :update do
@derive Jason.Encoder
field(:content, :string)
field(:maximum_retention_time, :string)

embeds_one(:source, Source, primary_key: false, on_replace: :update) do
@derive Jason.Encoder
field(:source_id, :string)
field(:name, :string)
field(:location, :string)
field(:description, :string)
field(:address, :string)
end
end

embeds_one(:tracks, Tracks, primary_key: false, on_replace: :update) do
@derive Jason.Encoder
embeds_many(:track, Track, primary_key: false, on_replace: :delete) do
@derive Jason.Encoder
field(:track_token, :string)

embeds_one(:configuration, Configuration, primary_key: false, on_replace: :update) do
@derive Jason.Encoder
field(:track_type, :string)
field(:description, :string)
end
end
end
end

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

def parse(doc) do
xmap(
doc,
recording_token: ~x"./tt:RecordingToken/text()"so,
configuration: ~x"./tt:Configuration"eo |> transform_by(&parse_configuration/1),
tracks: ~x"./tt:Tracks"eo |> transform_by(&parse_tracks/1)
)
end

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

def parse_configuration(doc) do
xmap(
doc,
content: ~x"./tt:Content/text()"so,
maximum_retention_time: ~x"./tt:MaximumRetentionTime/text()"so,
source: ~x"./tt:Source"eo |> transform_by(&parse_source/1)
)
end

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

def parse_source(doc) do
xmap(
doc,
source_id: ~x"./tt:SourceId/text()"so,
name: ~x"./tt:Name/text()"so,
location: ~x"./tt:Location/text()"so,
description: ~x"./tt:Description/text()"so,
address: ~x"./tt:Address/text()"so
)
end

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

def parse_tracks(doc) do
xmap(
doc,
track: ~x"./tt:Track"elo |> transform_by(&parse_track/1)
)
end

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

def parse_track(docs) do
Enum.map(docs, fn doc ->
xmap(
doc,
track_token: ~x"./tt:TrackToken/text()"so,
configuration: ~x"./tt:Configuration"eo |> transform_by(&parse_track_configuration/1)
)
end)
end

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

def parse_track_configuration(doc) do
xmap(
doc,
track_type: ~x"./tt:TrackType/text()"so,
description: ~x"./tt:Description/text()"so
)
end

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

@spec to_json(%Onvif.Recording.Recording{}) ::
{: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(:configuration, with: &configuration_changeset/2)
|> cast_embed(:tracks, with: &tracks_changeset/2)
end

def configuration_changeset(module, attrs) do
cast(module, attrs, [:content, :maximum_retention_time])
|> cast_embed(:source, with: &source_changeset/2)
end

def source_changeset(module, attrs) do
cast(module, attrs, [:source_id, :name, :location, :description, :address])
end

def tracks_changeset(module, attrs) do
cast(module, attrs, [])
|> cast_embed(:track, with: &track_changeset/2)
end

def track_changeset(module, attrs) do
cast(module, attrs, [:track_token])
|> cast_embed(:configuration, with: &track_configuration_changeset/2)
end

def track_configuration_changeset(module, attrs) do
cast(module, attrs, [:track_type, :description])
end
end
4 changes: 2 additions & 2 deletions test/recording/create_recording_test.exs
Original file line number Diff line number Diff line change
@@ -15,10 +15,10 @@ defmodule Onvif.Recording.CreateRecordingTest do

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

0 comments on commit e2db86a

Please sign in to comment.