|
| 1 | +# DAQiFi Device Interfaces |
| 2 | + |
| 3 | +This document provides examples and usage guidance for the DAQiFi device interfaces that were migrated from `daqifi-desktop` to establish the foundation for device interaction in the `daqifi-core` library. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The device interfaces provide a consistent API for discovering, connecting to, and communicating with DAQiFi hardware devices. They abstract away hardware implementation details and provide a clean interface for application developers. |
| 8 | + |
| 9 | +## Core Interfaces |
| 10 | + |
| 11 | +### IDevice |
| 12 | +The base interface for all DAQiFi devices, providing fundamental connection and communication capabilities. |
| 13 | + |
| 14 | +### IStreamingDevice |
| 15 | +Extends `IDevice` with data streaming functionality for devices that support continuous data acquisition. |
| 16 | + |
| 17 | +## Implementation Classes |
| 18 | + |
| 19 | +### DaqifiDevice |
| 20 | +The base implementation of `IDevice` that provides core device functionality. |
| 21 | + |
| 22 | +### DaqifiStreamingDevice |
| 23 | +Extends `DaqifiDevice` with streaming capabilities, implementing `IStreamingDevice`. |
| 24 | + |
| 25 | +## Usage Examples |
| 26 | + |
| 27 | +### Basic Device Connection |
| 28 | + |
| 29 | +```csharp |
| 30 | +using Daqifi.Core.Device; |
| 31 | +using System.Net; |
| 32 | + |
| 33 | +// Create a device instance |
| 34 | +var device = new DaqifiDevice("My DAQiFi Device", IPAddress.Parse("192.168.1.100")); |
| 35 | + |
| 36 | +// Subscribe to status changes |
| 37 | +device.StatusChanged += (sender, args) => |
| 38 | +{ |
| 39 | + Console.WriteLine($"Device status changed to: {args.Status}"); |
| 40 | +}; |
| 41 | + |
| 42 | +// Subscribe to messages |
| 43 | +device.MessageReceived += (sender, args) => |
| 44 | +{ |
| 45 | + Console.WriteLine($"Message received: {args.Message.Data}"); |
| 46 | +}; |
| 47 | + |
| 48 | +// Connect to the device |
| 49 | +device.Connect(); |
| 50 | + |
| 51 | +// Send a message |
| 52 | +device.Send(ScpiMessageProducer.GetDeviceInfo); |
| 53 | + |
| 54 | +// Disconnect when done |
| 55 | +device.Disconnect(); |
| 56 | +``` |
| 57 | + |
| 58 | +### Streaming Device Usage |
| 59 | + |
| 60 | +```csharp |
| 61 | +using Daqifi.Core.Device; |
| 62 | +using Daqifi.Core.Communication.Producers; |
| 63 | +using System.Net; |
| 64 | + |
| 65 | +// Create a streaming device |
| 66 | +var streamingDevice = new DaqifiStreamingDevice("My Streaming Device", IPAddress.Parse("192.168.1.100")); |
| 67 | + |
| 68 | +// Configure streaming frequency |
| 69 | +streamingDevice.StreamingFrequency = 1000; // 1000 Hz |
| 70 | +
|
| 71 | +// Connect to the device |
| 72 | +streamingDevice.Connect(); |
| 73 | + |
| 74 | +// Start streaming data |
| 75 | +streamingDevice.StartStreaming(); |
| 76 | +Console.WriteLine($"Started streaming at {streamingDevice.StreamingFrequency} Hz"); |
| 77 | + |
| 78 | +// Stream for some time... |
| 79 | +await Task.Delay(TimeSpan.FromSeconds(10)); |
| 80 | + |
| 81 | +// Stop streaming |
| 82 | +streamingDevice.StopStreaming(); |
| 83 | +Console.WriteLine("Stopped streaming"); |
| 84 | + |
| 85 | +// Disconnect |
| 86 | +streamingDevice.Disconnect(); |
| 87 | +``` |
| 88 | + |
| 89 | +### Error Handling |
| 90 | + |
| 91 | +```csharp |
| 92 | +try |
| 93 | +{ |
| 94 | + var device = new DaqifiDevice("Test Device"); |
| 95 | + |
| 96 | + // This will throw an exception because device is not connected |
| 97 | + device.Send(ScpiMessageProducer.GetDeviceInfo); |
| 98 | +} |
| 99 | +catch (InvalidOperationException ex) |
| 100 | +{ |
| 101 | + Console.WriteLine($"Error: {ex.Message}"); |
| 102 | + // Output: Error: Device is not connected. |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +### Custom Device Implementation |
| 107 | + |
| 108 | +```csharp |
| 109 | +using Daqifi.Core.Device; |
| 110 | +using Daqifi.Core.Communication.Messages; |
| 111 | + |
| 112 | +public class CustomDaqifiDevice : DaqifiDevice |
| 113 | +{ |
| 114 | + public CustomDaqifiDevice(string name, IPAddress? ipAddress = null) |
| 115 | + : base(name, ipAddress) |
| 116 | + { |
| 117 | + } |
| 118 | + |
| 119 | + public override void Send<T>(IOutboundMessage<T> message) |
| 120 | + { |
| 121 | + // Add custom logging |
| 122 | + Console.WriteLine($"Sending message: {message.Data}"); |
| 123 | + |
| 124 | + // Call base implementation |
| 125 | + base.Send(message); |
| 126 | + } |
| 127 | + |
| 128 | + protected override void OnMessageReceived(IInboundMessage<object> message) |
| 129 | + { |
| 130 | + // Add custom message processing |
| 131 | + Console.WriteLine($"Processing received message: {message.Data}"); |
| 132 | + |
| 133 | + // Call base implementation to fire events |
| 134 | + base.OnMessageReceived(message); |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## Connection Status Monitoring |
| 140 | + |
| 141 | +```csharp |
| 142 | +var device = new DaqifiDevice("Monitor Device"); |
| 143 | + |
| 144 | +device.StatusChanged += (sender, args) => |
| 145 | +{ |
| 146 | + switch (args.Status) |
| 147 | + { |
| 148 | + case ConnectionStatus.Disconnected: |
| 149 | + Console.WriteLine("Device disconnected"); |
| 150 | + break; |
| 151 | + case ConnectionStatus.Connecting: |
| 152 | + Console.WriteLine("Connecting to device..."); |
| 153 | + break; |
| 154 | + case ConnectionStatus.Connected: |
| 155 | + Console.WriteLine("Device connected successfully"); |
| 156 | + break; |
| 157 | + } |
| 158 | +}; |
| 159 | + |
| 160 | +// Check connection status |
| 161 | +if (device.IsConnected) |
| 162 | +{ |
| 163 | + Console.WriteLine("Device is currently connected"); |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +## Message Types |
| 168 | + |
| 169 | +The device interfaces work with strongly-typed messages: |
| 170 | + |
| 171 | +```csharp |
| 172 | +// String-based SCPI messages |
| 173 | +device.Send(ScpiMessageProducer.GetDeviceInfo); |
| 174 | +device.Send(ScpiMessageProducer.StartStreaming(1000)); |
| 175 | + |
| 176 | +// Custom message types can be created |
| 177 | +public class CustomMessage : IOutboundMessage<MyDataType> |
| 178 | +{ |
| 179 | + public MyDataType Data { get; set; } |
| 180 | + |
| 181 | + public byte[] GetBytes() |
| 182 | + { |
| 183 | + // Custom serialization logic |
| 184 | + return Encoding.UTF8.GetBytes(Data.ToString()); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +device.Send(new CustomMessage { Data = myData }); |
| 189 | +``` |
| 190 | + |
| 191 | +## Thread Safety |
| 192 | + |
| 193 | +The device implementations are not thread-safe by default. For multi-threaded scenarios, ensure proper synchronization: |
| 194 | + |
| 195 | +```csharp |
| 196 | +private readonly object _deviceLock = new object(); |
| 197 | + |
| 198 | +public void SafeSendMessage(IOutboundMessage<string> message) |
| 199 | +{ |
| 200 | + lock (_deviceLock) |
| 201 | + { |
| 202 | + if (device.IsConnected) |
| 203 | + { |
| 204 | + device.Send(message); |
| 205 | + } |
| 206 | + } |
| 207 | +} |
| 208 | +``` |
| 209 | + |
| 210 | +## Integration with Application |
| 211 | + |
| 212 | +```csharp |
| 213 | +public class DeviceManager |
| 214 | +{ |
| 215 | + private readonly List<IDevice> _devices = new(); |
| 216 | + |
| 217 | + public void AddDevice(IDevice device) |
| 218 | + { |
| 219 | + device.StatusChanged += OnDeviceStatusChanged; |
| 220 | + device.MessageReceived += OnDeviceMessageReceived; |
| 221 | + _devices.Add(device); |
| 222 | + } |
| 223 | + |
| 224 | + public async Task ConnectAllDevicesAsync() |
| 225 | + { |
| 226 | + foreach (var device in _devices) |
| 227 | + { |
| 228 | + device.Connect(); |
| 229 | + // Add delay or wait for connection status |
| 230 | + await Task.Delay(1000); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + private void OnDeviceStatusChanged(object sender, DeviceStatusEventArgs e) |
| 235 | + { |
| 236 | + var device = (IDevice)sender; |
| 237 | + Console.WriteLine($"Device '{device.Name}' status: {e.Status}"); |
| 238 | + } |
| 239 | + |
| 240 | + private void OnDeviceMessageReceived(object sender, MessageReceivedEventArgs e) |
| 241 | + { |
| 242 | + var device = (IDevice)sender; |
| 243 | + Console.WriteLine($"Device '{device.Name}' received: {e.Message.Data}"); |
| 244 | + } |
| 245 | +} |
| 246 | +``` |
| 247 | + |
| 248 | +## Features |
| 249 | + |
| 250 | +- **Clean Abstraction**: Hardware details are hidden behind well-defined interfaces |
| 251 | +- **Event-Driven**: Status changes and message reception are handled via events |
| 252 | +- **Type Safety**: Generic message types provide compile-time safety |
| 253 | +- **Extensible**: Interfaces can be implemented for custom device behaviors |
| 254 | +- **Modern C#**: Uses nullable reference types and modern C# features |
| 255 | +- **Cross-Platform**: Compatible with .NET 8.0 and 9.0 |
| 256 | + |
| 257 | +## Next Steps |
| 258 | + |
| 259 | +This interface foundation enables: |
| 260 | +- Device discovery services |
| 261 | +- Connection management |
| 262 | +- Message consumer implementation |
| 263 | +- Channel configuration |
| 264 | +- Protocol-specific implementations (TCP, UDP, Serial, etc.) |
0 commit comments