Skip to content

Commit 1b2a9b4

Browse files
committed
Update readme & abstract local file creation
1 parent e42690b commit 1b2a9b4

File tree

6 files changed

+117
-7
lines changed

6 files changed

+117
-7
lines changed

README.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,79 @@
11
# OSC.Net
2-
.NET Standard Library for Open Spherical Camera API
2+
.NET Standard Library for Open Spherical Camera API 2.0
3+
4+
## Example
5+
```csharp
6+
var cameraClient = new CameraClient();
7+
cameraClient.TakePicture("test.jpg");
8+
```
9+
10+
## Client
11+
12+
The library exposes `CameraClient` class which implements an `ICameraClient` interface, which has a set of extension methods on it i.e. `TakePicture()`.
13+
14+
### Create client default IP (192.168.42.1) and Port (80)
15+
```csharp
16+
var cameraClient = new CameraClient();
17+
```
18+
19+
### Create client supply IP and Port
20+
```csharp
21+
var cameraClient = new CameraClient(new IPEndPoint(IPAddress.Parse("192.168.42.1"), 80));
22+
```
23+
24+
### Create client supply Uri
25+
```csharp
26+
var cameraClient = new CameraClient(new Uri("http://192.168.42.1"));
27+
```
28+
29+
### HttpClientFactoryHandler
30+
31+
All `CameraClient` constructors takes an optional `HttpClientFactoryHandler` `createClient`parameter which allows you to override how the internally used HttpClient is created.
32+
33+
Example usage with `System.Net` `IHttpClientFactory`
34+
```csharp
35+
var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
36+
var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
37+
38+
var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
39+
40+
var cameraClient = new CameraClient(createClient: httpClientFactory.CreateClient);
41+
```
42+
43+
### CreateFileHandler
44+
45+
All `CameraClient` constructors takes an optional `CreateFileHandler` `createFile`parameter which allows you to override how local files are created.
46+
47+
```csharp
48+
var cameraClient = new CameraClient(
49+
createFile: path => File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None));
50+
```
51+
52+
## Take Picture
53+
54+
### Take picture and get uri to image
55+
56+
```csharp
57+
var pictureUri = await client.TakePicture();
58+
```
59+
60+
### Take picture and download image to supplied stream
61+
62+
```csharp
63+
var imageStream = new MemoryStream();
64+
await client.TakePicture(imageStream);
65+
```
66+
67+
### Take picture and save to supplied local path
68+
```csharp
69+
await client.TakePicture("test.jpg");
70+
```
71+
72+
### useLocalFileUri
73+
74+
All `TakePicture` methods takes an optional `useLocalFileUri` bool parameter, by default it's false. If set to true it'll use `ICameraClient.EndPoint` for construction an absolute uri to images. This is useful if using camera through proxy or firewall.
75+
76+
## Supported cameras
77+
78+
This library has been tested with
79+
* [Insta360 One X](https://www.insta360.com/product/insta360-onex/)

src/OSC.Net.Console/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public static async Task Main(string[] args)
2020
Environment.GetEnvironmentVariable("OSC.Net.Console.EndPoint"),
2121
UriKind.Absolute,
2222
out var endpoint)
23-
? new CameraClient(endpoint, httpClientFactory.CreateClient)
24-
: new CameraClient(httpClientFactory.CreateClient);
23+
? new CameraClient(endpoint, createClient: httpClientFactory.CreateClient)
24+
: new CameraClient(createClient: httpClientFactory.CreateClient);
2525

2626

2727
var currentDateTime = await client.GetDateTime();

src/OSC.Net/CameraClient.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.IO;
23
using System.Net;
34
using System.Net.Http;
45
using OSC.Net.Http;
6+
using OSC.Net.IO;
57

68
namespace OSC.Net
79
{
@@ -10,7 +12,11 @@ namespace OSC.Net
1012
/// </summary>
1113
public class CameraClient : ICameraClient
1214
{
15+
private static Stream DefaultCreateFile(string path)
16+
=> File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None);
17+
1318
private static HttpClient DefaultCreateClient(string name) => new HttpClient();
19+
1420
/// <summary>
1521
/// The Default Camera Ip Address.
1622
/// </summary>
@@ -30,6 +36,7 @@ public class CameraClient : ICameraClient
3036
public static readonly IPEndPoint DefaultIpEndPoint = new IPEndPoint(IPAddress.Parse(DefaultIp), DefaultPort);
3137

3238
private HttpClientFactoryHandler CreateClient { get; }
39+
private CreateFileHandler CreateFile { get; }
3340

3441
/// <inheritdoc />
3542
public Uri EndPoint { get; }
@@ -42,31 +49,38 @@ HttpClient ICameraClient.GetHttpClient()
4249
return client;
4350
}
4451

