|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Http; |
| 5 | +using OSC.Net.Http; |
| 6 | +using OSC.Net.IO; |
| 7 | + |
| 8 | +namespace OSC.Net |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Standard implementation of <see cref="ICameraClient"/>. |
| 12 | + /// </summary> |
| 13 | + public class CameraClient : ICameraClient |
| 14 | + { |
| 15 | + private static Stream DefaultCreateFile(string path) |
| 16 | + => File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None); |
| 17 | + |
| 18 | + private static HttpClient DefaultCreateClient(string name) => new HttpClient(); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// The Default Camera Ip Address. |
| 22 | + /// </summary> |
| 23 | + /// <value>192.168.42.1</value> |
| 24 | + public const string DefaultIp = "192.168.42.1"; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// The Default Camera Port. |
| 28 | + /// </summary> |
| 29 | + /// <value>80</value> |
| 30 | + public const int DefaultPort = 80; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// The Default Camera ip end point. |
| 34 | + /// </summary> |
| 35 | + /// <value>192.168.42.1:80</value> |
| 36 | + public static readonly IPEndPoint DefaultIpEndPoint = new IPEndPoint(IPAddress.Parse(DefaultIp), DefaultPort); |
| 37 | + |
| 38 | + private HttpClientFactoryHandler CreateClient { get; } |
| 39 | + private CreateFileHandler CreateFile { get; } |
| 40 | + |
| 41 | + /// <inheritdoc /> |
| 42 | + public Uri EndPoint { get; } |
| 43 | + |
| 44 | + /// <inheritdoc /> |
| 45 | + HttpClient ICameraClient.GetHttpClient() |
| 46 | + { |
| 47 | + var client = CreateClient(nameof(CameraClient)); |
| 48 | + client.BaseAddress = EndPoint; |
| 49 | + return client; |
| 50 | + } |
| 51 | + |
| 52 | + /// <inheritdoc /> |
| 53 | + Stream ICameraClient.CreateFile(string path) => CreateFile(path); |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Initializes a new instance of the <see cref="CameraClient"/> class using <see cref="DefaultIpEndPoint"/>. |
| 57 | + /// </summary> |
| 58 | + /// <param name="createFile">Optional handler to override local file creation.</param> |
| 59 | + /// <param name="createClient">Optional handler to override http client creation.</param> |
| 60 | + public CameraClient(CreateFileHandler createFile = null, HttpClientFactoryHandler createClient = null) : this(DefaultIpEndPoint, createFile, createClient) |
| 61 | + { |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Initializes a new instance of the <see cref="CameraClient"/> class. |
| 66 | + /// </summary> |
| 67 | + /// <param name="ipEndPoint">The camera ip address and port.</param> |
| 68 | + /// <param name="createFile">Optional handler to override local file creation.</param> |
| 69 | + /// <param name="createClient">Optional handler to override http client creation.</param> |
| 70 | + public CameraClient(IPEndPoint ipEndPoint, CreateFileHandler createFile = null, HttpClientFactoryHandler createClient = null) : this(new Uri($"http://{ipEndPoint}"), createFile, createClient) |
| 71 | + { |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Initializes a new instance of the <see cref="CameraClient"/> class. |
| 76 | + /// </summary> |
| 77 | + /// <param name="endPoint">The camera http end point.</param> |
| 78 | + /// <param name="createFile">Optional handler to override local file creation.</param> |
| 79 | + /// <param name="createClient">Optional handler to override http client creation.</param> |
| 80 | + public CameraClient(Uri endPoint, CreateFileHandler createFile = null, HttpClientFactoryHandler createClient = null) |
| 81 | + { |
| 82 | + EndPoint = endPoint; |
| 83 | + CreateFile = createFile ?? DefaultCreateFile; |
| 84 | + CreateClient = createClient ?? DefaultCreateClient; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments