Package fr.esrf.TangoApi

Examples of fr.esrf.TangoApi.DeviceProxy


     * @throws SalsaDeviceException
     */
    public static String getState(IDevice device) throws SalsaDeviceException {
        String state;
        if (device != null && device.getName() != null && !"".equals(device.getName().trim())) {
            DeviceProxy proxy = getDeviceProxy(device);
            if (proxy != null) {
                try {
                    state = StateUtilities.getNameForState(proxy.state());
                }
                catch (DevFailed e) {
                    SalsaDeviceException salsaDeviceException = new SalsaDeviceException(
                            "Error while trying to read the state of the device "
                                    + device.getName() + ".", e);
View Full Code Here


     * @param deviceName the name of the device.
     * @return
     * @throws SalsaDeviceException
     */
    private static DeviceProxy getDeviceProxy(String deviceName) throws SalsaDeviceException {
        DeviceProxy proxy;
        try {
            proxy = new DeviceProxy(deviceName);
        }
        catch (DevFailed e) {
            e.printStackTrace();
            SalsaDeviceException salsaDeviceException = new SalsaDeviceException(
                    "Error while creating a TangORB device for the device " + deviceName + ".", e);
View Full Code Here

        if (separatorPos < 0) {
            throw new SalsaDeviceException("Error : invalid device name " + name + " : no \"/\".",
                    false);
        }
        String attributeName = name.substring(separatorPos + 1);
        DeviceProxy proxy = getDeviceProxy(device);
        if (proxy == null) {
            return null;
        }
        AttributeInfo info = getAttributeInfo(proxy, attributeName);
        return info;
View Full Code Here

                    false);
        }
        String deviceName = name.substring(0, separatorPos);
        String attributeName = name.substring(separatorPos + 1);

        DeviceProxy proxy = getDeviceProxy(deviceName);
        TangoAttribute attribute = getTangoAttribute(proxy.get_name() + "/" + attributeName);
        AttributeInfo info = getAttributeInfo(proxy, attributeName);
        // Report.

        // Readable / writable.
        boolean readable;
        boolean writeable;
        switch (info.writable.value()) {
            case AttrWriteType._READ:
                readable = true;
                writeable = false;
                break;
            case AttrWriteType._READ_WITH_WRITE:
            case AttrWriteType._READ_WRITE:
                readable = true;
                writeable = true;
                break;
            case AttrWriteType._WRITE:
                readable = false;
                writeable = true;
                break;
            default:
                // Normally impossible.
                throw new SalsaDeviceException("Unknown write type for the device " + name + ".");
        }
        report.setReadable(readable);
        report.setWriteable(writeable);

        // Data and data format.
        try {
            TangoAttribute tangoAttribute = new TangoAttribute(name);
            String format = null;
            try {
                AttributeProxy attributeProxy = tangoAttribute.getAttributeProxy();
                if (attributeProxy != null) {
                    AttributeInfo attributeInfo = attributeProxy.get_info();
                    if (attributeInfo != null) {
                        format = attributeInfo.format;
                    }
                }
            }
            catch (Exception e) {
                // Ignore exception: it is not a problem if format is not recovered
                format = null;
            }
            report.setFormat(format);

            double[] rawData;
            switch (info.data_format.value()) {
                case AttrDataFormat._SCALAR:
                    report.setDimensionType(DimensionType.SCALAR);
                    if (readable) {
                        report.setReadScalarData(tangoAttribute.read(Double.class));
                    }
                    else {
                        report.setReadScalarData(null);
                    }
                    if (writeable) {
                        Double dataWritten = tangoAttribute.readWritten(Double.class);
                        report.setWriteScalarData(dataWritten);
                    }
                    else {
                        report.setWriteScalarData(null);
                    }
                    report.setReadSpectrumData(null);
                    report.setWriteSpectrumData(null);
                    report.setReadImageData(null);
                    report.setWriteImageData(null);
                    break;
                case AttrDataFormat._SPECTRUM:
                    report.setDimensionType(DimensionType.SPECTRUM);
                    rawData = (double[]) tangoAttribute.readArray(Double.TYPE);
                    int spectrumLength = attribute.getDeviceAttribute().getDimX();
                    Double[] readSpectrumData = null;
                    Double[] writeSpectrumData = null;

                    if (readable) {
                        readSpectrumData = new Double[spectrumLength];
                        writeSpectrumData = null;
                        for (int index = 0; index < spectrumLength; ++index) {
                            readSpectrumData[index] = rawData[index];
                        }
                    }

                    if (writeable) {
                        writeSpectrumData = new Double[spectrumLength];
                        for (int index = 0; index < spectrumLength; ++index) {
                            writeSpectrumData = tangoAttribute.readSpecOrImage(Double.class);

                        }

                    }
                    report.setReadScalarData(null);
                    report.setWriteScalarData(null);
                    report.setReadSpectrumData(readSpectrumData);
                    report.setWriteSpectrumData(writeSpectrumData);
                    report.setReadImageData(null);
                    report.setWriteImageData(null);
                    break;
                case AttrDataFormat._IMAGE:
                    report.setDimensionType(DimensionType.IMAGE);
                    rawData = (double[]) tangoAttribute.readArray(Double.TYPE);
                    int xDim = attribute.getDeviceAttribute().getDimX();
                    int yDim = attribute.getDeviceAttribute().getDimY();
                    Double[][] readImageData;
                    Double[][] writeImageData;
                    Double[] writtenImage = tangoAttribute.readSpecOrImage(Double.class);
                    int yOffset;
                    if (readable) {
                        readImageData = new Double[yDim][xDim];
                        if (writeable) {
                            writeImageData = new Double[yDim][xDim];

                            for (int yPos = 0; yPos < yDim; ++yPos) {
                                yOffset = yPos * xDim;

                                for (int xPos = 0; xPos < xDim; ++xPos) {
                                    readImageData[yPos][xPos] = rawData[xPos + yOffset];
                                    writeImageData[yPos][xPos] = writtenImage[xPos + yOffset];

                                }
                            }
                        }
                        else {
                            writeImageData = null;
                            for (int yPos = 0; yPos < yDim; ++yPos) {
                                yOffset = yPos * xDim;
                                for (int xPos = 0; xPos < xDim; ++xPos) {
                                    readImageData[yPos][xPos] = rawData[xPos + yOffset];
                                }
                            }
                        }
                    }
                    else {
                        readImageData = null;
                        if (writeable) {
                            writeImageData = new Double[yDim][xDim];
                            for (int yPos = 0; yPos < yDim; ++yPos) {
                                yOffset = yPos * xDim;
                                for (int xPos = 0; xPos < xDim; ++xPos) {
                                    writeImageData[yPos][xPos] = writtenImage[xPos + yOffset];
                                }
                            }
                        }
                        else {
                            writeImageData = null;
                        }
                    }
                    report.setReadScalarData(null);
                    report.setWriteScalarData(null);
                    report.setReadSpectrumData(null);
                    report.setWriteSpectrumData(null);
                    report.setReadImageData(readImageData);
                    report.setWriteImageData(writeImageData);
                    break;
                default:
                    // Normally impossible.
                    throw new SalsaDeviceException("Unknown data format for the device " + name
                            + ".");
            }
        }
        catch (DevFailed e) {
            e.printStackTrace();
            SalsaDeviceException salsaDeviceException = new SalsaDeviceException(
                    "Error while trying to read the TangORB attribute data for the device " + name
                            + ".", e);
            throw salsaDeviceException;
        }

        String quality;
        try {
            quality = QualityUtilities.getNameForQuality(attribute.getDeviceAttribute()
                    .getQuality());
        }
        catch (DevFailed e) {
            SalsaDeviceException salsaDeviceException = new SalsaDeviceException(
                    "Error while trying to read the quality of the TangORB attribute value for the device "
                            + name + ".", e);
            throw salsaDeviceException;
        }
        report.setQuality(quality);

        String state;
        try {
            state = StateUtilities.getNameForState(proxy.state());
        }
        catch (DevFailed e) {
            SalsaDeviceException salsaDeviceException = new SalsaDeviceException(
                    "Error while trying to read the state of the TangORB attribute value for the device "
                            + name + ".", e);
View Full Code Here

    public void startScan(IConfigEnergy config, String scanServerName) throws SalsaDeviceException {
        if (config.getDimensionX().getActuatorsList().size() == 0) {
            throw new SalsaDeviceException("Cannot start scan : no actuators were defined.");
        }
        int timeOut;
        DeviceProxy scanServerProxy = ScanServerManager.getScanServerProxy(scanServerName);
        if (scanServerProxy != null) {
            try {
                // Reads the initial value of the time out.
                timeOut = scanServerProxy.get_timeout_millis();
                try {
                    // Time out
                    scanServerProxy.set_timeout_millis(5000);

                    // Clean
                    command("Clean");

                    // ? Scan sequencing.
                    // scanServerProxy.write_attribute(new DeviceAttribute("scanSequencing", (short)
                    // 1));

                    // Partial mode for recording, must be manage by an external client
                    // setAttribute("dataRecorderPartialMode", false);

                    // Run name.
                    String runName = config.getName();
                    IDirectory directory = config.getDirectory();
                    while (directory != null) {
                        runName = directory.getName() + "." + runName;
                        directory = directory.getDirectory();
                    }
                    setAttribute("runName", runName);

                    // Scan number.
                    setAttribute("scanNumber", config.getScanNumber());

                    // Timebases
                    List<ITimebase> timebasesList = config.getTimebaseList();
                    List<String> timebasesNameList = new ArrayList<String>(timebasesList.size());
                    for (ITimebase timebase : timebasesList) {
                        if (timebase.isEnabled()) {
                            timebasesNameList.add(timebase.getName());
                        }
                    }
                    String[] timebasesNameArray = timebasesNameList
                            .toArray(new String[timebasesNameList.size()]);
                    setAttribute("timebases", timebasesNameArray);

                    // Sensors
                    List<ISensor> sensorsList = config.getSensorsList();
                    List<String> sensorsNameList = new ArrayList<String>(sensorsList.size());
                    for (ISensor sensor : sensorsList) {
                        if (sensor.isEnabled()) {
                            sensorsNameList.add(sensor.getName());
                        }
                    }
                    String[] sensorsNameArray = sensorsNameList.toArray(new String[sensorsNameList
                            .size()]);
                    setAttribute("sensors", sensorsNameArray);

                    // Actuators
                    List<IActuator> actuatorsList;
                    List<String> actuatorsNamesList;
                    String[] actuatorsNamesArray;
                    IDimensionEnergy dimension = config.getDimensionX();
                    actuatorsList = dimension.getActuatorsList();
                    actuatorsNamesList = new ArrayList<String>(actuatorsList.size());
                    for (IActuator actuator : actuatorsList) {
                        if (actuator.isEnabled()) {
                            actuatorsNamesList.add(actuator.getName());
                        }
                    }
                    actuatorsNamesArray = actuatorsNamesList.toArray(new String[actuatorsNamesList
                            .size()]);
                    setAttribute("actuators", actuatorsNamesArray);

                    // Dimensions
                    // Tango exchanges trajectories as double arrays that contains the positions, in
                    // order,
                    // actuator after actuator, range after range, of the trajectories of all the
                    // actuators.
                    // There is one such array per dimension.
                    double[] allActuatorsPositionsArray;
                    double initialValue;
                    // Contains the positions in order, range after range, of the trajectories of an
                    // actuator.
                    List<Double> actuatorPositionsList;
                    // Contains the positions in order, actuator after actuator, range after range,
                    // of
                    // the trajectories of all the actuators.
                    List<Double> allActuatorsPositionsList;
                    // The list of integrations times.
                    List<Double> integrationsTimesList;
                    double[] integrationsTimesArray;
                    // The list of speeds.
                    List<Double> speedList = new ArrayList<Double>();
                    double[] speedArray;
                    // The number of points, which is the total steps numbers + 1 per range.
                    int totalStepsNumber;
                    // The actuators used for this dimension
                    List<IActuator> dimensionActuatorsList;
                    // Dimension.
                    // Initial computations.
                    dimensionActuatorsList = dimension.getActuatorsList();
                    // The positions, sorted as Tango expect them.
                    allActuatorsPositionsList = new ArrayList<Double>();
                    // The number of enabled actuators.
                    int enabledActuatorsNumber = 0;
                    // The positions must be sorted by actuator, so we loop over the actuators.
                    for (IActuator actuator : dimensionActuatorsList) {
                        if (actuator.isEnabled()) {
                            initialValue = ActuatorConnector.getData(actuator);
                            actuatorPositionsList = new ArrayList<Double>();
                            // For each actuators, the positions must be sorted by range.
                            for (ITrajectory trajectory : findActuatorTrajectories(dimension,
                                    actuator)) {
                                actuatorPositionsList.addAll(TrajectoryCalculator
                                        .calculateLinearTrajectoriesPosition(
                                                (ITrajectoryEnergy) trajectory, initialValue));
                                // The speeds must be sorted in the same order, so we read them
                                // here.
                                speedList.add(trajectory.getSpeed());
                            }
                            allActuatorsPositionsList.addAll(actuatorPositionsList);
                            ++enabledActuatorsNumber;
                        }
                    }

                    // Integration Time and steps number.
                    config.getDimensionX().getRangesEnergyList().get(0).getIntegrationTime();
                    integrationsTimesList = new ArrayList<Double>();
                    int stepsNumber;
                    int index;
                    double integrationTime;
                    totalStepsNumber = 0;
                    for (IRangeEnergy range : dimension.getRangesEnergyList()) {
                        stepsNumber = range.getStepsNumber();
                        integrationTime = range.getIntegrationTime();
                        for (index = 0; index < stepsNumber + 1; ++index) {
                            integrationsTimesList.add(integrationTime);
                        }
                        totalStepsNumber += stepsNumber + 1;
                    }
                    integrationsTimesArray = toDoubleArray(integrationsTimesList);
                    setAttribute("integrationTimes", integrationsTimesArray);
                    setAttribute("pointNumber", totalStepsNumber);

                    // Trajectories.
                    // Builds the array from the list.
                    allActuatorsPositionsArray = toDoubleArray(allActuatorsPositionsList);
                    // Sends the array to Tango.
                    DeviceAttribute trajectoriesAttribute = new DeviceAttribute("trajectories");
                    if (enabledActuatorsNumber != 0) {
                        trajectoriesAttribute.insert(allActuatorsPositionsArray, totalStepsNumber,
                                enabledActuatorsNumber);
                    }
                    else {
                        trajectoriesAttribute.insert(new double[] {}, 0, 1);
                    }
                    scanServerProxy.write_attribute(trajectoriesAttribute);

                    // Speed.
                    speedArray = toDoubleArray(speedList);
                    setAttribute("scanSpeed", speedArray);

                    // Actuator delay.
                    double actuatorsDelay = config.getActuatorsDelay();
                    setAttribute("actuatorsDelay", actuatorsDelay);

                    // Zig zag.
                    boolean zigzag = config.isZigzag();
                    setAttribute("zigzag", zigzag);

                    // Enable actuator speed.
                    boolean enableScanSpeed = config.isEnableScanSpeed();
                    setAttribute("enableScanSpeed", enableScanSpeed);

                    // Post scan behaviour.
                    IPostScanBehaviour postScanBehaviour = config.getScanAddOn()
                            .getPostScanBehaviour();
                    Behaviour behaviour = postScanBehaviour.getBehaviour();
                    if (behaviour == null) {
                        behaviour = Behaviour.NOOP;
                    }
                    int behaviourType = behaviour.getType();
                    setAttribute("afterRunActionType", behaviourType);
                    if (behaviour.getArgumentCount() >= 1) {
                        int behaviourSensorIndex = postScanBehaviour.getSensor();
                        setAttribute("afterRunActionSensor", behaviourSensorIndex);
                    }
                    else if (behaviour.getArgumentCount() >= 2) {
                        int behaviourActuatorIndex = postScanBehaviour.getActuator();
                        setAttribute("afterRunActionActuator", behaviourActuatorIndex);
                    }

                    // Error strategies.
                    if (config.getScanAddOn() != null
                            && config.getScanAddOn().getErrorStrategy() != null) {

                        IErrorStrategy errorStrat = config.getScanAddOn().getErrorStrategy();
                        IErrorStrategyItem[] categoriesESI = new IErrorStrategyItem[] {
                                errorStrat.getActuatorsErrorStrategy(),
                                errorStrat.getSensorsErrorStrategy(),
                                errorStrat.getTimebasesErrorStrategy(),
                                errorStrat.getHooksErrorStrategy() };
                        String[] categoriesStr = new String[] { "actuators", "sensors",
                                "timebases", "hooks" };

                        for (int i = 0; i < categoriesStr.length; i++) {
                            String cat = categoriesStr[i];
                            IErrorStrategyItem esi = categoriesESI[i];

                            double errorStrategyTimeOut = esi.getTimeOut();
                            int errorStrategyRetryCount = esi.getRetryCount();
                            double errorStrategyRetryTimeOut = esi.getTimeBetweenRetries();
                            int errorStrategyType = esi.getStrategy().ordinal();

                            // Time out.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat + "TimeOut",
                                    errorStrategyTimeOut));
                            // Retry count.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat + "RetryCount",
                                    errorStrategyRetryCount));
                            // Retry time out.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat
                                    + "RetryTimeOut", errorStrategyRetryTimeOut));
                            // Error strategy.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat
                                    + "ErrorStrategy", errorStrategyType));
                        }
                        // Context validation error strategy.
                        scanServerProxy.write_attribute(new DeviceAttribute(
                                "contextValidationErrorStrategy", errorStrat
                                        .getContextValidationStrategy().ordinal()));

                        scanServerProxy.write_attribute(new DeviceAttribute("contextValidation",
                                errorStrat.getContextValidationDevice()));
                    }

                    /* Hooks */
                    SetHooks(config.getScanAddOn(), scanServerProxy);

                    // Hardware continuous scan : no.
                    setAttribute("hwContinuous", false);

                    // On the fly mode (software continuous mode).
                    boolean onTheFly = config.isOnTheFly();
                    setAttribute("onTheFly", onTheFly);

                    // Fin configuration du scan.

                    // D�marrage du scan.
                    command("Start");
                }
                finally {
                    // Sets the time out back to the initial value.
                    scanServerProxy.set_timeout_millis(timeOut);
                }
            }
            catch (DevFailed e) {
                e.printStackTrace();
                String message = e.getMessage();
View Full Code Here

        if (config.getDimensionX().getActuatorsList().size() == 0) {
            throw new SalsaDeviceException(
                    "Cannot start scan : no actuators were defined for dimension X.");
        }
        int timeOut;
        DeviceProxy scanServerProxy = getScanServerProxy();
        if (scanServerProxy != null) {
            try {
                // Reads the initial value of the time out.
                timeOut = scanServerProxy.get_timeout_millis();
                try {
                    // Time out
                    scanServerProxy.set_timeout_millis(5000);

                    // Clean
                    command("Clean");

                    // ? Scan sequencing.
                    // setAttribute("scanSequencing", (short) 1));

                    // Partial mode for recording.
                    setAttribute("dataRecorderPartialMode", false);

                    // Run name.
                    String runName = config.getName();
                    IDirectory directory = config.getDirectory();
                    while (directory != null) {
                        runName = directory.getName() + "." + runName;
                        directory = directory.getDirectory();
                    }
                    setAttribute("runName", runName);

                    // Scan number.
                    setAttribute("scanNumber", config.getScanNumber());

                    // Timebases
                    List<ITimebase> timebasesList = config.getTimebaseList();
                    List<String> timebasesNameList = new ArrayList<String>(timebasesList.size());
                    for (ITimebase timebase : timebasesList) {
                        if (timebase.isEnabled()) {
                            timebasesNameList.add(timebase.getName());
                        }
                    }
                    String[] timebasesNameArray = timebasesNameList
                            .toArray(new String[timebasesNameList.size()]);
                    setAttribute("timebases", timebasesNameArray);

                    // Sensors
                    List<ISensor> sensorsList = config.getSensorsList();
                    List<String> sensorsNameList = new ArrayList<String>(sensorsList.size());
                    for (ISensor sensor : sensorsList) {
                        if (sensor.isEnabled()) {
                            sensorsNameList.add(sensor.getName());
                        }
                    }
                    String[] sensorsNameArray = sensorsNameList.toArray(new String[sensorsNameList
                            .size()]);
                    setAttribute("sensors", sensorsNameArray);

                    // Actuators X
                    List<IActuator> actuatorsXList;
                    List<String> actuatorsNamesXList;
                    String[] actuatorsNamesXArray;
                    IDimension2DX dimensionX = config.getDimensionX();
                    actuatorsXList = dimensionX.getActuatorsList();
                    actuatorsNamesXList = new ArrayList<String>(actuatorsXList.size());
                    for (IActuator actuator : actuatorsXList) {
                        if (actuator.isEnabled()) {
                            actuatorsNamesXList.add(actuator.getName());
                        }
                    }
                    actuatorsNamesXArray = actuatorsNamesXList
                            .toArray(new String[actuatorsNamesXList.size()]);
                    setAttribute("actuators", actuatorsNamesXArray);

                    // Actuators y
                    List<IActuator> actuatorsYList;
                    List<String> actuatorsNamesYList;
                    String[] actuatorsNamesYArray;
                    IDimension2DY dimensionY = config.getDimensionY();
                    actuatorsYList = dimensionY.getActuatorsList();
                    actuatorsNamesYList = new ArrayList<String>(actuatorsYList.size());
                    for (IActuator actuator : actuatorsYList) {
                        if (actuator.isEnabled()) {
                            actuatorsNamesYList.add(actuator.getName());
                        }
                    }
                    actuatorsNamesYArray = actuatorsNamesYList
                            .toArray(new String[actuatorsNamesYList.size()]);
                    setAttribute("actuators2", actuatorsNamesYArray);

                    // Dimensions X
                    // Tango exchanges trajectories as double arrays that contains the positions, in
                    // order,
                    // actuator after actuator, range after range, of the trajectories of all the
                    // actuators.
                    // There is one such array per dimension.
                    double[] allActuatorsPositionsXArray;
                    double initialValueX;

                    // Contains the positions in order, range after range, of the trajectories of an
                    // actuator.
                    List<Double> actuatorPositionsXList;
                    // Contains the positions in order, actuator after actuator, range after range,
                    // of
                    // the trajectories of all the actuators.
                    List<Double> allActuatorsPositionsXList;
                    // The list of integrations times.
                    List<Double> integrationsTimesXList;
                    double[] integrationsTimesXArray;
                    int integrationTimeIndexX;
                    // The list of speeds.
                    List<Double> speedXList = new ArrayList<Double>();
                    double[] speedXArray;
                    // The number of points, which is the total steps numbers + 1 per range.
                    int totalStepsNumberX;
                    // The actuators used for this dimension
                    List<IActuator> dimensionActuatorsXList;
                    // Tango API
                    dimensionActuatorsXList = dimensionX.getActuatorsList();
                    // The positions, sorted as Tango expect them.
                    allActuatorsPositionsXList = new ArrayList<Double>();
                    // The number of enabled actuators.
                    int enabledActuatorsXNumber = 0;
                    // The positions must be sorted by actuator, so we loop over the actuators.
                    for (IActuator actuator : dimensionActuatorsXList) {
                        if (actuator.isEnabled()) {
                            initialValueX = ActuatorConnector.getData(actuator);
                            actuatorPositionsXList = new ArrayList<Double>();
                            // For each actuators, the positions must be sorted by range.
                            for (ITrajectory2DX trajectory : findActuatorTrajectories(dimensionX,
                                    actuator)) {
                                actuatorPositionsXList.addAll(TrajectoryCalculator
                                        .calculateLinearTrajectoriesPosition(trajectory,
                                                initialValueX));
                                // The speeds must be sorted in the same order, so we read them
                                // here.
                                speedXList.add(trajectory.getSpeed());
                            }
                            allActuatorsPositionsXList.addAll(actuatorPositionsXList);
                            ++enabledActuatorsXNumber;
                        }
                    }

                    // Integration Time and steps number.
                    integrationsTimesXList = new ArrayList<Double>(dimensionX.getRangesList()
                            .size());
                    int stepsNumberX;
                    totalStepsNumberX = 0;
                    for (IRange2DX range : dimensionX.getRangesList()) {
                        stepsNumberX = range.getStepsNumber();
                        for (integrationTimeIndexX = 0; integrationTimeIndexX < stepsNumberX + 1; ++integrationTimeIndexX) {
                            integrationsTimesXList.add(range.getIntegrationTime());
                        }
                        totalStepsNumberX += stepsNumberX + 1;
                    }
                    integrationsTimesXArray = toDoubleArray(integrationsTimesXList);
                    setAttribute("integrationTimes", integrationsTimesXArray);
                    setAttribute("pointNumber", totalStepsNumberX);

                    // Trajectories
                    // Builds the array from the list.
                    allActuatorsPositionsXArray = toDoubleArray(allActuatorsPositionsXList);
                    // Sends the array to Tango.
                    DeviceAttribute trajectoriesXAttribute = new DeviceAttribute("trajectories");
                    if (enabledActuatorsXNumber != 0) {
                        trajectoriesXAttribute.insert(allActuatorsPositionsXArray,
                                totalStepsNumberX, enabledActuatorsXNumber);
                    }
                    else {
                        trajectoriesXAttribute.insert(new double[] {}, 0, 1);
                    }
                    scanServerProxy.write_attribute(trajectoriesXAttribute);

                    // Speed.
                    speedXArray = toDoubleArray(speedXList);
                    setAttribute("scanSpeed", speedXArray);

                    // Dimensions Y
                    // Tango exchanges trajectories as double arrays that contains the positions, in
                    // order,
                    // actuator after actuator, range after range, of the trajectories of all the
                    // actuators.
                    // There is one such array per dimension.
                    double[] allActuatorsPositionsYArray;
                    double initialValueY;

                    // Contains the positions in order, range after range, of the trajectories of an
                    // actuator.
                    List<Double> actuatorPositionsYList;
                    // Contains the positions in order, actuator after actuator, range after range,
                    // of
                    // the trajectories of all the actuators.
                    List<Double> allActuatorsPositionsYList;
                    // The number of points, which is the total steps numbers + 1 per range.
                    int totalStepsNumberY;
                    // The actuators used for this dimension
                    List<IActuator> dimensionActuatorsYList;
                    // Dimension
                    dimensionActuatorsYList = dimensionY.getActuatorsList();
                    // The positions, sorted as Tango expect them.
                    allActuatorsPositionsYList = new ArrayList<Double>();
                    // The number of enabled actuators.
                    int enabledActuatorsYNumber = 0;
                    // The positions must be sorted by actuator, so we loop over the actuators.
                    for (IActuator actuator : dimensionActuatorsYList) {
                        if (actuator.isEnabled()) {
                            initialValueY = ActuatorConnector.getData(actuator);
                            actuatorPositionsYList = new ArrayList<Double>();
                            // For each actuators, the positions must be sorted by range.
                            for (ITrajectory2DY trajectory : findActuatorTrajectories(dimensionY,
                                    actuator)) {
                                actuatorPositionsYList.addAll(TrajectoryCalculator
                                        .calculateLinearTrajectoriesPosition(trajectory,
                                                initialValueY));
                            }
                            allActuatorsPositionsYList.addAll(actuatorPositionsYList);
                            ++enabledActuatorsYNumber;
                        }
                    }

                    // The number of points
                    totalStepsNumberY = 0;
                    for (IRange2DY range : dimensionY.getRangesList()) {
                        totalStepsNumberY += range.getStepsNumber() + 1;
                    }
                    setAttribute("pointNumber2", totalStepsNumberY);

                    // Builds the array from the list.
                    allActuatorsPositionsYArray = toDoubleArray(allActuatorsPositionsYList);
                    DeviceAttribute trajectoriesYAttribute = new DeviceAttribute("trajectories2");
                    if (enabledActuatorsYNumber > 0) {
                        // Sends the array to Tango.
                        trajectoriesYAttribute.insert(allActuatorsPositionsYArray,
                                totalStepsNumberY, enabledActuatorsYNumber);
                    }
                    else {
                        trajectoriesYAttribute.insert(new double[] {}, 0, 1);
                    }
                    scanServerProxy.write_attribute(trajectoriesYAttribute);

                    // Actuator delay.
                    double actuatorsDelay = config.getActuatorsDelay();
                    setAttribute("actuatorsDelay", actuatorsDelay);

                    // Zig zag.
                    boolean zigzag = config.isZigzag();
                    setAttribute("zigzag", zigzag);

                    // Enable actuator speed.
                    boolean enableScanSpeed = config.isEnableScanSpeed();
                    setAttribute("enableScanSpeed", enableScanSpeed);

                    // Post scan behaviour.
                    IPostScanBehaviour postScanBehaviour = config.getScanAddOn()
                            .getPostScanBehaviour();
                    Behaviour behaviour = postScanBehaviour.getBehaviour();
                    if (behaviour == null) {
                        behaviour = Behaviour.NOOP;
                    }
                    int behaviourType = behaviour.getType();
                    setAttribute("afterRunActionType", behaviourType);
                    if (behaviour.getArgumentCount() >= 1) {
                        int behaviourSensorIndex = postScanBehaviour.getSensor();
                        setAttribute("afterRunActionSensor", behaviourSensorIndex);
                    }
                    else if (behaviour.getArgumentCount() >= 2) {
                        int behaviourActuatorIndex = postScanBehaviour.getActuator();
                        setAttribute("afterRunActionActuator", behaviourActuatorIndex);
                    }

                    // Error strategies.
                    if (config.getScanAddOn() != null
                            && config.getScanAddOn().getErrorStrategy() != null) {

                        IErrorStrategy errorStrat = config.getScanAddOn().getErrorStrategy();
                        IErrorStrategyItem[] categoriesESI = new IErrorStrategyItem[] {
                                errorStrat.getActuatorsErrorStrategy(),
                                errorStrat.getSensorsErrorStrategy(),
                                errorStrat.getTimebasesErrorStrategy(),
                                errorStrat.getHooksErrorStrategy() };
                        String[] categoriesStr = new String[] { "actuators", "sensors",
                                "timebases", "hooks" };

                        for (int i = 0; i < categoriesStr.length; i++) {
                            String cat = categoriesStr[i];
                            IErrorStrategyItem esi = categoriesESI[i];

                            double errorStrategyTimeOut = esi.getTimeOut();
                            int errorStrategyRetryCount = esi.getRetryCount();
                            double errorStrategyRetryTimeOut = esi.getTimeBetweenRetries();
                            int errorStrategyType = esi.getStrategy().ordinal();

                            // Time out.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat + "TimeOut",
                                    errorStrategyTimeOut));
                            // Retry count.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat + "RetryCount",
                                    errorStrategyRetryCount));
                            // Retry time out.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat
                                    + "RetryTimeOut", errorStrategyRetryTimeOut));
                            // Error strategy.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat
                                    + "ErrorStrategy", errorStrategyType));
                        }
                        // Context validation error strategy.
                        scanServerProxy.write_attribute(new DeviceAttribute(
                                "contextValidationErrorStrategy", errorStrat
                                        .getContextValidationStrategy().ordinal()));

                        scanServerProxy.write_attribute(new DeviceAttribute("contextValidation",
                                errorStrat.getContextValidationDevice()));
                    }

                    /* Hooks */
                    SetHooks(config.getScanAddOn(), scanServerProxy);

                    // Hardware continuous scan : no.
                    setAttribute("hwContinuous", false);

                    // On the fly mode (software continuous mode).
                    boolean onTheFly = config.isOnTheFly();
                    setAttribute("onTheFly", onTheFly);

                    // Fin configuration du scan.

                    // D�marrage du scan.
                    command("Start");
                }
                finally {
                    // Sets the time out back to the initial value.
                    scanServerProxy.set_timeout_millis(timeOut);
                }
            }
            catch (DevFailed e) {
                e.printStackTrace();
                String message = e.getMessage();
View Full Code Here

     * @param scanServerName the name of the scan server to use.
     * @throws SalsaDeviceException
     */
    public void startScan(IConfig1D config, String scanServerName) throws SalsaDeviceException {
        int timeOut;
        DeviceProxy scanServerProxy = getScanServerProxy();
        if (scanServerProxy != null) {
            try {
                // Reads the initial value of the time out.
                timeOut = scanServerProxy.get_timeout_millis();
                try {
                    // Time out
                    scanServerProxy.set_timeout_millis(5000);

                    // Clean
                    command("Clean");

                    // ? Scan sequencing.
                    // scanServerProxy.write_attribute(new DeviceAttribute("scanSequencing", (short)
                    // 1));

                    // Partial mode for recording.
                    setAttribute("dataRecorderPartialMode", false);

                    // Run name.
                    String runName = config.getName();
                    IDirectory directory = config.getDirectory();
                    while (directory != null) {
                        runName = directory.getName() + "." + runName;
                        directory = directory.getDirectory();
                    }
                    setAttribute("runName", runName);

                    // Scan number.
                    setAttribute("scanNumber", config.getScanNumber());

                    // Timebases
                    List<ITimebase> timebasesList = config.getTimebaseList();
                    List<String> timebasesNameList = new ArrayList<String>(timebasesList.size());
                    for (ITimebase timebase : timebasesList) {
                        if (timebase.isEnabled()) {
                            timebasesNameList.add(timebase.getName());
                        }
                    }
                    String[] timebasesNameArray = timebasesNameList
                            .toArray(new String[timebasesNameList.size()]);
                    setAttribute("timebases", timebasesNameArray);

                    // Sensors
                    List<ISensor> sensorsList = config.getSensorsList();
                    List<String> sensorsNameList = new ArrayList<String>(sensorsList.size());
                    for (ISensor sensor : sensorsList) {
                        if (sensor.isEnabled()) {
                            sensorsNameList.add(sensor.getName());
                        }
                    }
                    String[] sensorsNameArray = sensorsNameList.toArray(new String[sensorsNameList
                            .size()]);
                    setAttribute("sensors", sensorsNameArray);

                    // Actuators
                    List<IActuator> actuatorsList;
                    List<String> actuatorsNamesList;
                    String[] actuatorsNamesArray;
                    IDimension1D dimension = config.getDimensionX();
                    actuatorsList = dimension.getActuatorsList();
                    actuatorsNamesList = new ArrayList<String>(actuatorsList.size());
                    for (IActuator actuator : actuatorsList) {
                        if (actuator.isEnabled()) {
                            actuatorsNamesList.add(actuator.getName());
                        }
                    }
                    actuatorsNamesArray = actuatorsNamesList.toArray(new String[actuatorsNamesList
                            .size()]);
                    setAttribute("actuators", actuatorsNamesArray);

                    // Dimensions
                    // Tango exchanges trajectories as double arrays that contains the positions, in
                    // order,
                    // actuator after actuator, range after range, of the trajectories of all the
                    // actuators.
                    // There is one such array per dimension.
                    double[] allActuatorsPositionsArray;
                    double initialValue;
                    // Contains the positions in order, range after range, of the trajectories of an
                    // actuator.
                    List<Double> actuatorPositionsList;
                    // Contains the positions in order, actuator after actuator, range after range,
                    // of
                    // the trajectories of all the actuators.
                    List<Double> allActuatorsPositionsList;
                    // The list of integrations times.
                    List<Double> integrationsTimesList;
                    double[] integrationsTimesArray;
                    int integrationTimeIndex;
                    // The list of speeds.
                    List<Double> speedList = new ArrayList<Double>();
                    double[] speedArray;
                    // The number of points, which is the total steps numbers + 1 per range.
                    int totalStepsNumber;
                    // The actuators used for this dimension
                    List<IActuator> dimensionActuatorsList;
                    // Dimension.
                    // Initial computations.
                    dimensionActuatorsList = dimension.getActuatorsList();
                    // The positions, sorted as Tango expect them.
                    allActuatorsPositionsList = new ArrayList<Double>();
                    // The number of enabled actuators.
                    int enabledActuatorsNumber = 0;
                    // The positions must be sorted by actuator, so we loop over the actuators.
                    for (IActuator actuator : dimensionActuatorsList) {
                        if (actuator.isEnabled()) {
                            initialValue = ActuatorConnector.getData(actuator);
                            actuatorPositionsList = new ArrayList<Double>();
                            // For each actuators, the positions must be sorted by range.
                            for (ITrajectory trajectory : findActuatorTrajectories(dimension,
                                    actuator)) {
                                actuatorPositionsList.addAll(TrajectoryCalculator
                                        .calculateLinearTrajectoriesPosition(
                                                (ITrajectory1D) trajectory, initialValue));
                                // The speeds must be sorted in the same order, so we read them
                                // here.
                                speedList.add(trajectory.getSpeed());
                            }
                            allActuatorsPositionsList.addAll(actuatorPositionsList);
                            ++enabledActuatorsNumber;
                        }
                    }

                    // Integration Time and steps number.
                    integrationsTimesList = new ArrayList<Double>(dimension.getRangesXList().size());
                    int stepsNumber;
                    totalStepsNumber = 0;
                    for (IRange1D range : dimension.getRangesXList()) {
                        stepsNumber = range.getStepsNumber();
                        for (integrationTimeIndex = 0; integrationTimeIndex < stepsNumber + 1; ++integrationTimeIndex) {
                            integrationsTimesList.add(range.getIntegrationTime());
                        }
                        totalStepsNumber += stepsNumber + 1;
                    }
                    integrationsTimesArray = toDoubleArray(integrationsTimesList);
                    setAttribute("integrationTimes", integrationsTimesArray);
                    setAttribute("pointNumber", totalStepsNumber);

                    // Trajectories.
                    // Builds the array from the list.
                    allActuatorsPositionsArray = toDoubleArray(allActuatorsPositionsList);
                    // Sends the array to Tango.
                    DeviceAttribute trajectoriesAttribute = new DeviceAttribute("trajectories");
                    if (enabledActuatorsNumber != 0) {
                        trajectoriesAttribute.insert(allActuatorsPositionsArray, totalStepsNumber,
                                enabledActuatorsNumber);
                    }
                    else {
                        trajectoriesAttribute.insert(new double[] {}, 0, 1);
                    }
                    scanServerProxy.write_attribute(trajectoriesAttribute);

                    // Speed.
                    speedArray = toDoubleArray(speedList);
                    setAttribute("scanSpeed", speedArray);

                    // Actuator delay.
                    double actuatorsDelay = config.getActuatorsDelay();
                    setAttribute("actuatorsDelay", actuatorsDelay);

                    // Zig zag.
                    boolean zigzag = config.isZigzag();
                    setAttribute("zigzag", zigzag);

                    // Enable actuator speed.
                    boolean enableScanSpeed = config.isEnableScanSpeed();
                    setAttribute("enableScanSpeed", enableScanSpeed);

                    // Post scan behaviour.
                    IPostScanBehaviour postScanBehaviour = config.getScanAddOn()
                            .getPostScanBehaviour();
                    Behaviour behaviour = postScanBehaviour.getBehaviour();
                    if (behaviour == null) {
                        behaviour = Behaviour.NOOP;
                    }
                    int behaviourType = behaviour.getType();
                    setAttribute("afterRunActionType", behaviourType);
                    if (behaviour.getArgumentCount() >= 1) {
                        int behaviourSensorIndex = postScanBehaviour.getSensor();
                        setAttribute("afterRunActionSensor", behaviourSensorIndex);
                    }
                    else if (behaviour.getArgumentCount() >= 2) {
                        int behaviourActuatorIndex = postScanBehaviour.getActuator();
                        setAttribute("afterRunActionActuator", behaviourActuatorIndex);
                    }

                    // Error strategies.
                    if (config.getScanAddOn() != null
                            && config.getScanAddOn().getErrorStrategy() != null) {

                        IErrorStrategy errorStrat = config.getScanAddOn().getErrorStrategy();
                        IErrorStrategyItem[] categoriesESI = new IErrorStrategyItem[] {
                                errorStrat.getActuatorsErrorStrategy(),
                                errorStrat.getSensorsErrorStrategy(),
                                errorStrat.getTimebasesErrorStrategy(),
                                errorStrat.getHooksErrorStrategy() };
                        String[] categoriesStr = new String[] { "actuators", "sensors",
                                "timebases", "hooks" };

                        for (int i = 0; i < categoriesStr.length; i++) {
                            String cat = categoriesStr[i];
                            IErrorStrategyItem esi = categoriesESI[i];

                            double errorStrategyTimeOut = esi.getTimeOut();
                            int errorStrategyRetryCount = esi.getRetryCount();
                            double errorStrategyRetryTimeOut = esi.getTimeBetweenRetries();
                            int errorStrategyType = esi.getStrategy().ordinal();

                            // Time out.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat + "TimeOut",
                                    errorStrategyTimeOut));
                            // Retry count.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat + "RetryCount",
                                    errorStrategyRetryCount));
                            // Retry time out.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat
                                    + "RetryTimeOut", errorStrategyRetryTimeOut));
                            // Error strategy.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat
                                    + "ErrorStrategy", errorStrategyType));
                        }
                        // Context validation error strategy.
                        scanServerProxy.write_attribute(new DeviceAttribute(
                                "contextValidationErrorStrategy", errorStrat
                                        .getContextValidationStrategy().ordinal()));

                        scanServerProxy.write_attribute(new DeviceAttribute("contextValidation",
                                errorStrat.getContextValidationDevice()));
                    }

                    /* Hooks */
                    SetHooks(config.getScanAddOn(), scanServerProxy);

                    // Hardware continuous scan : no.
                    setAttribute("hwContinuous", false);

                    // On the fly mode (software continuous mode).
                    boolean onTheFly = config.isOnTheFly();
                    setAttribute("onTheFly", onTheFly);

                    // Fin configuration du scan.

                    // D�marrage du scan.
                    command("Start");
                }
                finally {
                    // Sets the time out back to the initial value.
                    scanServerProxy.set_timeout_millis(timeOut);
                }
            }
            catch (DevFailed e) {
                e.printStackTrace();
                String message = e.getMessage();
View Full Code Here

     * @return
     * @throws SalsaDeviceException
     */
    public IScanResult1D readScanResult() throws SalsaDeviceException {
        IScanResult1D scanResult = null;
        DeviceProxy scanServerProxy = getScanServerProxy();
        if (scanServerProxy != null) {
            if (isScanResultReady()) {

                int pointIndex;
                try {
                    // Type
                    DeviceAttribute scanTypeAttribute = scanServerProxy.read_attribute("scanType");
                    int scanType = scanTypeAttribute.extractLong();
                    if (scanType != 1 && scanType != 0) {
                        return null;
                    }

                    scanResult = new ScanResult1DImpl();
                    scanResult.setScanServer(scanServerName);

                    scanResult.setResultType(IScanResult.ResultType.RESULT_1D);

                    // Name.
                    DeviceAttribute runNameAttribute = scanServerProxy.read_attribute("runName");
                    String runName = runNameAttribute.extractString();
                    scanResult.setRunName(runName);

                    // Time
                    double[] timesArray = scanServerProxy.read_attribute("sensorsTimeStamps")
                            .extractDoubleArray();
                    List<IScanPoint> scanPointsList = new ArrayList<IScanPoint>(timesArray.length);
                    IScanPoint scanPoint;
                    for (double time : timesArray) {
                        scanPoint = new ScanPointImpl();
                        scanPoint.setTime(time);
                        scanPointsList.add(scanPoint);
                    }

                    // Sensors
                    String[] sensorsNamesArrayTmp = scanServerProxy.read_attribute("sensors")
                            .extractStringArray();
                    // The sensorsNamesArrayTmp contains the data twice : once for reading and once
                    // for
                    // writing.
                    String[] sensorsNamesArray = new String[sensorsNamesArrayTmp.length / 2];
                    for (int sensorIndex = 0; sensorIndex < sensorsNamesArray.length; ++sensorIndex) {
                        sensorsNamesArray[sensorIndex] = sensorsNamesArrayTmp[sensorIndex];
                    }

                    String[] sensorsValueKeysArray = scanServerProxy.read_attribute(
                            "sensorsDataList").extractStringArray();

                    ISensor sensor;
                    String sensorValueKey;
                    String sensorName;
                    double[] sensorValuesArray;
                    TangoAttribute attribute;
                    for (int sensorIndex = 0; sensorIndex < sensorsNamesArray.length
                            && sensorIndex < sensorsValueKeysArray.length; ++sensorIndex) {
                        sensorName = sensorsNamesArray[sensorIndex];
                        sensor = new SensorImpl();
                        sensor.setName(sensorName);
                        sensor.setEnabled(true);
                        scanResult.getSensorsList().add(sensor);

                        sensorValueKey = sensorsValueKeysArray[sensorIndex];
                        attribute = new TangoAttribute(scanServerName + "/" + sensorValueKey);
                        sensorValuesArray = (double[]) attribute.readArray(Double.TYPE);
                        sensor.setScanServerAttributeName(scanServerProxy.get_name() + "/"
                                + sensorValueKey);

                        pointIndex = 0;
                        for (double sensorValue : sensorValuesArray) {
                            // The reading can occur during the scan. Because of this, more values
                            // can
                            // have been added since
                            // scanPointsList was initialized. Hence the test scanPointsList.size()
                            // >
                            // pointIndex.
                            if (scanPointsList.size() > pointIndex) {
                                scanPoint = scanPointsList.get(pointIndex);
                                scanPoint.getSensorsValuesMap().put(sensor, sensorValue);
                            }
                            ++pointIndex;
                        }
                    }

                    // Actuators dimension X
                    String[] actuatorsXNamesArrayTmp = scanServerProxy.read_attribute("actuators")
                            .extractStringArray();
                    // The actuatorsXNamesArrayTmp contains the data twice : once for reading and
                    // once
                    // for writing.
                    String actuatorXName;
                    String[] actuatorsXNamesArray = new String[actuatorsXNamesArrayTmp.length / 2];
                    for (int actuatorXIndex = 0; actuatorXIndex < actuatorsXNamesArray.length; ++actuatorXIndex) {
                        actuatorsXNamesArray[actuatorXIndex] = actuatorsXNamesArrayTmp[actuatorXIndex];
                    }

                    String[] actuatorsXValueKeysArray = scanServerProxy.read_attribute(
                            "actuatorsDataList").extractStringArray();
                    IActuator actuatorX;
                    String actuatorXValueKey;
                    double[] actuatorXValuesArray;
                    for (int actuatorXIndex = 0; actuatorXIndex < actuatorsXNamesArray.length
                            && actuatorXIndex < actuatorsXValueKeysArray.length; ++actuatorXIndex) {
                        actuatorXName = actuatorsXNamesArray[actuatorXIndex];
                        actuatorX = new ActuatorImpl();
                        actuatorX.setName(actuatorXName);
                        actuatorX.setEnabled(true);
                        scanResult.getActuatorsXList().add(actuatorX);

                        actuatorXValueKey = actuatorsXValueKeysArray[actuatorXIndex];

                        attribute = new TangoAttribute(scanServerName + "/" + actuatorXValueKey);
                        actuatorXValuesArray = (double[]) attribute.readArray(Double.TYPE);
                        actuatorX.setScanServerAttributeName(scanServerProxy.get_name() + "/"
                                + actuatorXValueKey);

                        pointIndex = 0;
                        for (double actuatorXValue : actuatorXValuesArray) {
                            // The reading can occur during the scan. Because of this, more values
View Full Code Here

    public void startScan(IConfigEnergy config, String scanServerName) throws SalsaDeviceException {
        if (config.getDimensionX().getActuatorsList().size() == 0) {
            throw new SalsaDeviceException("Cannot start scan : no actuators were defined.");
        }
        int timeOut;
        DeviceProxy scanServerProxy = getScanServerProxy();
        if (scanServerProxy != null) {
            try {
                // Reads the initial value of the time out.
                timeOut = scanServerProxy.get_timeout_millis();
                try {
                    // Time out
                    scanServerProxy.set_timeout_millis(5000);

                    // Clean
                    command("Clean");

                    // ? Scan sequencing.
                    // scanServerProxy.write_attribute(new DeviceAttribute("scanSequencing", (short)
                    // 1));

                    // Partial mode for recording.
                    setAttribute("dataRecorderPartialMode", false);

                    // Run name.
                    String runName = config.getName();
                    IDirectory directory = config.getDirectory();
                    while (directory != null) {
                        runName = directory.getName() + "." + runName;
                        directory = directory.getDirectory();
                    }
                    setAttribute("runName", runName);

                    // Scan number.
                    setAttribute("scanNumber", config.getScanNumber());

                    // Timebases
                    List<ITimebase> timebasesList = config.getTimebaseList();
                    List<String> timebasesNameList = new ArrayList<String>(timebasesList.size());
                    for (ITimebase timebase : timebasesList) {
                        if (timebase.isEnabled()) {
                            timebasesNameList.add(timebase.getName());
                        }
                    }
                    String[] timebasesNameArray = timebasesNameList
                            .toArray(new String[timebasesNameList.size()]);
                    setAttribute("timebases", timebasesNameArray);

                    // Sensors
                    List<ISensor> sensorsList = config.getSensorsList();
                    List<String> sensorsNameList = new ArrayList<String>(sensorsList.size());
                    for (ISensor sensor : sensorsList) {
                        if (sensor.isEnabled()) {
                            sensorsNameList.add(sensor.getName());
                        }
                    }
                    String[] sensorsNameArray = sensorsNameList.toArray(new String[sensorsNameList
                            .size()]);
                    setAttribute("sensors", sensorsNameArray);

                    // Actuators
                    List<IActuator> actuatorsList;
                    List<String> actuatorsNamesList;
                    String[] actuatorsNamesArray;
                    IDimensionEnergy dimension = config.getDimensionX();
                    actuatorsList = dimension.getActuatorsList();
                    actuatorsNamesList = new ArrayList<String>(actuatorsList.size());
                    for (IActuator actuator : actuatorsList) {
                        if (actuator.isEnabled()) {
                            actuatorsNamesList.add(actuator.getName());
                        }
                    }
                    actuatorsNamesArray = actuatorsNamesList.toArray(new String[actuatorsNamesList
                            .size()]);
                    setAttribute("actuators", actuatorsNamesArray);

                    // Dimensions
                    // Tango exchanges trajectories as double arrays that contains the positions, in
                    // order,
                    // actuator after actuator, range after range, of the trajectories of all the
                    // actuators.
                    // There is one such array per dimension.
                    double[] allActuatorsPositionsArray;
                    double initialValue;
                    // Contains the positions in order, range after range, of the trajectories of an
                    // actuator.
                    List<Double> actuatorPositionsList;
                    // Contains the positions in order, actuator after actuator, range after range,
                    // of
                    // the trajectories of all the actuators.
                    List<Double> allActuatorsPositionsList;
                    // The list of integrations times.
                    List<Double> integrationsTimesList;
                    double[] integrationsTimesArray;
                    // The list of speeds.
                    List<Double> speedList = new ArrayList<Double>();
                    double[] speedArray;
                    // The number of points, which is the total steps numbers + 1 per range.
                    int totalStepsNumber;
                    // The actuators used for this dimension
                    List<IActuator> dimensionActuatorsList;
                    // Dimension.
                    // Initial computations.
                    dimensionActuatorsList = dimension.getActuatorsList();
                    // The positions, sorted as Tango expect them.
                    allActuatorsPositionsList = new ArrayList<Double>();
                    // The number of enabled actuators.
                    int enabledActuatorsNumber = 0;
                    // The positions must be sorted by actuator, so we loop over the actuators.
                    for (IActuator actuator : dimensionActuatorsList) {
                        if (actuator.isEnabled()) {
                            initialValue = ActuatorConnector.getData(actuator);
                            actuatorPositionsList = new ArrayList<Double>();
                            // For each actuators, the positions must be sorted by range.
                            for (ITrajectory trajectory : findActuatorTrajectories(dimension,
                                    actuator)) {
                                actuatorPositionsList.addAll(TrajectoryCalculator
                                        .calculateLinearTrajectoriesPosition(
                                                (ITrajectoryEnergy) trajectory, initialValue));
                                // The speeds must be sorted in the same order, so we read them
                                // here.
                                speedList.add(trajectory.getSpeed());
                            }
                            allActuatorsPositionsList.addAll(actuatorPositionsList);
                            ++enabledActuatorsNumber;
                        }
                    }

                    // Integration Time and steps number.
                    config.getDimensionX().getRangesEnergyList().get(0).getIntegrationTime();
                    integrationsTimesList = new ArrayList<Double>();
                    int stepsNumber;
                    int index;
                    double integrationTime;
                    totalStepsNumber = 0;
                    for (IRangeEnergy range : dimension.getRangesEnergyList()) {
                        stepsNumber = range.getStepsNumber();
                        integrationTime = range.getIntegrationTime();
                        for (index = 0; index < stepsNumber + 1; ++index) {
                            integrationsTimesList.add(integrationTime);
                        }
                        totalStepsNumber += stepsNumber + 1;
                    }
                    integrationsTimesArray = toDoubleArray(integrationsTimesList);
                    setAttribute("integrationTimes", integrationsTimesArray);
                    setAttribute("pointNumber", totalStepsNumber);

                    // Trajectories.
                    // Builds the array from the list.
                    allActuatorsPositionsArray = toDoubleArray(allActuatorsPositionsList);
                    // Sends the array to Tango.
                    DeviceAttribute trajectoriesAttribute = new DeviceAttribute("trajectories");
                    if (enabledActuatorsNumber != 0) {
                        trajectoriesAttribute.insert(allActuatorsPositionsArray, totalStepsNumber,
                                enabledActuatorsNumber);
                    }
                    else {
                        trajectoriesAttribute.insert(new double[] {}, 0, 1);
                    }
                    scanServerProxy.write_attribute(trajectoriesAttribute);

                    // Speed.
                    speedArray = toDoubleArray(speedList);
                    setAttribute("scanSpeed", speedArray);

                    // Actuator delay.
                    double actuatorsDelay = config.getActuatorsDelay();
                    setAttribute("actuatorsDelay", actuatorsDelay);

                    // Zig zag.
                    boolean zigzag = config.isZigzag();
                    setAttribute("zigzag", zigzag);

                    // Enable actuator speed.
                    boolean enableScanSpeed = config.isEnableScanSpeed();
                    setAttribute("enableScanSpeed", enableScanSpeed);

                    // Post scan behaviour.
                    IPostScanBehaviour postScanBehaviour = config.getScanAddOn()
                            .getPostScanBehaviour();
                    Behaviour behaviour = postScanBehaviour.getBehaviour();
                    if (behaviour == null) {
                        behaviour = Behaviour.NOOP;
                    }
                    int behaviourType = behaviour.getType();
                    setAttribute("afterRunActionType", behaviourType);
                    if (behaviour.getArgumentCount() >= 1) {
                        int behaviourSensorIndex = postScanBehaviour.getSensor();
                        setAttribute("afterRunActionSensor", behaviourSensorIndex);
                    }
                    else if (behaviour.getArgumentCount() >= 2) {
                        int behaviourActuatorIndex = postScanBehaviour.getActuator();
                        setAttribute("afterRunActionActuator", behaviourActuatorIndex);
                    }

                    // Error strategies.
                    if (config.getScanAddOn() != null
                            && config.getScanAddOn().getErrorStrategy() != null) {

                        IErrorStrategy errorStrat = config.getScanAddOn().getErrorStrategy();
                        IErrorStrategyItem[] categoriesESI = new IErrorStrategyItem[] {
                                errorStrat.getActuatorsErrorStrategy(),
                                errorStrat.getSensorsErrorStrategy(),
                                errorStrat.getTimebasesErrorStrategy(),
                                errorStrat.getHooksErrorStrategy() };
                        String[] categoriesStr = new String[] { "actuators", "sensors",
                                "timebases", "hooks" };

                        for (int i = 0; i < categoriesStr.length; i++) {
                            String cat = categoriesStr[i];
                            IErrorStrategyItem esi = categoriesESI[i];

                            double errorStrategyTimeOut = esi.getTimeOut();
                            int errorStrategyRetryCount = esi.getRetryCount();
                            double errorStrategyRetryTimeOut = esi.getTimeBetweenRetries();
                            int errorStrategyType = esi.getStrategy().ordinal();

                            // Time out.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat + "TimeOut",
                                    errorStrategyTimeOut));
                            // Retry count.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat + "RetryCount",
                                    errorStrategyRetryCount));
                            // Retry time out.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat
                                    + "RetryTimeOut", errorStrategyRetryTimeOut));
                            // Error strategy.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat
                                    + "ErrorStrategy", errorStrategyType));
                        }
                        // Context validation error strategy.
                        scanServerProxy.write_attribute(new DeviceAttribute(
                                "contextValidationErrorStrategy", errorStrat
                                        .getContextValidationStrategy().ordinal()));

                        scanServerProxy.write_attribute(new DeviceAttribute("contextValidation",
                                errorStrat.getContextValidationDevice()));
                    }

                    /* Hooks */
                    SetHooks(config.getScanAddOn(), scanServerProxy);

                    // Hardware continuous scan : no.
                    setAttribute("hwContinuous", false);

                    // On the fly mode (software continuous mode).
                    boolean onTheFly = config.isOnTheFly();
                    setAttribute("onTheFly", onTheFly);

                    // Fin configuration du scan.

                    // D�marrage du scan.
                    command("Start");
                }
                finally {
                    // Sets the time out back to the initial value.
                    scanServerProxy.set_timeout_millis(timeOut);
                }
            }
            catch (DevFailed e) {
                e.printStackTrace();
                String message = e.getMessage();
View Full Code Here

     * @param scanServerName the name of the scan server to use.
     * @throws SalsaDeviceException
     */
    public void startScan(IConfig1D config, String scanServerName) throws SalsaDeviceException {
        int timeOut;
        DeviceProxy scanServerProxy = ScanServerManager.getScanServerProxy(scanServerName);
        if (scanServerProxy != null) {
            try {
                // Reads the initial value of the time out.
                timeOut = scanServerProxy.get_timeout_millis();
                try {
                    // Time out
                    scanServerProxy.set_timeout_millis(5000);

                    // Clean
                    command("Clean");

                    // Partial mode for recording,clean set mode to false
                    setAttribute("dataRecorderPartialMode", isDataRecorderPartialMode());

                    // Run name.
                    String runName = config.getName();
                    IDirectory directory = config.getDirectory();
                    while (directory != null) {
                        runName = directory.getName() + "." + runName;
                        directory = directory.getDirectory();
                    }
                    setAttribute("runName", runName);

                    // Scan number.
                    setAttribute("scanNumber", config.getScanNumber());

                    // Timebases
                    List<ITimebase> timebasesList = config.getTimebaseList();
                    List<String> timebasesNameList = new ArrayList<String>(timebasesList.size());
                    for (ITimebase timebase : timebasesList) {
                        if (timebase.isEnabled()) {
                            timebasesNameList.add(timebase.getName());
                        }
                    }
                    String[] timebasesNameArray = timebasesNameList
                            .toArray(new String[timebasesNameList.size()]);
                    setAttribute("timebases", timebasesNameArray);

                    // Sensors
                    List<ISensor> sensorsList = config.getSensorsList();
                    List<String> sensorsNameList = new ArrayList<String>(sensorsList.size());
                    for (ISensor sensor : sensorsList) {
                        if (sensor.isEnabled()) {
                            sensorsNameList.add(sensor.getName());
                        }
                    }
                    String[] sensorsNameArray = sensorsNameList.toArray(new String[sensorsNameList
                            .size()]);
                    setAttribute("sensors", sensorsNameArray);

                    // Actuators

                    List<String> actuatorsNamesList;
                    String[] actuatorsNamesArray;
                    IDimension1D dimension = config.getDimensionX();
                    List<IActuator> actuatorsList = dimension.getActuatorsList();
                    actuatorsNamesList = new ArrayList<String>(actuatorsList.size());
                    for (IActuator actuator : actuatorsList) {
                        if (actuator.isEnabled()) {
                            actuatorsNamesList.add(actuator.getName());
                        }
                    }
                    actuatorsNamesArray = actuatorsNamesList.toArray(new String[actuatorsNamesList
                            .size()]);
                    setAttribute("actuators", actuatorsNamesArray);

                    // Dimensions
                    // Tango exchanges trajectories as double arrays that contains the positions, in
                    // order,
                    // actuator after actuator, range after range, of the trajectories of all the
                    // actuators.
                    // There is one such array per dimension.
                    double[] allActuatorsPositionsArray;
                    double initialValue;
                    // Contains the positions in order, range after range, of the trajectories of an
                    // actuator.
                    List<Double> actuatorPositionsList;
                    // Contains the positions in order, actuator after actuator, range after range,
                    // of
                    // the trajectories of all the actuators.
                    List<Double> allActuatorsPositionsList;
                    // The list of integrations times.
                    List<Double> integrationsTimesList;
                    double[] integrationsTimesArray;
                    int integrationTimeIndex;
                    // The list of speeds.
                    List<Double> speedList = new ArrayList<Double>();
                    double[] speedArray;
                    // The number of points, which is the total steps numbers + 1 per range.
                    int totalStepsNumber;
                    // The positions, sorted as Tango expect them.
                    allActuatorsPositionsList = new ArrayList<Double>();
                    // The number of enabled actuators.
                    int enabledActuatorsNumber = 0;
                    // The positions must be sorted by actuator, so we loop over the actuators.
                    for (int index = 0; index < actuatorsList.size(); index++) {
                        IActuator actuator = actuatorsList.get(index);
                        if (actuator.isEnabled()) {
                            initialValue = ActuatorConnector.getData(actuator);
                            actuatorPositionsList = new ArrayList<Double>();
                            // For each actuators, the positions must be sorted by range.
                            List<ITrajectory> trajectoryList = findActuatorTrajectories(dimension,
                                    index);
                            for (ITrajectory trajectory : trajectoryList) {
                                actuatorPositionsList.addAll(TrajectoryCalculator
                                        .calculateLinearTrajectoriesPosition(
                                                (ITrajectory1D) trajectory, initialValue));
                                // The speeds must be sorted in the same order, so we read them
                                // here.
                                speedList.add(trajectory.getSpeed());
                                // speedList.addAll(TrajectoryCalculator
                                // .calculateScanSpeedList(trajectory));
                            }
                            // System.out.println(actuator.getName() + "="
                            // + Arrays.toString(actuatorPositionsList.toArray()));
                            allActuatorsPositionsList.addAll(actuatorPositionsList);
                            ++enabledActuatorsNumber;
                        }
                    }

                    // Integration Time and steps number.
                    integrationsTimesList = new ArrayList<Double>(dimension.getRangesXList().size());
                    int stepsNumber;
                    totalStepsNumber = 0;
                    for (IRange1D range : dimension.getRangesXList()) {
                        stepsNumber = range.getStepsNumber();
                        for (integrationTimeIndex = 0; integrationTimeIndex < stepsNumber + 1; ++integrationTimeIndex) {
                            integrationsTimesList.add(range.getIntegrationTime());
                        }
                        totalStepsNumber += stepsNumber + 1;
                    }
                    integrationsTimesArray = toDoubleArray(integrationsTimesList);
                    setAttribute("integrationTimes", integrationsTimesArray);
                    setAttribute("pointNumber", totalStepsNumber);

                    // Trajectories.
                    // Builds the array from the list.
                    allActuatorsPositionsArray = toDoubleArray(allActuatorsPositionsList);
                    // Sends the array to Tango.
                    DeviceAttribute trajectoriesAttribute = new DeviceAttribute("trajectories");
                    if (enabledActuatorsNumber != 0) {
                        trajectoriesAttribute.insert(allActuatorsPositionsArray, totalStepsNumber,
                                enabledActuatorsNumber);
                    }
                    else {
                        trajectoriesAttribute.insert(new double[] {}, 0, 1);
                    }
                    scanServerProxy.write_attribute(trajectoriesAttribute);

                    // Speed.
                    speedArray = toDoubleArray(speedList);
                    setAttribute("scanSpeed", speedArray);

                    // Actuator delay.
                    double actuatorsDelay = config.getActuatorsDelay();
                    setAttribute("actuatorsDelay", actuatorsDelay);

                    // Zig zag.
                    boolean zigzag = config.isZigzag();
                    setAttribute("zigzag", zigzag);

                    // Enable actuator speed.
                    boolean enableScanSpeed = config.isEnableScanSpeed();
                    setAttribute("enableScanSpeed", enableScanSpeed);

                    // Post scan behaviour.
                    IPostScanBehaviour postScanBehaviour = config.getScanAddOn()
                            .getPostScanBehaviour();
                    Behaviour behaviour = postScanBehaviour.getBehaviour();
                    if (behaviour == null) {
                        behaviour = Behaviour.NOOP;
                    }
                    int behaviourType = behaviour.getType();
                    setAttribute("afterRunActionType", behaviourType);
                    if (behaviour.getArgumentCount() >= 1) {
                        int behaviourSensorIndex = postScanBehaviour.getSensor();
                        setAttribute("afterRunActionSensor", behaviourSensorIndex);
                    }
                    else if (behaviour.getArgumentCount() >= 2) {
                        int behaviourActuatorIndex = postScanBehaviour.getActuator();
                        setAttribute("afterRunActionActuator", behaviourActuatorIndex);
                    }

                    // Error strategies.
                    if (config.getScanAddOn() != null
                            && config.getScanAddOn().getErrorStrategy() != null) {

                        IErrorStrategy errorStrat = config.getScanAddOn().getErrorStrategy();
                        IErrorStrategyItem[] categoriesESI = new IErrorStrategyItem[] {
                                errorStrat.getActuatorsErrorStrategy(),
                                errorStrat.getSensorsErrorStrategy(),
                                errorStrat.getTimebasesErrorStrategy(),
                                errorStrat.getHooksErrorStrategy() };
                        String[] categoriesStr = new String[] { "actuators", "sensors",
                                "timebases", "hooks" };

                        for (int i = 0; i < categoriesStr.length; i++) {
                            String cat = categoriesStr[i];
                            IErrorStrategyItem esi = categoriesESI[i];

                            double errorStrategyTimeOut = esi.getTimeOut();
                            int errorStrategyRetryCount = esi.getRetryCount();
                            double errorStrategyRetryTimeOut = esi.getTimeBetweenRetries();
                            int errorStrategyType = esi.getStrategy().ordinal();

                            // Time out.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat + "TimeOut",
                                    errorStrategyTimeOut));
                            // Retry count.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat + "RetryCount",
                                    errorStrategyRetryCount));
                            // Retry time out.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat
                                    + "RetryTimeOut", errorStrategyRetryTimeOut));
                            // Error strategy.
                            scanServerProxy.write_attribute(new DeviceAttribute(cat
                                    + "ErrorStrategy", errorStrategyType));
                        }
                        // Context validation error strategy.
                        scanServerProxy.write_attribute(new DeviceAttribute(
                                "contextValidationErrorStrategy", errorStrat
                                        .getContextValidationStrategy().ordinal()));

                        String value = errorStrat.getContextValidationDevice();
                        if (value != null && value.isEmpty()) {
                            scanServerProxy.write_attribute(new DeviceAttribute(
                                    "contextValidation", value));
                        }
                    }

                    /* Hooks */
                    SetHooks(config.getScanAddOn(), scanServerProxy);

                    // Hardware continuous scan : no.
                    setAttribute("hwContinuous", false);

                    // On the fly mode (software continuous mode).
                    boolean onTheFly = config.isOnTheFly();
                    setAttribute("onTheFly", onTheFly);

                    // Fin configuration du scan.

                    // D�marrage du scan.
                    command("Start");
                }
                finally {
                    // Sets the time out back to the initial value.
                    scanServerProxy.set_timeout_millis(timeOut);
                }
            }
            catch (DevFailed e) {
                e.printStackTrace();
                String message = e.getMessage();
View Full Code Here

TOP

Related Classes of fr.esrf.TangoApi.DeviceProxy

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.