52+
/// <inheritdoc />
53+
Stream ICameraClient.CreateFile(string path) => CreateFile(path);
54+
4555
/// <summary>
4656
/// Initializes a new instance of the <see cref="CameraClient"/> class using <see cref="DefaultIpEndPoint"/>.
4757
/// </summary>
58+
/// <param name="createFile">Optional handler to override local file creation.</param>
4859
/// <param name="createClient">Optional handler to override http client creation.</param>
49-
public CameraClient(HttpClientFactoryHandler createClient = null) : this(DefaultIpEndPoint, createClient)
60+
public CameraClient(CreateFileHandler createFile = null, HttpClientFactoryHandler createClient = null) : this(DefaultIpEndPoint, createFile, createClient)
5061
{
5162
}
5263

5364
/// <summary>
5465
/// Initializes a new instance of the <see cref="CameraClient"/> class.
5566
/// </summary>
5667
/// <param name="ipEndPoint">The camera ip address and port.</param>
68+
/// <param name="createFile">Optional handler to override local file creation.</param>
5769
/// <param name="createClient">Optional handler to override http client creation.</param>
58-
public CameraClient(IPEndPoint ipEndPoint, HttpClientFactoryHandler createClient = null) : this(new Uri($"http://{ipEndPoint}"), createClient)
70+
public CameraClient(IPEndPoint ipEndPoint, CreateFileHandler createFile = null, HttpClientFactoryHandler createClient = null) : this(new Uri($"http://{ipEndPoint}"), createFile, createClient)
5971
{
6072
}
6173

6274
/// <summary>
6375
/// Initializes a new instance of the <see cref="CameraClient"/> class.
6476
/// </summary>
6577
/// <param name="endPoint">The camera http end point.</param>
78+
/// <param name="createFile">Optional handler to override local file creation.</param>
6679
/// <param name="createClient">Optional handler to override http client creation.</param>
67-
public CameraClient(Uri endPoint, HttpClientFactoryHandler createClient = null)
80+
public CameraClient(Uri endPoint, CreateFileHandler createFile = null, HttpClientFactoryHandler createClient = null)
6881
{
6982
EndPoint = endPoint;
83+
CreateFile = createFile ?? DefaultCreateFile;
7084
CreateClient = createClient ?? DefaultCreateClient;
7185
}
7286
}

src/OSC.Net/Commands.TakePicture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static async Task TakePicture(this ICameraClient client, Stream targetStr
6767
/// <param name="useLocalFileUri">Optional flag for if absolute uri returned from camera should be used or absolute uri created from <see cref="ICameraClient.EndPoint"/> and relative uri, useful if called through a proxy. Default value <c>false</c></param>
6868
public static async Task TakePicture(this ICameraClient client, string path, bool useLocalFileUri = false)
6969
{
70-
using (var targetStream = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None))
70+
using (var targetStream = client.CreateFile(path))
7171
{
7272
await client.TakePicture(targetStream, useLocalFileUri);
7373
}

src/OSC.Net/ICameraClient.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Net.Http;
34

45
namespace OSC.Net
@@ -18,5 +19,12 @@ public interface ICameraClient
1819
/// </summary>
1920
/// <returns></returns>
2021
HttpClient GetHttpClient();
22+
23+
/// <summary>
24+
/// Creates a new file for given path.
25+
/// </summary>
26+
/// <param name="path">Local path.</param>
27+
/// <returns><see cref="Stream"/> to file.</returns>
28+
Stream CreateFile(string path);
2129
}
2230
}

src/OSC.Net/IO/CreateFileHandler.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.IO;
2+
3+
namespace OSC.Net.IO
4+
{
5+
/// <summary>
6+
/// Handler used to override file creation.
7+
/// </summary>
8+
/// <param name="path">Local file path.</param>
9+
/// <returns>Stream to file.</returns>
10+
public delegate Stream CreateFileHandler(string path);
11+
}

0 commit comments

Comments
 (0)