Search in sources :

Example 1 with FirmwareSetting

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

the class TinyGFirmwareSettings method setValue.

@Override
public FirmwareSetting setValue(String key, 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 2 with FirmwareSetting

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

the class TinyGFirmwareSettingsCommunicatorListener method extractSettingFromResponse.

private void extractSettingFromResponse(TinyGSettingGroupType group, JsonObject groupSettingsJson) {
    for (TinyGSettingType setting : group.getSettingTypes()) {
        if (groupSettingsJson.has(setting.getSettingName())) {
            String key = group.getGroupName() + setting.getSettingName();
            String value = groupSettingsJson.get(setting.getSettingName()).getAsString();
            String description = group.getDescription() + " - " + setting.getDescription();
            String shortDescription = group.getDescription() + " - " + setting.getShortDescription();
            updatedSetting = new FirmwareSetting(key, value, setting.getType(), description, shortDescription);
            newSetting = null;
            listeners.forEach(listener -> listener.onUpdatedFirmwareSetting(updatedSetting));
        }
    }
}
Also used : FirmwareSetting(com.willwinder.universalgcodesender.firmware.FirmwareSetting)

Example 3 with FirmwareSetting

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

the class GrblFirmwareSettingsTest method settingMessagesShouldBeSentAsEvents.

@Test
public void settingMessagesShouldBeSentAsEvents() {
    IFirmwareSettingsListener firmwareSettingsListener = mock(IFirmwareSettingsListener.class);
    target.addListener(firmwareSettingsListener);
    ArgumentCaptor<FirmwareSetting> firmwareSettingArgumentCaptor = ArgumentCaptor.forClass(FirmwareSetting.class);
    doNothing().when(firmwareSettingsListener).onUpdatedFirmwareSetting(firmwareSettingArgumentCaptor.capture());
    // Emulate settings messages from the controller
    target.rawResponseListener("$0=10");
    target.rawResponseListener("$0=11");
    List<FirmwareSetting> settingUpdates = firmwareSettingArgumentCaptor.getAllValues();
    assertEquals(2, settingUpdates.size());
    assertEquals("10", settingUpdates.get(0).getValue());
    assertEquals("11", settingUpdates.get(1).getValue());
}
Also used : IFirmwareSettingsListener(com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener) FirmwareSetting(com.willwinder.universalgcodesender.firmware.FirmwareSetting) Test(org.junit.Test)

Example 4 with FirmwareSetting

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

the class GrblFirmwareSettingsTest method setValueShouldNotUpdateOnError.

@Test
public void setValueShouldNotUpdateOnError() throws Exception {
    // Given
    when(controller.isStreaming()).thenReturn(false);
    when(controller.isCommOpen()).thenReturn(true);
    target.rawResponseListener("$0=10");
    // Add a listener
    IFirmwareSettingsListener firmwareSettingsListener = mock(IFirmwareSettingsListener.class);
    target.addListener(firmwareSettingsListener);
    // When
    // Try to update the value in it's own thread
    Future<?> setValueFuture = Executors.newCachedThreadPool().submit(() -> {
        try {
            return target.setValue("$0", "11");
        } catch (FirmwareSettingsException e) {
            fail("Should never get here but got exception: " + e);
        }
        return null;
    });
    // Simulate the response from the controller
    Thread.sleep(200);
    target.rawResponseListener("error");
    // Wait until the value gets updated
    FirmwareSetting setting = (FirmwareSetting) setValueFuture.get();
    // Then
    assertNotNull(setting);
    assertEquals("10", setting.getValue());
    verify(controller, times(1)).sendCommandImmediately(any());
    verify(firmwareSettingsListener, times(0)).onUpdatedFirmwareSetting(any());
}
Also used : IFirmwareSettingsListener(com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener) FirmwareSetting(com.willwinder.universalgcodesender.firmware.FirmwareSetting) FirmwareSettingsException(com.willwinder.universalgcodesender.firmware.FirmwareSettingsException) Test(org.junit.Test)

Example 5 with FirmwareSetting

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

the class GrblFirmwareSettingsTest method setValueShouldUpdateOnController.

@Test
public void setValueShouldUpdateOnController() throws Exception {
    // Given
    when(controller.isStreaming()).thenReturn(false);
    when(controller.isCommOpen()).thenReturn(true);
    target.rawResponseListener("$0=10");
    // Add a listener
    IFirmwareSettingsListener firmwareSettingsListener = mock(IFirmwareSettingsListener.class);
    ArgumentCaptor<FirmwareSetting> firmwareSettingArgumentCaptor = ArgumentCaptor.forClass(FirmwareSetting.class);
    doNothing().when(firmwareSettingsListener).onUpdatedFirmwareSetting(firmwareSettingArgumentCaptor.capture());
    target.addListener(firmwareSettingsListener);
    // When
    // Try to update the value in it's own thread
    Future<?> setValueFuture = Executors.newCachedThreadPool().submit(() -> {
        try {
            return target.setValue("$0", "11");
        } catch (FirmwareSettingsException e) {
            fail("Should never get here but got exception: " + e);
        }
        return null;
    });
    // Simulate the response from the controller
    Thread.sleep(200);
    target.rawResponseListener("ok");
    // Wait until the value gets updated
    FirmwareSetting firmwareSetting = (FirmwareSetting) setValueFuture.get();
    // Then
    assertEquals("11", firmwareSetting.getValue());
    verify(controller, times(1)).sendCommandImmediately(any());
    verify(firmwareSettingsListener, times(1)).onUpdatedFirmwareSetting(any());
    assertEquals(1, firmwareSettingArgumentCaptor.getAllValues().size());
    assertEquals("11", firmwareSettingArgumentCaptor.getAllValues().get(0).getValue());
}
Also used : IFirmwareSettingsListener(com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener) FirmwareSetting(com.willwinder.universalgcodesender.firmware.FirmwareSetting) FirmwareSettingsException(com.willwinder.universalgcodesender.firmware.FirmwareSettingsException) Test(org.junit.Test)

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