/// <summary> /// This ends the SensorMonitoringRoutine. This should only be executed when "Shutdown RT" is clicked. /// </summary> /// <param name="rebooting">This will tell the function if we are rebooting, or shutting down indefinitely.</param> /// <returns>If ended successfully, return true. Else, return false.</returns> public void EndSensorMonitoringRoutine(bool rebooting = false) { CurrentlyRunning = false; // The stream will only be null if the sensor monitoring thread has not been called if (Stream != null) { Stream.Close(); Stream.Dispose(); } Server.Stop(); if (Timeout.Enabled) { Timeout.Stop(); } SensorMonitoringThread.Join(); if (!rebooting) { // We want to keep using the timer if we are rebooting, not destroy it. Status = SensorNetworkStatusEnum.None; Timeout.Dispose(); if (SimulationSensorNetwork != null) { SimulationSensorNetwork.EndSimulationSensorNetwork(); } } else { Status = SensorNetworkStatusEnum.Rebooting; SensorMonitoringThread = new Thread(() => { SensorMonitoringRoutine(); }); } }
/// <summary> /// Constructor used to set the values needed to initialize the components of the class. It will not start waiting for data until /// StartSensorMonitoringRoutine() is called. /// /// The reason the client IP address is a string and not an IPAddress is because none of the TCPClient class's /// constructors take an IPAddress. :( /// </summary> /// <param name="serverIPAddress">This is the IP address the SensorNetworkServer is "listening" to.</param> /// <param name="serverPort">This is the port the SensorNetworkServer is "listening" to.</param> /// <param name="clientIPAddress">This is the IP address of the SensorNetwork that we will be sending the sensor initialization to.</param> /// <param name="clientPort">This is the port of the SensorNetwork that we will be sending the sensor initialization to.</param> /// <param name="telescopeId">The Radio Telescope that the SensorNetworkConfig will apply to.</param> /// <param name="isSimulation">Tells the SensorNetworkServer if it should initialize the SimulationSensorNetwork, /// or if it is connecting to the production hardware (or maybe an outside simulation).</param> public SensorNetworkServer(IPAddress serverIPAddress, int serverPort, string clientIPAddress, int clientPort, int telescopeId, bool isSimulation) { // Initialize main parts of the Sensor Network Server = new TcpListener(serverIPAddress, serverPort); InitializationClient = new SensorNetworkClient(clientIPAddress, clientPort, telescopeId); // Sensor data initialization CurrentElevationMotorTemp = new Temperature[1]; CurrentElevationMotorTemp[0] = new Temperature(); CurrentAzimuthMotorTemp = new Temperature[1]; CurrentAzimuthMotorTemp[0] = new Temperature(); CurrentAbsoluteOrientation = new Orientation(); CurrentElevationMotorAccl = new Acceleration[1]; CurrentElevationMotorAccl[0] = new Acceleration(); CurrentAzimuthMotorAccl = new Acceleration[1]; CurrentAzimuthMotorAccl[0] = new Acceleration(); CurrentCounterbalanceAccl = new Acceleration[1]; CurrentCounterbalanceAccl[0] = new Acceleration(); AbsoluteOrientationOffset = new Orientation(); // Sensor error initialization SensorStatuses = new SensorStatuses(); SensorStatuses.AzimuthAbsoluteEncoderStatus = SensorNetworkSensorStatus.Okay; SensorStatuses.ElevationAbsoluteEncoderStatus = SensorNetworkSensorStatus.Okay; SensorStatuses.AzimuthTemperature1Status = SensorNetworkSensorStatus.Okay; SensorStatuses.AzimuthTemperature2Status = SensorNetworkSensorStatus.Okay; SensorStatuses.ElevationTemperature1Status = SensorNetworkSensorStatus.Okay; SensorStatuses.ElevationTemperature2Status = SensorNetworkSensorStatus.Okay; SensorStatuses.AzimuthAccelerometerStatus = SensorNetworkSensorStatus.Okay; SensorStatuses.ElevationAccelerometerStatus = SensorNetworkSensorStatus.Okay; SensorStatuses.CounterbalanceAccelerometerStatus = SensorNetworkSensorStatus.Okay; // Initialize threads and additional processes, if applicable SensorMonitoringThread = new Thread(() => { SensorMonitoringRoutine(); }); SensorMonitoringThread.Name = "SensorMonitorThread"; // We only want to run the internal simulation if the user selected to run the Simulated Sensor Network if (isSimulation) { SimulationSensorNetwork = new SimulationSensorNetwork(serverIPAddress.ToString(), serverPort, IPAddress.Parse(clientIPAddress), clientPort); } else { SimulationSensorNetwork = null; } // Initialize the timeout timer but don't start it yet Timeout = new System.Timers.Timer(); Timeout.Elapsed += TimedOut; // TimedOut is the function at the bottom that executes when this elapses Timeout.AutoReset = false; AccBlob = new AccelerationBlob(); }
/// <summary> /// This starts the SensorMonitoringRoutine. Calling this will immediately begin initialization. /// </summary> /// <returns>If started successfully, return true. Else, return false.</returns> public void StartSensorMonitoringRoutine(bool rebooting = false) { Server.Start(); CurrentlyRunning = true; Status = SensorNetworkStatusEnum.Initializing; Timeout.Interval = InitializationClient.SensorNetworkConfig.TimeoutInitialization; Timeout.Start(); SensorMonitoringThread.Start(); if (SimulationSensorNetwork != null && !rebooting) { SimulationSensorNetwork.StartSimulationSensorNetwork(); } }