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

C#12 & .NET 8 updates #81

Draft
wants to merge 27 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a29ad21
Refactor code for readability and modern C# features
alafleur-genetec Sep 20, 2024
34fc417
Update copyright and license to Apache 2.0
alafleur-genetec Sep 20, 2024
63475a8
Add Genetec.Dap.CodeSamples namespace to classes
alafleur-genetec Sep 20, 2024
509245b
Simplify null check and remove unused usings
alafleur-genetec Sep 20, 2024
a1fe447
Refactor array initializations to shorthand syntax
alafleur-genetec Sep 20, 2024
5c5f8bb
Refactor property initializations to use array syntax
alafleur-genetec Sep 20, 2024
ec2baea
Improve string handling in video sequence and XML display
alafleur-genetec Sep 20, 2024
246e024
Use explicit type declarations for clarity and safety
alafleur-genetec Sep 20, 2024
cddfbe6
Refactor constructors to use parameters directly
alafleur-genetec Sep 20, 2024
095e3fe
Refactor CustomActionTypeDescriptor initialization
alafleur-genetec Sep 20, 2024
39ee067
Refactor namespaces and access modifiers
alafleur-genetec Sep 21, 2024
e628ee7
Update C# LangVersion to 12 in all .csproj files
alafleur-genetec Sep 21, 2024
9852a6a
Refactor object instantiation to use shorthand syntax
alafleur-genetec Sep 21, 2024
6e1857e
Refactor classes to records for immutability
alafleur-genetec Sep 21, 2024
d73f789
Refactor main logic into async RunSample method
alafleur-genetec Sep 27, 2024
6f78fba
Refactor methods to include Engine parameter
alafleur-genetec Sep 27, 2024
273b2c2
Refactor main logic into async RunSample method
alafleur-genetec Sep 27, 2024
de16279
Update package and ensure config file is copied
alafleur-genetec Sep 28, 2024
bdffb9d
Update .csproj files for .NET 8.0 and WPF support
alafleur-genetec Sep 28, 2024
8bf535e
Updated the C# language version to 12
alafleur-genetec Sep 28, 2024
0ea57a5
Refactor to introduce MediaPlayerApp class
alafleur-genetec Sep 28, 2024
4304392
Refactor and reformat Program class for readability
alafleur-genetec Sep 28, 2024
999c341
Merged branch origin-main into branch code-refactoring-to-c#12
alafleur-genetec Sep 29, 2024
7702a1d
Update project files and refactor Program.cs
alafleur-genetec Sep 29, 2024
b1baaa5
Merge remote-tracking branch 'origin/main' into code-refactoring-to-c#12
alafleur-genetec Oct 2, 2024
9c3996e
Merged origin/main
alafleur-genetec Oct 3, 2024
b27a23d
merged from main
alafleur-genetec Nov 29, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
using System.Threading.Tasks;
using Genetec.Sdk.Media.Export;

public static class FileCryptingManagerExtensions
namespace Genetec.Dap.CodeSamples;

