Example #1
0
 public CompanyAssistantController()
 {
     SessionModel.Initialize();
     client = new InitializationClient(SessionModel.AuthorizationHeader, ClientConfiguration.GetAdapterFromConfig());
 }
Example #2
0
        /// <summary>
        /// This is used internally by the SensorNetworkServer to tell what kind of data the Sensor Network
        /// is sending. It may send data, in which case a transit ID will be present, or it may ask for
        /// an initialization.
        /// </summary>
        /// <param name="data">The data we are interpreting.</param>
        /// <param name="receivedDataSize">This will be used to tell if our data is complete.</param>
        private bool InterpretData(byte[] data, int receivedDataSize)
        {
            bool success = false;

            if (Encoding.ASCII.GetString(data, 0, receivedDataSize).Equals("Send Sensor Configuration"))
            { // Reaching here means that we've received a request for sensor initialization
                Status = SensorNetworkStatusEnum.Initializing;

                // If the init sending fails, set status to reflect that
                if (!InitializationClient.SendSensorInitialization())
                {
                    Status = SensorNetworkStatusEnum.InitializationSendingFailed;
                    if (Timeout.Enabled)
                    {
                        Timeout.Stop();
                    }

                    pushNotification.sendToAllAdmins("Sensor Network Error", $"Status: {Status}");
                }
                else
                {
                    logger.Info($"{Utilities.GetTimeStamp()}: Successfully sent sensor initialization to the Sensor Network.");
                    success = true;
                }
            }
            else
            {
                Status = SensorNetworkStatusEnum.ReceivingData;

                // Bytes 1-3 of the data contain the overall size of the packet
                UInt32 expectedDataSize = (UInt32)(data[1] << 24 | data[2] << 16 | data[3] << 8 | data[4]);

                // Check that the data we received is the same size as what we expect
                if (expectedDataSize == receivedDataSize)
                {
                    // Byte 0 contains the transmit ID
                    int receivedTransitId = data[0];

                    // Verify that the first byte contains the "success" code (transit ID)
                    if (receivedTransitId == SensorNetworkConstants.TransitIdSuccess)
                    {
                        // At this point, we may begin parsing the data

                        // Sensor statuses and error codes
                        BitArray sensorStatus = new BitArray(new byte[] { data[5] });             // sensor statuses
                        UInt32   sensorErrors = (UInt32)(data[6] << 16 | data[7] << 8 | data[8]); // sensor self-tests | adxl error codes and azimuth encoder error code | temp sensor error codes

                        // Acquire the sample sizes for each sensor
                        UInt16 elAcclSize       = (UInt16)(data[9] << 8 | data[10]);
                        UInt16 azAcclSize       = (UInt16)(data[11] << 8 | data[12]);
                        UInt16 cbAcclSize       = (UInt16)(data[13] << 8 | data[14]);
                        UInt16 elTempSensorSize = (UInt16)(data[15] << 8 | data[16]);
                        UInt16 azTempSensorSize = (UInt16)(data[17] << 8 | data[18]);
                        UInt16 elEncoderSize    = (UInt16)(data[19] << 8 | data[20]);
                        UInt16 azEncoderSize    = (UInt16)(data[21] << 8 | data[22]);

                        // TODO: Outside of right here, we aren't doing anything with the sensor statuses. These should
                        // be updated along with the sensor data on the diagnostics form. How this looks is up to you. (issue #353)
                        SensorStatuses = ParseSensorStatuses(sensorStatus, sensorErrors);

                        // This is the index we start reading sensor data
                        int k = 23;

                        // If no data comes through for a sensor (i.e. the size is 0), then it will not be updated,
                        // otherwise the UI value would temporarily be set to 0, which would be inaccurate

                        // Accelerometer 1 (elevation)
                        if (elAcclSize > 0)
                        {
                            //Create array of acceleration objects
                            CurrentElevationMotorAccl = GetAccelerationFromBytes(ref k, data, elAcclSize, SensorLocationEnum.EL_MOTOR);

                            //add to the Elevation Acceleration Blob
                            ElevationAccBlob.BuildAccelerationBlob(CurrentElevationMotorAccl);
                        }

                        // Accelerometer 2 (azimuth)
                        if (azAcclSize > 0)
                        {
                            CurrentAzimuthMotorAccl = GetAccelerationFromBytes(ref k, data, azAcclSize, SensorLocationEnum.AZ_MOTOR);

                            //add to the Azimuth Acceleration Blob
                            AzimuthAccBlob.BuildAccelerationBlob(CurrentAzimuthMotorAccl);
                        }

                        // Accelerometer 3 (counterbalance)
                        if (cbAcclSize > 0)
                        {
                            CurrentCounterbalanceAccl = GetAccelerationFromBytes(ref k, data, cbAcclSize, SensorLocationEnum.COUNTERBALANCE);

                            //add to the Counterbalance Acceleration Blob
                            CounterbalanceAccBlob.BuildAccelerationBlob(CurrentCounterbalanceAccl);

                            // If there is new counterbalance accelerometer data, update the elevation position
                            UpdateCBAccelElevationPosition();
                        }

                        // Elevation temperature
                        if (elTempSensorSize > 0)
                        {
                            CurrentElevationMotorTemp = GetTemperatureFromBytes(ref k, data, elTempSensorSize, SensorLocationEnum.EL_MOTOR);
                            Database.DatabaseOperations.AddSensorData(CurrentElevationMotorTemp);
                        }

                        // Azimuth temperature
                        if (azTempSensorSize > 0)
                        {
                            CurrentAzimuthMotorTemp = GetTemperatureFromBytes(ref k, data, azTempSensorSize, SensorLocationEnum.AZ_MOTOR);
                            Database.DatabaseOperations.AddSensorData(CurrentAzimuthMotorTemp);
                        }

                        // Elevation absolute encoder
                        if (elEncoderSize > 0)
                        {
                            CurrentAbsoluteOrientation.Elevation = GetElevationAxisPositionFromBytes(ref k, data, AbsoluteOrientationOffset.Elevation, CurrentAbsoluteOrientation.Elevation);
                        }

                        // Azimuth absolute encoder
                        if (azEncoderSize > 0)
                        {
                            CurrentAbsoluteOrientation.Azimuth = GetAzimuthAxisPositionFromBytes(ref k, data, AbsoluteOrientationOffset.Azimuth, CurrentAbsoluteOrientation.Azimuth);
                        }

                        success = true;
                    }

                    // This may be replaced with different errors at some point (that have different transit IDs),
                    // though there are currently no plans for this. Right now, it is treated as an error overall:
                    // We should NOT be receiving anything other than TransitIdSuccess.
                    else
                    {
                        if (Status != SensorNetworkStatusEnum.TransitIdError)
                        {
                            logger.Error($"{Utilities.GetTimeStamp()}: Transit ID error: Expected " +
                                         $"ID {SensorNetworkConstants.TransitIdSuccess}, received ID {receivedTransitId})");

                            Status = SensorNetworkStatusEnum.TransitIdError;
                        }
                    }
                }
            }

            return(success);
        }