2626import javafx .scene .control .Button ;
2727import javafx .scene .control .ComboBox ;
2828import javafx .scene .control .PasswordField ;
29- import javafx .scene .control .TextField ;
3029import javafx .scene .input .InputEvent ;
3130import lombok .extern .slf4j .Slf4j ;
3231import org .dpsoftware .MainSingleton ;
4544import java .util .concurrent .Executors ;
4645import java .util .concurrent .ScheduledExecutorService ;
4746import java .util .concurrent .TimeUnit ;
48- import java .util .concurrent .atomic .AtomicInteger ;
47+ import java .util .concurrent .atomic .AtomicBoolean ;
4948import java .util .regex .Matcher ;
5049import java .util .regex .Pattern ;
5150
@@ -69,8 +68,6 @@ public class ImprovDialogController {
6968 public Button cancelButton ;
7069 @ FXML
7170 private SettingsController settingsController ;
72- @ FXML
73- public TextField deviceName ;
7471
7572 /**
7673 * Inject main controller containing the TabPane
@@ -99,7 +96,6 @@ protected void initialize() {
9996 if (comPort != null && !comPort .getItems ().isEmpty ()) {
10097 comPort .setValue (comPort .getItems ().getFirst ());
10198 }
102- deviceName .setText (MainSingleton .getInstance ().config .getOutputDevice ());
10399 baudrate .setValue (Enums .BaudRate .BAUD_RATE_115200 .getBaudRate ());
104100 });
105101 }
@@ -112,7 +108,6 @@ private void setTooltips() {
112108 GuiManager .createTooltip (Constants .TOOLTIP_IMPROV_PWD , wifiPwd );
113109 GuiManager .createTooltip (Constants .TOOLTIP_IMPROV_COM , comPort );
114110 GuiManager .createTooltip (Constants .TOOLTIP_IMPROV_BAUD , baudrate );
115- GuiManager .createTooltip (Constants .TOOLTIP_DEV_NAME , deviceName );
116111 }
117112
118113 /**
@@ -150,25 +145,15 @@ public void saveAndClose(InputEvent e) {
150145 wifiPwd .commitValue ();
151146 baudrate .commitValue ();
152147 comPort .commitValue ();
153- manageImprov ();
154- CommonUtility .closeCurrentStage (e );
155- }
156-
157- /**
158- * Program device using the IMPROV WiFi protocol
159- */
160- private void manageImprov () {
161- SerialManager serialManager = new SerialManager ();
162- serialManager .initSerial (comPort .getValue (), baudrate .getValue ());
148+ AtomicBoolean error = new AtomicBoolean (false );
149+ AtomicBoolean postponed = new AtomicBoolean (false );
163150 ScheduledExecutorService scheduler = Executors .newScheduledThreadPool (1 );
164- AtomicInteger retryNumber = new AtomicInteger (0 );
165- MainSingleton .getInstance ().guiManager .pipelineManager .stopCapturePipeline ();
166- final int MAX_RETRY = 5 ;
167151 Runnable checkAndRun = () -> {
168- boolean improvError ;
169- retryNumber .getAndIncrement ();
170- log .debug ("Trying to send an Improv WiFi command" );
171- if (MainSingleton .getInstance ().config != null && MainSingleton .getInstance ().serial != null && MainSingleton .getInstance ().serial .isOpen ()) {
152+ if (MainSingleton .getInstance ().config != null ) {
153+ if (postponed .get ()) {
154+ CommonUtility .sleepSeconds (5 );
155+ }
156+ MainSingleton .getInstance ().guiManager .pipelineManager .stopCapturePipeline ();
172157 MainSingleton .getInstance ().config .setOutputDevice (settingsController .modeTabController .serialPort .getValue ());
173158 MainSingleton .getInstance ().config .setMqttEnable (settingsController .networkTabController .mqttEnable .isSelected ());
174159 if (MainSingleton .getInstance ().config .isMqttEnable ()) {
@@ -178,71 +163,20 @@ private void manageImprov() {
178163 MainSingleton .getInstance ().config .setMqttUsername (settingsController .networkTabController .mqttUser .getText ());
179164 MainSingleton .getInstance ().config .setMqttPwd (settingsController .networkTabController .mqttPwd .getText ());
180165 }
181- try {
182- improvError = sendImprov ();
183- } catch (IOException ex ) {
184- log .error (ex .getMessage ());
185- improvError = true ;
186- }
187- } else {
188- improvError = true ;
189- }
190- if (!improvError || retryNumber .get () >= MAX_RETRY ) {
166+ error .set (improvWiFiCommand ());
191167 scheduler .shutdown ();
192- if (MainSingleton .getInstance ().communicationError ) {
193- MainSingleton .getInstance ().guiManager .showLocalizedNotification (CommonUtility .getWord (Constants .FIRMWARE_PROVISION_NOTIFY ),
194- CommonUtility .getWord (Constants .FIRMWARE_PROVISION_NOTIFY_HEADER ), Constants .FIREFLY_LUCIFERIN , TrayIcon .MessageType .ERROR );
168+ } else {
169+ postponed .set (true );
170+ if (!postponed .get ()) {
171+ // logger isn't initialized yet, don't use the logger here.
172+ System .out .println ("Postponing provisioning..." );
195173 }
196174 }
197175 };
198- scheduler .scheduleAtFixedRate (checkAndRun , 100 , 2000 , TimeUnit .MILLISECONDS );
199- }
200-
201- /**
202- * Send improv wifi msg
203- *
204- * @return error
205- * @throws IOException can't open port
206- */
207- private boolean sendImprov () throws IOException {
208- byte version = 0x01 ;
209- byte rpcPacketType = 0x03 ;
210- byte rpcCommandType = 0x01 ;
211- byte [] ssidBytes = ssid .getValue ().getBytes (StandardCharsets .UTF_8 );
212- byte [] passBytes = wifiPwd .getText ().getBytes (StandardCharsets .UTF_8 );
213- int dataLen = 1 + ssidBytes .length + 1 + passBytes .length ;
214- int packetLen = Constants .IMPROV_HEADER .length + 1 + 1 + 1 + 1 + 1 + dataLen + 1 ;
215- byte [] packet = new byte [packetLen ];
216- int idx = 0 ;
217- // Header
218- for (byte b : Constants .IMPROV_HEADER ) packet [idx ++] = b ;
219- packet [idx ++] = version ;
220- packet [idx ++] = rpcPacketType ;
221- packet [idx ++] = (byte ) (dataLen + 2 );
222- packet [idx ++] = rpcCommandType ;
223- // Data
224- packet [idx ++] = (byte ) (ssidBytes .length + passBytes .length );
225- packet [idx ++] = (byte ) ssidBytes .length ;
226- System .arraycopy (ssidBytes , 0 , packet , idx , ssidBytes .length );
227- idx += ssidBytes .length ;
228- packet [idx ++] = (byte ) passBytes .length ;
229- System .arraycopy (passBytes , 0 , packet , idx , passBytes .length );
230- idx += passBytes .length ;
231- // Checksum
232- int checksum = 0 ;
233- for (int i = 0 ; i < idx ; i ++) {
234- checksum += packet [i ] & 0xFF ;
176+ scheduler .scheduleAtFixedRate (checkAndRun , 0 , 500 , TimeUnit .MILLISECONDS );
177+ if (!error .get ()) {
178+ CommonUtility .closeCurrentStage (e );
235179 }
236- byte checksumByte = (byte ) (checksum & 0xFF );
237- packet [idx ] = checksumByte ;
238- if (MainSingleton .getInstance ().output != null ) {
239- log .debug ("Improv WiFi packet sent" );
240- MainSingleton .getInstance ().improvActive = deviceName .getText ();
241- MainSingleton .getInstance ().output .write (packet );
242- } else {
243- return true ;
244- }
245- return false ;
246180 }
247181
248182 /**
@@ -332,4 +266,58 @@ class NetworkInfo {
332266 }
333267 }
334268
269+ /**
270+ * Program device using the IMPROV WiFi protocol
271+ *
272+ * @return true if there is a serial error
273+ */
274+ public boolean improvWiFiCommand () {
275+ SerialManager serialManager = new SerialManager ();
276+ serialManager .closeSerial ();
277+ serialManager .initSerial (comPort .getValue (), baudrate .getValue ());
278+ try {
279+ byte version = 0x01 ;
280+ byte rpcPacketType = 0x03 ;
281+ byte rpcCommandType = 0x01 ;
282+ byte [] ssidBytes = ssid .getValue ().getBytes (StandardCharsets .UTF_8 );
283+ byte [] passBytes = wifiPwd .getText ().getBytes (StandardCharsets .UTF_8 );
284+ int dataLen = 1 + ssidBytes .length + 1 + passBytes .length ;
285+ int packetLen = Constants .IMPROV_HEADER .length + 1 + 1 + 1 + 1 + 1 + dataLen + 1 ;
286+ byte [] packet = new byte [packetLen ];
287+ int idx = 0 ;
288+ // Header
289+ for (byte b : Constants .IMPROV_HEADER ) packet [idx ++] = b ;
290+ packet [idx ++] = version ;
291+ packet [idx ++] = rpcPacketType ;
292+ packet [idx ++] = (byte ) (dataLen + 2 );
293+ packet [idx ++] = rpcCommandType ;
294+ // Data
295+ packet [idx ++] = (byte ) (ssidBytes .length + passBytes .length );
296+ packet [idx ++] = (byte ) ssidBytes .length ;
297+ System .arraycopy (ssidBytes , 0 , packet , idx , ssidBytes .length );
298+ idx += ssidBytes .length ;
299+ packet [idx ++] = (byte ) passBytes .length ;
300+ System .arraycopy (passBytes , 0 , packet , idx , passBytes .length );
301+ idx += passBytes .length ;
302+ // Checksum
303+ int checksum = 0 ;
304+ for (int i = 0 ; i < idx ; i ++) {
305+ checksum += packet [i ] & 0xFF ;
306+ }
307+ byte checksumByte = (byte ) (checksum & 0xFF );
308+ packet [idx ] = checksumByte ;
309+ if (MainSingleton .getInstance ().output == null ) {
310+ MainSingleton .getInstance ().guiManager .showLocalizedNotification (CommonUtility .getWord (Constants .FIRMWARE_PROVISION_NOTIFY ),
311+ CommonUtility .getWord (Constants .FIRMWARE_PROVISION_NOTIFY_HEADER ), Constants .FIREFLY_LUCIFERIN , TrayIcon .MessageType .ERROR );
312+ return true ;
313+ } else {
314+ MainSingleton .getInstance ().output .write (packet );
315+ }
316+ } catch (IOException ex ) {
317+ log .error (ex .getMessage ());
318+ return true ;
319+ }
320+ return false ;
321+ }
322+
335323}
0 commit comments