Search in sources :

Example 6 with FirmwareSetting

use of com.willwinder.universalgcodesender.firmware.FirmwareSetting in project Universal-G-Code-Sender by winder.

the class GrblFirmwareSettingsCommunicatorListener method updateSettingOnController.

/**
 * Sends a command to update a setting on the controller. The method will block until we get a response
 * from the controller or if a timeout is triggered if the setting took too long to update.
 *
 * @param setting the setting to update
 * @return the updated setting or an empty optional if it couldn't be updated.
 * @throws FirmwareSettingsException will be thrown if the controller isn't ready to receive setting updates or if
 *                                   the update took to long and caused a timeout
 */
public Optional<FirmwareSetting> updateSettingOnController(FirmwareSetting setting) throws FirmwareSettingsException {
    if (isUpdatingSettings()) {
        throw new FirmwareSettingsException("The settings are being updated in another thread.");
    }
    if (!canSendToController()) {
        throw new FirmwareSettingsException("The controller is not ready to receive commands.");
    }
    boolean previousSingleStepMode = controller.getSingleStepMode();
    boolean previousStatusUpdatesEnabled = controller.getStatusUpdatesEnabled();
    controller.setStatusUpdatesEnabled(false);
    controller.setSingleStepMode(true);
    try {
        try {
            updatedSetting = null;
            newSetting = setting;
            GcodeCommand command = controller.createCommand(setting.getKey() + "=" + setting.getValue());
            controller.sendCommandImmediately(command);
        } catch (Exception e) {
            throw new FirmwareSettingsException("Couldn't send update setting command to the controller: " + setting.getKey() + "=" + setting.getValue() + ".", e);
        }
        waitUntilUpdateFinished();
    } finally {
        controller.setSingleStepMode(previousSingleStepMode);
        controller.setStatusUpdatesEnabled(previousStatusUpdatesEnabled);
    }
    // Reset internal states
    Optional<FirmwareSetting> result = Optional.ofNullable(updatedSetting);
    updatedSetting = null;
    newSetting = null;
    return result;
}
Also used : FirmwareSetting(com.willwinder.universalgcodesender.firmware.FirmwareSetting) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) FirmwareSettingsException(com.willwinder.universalgcodesender.firmware.FirmwareSettingsException) FirmwareSettingsException(com.willwinder.universalgcodesender.firmware.FirmwareSettingsException) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with FirmwareSetting

use of com.willwinder.universalgcodesender.firmware.FirmwareSetting in project Universal-G-Code-Sender by winder.

the class GrblFirmwareSettingsCommunicatorListener method convertMessageToSetting.

/**
 * Converts a response message to a firmware setting. If the response message isn't in the format
 * {@code $[key]=[value]} an empty optional will be returned.
 *
 * @param response the response message from the controller
 * @return the converted firmware setting or an empty optional if the response was unknown.
 */
private Optional<FirmwareSetting> convertMessageToSetting(String response) {
    Matcher settingMatcher = SETTING_MESSAGE_REGEX.matcher(response);
    if (!settingMatcher.find()) {
        return Optional.empty();
    }
    String key = "$" + settingMatcher.group(1);
    String value = settingMatcher.group(2);
    String units = "";
    String description = "";
    String shortDescription = "";
    String[] lookup = grblLookups.lookup(settingMatcher.group(1));
    if (lookup != null) {
        shortDescription = lookup[1];
        units = lookup[2];
        description = lookup[3];
    }
    return Optional.of(new FirmwareSetting(key, value, units, description, shortDescription));
}
Also used : Matcher(java.util.regex.Matcher) FirmwareSetting(com.willwinder.universalgcodesender.firmware.FirmwareSetting)

Example 8 with FirmwareSetting

use of com.willwinder.universalgcodesender.firmware.FirmwareSetting in project Universal-G-Code-Sender by winder.

the class GrblFirmwareSettings method setValue.

/**
 * Sets a property value on the controller. It will wait until the setting has been stored,
 * if this fails a {@link FirmwareSettingsException} will be thrown.
 *
 * @param key   the name of the setting to update
 * @param value the value of the setting
 * @return the value stored on the controller
 * @throws FirmwareSettingsException if the value couldn't be persisted on the controller.
 */
@Override
public synchronized FirmwareSetting setValue(final String key, final String value) throws FirmwareSettingsException {
    final FirmwareSetting oldSetting = getSetting(key).orElseThrow(() -> new FirmwareSettingsException("Couldn't find setting with key " + key + " to update."));
    // The setting already contains the value so we do not update
    if (oldSetting.getValue().equals(value)) {
        return oldSetting;
    }
    // Make a copy of existing property and send it to our controller
    final FirmwareSetting newSetting = new FirmwareSetting(oldSetting.getKey(), value, oldSetting.getUnits(), oldSetting.getDescription(), oldSetting.getShortDescription());
    return serialCommunicatorDelegate.updateSettingOnController(newSetting).orElse(oldSetting);
}
Also used : FirmwareSetting(com.willwinder.universalgcodesender.firmware.FirmwareSetting) FirmwareSettingsException(com.willwinder.universalgcodesender.firmware.FirmwareSettingsException)

