Exemplo n.º 1
0
        public ExecuteTCPCommandResult ExecuteRLCommand(String[] splitCommandString)
        {
            // Convert version from string to double. This is the first value in our string before the "|" character.
            // From here we will direct to the appropriate parsing for said version
            double version = 0.0;

            try
            {
                version = Double.Parse(splitCommandString[TCPCommunicationConstants.VERSION_NUM]);
            }
            catch (Exception e)
            {
                String errMessage = TCPCommunicationConstants.VERSION_CONVERSION_ERR + e.Message;
                return(new ExecuteTCPCommandResult(MovementResult.InvalidCommand, errMessage));
            }

            // Use appropriate parsing for given version
            if (version == 1.0)
            {
                // command is placed after pike and before colon; get it here
                // <VERSION> | <COMMANDTYPE> | <NAME<VALUES>> | TIME
                string command = splitCommandString[TCPCommunicationConstants.COMMAND_TYPE];

                if (command == "ORIENTATION_MOVE")
                {
                    // we have a move command coming in
                    rtController.ExecuteRadioTelescopeControlledStop(MovementPriority.GeneralStop);

                    // get azimuth and orientation
                    double azimuth   = 0.0;
                    double elevation = 0.0;
                    try
                    {
                        azimuth   = Convert.ToDouble(splitCommandString[TCPCommunicationConstants.ORIENTATION_MOVE_AZ]);
                        elevation = Convert.ToDouble(splitCommandString[TCPCommunicationConstants.ORIENTATION_MOVE_EL]);
                    }
                    catch (Exception e)
                    {
                        String errMessage = TCPCommunicationConstants.AZ_EL_CONVERSION_ERR + e.Message;
                        return(new ExecuteTCPCommandResult(MovementResult.InvalidCommand, errMessage));
                    }

                    logger.Debug(Utilities.GetTimeStamp() + ": Azimuth " + azimuth);
                    logger.Debug(Utilities.GetTimeStamp() + ": Elevation " + elevation);

                    Orientation movingTo = new Orientation(azimuth, elevation);

                    // check result of movement, if it fails we return the result type along with an error message.
                    // The command parse was successful however, so we indicate that
                    MovementResult result = rtController.MoveRadioTelescopeToOrientation(movingTo, MovementPriority.Manual);
                    if (result != MovementResult.Success)
                    {
                        return(new ExecuteTCPCommandResult(result, TCPCommunicationConstants.ORIENTATION_MOVE_ERR + result.ToString()));
                    }
                    else
                    // everything was successful
                    {
                        return(new ExecuteTCPCommandResult(result));
                    }
                }
                else if (command == "RELATIVE_MOVE")
                {
                    // we have a relative movement command
                    rtController.ExecuteRadioTelescopeControlledStop(MovementPriority.GeneralStop);

                    // get azimuth and orientation
                    double azimuth   = 0.0;
                    double elevation = 0.0;

                    try
                    {
                        azimuth   = Convert.ToDouble(splitCommandString[TCPCommunicationConstants.RELATIVE_MOVE_AZ]);
                        elevation = Convert.ToDouble(splitCommandString[TCPCommunicationConstants.RELATIVE_MOVE_EL]);
                    }
                    catch (Exception e)
                    {
                        String errMessage = TCPCommunicationConstants.AZ_EL_CONVERSION_ERR + e.Message;
                        return(new ExecuteTCPCommandResult(MovementResult.InvalidCommand, errMessage));
                    }

                    logger.Debug(Utilities.GetTimeStamp() + ": Azimuth " + azimuth);
                    logger.Debug(Utilities.GetTimeStamp() + ": Elevation " + elevation);

                    Orientation movingBy = new Orientation(azimuth, elevation);

                    MovementResult result = rtController.MoveRadioTelescopeByXDegrees(movingBy, MovementPriority.Manual);
                    if (result != MovementResult.Success)
                    {
                        // the actual movement failed
                        return(new ExecuteTCPCommandResult(result, TCPCommunicationConstants.RELATIVE_MOVE_ERR + result.ToString()));
                    }
                    else
                    {
                        // everything was successful
                        return(new ExecuteTCPCommandResult(result));
                    }
                }
                else if (command == "SET_OVERRIDE")
                {
                    string sensorToOverride = splitCommandString[TCPCommunicationConstants.SENSOR_TO_OVERRIDE];
                    bool   doOverride       = splitCommandString[TCPCommunicationConstants.DO_OVERRIDE] == "TRUE" ? true : false;
                    switch (sensorToOverride)
                    {
                    // Non-PLC Overrides
                    case "WEATHER_STATION":
                        controlRoom.weatherStationOverride = doOverride;
                        rtController.setOverride("weather station", doOverride);
                        DatabaseOperations.SetOverrideForSensor(SensorItemEnum.WEATHER_STATION, doOverride);
                        break;

                    // PLC Overrides
                    case "MAIN_GATE":
                        rtController.setOverride("main gate", doOverride);
                        break;

                    // Proximity overrides
                    case "ELEVATION_LIMIT_0":
                        rtController.setOverride("elevation proximity (1)", doOverride);
                        break;

                    case "ELEVATION_LIMIT_90":
                        rtController.setOverride("elevation proximity (2)", doOverride);
                        break;

                    // Sensor network overrides
                    case "AZ_ABS_ENC":
                        rtController.setOverride("azimuth absolute encoder", doOverride);
                        break;

                    case "EL_ABS_ENC":
                        rtController.setOverride("elevation absolute encoder", doOverride);
                        break;

                    case "AZ_ACC":
                        rtController.setOverride("azimuth motor accelerometer", doOverride);
                        break;

                    case "EL_ACC":
                        rtController.setOverride("elevation motor accelerometer", doOverride);
                        break;

                    case "CB_ACC":
                        rtController.setOverride("counterbalance accelerometer", doOverride);
                        break;

                    case "AZIMUTH_MOT_TEMP":
                        rtController.setOverride("azimuth motor temperature", doOverride);
                        break;

                    case "ELEVATION_MOT_TEMP":
                        rtController.setOverride("elevation motor temperature", doOverride);
                        break;

                    // If no case is reached, the sensor is not valid. Return appropriately
                    default:
                        return(new ExecuteTCPCommandResult(MovementResult.InvalidCommand, TCPCommunicationConstants.INVALID_SENSOR_OVERRIDE + sensorToOverride));
                    }

                    return(new ExecuteTCPCommandResult(MovementResult.Success));
                }
                else if (command == "SCRIPT")
                {
                    // we have a move command coming in
                    rtController.ExecuteRadioTelescopeControlledStop(MovementPriority.GeneralStop);

                    // Retrieve script name used for switch case
                    string         script = splitCommandString[TCPCommunicationConstants.SCRIPT_NAME];
                    MovementResult result = MovementResult.None;

                    logger.Debug(Utilities.GetTimeStamp() + ": Script " + script);

                    switch (script)
                    {
                    case "DUMP":
                        result = rtController.SnowDump(MovementPriority.Manual);
                        break;

                    case "FULL_EV":
                        result = rtController.FullElevationMove(MovementPriority.Manual);
                        break;

                    case "THERMAL_CALIBRATE":
                        result = rtController.ThermalCalibrateRadioTelescope(MovementPriority.Manual);
                        break;

                    case "STOW":
                        result = rtController.MoveRadioTelescopeToOrientation(MiscellaneousConstants.Stow, MovementPriority.Manual);
                        break;

                    case "FULL_CLOCK":
                        result = rtController.MoveRadioTelescopeByXDegrees(new Orientation(360, 0), MovementPriority.Manual);
                        break;

                    case "FULL_COUNTER":
                        result = rtController.MoveRadioTelescopeByXDegrees(new Orientation(-360, 0), MovementPriority.Manual);
                        break;

                    case "HOME":
                        result = rtController.HomeTelescope(MovementPriority.Manual);
                        break;

                    case "HARDWARE_MVMT_SCRIPT":
                        result = rtController.ExecuteHardwareMovementScript(MovementPriority.Manual);
                        break;

                    default:
                        // If no command is found, result = invalid
                        result = MovementResult.InvalidCommand;
                        break;
                    }

                    // Return based off of movement result
                    if (result != MovementResult.Success)
                    {
                        return(new ExecuteTCPCommandResult(result, TCPCommunicationConstants.SCRIPT_ERR + result.ToString()));
                    }
                    else
                    {
                        // everything was successful
                        return(new ExecuteTCPCommandResult(result));
                    }
                }
                else if (command == "STOP_RT")
                {
                    rtController.RadioTelescope.PLCDriver.InterruptMovementAndWaitUntilStopped();

                    return(new ExecuteTCPCommandResult(MovementResult.Success, TCPCommunicationConstants.ALL_STOP_ERR));
                }
                else if (command == "SENSOR_INIT")
                {
                    // Retrieve sensor init values from the comma separated portion of the string
                    string[] splitData = splitCommandString[TCPCommunicationConstants.SENSOR_INIT_VALUES].Split(',');

                    var config = rtController.RadioTelescope.SensorNetworkServer.InitializationClient.SensorNetworkConfig;

                    // Set all the sensors to their new initialization
                    config.AzimuthTemp1Init                = splitData[(int)SensorInitializationEnum.AzimuthTemp].Equals("1");
                    config.ElevationTemp1Init              = splitData[(int)SensorInitializationEnum.ElevationTemp].Equals("1");
                    config.AzimuthAccelerometerInit        = splitData[(int)SensorInitializationEnum.AzimuthAccelerometer].Equals("1");
                    config.ElevationAccelerometerInit      = splitData[(int)SensorInitializationEnum.ElevationAccelerometer].Equals("1");
                    config.CounterbalanceAccelerometerInit = splitData[(int)SensorInitializationEnum.CounterbalanceAccelerometer].Equals("1");
                    config.AzimuthEncoderInit              = splitData[(int)SensorInitializationEnum.AzimuthEncoder].Equals("1");
                    config.ElevationEncoderInit            = splitData[(int)SensorInitializationEnum.ElevationEncoder].Equals("1");

                    // Set the timeout values
                    config.TimeoutDataRetrieval  = (int)(double.Parse(splitData[7]) * 1000);
                    config.TimeoutInitialization = (int)(double.Parse(splitData[8]) * 1000);

                    // Reboot
                    rtController.RadioTelescope.SensorNetworkServer.RebootSensorNetwork();

                    return(new ExecuteTCPCommandResult(MovementResult.Success));
                }
                else if (command == "REQUEST")
                {
                    switch (splitCommandString[TCPCommunicationConstants.REQUEST_TYPE])
                    {
                    case "MVMT_DATA":
                        return(new ExecuteTCPCommandResult(MovementResult.Success, GetMovementData()));

                    default:
                        return(new ExecuteTCPCommandResult(MovementResult.InvalidCommand, TCPCommunicationConstants.INVALID_REQUEST_TYPE + splitCommandString[TCPCommunicationConstants.REQUEST_TYPE]));
                    }
                }

                // can't find a keyword then we return Invalid Command sent
                return(new ExecuteTCPCommandResult(MovementResult.InvalidCommand, TCPCommunicationConstants.COMMAND_NOT_FOUND + command));
            }
            // Version is not found; add new versions here
            else
            {
                return(new ExecuteTCPCommandResult(MovementResult.InvalidCommand, TCPCommunicationConstants.VERSION_NOT_FOUND + version));
            }
        }