/// <summary> /// This will read into each data array and pull out a subarray based on the index value, and then encode those subarrays. /// </summary> /// <param name="azTempIdx">Azimuth temperature data array index that we are pulling from.</param> /// <param name="elTempIdx">Elevation temperature data array index that we are pulling from.</param> /// <param name="elEncIdx">Elevation encoder data array index that we are pulling from.</param> /// <param name="azEncIdx">Azimuth encoder data array index that we are pulling from.</param> /// <param name="elAccIdx">Elevation accelerometer data array index that we are pulling from.</param> /// <param name="azAccIdx">Azimuth accelerometer data array index that we are pulling from.</param> /// <param name="cbAccIdx">Counterbalance accelerometer data array index that we are pulling from.</param> /// <returns></returns> public SimulationSubArrayData BuildSubArrays(ref int?elTempIdx, ref int?azTempIdx, ref int?elEncIdx, ref int?azEncIdx, ref int?elAccIdx, ref int?azAccIdx, ref int?cbAccIdx) { SimulationSubArrayData subArrays = new SimulationSubArrayData(); // If the sensors are initialized, give them their subarrays, while also updating the index so that // this knows what subarrays to go to next if (ElevationTempData != null && elTempIdx != null) { subArrays.ElevationTemps = new double[1]; Array.Copy(ElevationTempData, elTempIdx ?? 0, subArrays.ElevationTemps, 0, 1); // Increment to next index, or back to 0 if we've reached the end of the main array if (elTempIdx + 1 > ElevationTempData.Length - 1) { elTempIdx = 0; } else { elTempIdx++; } } else { subArrays.ElevationTemps = new double[0]; } if (AzimuthTempData != null && azTempIdx != null) { subArrays.AzimuthTemps = new double[1]; Array.Copy(AzimuthTempData, azTempIdx ?? 0, subArrays.AzimuthTemps, 0, 1); // Increment to next index, or back to 0 if we've reached the end of the main array if (azTempIdx + 1 > AzimuthTempData.Length - 1) { azTempIdx = 0; } else { azTempIdx++; } } else { subArrays.AzimuthTemps = new double[0]; } if (ElevationEncoderData != null && elEncIdx != null) { subArrays.ElevationEnc = new double[1]; Array.Copy(ElevationEncoderData, elEncIdx ?? 0, subArrays.ElevationEnc, 0, 1); // Increment to next index, or back to 0 if we've reached the end of the main array if (elEncIdx + 1 > ElevationEncoderData.Length - 1) { elEncIdx = 0; } else { elEncIdx++; } } else { subArrays.ElevationEnc = new double[0]; } if (AzimuthEncoderData != null && azEncIdx != null) { subArrays.AzimuthEnc = new double[1]; Array.Copy(AzimuthEncoderData, azEncIdx ?? 0, subArrays.AzimuthEnc, 0, 1); // Increment to next index, or back to 0 if we've reached the end of the main array if (azEncIdx + 1 > AzimuthEncoderData.Length - 1) { azEncIdx = 0; } else { azEncIdx++; } } else { subArrays.AzimuthEnc = new double[0]; } if (AzimuthAccData != null && azAccIdx != null) { subArrays.AzimuthAccl = new RawAccelerometerData[100]; Array.Copy(AzimuthAccData, azAccIdx ?? 0, subArrays.AzimuthAccl, 0, 100); // Increment to next index, or back to 0 if we've reached the end of the main array if (azAccIdx + 199 > AzimuthAccData.Length - 1) { azAccIdx = 0; } else { azAccIdx += 100; } } else { subArrays.AzimuthAccl = new RawAccelerometerData[0]; } if (ElevationAccData != null && elAccIdx != null) { subArrays.ElevationAccl = new RawAccelerometerData[100]; Array.Copy(ElevationAccData, elAccIdx ?? 0, subArrays.ElevationAccl, 0, 100); // Increment to next index, or back to 0 if we've reached the end of the main array if (elAccIdx + 199 > ElevationAccData.Length - 1) { elAccIdx = 0; } else { elAccIdx += 100; } } else { subArrays.ElevationAccl = new RawAccelerometerData[0]; } if (CounterbalanceAccData != null && cbAccIdx != null) { subArrays.CounterBAccl = new RawAccelerometerData[100]; Array.Copy(CounterbalanceAccData, cbAccIdx ?? 0, subArrays.CounterBAccl, 0, 100); // Increment to next index, or back to 0 if we've reached the end of the main array if (cbAccIdx + 199 > CounterbalanceAccData.Length - 1) { cbAccIdx = 0; } else { cbAccIdx += 100; } } else { subArrays.CounterBAccl = new RawAccelerometerData[0]; } return(subArrays); }
private void SimulationSensorMonitor() { // First, we want to connect to the SensorNetworkServer if (CurrentlyRunning) { WaitForAndConnectToServer(); } // Next, we want to request initialization and receive it byte[] receivedInit = new byte[0]; if (CurrentlyRunning) { receivedInit = RequestAndAcquireSensorInitialization(); } // At this point, we have the initialization and can initialize the sensors if (CurrentlyRunning) { InitializeSensors(receivedInit); } // Now we can grab the CSV data for ONLY the initialized sensors... if (CurrentlyRunning) { ReadFakeDataFromCSV(); } // Keep track of the indexes for each data array, because we are only extracting a small subsection of each one. // We want to know what subsection we just got so we can get the next subsection in the next iteration int?elTempIdx = 0; int?azTempIdx = 0; int?elEncIdx = 0; int?azEncIdx = 0; int?elAccIdx = 0; int?azAccIdx = 0; int?cbAccIdx = 0; // This will tell us if we are rebooting or not. We will only reboot if the connection is randomly terminated. bool reboot = false; // Now we enter the "super loop" while (CurrentlyRunning) { // Convert subarrays to bytes SimulationSubArrayData subArrays = BuildSubArrays(ref elTempIdx, ref azTempIdx, ref elEncIdx, ref azEncIdx, ref elAccIdx, ref azAccIdx, ref cbAccIdx); SensorStatuses statuses = new SensorStatuses { // TODO: Write the values of each sensor status in here so it can get be encoded (issue #376) }; byte[] dataToSend = ConvertDataArraysToBytes( subArrays.ElevationAccl, subArrays.AzimuthAccl, subArrays.CounterBAccl, subArrays.ElevationTemps, subArrays.AzimuthTemps, subArrays.ElevationEnc, subArrays.AzimuthEnc, statuses ); // We have to check for CurrentlyRunning down here because we don't know when the connection is going to be terminated, and // it could very well be in the middle of the loop. if (CurrentlyRunning) { try { // Send arrays ClientStream.Write(dataToSend, 0, dataToSend.Length); Thread.Sleep(SensorNetworkConstants.DataSendingInterval); } // This will be reached if the connection is unexpectedly terminated (like it is during sensor reinitialization) catch { CurrentlyRunning = false; reboot = true; } } } // If the server disconnects, that triggers a reboot if (reboot) { CurrentlyRunning = true; Client.Dispose(); ClientStream.Dispose(); SimulationSensorMonitor(); } }