static class FileCryptingManagerExtensions
{
/// <summary>
/// Encrypts a file asynchronously using the specified password.
Expand Down
9 changes: 8 additions & 1 deletion Samples/Media SDK/FileCryptingManagerSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
using System;
// Copyright 2024 Genetec Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
Expand Down
13 changes: 10 additions & 3 deletions Samples/Media SDK/MediaFileSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
using System;
// Copyright 2024 Genetec Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

using System;
using System.IO;
using Genetec.Dap.CodeSamples;
using Genetec.Sdk.Media;
Expand All @@ -17,7 +24,7 @@ void RunSample()
Console.WriteLine($"\nMedia file path: {mediaFile}");

Console.WriteLine("\nUnique Sources:");
foreach (var source in mediaFile.UniqueSources)
foreach (UniqueSource source in mediaFile.UniqueSources)
{
Console.WriteLine($"Collection: {source.Collection} ({source.CollectionName})");
Console.WriteLine($"Encoder: {source.Encoder} ({source.EncoderName})");
Expand All @@ -29,7 +36,7 @@ void RunSample()
}

Console.WriteLine("Contained Files:");
foreach (var file in mediaFile.ContainedFiles)
foreach (MediaFile.ContainedFile file in mediaFile.ContainedFiles)
{
Console.WriteLine($"Filename: {file.Filename}");
Console.WriteLine($"Start Time: {file.StartTime}");
Expand Down
4 changes: 3 additions & 1 deletion Samples/Media SDK/MediaFileSample/StreamMediaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

using System;

public static class StreamMediaType
namespace Genetec.Dap.CodeSamples;

static class StreamMediaType
{
public static readonly Guid Legacy = new("E5FDCEF3-0A63-4615-A2A2-BB34C892FB97");
public static readonly Guid Video = new("4A0425E9-5F15-4675-83EB-2777CE199CB3");
Expand Down
4 changes: 3 additions & 1 deletion Samples/Media SDK/MediaFileSample/StreamUsageType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

using System;

public static class StreamUsageType
namespace Genetec.Dap.CodeSamples;

static class StreamUsageType
{
public static readonly Guid Live = new("e30e6525-2202-4502-8101-5a75ee15f04b");
public static readonly Guid Archiving = new("bac8d8bf-ee2a-41ac-b62c-3c2dd00020a5");
Expand Down
125 changes: 64 additions & 61 deletions Samples/Media SDK/OverlaySample/BouncingBall.cs
Original file line number Diff line number Diff line change
@@ -1,82 +1,85 @@
// Copyright (C) 2023 by Genetec, Inc. All rights reserved.
// May be used only in accordance with a valid Source Code License Agreement.
// Copyright 2024 Genetec Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

namespace Genetec.Dap.CodeSamples
namespace Genetec.Dap.CodeSamples;

using System.Windows;
using System.Windows.Media;
using Sdk.Media.Overlay;

class BouncingBall
{
using System.Windows;
using System.Windows.Media;
using Sdk.Media.Overlay;
public double CanvasWidth = 1290;

public class BouncingBall
{
public double CanvasWidth = 1290;
public double CanvasHeight = 100;

public double CanvasHeight = 100;
private double m_x;

private double m_x;
private double m_y;

private double m_y;
private readonly double m_radius;

private readonly double m_radius;
private double m_horizontalVelocity;

private double m_horizontalVelocity;
private double m_verticalVelocity;

private double m_verticalVelocity;
private readonly SolidColorBrush m_brush;

private readonly SolidColorBrush m_brush;
private readonly Pen m_pen;

private readonly Pen m_pen;
public BouncingBall(double x, double y, double horizontalVelocity, double verticalVelocity, double radius)
{
m_x = x;
m_y = y;
m_horizontalVelocity = horizontalVelocity;
m_verticalVelocity = verticalVelocity;
m_radius = radius;

public BouncingBall(double x, double y, double horizontalVelocity, double verticalVelocity, double radius)
{
m_x = x;
m_y = y;
m_horizontalVelocity = horizontalVelocity;
m_verticalVelocity = verticalVelocity;
m_radius = radius;
m_brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Red"));
m_brush.Freeze();

m_brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Red"));
m_brush.Freeze();
m_pen = new Pen(m_brush, 1.0);
m_pen.Freeze();
}

m_pen = new Pen(m_brush, 1.0);
m_pen.Freeze();
}
public void Draw(Layer layer)
{
Update();
layer.DrawEllipse(m_brush, m_pen, new Point(m_x, m_y), m_radius, m_radius);
}

private void Update()
{
// Update position using basic physics equations
m_x += m_horizontalVelocity * 0.5f;
m_y += m_verticalVelocity * 0.5f;

public void Draw(Layer layer)
// Check if ball has collided with the left or right boundary
if (m_x - m_radius < 0)
{
Update();
layer.DrawEllipse(m_brush, m_pen, new Point(m_x, m_y), m_radius, m_radius);
m_x = m_radius;
m_horizontalVelocity = -m_horizontalVelocity;
}
else if (m_x + m_radius > CanvasWidth)
{
m_x = CanvasWidth - m_radius;
m_horizontalVelocity = -m_horizontalVelocity;
}

private void Update()
// Check if ball has collided with the top or bottom boundary
if (m_y - m_radius < 0)
{
m_y = m_radius;
m_verticalVelocity = -m_verticalVelocity;
}
else if (m_y + m_radius > CanvasHeight)
{
// Update position using basic physics equations
m_x += m_horizontalVelocity * 0.5f;
m_y += m_verticalVelocity * 0.5f;

// Check if ball has collided with the left or right boundary
if (m_x - m_radius < 0)
{
m_x = m_radius;
m_horizontalVelocity = -m_horizontalVelocity;
}
else if (m_x + m_radius > CanvasWidth)
{
m_x = CanvasWidth - m_radius;
m_horizontalVelocity = -m_horizontalVelocity;
}

// Check if ball has collided with the top or bottom boundary
if (m_y - m_radius < 0)
{
m_y = m_radius;
m_verticalVelocity = -m_verticalVelocity;
}
else if (m_y + m_radius > CanvasHeight)
{
m_y = CanvasHeight - m_radius;
m_verticalVelocity = -m_verticalVelocity;
}
m_y = CanvasHeight - m_radius;
m_verticalVelocity = -m_verticalVelocity;
}
}
}
}
105 changes: 51 additions & 54 deletions Samples/Media SDK/OverlaySample/BouncingBallOverlay.cs
Original file line number Diff line number Diff line change
@@ -1,72 +1,69 @@
// Copyright (C) 2023 by Genetec, Inc. All rights reserved.
// May be used only in accordance with a valid Source Code License Agreement.
// Copyright 2024 Genetec Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

namespace Genetec.Dap.CodeSamples
namespace Genetec.Dap.CodeSamples;

using System;
using System.Threading;
using System.Threading.Tasks;
using Sdk.Media.Overlay;

class BouncingBallOverlay(Guid cameraId)
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Sdk.Media.Overlay;
private readonly Guid m_layerId = new("69A64ACE-6DDC-4142-AD04-06690D8591B3");
private Task m_task;
private CancellationTokenSource m_cancellationTokenSource;

public class BouncingBallOverlay
public void Stop()
{
private readonly Guid m_cameraId;
private readonly Guid m_layerId = new Guid("69A64ACE-6DDC-4142-AD04-06690D8591B3");
private Task m_task;
private CancellationTokenSource m_cancellationTokenSource;
m_cancellationTokenSource?.Cancel();
m_cancellationTokenSource?.Dispose();
}

public BouncingBallOverlay(Guid cameraId)
{
m_cameraId = cameraId;
}
public void Start()
{
if (m_task is { IsCompleted: false })
return;

public void Stop()
{
m_cancellationTokenSource?.Cancel();
m_cancellationTokenSource?.Dispose();
}
m_cancellationTokenSource?.Dispose();
m_cancellationTokenSource = new CancellationTokenSource();

public void Start()
{
if (m_task is { IsCompleted: false })
return;
CancellationToken token = m_cancellationTokenSource.Token;

m_cancellationTokenSource?.Dispose();
m_cancellationTokenSource = new CancellationTokenSource();
m_task = Task.Run(async () =>
{
var ball = new BouncingBall(50, 50, 50, 50, 25)
{
CanvasHeight = 960,
CanvasWidth = 1280
};

CancellationToken token = m_cancellationTokenSource.Token;
Overlay overlay = OverlayFactory.Get(cameraId, "Bouncing ball");

m_task = Task.Run(async () =>
if (overlay.DrawingSurfaceWidth == 0 || overlay.DrawingSurfaceHeight == 0)
{
var ball = new BouncingBall(50, 50, 50, 50, 25)
{
CanvasHeight = 960,
CanvasWidth = 1280
};

Overlay overlay = OverlayFactory.Get(m_cameraId, "Bouncing ball");
overlay.Initialize(1280, 960);
}

if (overlay.DrawingSurfaceWidth == 0 || overlay.DrawingSurfaceHeight == 0)
{
overlay.Initialize(1280, 960);
}
await overlay.WaitUntilReadyForUpdate();

await overlay.WaitUntilReadyForUpdate();
Layer layer = overlay.CreateLayer(m_layerId, "Bouncing ball");

Layer layer = overlay.CreateLayer(m_layerId, "Bouncing ball");
while (!token.IsCancellationRequested)
{
ball.Draw(layer);

while (!token.IsCancellationRequested)
if (!layer.Update())
{
ball.Draw(layer);

if (!layer.Update())
{
Console.WriteLine("Update failed");
}

await Task.Delay(10, token);
Console.WriteLine("Update failed");
}
}, token);
}

await Task.Delay(10, token);
}
}, token);
}
}
}
Loading