Example 9 with FirmwareSetting

use of com.willwinder.universalgcodesender.firmware.FirmwareSetting in project Universal-G-Code-Sender by winder.

the class GrblFirmwareSettings method setValue.

/**
 * Sets a property value on the controller. It will wait until the setting has been stored,
 * if this fails a {@link FirmwareSettingsException} will be thrown.
 *
 * @param key   the name of the setting to update
 * @param value the value of the setting
 * @return the value stored on the controller
 * @throws FirmwareSettingsException if the value couldn't be persisted on the controller.
 */
public FirmwareSetting setValue(final String key, final double value) throws FirmwareSettingsException {
    final FirmwareSetting oldSetting = getSetting(key).orElseThrow(() -> new FirmwareSettingsException("Couldn't find setting with key " + key + " to update."));
    // The setting already contains the value so we do not update
    if (getValueAsDouble(key) == value) {
        return oldSetting;
    }
    DecimalFormat decimalFormat = new DecimalFormat("0.0##", Localization.dfs);
    return setValue(key, decimalFormat.format(value));
}
Also used : DecimalFormat(java.text.DecimalFormat) FirmwareSetting(com.willwinder.universalgcodesender.firmware.FirmwareSetting) FirmwareSettingsException(com.willwinder.universalgcodesender.firmware.FirmwareSettingsException)

Example 10 with FirmwareSetting

use of com.willwinder.universalgcodesender.firmware.FirmwareSetting in project Universal-G-Code-Sender by winder.

the class TinyGFirmwareSettingsCommunicatorListener method updateSettingOnController.

/**
 * Sends a command to update a setting on the controller. The method will block until we get a response
 * from the controller or if a timeout is triggered if the setting took too long to update.
 *
 * @param setting the setting to update
 * @return the updated setting or an empty optional if it couldn't be updated.
 * @throws FirmwareSettingsException will be thrown if the controller isn't ready to receive setting updates or if
 *                                   the update took to long and caused a timeout
 */
public Optional<FirmwareSetting> updateSettingOnController(FirmwareSetting setting) throws FirmwareSettingsException {
    if (isUpdatingSettings()) {
        throw new FirmwareSettingsException("The settings are being updated in another thread.");
    }
    if (!canSendToController()) {
        throw new FirmwareSettingsException("The controller is not ready to receive commands.");
    }
    boolean previousSingleStepMode = controller.getSingleStepMode();
    boolean previousStatusUpdatesEnabled = controller.getStatusUpdatesEnabled();
    controller.setStatusUpdatesEnabled(false);
    controller.setSingleStepMode(true);
    try {
        try {
            updatedSetting = null;
            newSetting = setting;
            TinyGSettingGroupType groupType = TinyGSettingGroupType.fromSettingKey(setting.getKey()).orElseThrow(() -> new IllegalArgumentException("Unknown setting group for key " + setting.getKey()));
            TinyGSettingType settingType = TinyGSettingType.fromSettingKey(setting.getKey()).orElseThrow(() -> new IllegalArgumentException("Unknown setting type for key " + setting.getKey()));
            GcodeCommand command = controller.createCommand("{" + groupType.getGroupName() + ": {" + settingType.getSettingName() + ":" + setting.getValue() + "}}");
            controller.sendCommandImmediately(command);
        } catch (Exception e) {
            throw new FirmwareSettingsException("Couldn't send update setting command to the controller: " + setting.getKey() + "=" + setting.getValue() + ".", e);
        }
        waitUntilUpdateFinished();
    } finally {
        controller.setSingleStepMode(previousSingleStepMode);
        controller.setStatusUpdatesEnabled(previousStatusUpdatesEnabled);
    }
    // Reset internal states
    Optional<FirmwareSetting> result = Optional.ofNullable(updatedSetting);
    updatedSetting = null;
    newSetting = null;
    return result;
}
Also used : FirmwareSetting(com.willwinder.universalgcodesender.firmware.FirmwareSetting) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) TinyGGcodeCommand(com.willwinder.universalgcodesender.types.TinyGGcodeCommand) FirmwareSettingsException(com.willwinder.universalgcodesender.firmware.FirmwareSettingsException) FirmwareSettingsException(com.willwinder.universalgcodesender.firmware.FirmwareSettingsException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

FirmwareSetting (com.willwinder.universalgcodesender.firmware.FirmwareSetting)12 FirmwareSettingsException (com.willwinder.universalgcodesender.firmware.FirmwareSettingsException)8 IFirmwareSettingsListener (com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener)3 Test (org.junit.Test)3 GcodeCommand (com.willwinder.universalgcodesender.types.GcodeCommand)2 TimeoutException (java.util.concurrent.TimeoutException)2 TinyGGcodeCommand (com.willwinder.universalgcodesender.types.TinyGGcodeCommand)1 DecimalFormat (java.text.DecimalFormat)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1