| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using NLog; |
| | | 4 | | using Prise.Plugin; |
| | | 5 | | using ReFlex.Core.Common.Components; |
| | | 6 | | using ReFlex.Core.Common.Util; |
| | | 7 | | using ReFlex.Core.Networking.Event; |
| | | 8 | | using ReFlex.Core.Networking.Util; |
| | | 9 | | using ReFlex.Core.Tracking.Interfaces; |
| | | 10 | | using ReFlex.Core.Tracking.Util; |
| | | 11 | | using WebSocketServer = ReFlex.Core.Networking.Components.WebSocketServer; |
| | | 12 | | |
| | | 13 | | namespace ReFlex.Sensor.EmulatorModule |
| | | 14 | | { |
| | | 15 | | [Plugin(PluginType = typeof(IDepthCamera))] |
| | | 16 | | public class EmulatorCamera : WebSocketServer, IDepthCamera |
| | | 17 | | { |
| | | 18 | | #region Fields |
| | | 19 | | |
| | 0 | 20 | | private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); |
| | | 21 | | |
| | | 22 | | private EmulatedPointCloud _pointCloud; |
| | | 23 | | private readonly int _port; |
| | | 24 | | private EmulatorService _service; |
| | | 25 | | |
| | | 26 | | #endregion |
| | | 27 | | |
| | | 28 | | #region Properties |
| | | 29 | | |
| | 0 | 30 | | public string ModelDescription => $"Emulator [{Address}]"; |
| | | 31 | | |
| | 0 | 32 | | public CameraType CameraType => CameraType.Emulator; |
| | | 33 | | |
| | | 34 | | public DepthCameraState State |
| | | 35 | | { |
| | | 36 | | get |
| | 0 | 37 | | { |
| | 0 | 38 | | if (!IsStarted) |
| | 0 | 39 | | return DepthCameraState.Disconnected; |
| | 0 | 40 | | return IsStarted ? DepthCameraState.Streaming : DepthCameraState.Connected; |
| | 0 | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | public StreamParameter StreamParameter { get; private set; } |
| | | 45 | | |
| | | 46 | | #endregion |
| | | 47 | | |
| | | 48 | | #region Events |
| | | 49 | | |
| | | 50 | | public event EventHandler<DepthCameraState> StateChanged; |
| | | 51 | | public event EventHandler<DepthCameraFrame> FrameReady; |
| | | 52 | | public event EventHandler<ImageByteArray> DepthImageReady; |
| | | 53 | | |
| | | 54 | | #endregion |
| | | 55 | | |
| | | 56 | | #region Constructor |
| | | 57 | | |
| | 0 | 58 | | public EmulatorCamera() : this("127.0.0.1",8080,"Emulator") |
| | 0 | 59 | | { |
| | | 60 | | |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// creates the <see cref="ReFlex.Core.Networking.Components.WebSocketClient"/> |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="address">adress to which the emulator sends its websocket events</param> |
| | | 67 | | /// <param name="port">the port on which they communicate</param> |
| | | 68 | | /// <param name="endpoint">endpoint of the eumlator</param> |
| | 0 | 69 | | public EmulatorCamera(string address, int port, string endpoint) : base(address, port, endpoint) |
| | 0 | 70 | | { |
| | 0 | 71 | | _port = port; |
| | 0 | 72 | | StateChanged?.Invoke(this, State); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | #endregion |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// |
| | | 79 | | /// </summary> |
| | | 80 | | public void Initialize() |
| | 0 | 81 | | { |
| | | 82 | | // nothing to do here... |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// initialize (fake) depth value |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="parameter"></param> |
| | | 89 | | public void EnableStream(StreamParameter parameter) |
| | 0 | 90 | | { |
| | 0 | 91 | | var param = new EmulatorParameters |
| | 0 | 92 | | { |
| | 0 | 93 | | HeightInMeters = 1.6f, |
| | 0 | 94 | | WidthInMeters = 2.4f, |
| | 0 | 95 | | MaxDepthInMeter = 2.1f, |
| | 0 | 96 | | MinDepthInMeter = 0.8f, |
| | 0 | 97 | | PlaneDistanceInMeter = 1.45f, |
| | 0 | 98 | | Radius = (int) (parameter.Width * 0.4) |
| | 0 | 99 | | }; |
| | | 100 | | |
| | 0 | 101 | | StreamParameter = parameter; |
| | | 102 | | |
| | 0 | 103 | | _pointCloud = new EmulatedPointCloud(param); |
| | 0 | 104 | | _pointCloud.InitializePointCloud(parameter); |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | public IList<StreamParameter> GetPossibleConfigurations() |
| | 0 | 108 | | { |
| | 0 | 109 | | var configs = new List<StreamParameter> |
| | 0 | 110 | | { |
| | 0 | 111 | | new StreamParameter(50, 50, 30), |
| | 0 | 112 | | new StreamParameter(64, 36, 30), |
| | 0 | 113 | | new StreamParameter(64, 48, 30), |
| | 0 | 114 | | new StreamParameter(100, 100, 30), |
| | 0 | 115 | | new StreamParameter(128, 72, 30), |
| | 0 | 116 | | new StreamParameter(128, 96, 30), |
| | 0 | 117 | | new StreamParameter(640, 480, 30), |
| | 0 | 118 | | new StreamParameter(800, 600, 30), |
| | 0 | 119 | | new StreamParameter(1920, 1080, 30), |
| | 0 | 120 | | new StreamParameter(2560, 1440, 30) |
| | 0 | 121 | | }; |
| | | 122 | | |
| | 0 | 123 | | return configs; |
| | 0 | 124 | | } |
| | | 125 | | |
| | | 126 | | public void StartStream() |
| | 0 | 127 | | { |
| | 0 | 128 | | Start(); |
| | 0 | 129 | | StateChanged?.Invoke(this, State); |
| | 0 | 130 | | } |
| | | 131 | | |
| | | 132 | | public void StopStream() |
| | 0 | 133 | | { |
| | 0 | 134 | | Stop(); |
| | 0 | 135 | | StateChanged?.Invoke(this, State); |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | |
| | | 139 | | protected override void InitServices() |
| | 0 | 140 | | { |
| | 0 | 141 | | _service = new EmulatorService(); |
| | | 142 | | |
| | 0 | 143 | | Server?.AddWebSocketService(Endpoint, () => _service); |
| | | 144 | | |
| | 0 | 145 | | _service.InteractionsReceived += InteractionsReceivedFromEmulatorInstance; |
| | | 146 | | |
| | 0 | 147 | | _service.Closed += OnServiceClosed; |
| | | 148 | | |
| | 0 | 149 | | } |
| | | 150 | | |
| | | 151 | | private void InteractionsReceivedFromEmulatorInstance(object sender, InteractionsReceivedEventArgs e) |
| | 0 | 152 | | { |
| | 0 | 153 | | _pointCloud.Reset(); |
| | 0 | 154 | | _pointCloud.UpdateFromInteractions(e.Interactions); |
| | 0 | 155 | | FrameReady?.Invoke(this, new DepthCameraFrame { Depth = _pointCloud.Points }); |
| | 0 | 156 | | DepthImageReady?.Invoke(this, _pointCloud.GetUpdatedDepthImage()); |
| | 0 | 157 | | } |
| | | 158 | | |
| | | 159 | | private void OnServiceClosed(object sender, EventArgs args) |
| | 0 | 160 | | { |
| | 0 | 161 | | Logger.Warn($"[{GetType().Name}]: Handling Closed Event for WebsocketService. Restarting..."); |
| | | 162 | | |
| | 0 | 163 | | Stop(); |
| | | 164 | | |
| | 0 | 165 | | _service.Closed -= OnServiceClosed; |
| | 0 | 166 | | Server?.RemoveWebSocketService(Endpoint); |
| | | 167 | | |
| | 0 | 168 | | Start(); |
| | | 169 | | |
| | 0 | 170 | | Logger.Info($"[{GetType().Name}]: Restarting Complete."); |
| | 0 | 171 | | } |
| | | 172 | | } |
| | | 173 | | } |