< Summary - ReFlex - Library

Information
Class: ReFlex.Sensor.EmulatorModule.EmulatorCamera
Assembly: ReFlex.Sensor.EmulatorModule
File(s): D:\a\reflex\reflex\library\src\Sensor\EmulatorModule\EmulatorCamera.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 77
Coverable lines: 77
Total lines: 173
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 18
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
get_ModelDescription()100%210%
get_CameraType()100%210%
get_State()0%2040%
get_StreamParameter()100%210%
.ctor()100%210%
.ctor(...)0%620%
Initialize()100%210%
EnableStream(...)100%210%
GetPossibleConfigurations()100%210%
StartStream()0%620%
StopStream()0%620%
InitServices()0%620%
InteractionsReceivedFromEmulatorInstance(...)0%2040%
OnServiceClosed(...)0%620%

File(s)

D:\a\reflex\reflex\library\src\Sensor\EmulatorModule\EmulatorCamera.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using NLog;
 4using Prise.Plugin;
 5using ReFlex.Core.Common.Components;
 6using ReFlex.Core.Common.Util;
 7using ReFlex.Core.Networking.Event;
 8using ReFlex.Core.Networking.Util;
 9using ReFlex.Core.Tracking.Interfaces;
 10using ReFlex.Core.Tracking.Util;
 11using WebSocketServer = ReFlex.Core.Networking.Components.WebSocketServer;
 12
 13namespace ReFlex.Sensor.EmulatorModule
 14{
 15    [Plugin(PluginType = typeof(IDepthCamera))]
 16    public class EmulatorCamera : WebSocketServer, IDepthCamera
 17    {
 18        #region Fields
 19
 020        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
 030        public string ModelDescription => $"Emulator [{Address}]";
 31
 032        public CameraType CameraType => CameraType.Emulator;
 33
 34        public DepthCameraState State
 35        {
 36            get
 037            {
 038                if  (!IsStarted)
 039                    return DepthCameraState.Disconnected;
 040                return IsStarted ? DepthCameraState.Streaming : DepthCameraState.Connected;
 041            }
 42        }
 43
 044        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
 058        public EmulatorCamera() : this("127.0.0.1",8080,"Emulator")
 059        {
 60
 061        }
 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>
 069        public EmulatorCamera(string address, int port, string endpoint) : base(address, port, endpoint)
 070        {
 071            _port = port;
 072            StateChanged?.Invoke(this, State);
 073        }
 74
 75        #endregion
 76
 77        /// <summary>
 78        ///
 79        /// </summary>
 80        public void Initialize()
 081        {
 82            // nothing to do here...
 083        }
 84
 85        /// <summary>
 86        /// initialize (fake) depth value
 87        /// </summary>
 88        /// <param name="parameter"></param>
 89        public void EnableStream(StreamParameter parameter)
 090        {
 091            var param = new EmulatorParameters
 092            {
 093                HeightInMeters = 1.6f,
 094                WidthInMeters = 2.4f,
 095                MaxDepthInMeter = 2.1f,
 096                MinDepthInMeter = 0.8f,
 097                PlaneDistanceInMeter = 1.45f,
 098                Radius = (int) (parameter.Width * 0.4)
 099            };
 100
 0101            StreamParameter = parameter;
 102
 0103            _pointCloud = new EmulatedPointCloud(param);
 0104            _pointCloud.InitializePointCloud(parameter);
 0105        }
 106
 107        public IList<StreamParameter> GetPossibleConfigurations()
 0108        {
 0109            var configs = new List<StreamParameter>
 0110            {
 0111                new StreamParameter(50, 50, 30),
 0112                new StreamParameter(64, 36, 30),
 0113                new StreamParameter(64, 48, 30),
 0114                new StreamParameter(100, 100, 30),
 0115                new StreamParameter(128, 72, 30),
 0116                new StreamParameter(128, 96, 30),
 0117                new StreamParameter(640, 480, 30),
 0118                new StreamParameter(800, 600, 30),
 0119                new StreamParameter(1920, 1080, 30),
 0120                new StreamParameter(2560, 1440, 30)
 0121            };
 122
 0123            return configs;
 0124        }
 125
 126        public void StartStream()
 0127        {
 0128            Start();
 0129            StateChanged?.Invoke(this, State);
 0130        }
 131
 132        public void StopStream()
 0133        {
 0134            Stop();
 0135            StateChanged?.Invoke(this, State);
 0136        }
 137
 138
 139        protected override void InitServices()
 0140        {
 0141            _service = new EmulatorService();
 142
 0143            Server?.AddWebSocketService(Endpoint, () => _service);
 144
 0145            _service.InteractionsReceived += InteractionsReceivedFromEmulatorInstance;
 146
 0147            _service.Closed += OnServiceClosed;
 148
 0149        }
 150
 151        private void InteractionsReceivedFromEmulatorInstance(object sender, InteractionsReceivedEventArgs e)
 0152        {
 0153            _pointCloud.Reset();
 0154            _pointCloud.UpdateFromInteractions(e.Interactions);
 0155            FrameReady?.Invoke(this, new DepthCameraFrame { Depth = _pointCloud.Points });
 0156            DepthImageReady?.Invoke(this, _pointCloud.GetUpdatedDepthImage());
 0157        }
 158
 159        private void OnServiceClosed(object sender, EventArgs args)
 0160        {
 0161            Logger.Warn($"[{GetType().Name}]: Handling Closed Event for WebsocketService. Restarting...");
 162
 0163            Stop();
 164
 0165            _service.Closed -= OnServiceClosed;
 0166            Server?.RemoveWebSocketService(Endpoint);
 167
 0168            Start();
 169
 0170            Logger.Info($"[{GetType().Name}]: Restarting Complete.");
 0171        }
 172    }
 173}