4242import java .awt .*;
4343import java .io .IOException ;
4444import java .nio .charset .StandardCharsets ;
45- import java .util .*;
45+ import java .util .Date ;
46+ import java .util .HashMap ;
47+ import java .util .Map ;
48+ import java .util .UUID ;
4649import java .util .concurrent .*;
4750import java .util .regex .Matcher ;
4851import java .util .regex .Pattern ;
@@ -93,52 +96,7 @@ public void initSerial(String portName, String baudrate) {
9396 }
9497 }
9598 MainSingleton .getInstance ().serial .setDTRandRTS (false , false );
96- if (MainSingleton .getInstance ().serial != null && MainSingleton .getInstance ().serial .openPort ()) {
97- int baudrateToUse = baudrate .isEmpty () ? Integer .parseInt (MainSingleton .getInstance ().config .getBaudRate ()) : Integer .parseInt (baudrate );
98- MainSingleton .getInstance ().serial .setComPortParameters (baudrateToUse , 8 , 1 , SerialPort .NO_PARITY );
99- MainSingleton .getInstance ().serial .setComPortTimeouts (SerialPort .TIMEOUT_READ_SEMI_BLOCKING , readTimeout , writeTimeout );
100- log .info ("{}{}" , CommonUtility .getWord (Constants .SERIAL_PORT_IN_USE ), MainSingleton .getInstance ().serial .getSystemPortName ());
101- GlowWormDevice gwDevice = new GlowWormDevice ();
102- gwDevice .setDeviceName (Constants .USB_DEVICE );
103- gwDevice .setDeviceIP (MainSingleton .getInstance ().serial .getSystemPortName ());
104- gwDevice .setDhcpInUse (false );
105- gwDevice .setWifi (Constants .DASH );
106- gwDevice .setDeviceVersion (Constants .DASH );
107- gwDevice .setDeviceBoard (Constants .DASH );
108- gwDevice .setMac (Constants .DASH );
109- gwDevice .setGpio (Constants .DASH );
110- gwDevice .setNumberOfLEDSconnected (Constants .DASH );
111- gwDevice .setLastSeen (MainSingleton .getInstance ().formatter .format (new Date ()));
112- gwDevice .setFirmwareType (Constants .DASH );
113- gwDevice .setBaudRate (Constants .DASH );
114- gwDevice .setMqttTopic (Constants .DASH );
115- gwDevice .setColorMode (Constants .DASH );
116- gwDevice .setColorOrder (Enums .ColorOrder .GRB_GRBW .name ());
117- gwDevice .setLdrValue (Constants .DASH );
118- gwDevice .setRelayPin (Constants .DASH );
119- gwDevice .setRelayInvertedPin (false );
120- gwDevice .setSbPin (Constants .DASH );
121- gwDevice .setLdrPin (Constants .DASH );
122- gwDevice .setGpioClock (Constants .DASH );
123- GuiSingleton .getInstance ().deviceTableData .add (gwDevice );
124- GuiManager guiManager = new GuiManager ();
125- if (numberOfSerialDevices > 1 && MainSingleton .getInstance ().config .getOutputDevice ().equals (Constants .SERIAL_PORT_AUTO ) && portName .isEmpty ()) {
126- MainSingleton .getInstance ().communicationError = true ;
127- guiManager .showLocalizedNotification (Constants .SERIAL_PORT_AMBIGUOUS ,
128- Constants .SERIAL_PORT_AMBIGUOUS_CONTEXT , Constants .SERIAL_ERROR_TITLE , TrayIcon .MessageType .ERROR );
129- log .error (Constants .SERIAL_ERROR_OPEN_HEADER );
130- }
131- log .info ("Connected: Serial {}" , MainSingleton .getInstance ().serial .getDescriptivePortName ());
132- if (MainSingleton .getInstance ().guiManager != null ) {
133- MainSingleton .getInstance ().guiManager .trayIconManager .resetTray ();
134- }
135- MainSingleton .getInstance ().serialConnected = true ;
136- MainSingleton .getInstance ().communicationError = false ;
137- MainSingleton .getInstance ().output = MainSingleton .getInstance ().serial .getOutputStream ();
138- listenSerialEvents ();
139- } else {
140- MainSingleton .getInstance ().communicationError = true ;
141- }
99+ openSerial (portName , baudrate , readTimeout , writeTimeout , numberOfSerialDevices );
142100 } catch (Exception e ) {
143101 log .error (e .getMessage ());
144102 MainSingleton .getInstance ().communicationError = true ;
@@ -150,6 +108,73 @@ public void initSerial(String portName, String baudrate) {
150108 }
151109 }
152110
111+ /**
112+ * Open serial
113+ *
114+ * @param portName serial port
115+ * @param baudrate baudrate
116+ * @param readTimeout read timeout
117+ * @param writeTimeout write timeout
118+ * @param numberOfSerialDevices number of serial devices
119+ */
120+ private void openSerial (String portName , String baudrate , int readTimeout , int writeTimeout , int numberOfSerialDevices ) {
121+ if (MainSingleton .getInstance ().serial != null && MainSingleton .getInstance ().serial .openPort ()) {
122+ int baudrateToUse = baudrate .isEmpty () ? Integer .parseInt (MainSingleton .getInstance ().config .getBaudRate ()) : Integer .parseInt (baudrate );
123+ MainSingleton .getInstance ().serial .setComPortParameters (baudrateToUse , 8 , 1 , SerialPort .NO_PARITY );
124+ MainSingleton .getInstance ().serial .setComPortTimeouts (SerialPort .TIMEOUT_READ_SEMI_BLOCKING , readTimeout , writeTimeout );
125+ log .info ("{}{}" , CommonUtility .getWord (Constants .SERIAL_PORT_IN_USE ), MainSingleton .getInstance ().serial .getSystemPortName ());
126+ GlowWormDevice gwDevice = createDefaultDevice ();
127+ GuiSingleton .getInstance ().deviceTableData .add (gwDevice );
128+ GuiManager guiManager = new GuiManager ();
129+ if (numberOfSerialDevices > 1 && MainSingleton .getInstance ().config .getOutputDevice ().equals (Constants .SERIAL_PORT_AUTO ) && portName .isEmpty ()) {
130+ MainSingleton .getInstance ().communicationError = true ;
131+ guiManager .showLocalizedNotification (Constants .SERIAL_PORT_AMBIGUOUS , Constants .SERIAL_PORT_AMBIGUOUS_CONTEXT , Constants .SERIAL_ERROR_TITLE , TrayIcon .MessageType .ERROR );
132+ log .error (Constants .SERIAL_ERROR_OPEN_HEADER );
133+ }
134+ log .info ("Connected: Serial {}" , MainSingleton .getInstance ().serial .getDescriptivePortName ());
135+ if (MainSingleton .getInstance ().guiManager != null ) {
136+ MainSingleton .getInstance ().guiManager .trayIconManager .resetTray ();
137+ }
138+ MainSingleton .getInstance ().serialConnected = true ;
139+ MainSingleton .getInstance ().communicationError = false ;
140+ MainSingleton .getInstance ().output = MainSingleton .getInstance ().serial .getOutputStream ();
141+ listenSerialEvents ();
142+ } else {
143+ MainSingleton .getInstance ().communicationError = true ;
144+ }
145+ }
146+
147+ /**
148+ * Create a default device
149+ *
150+ * @return default device
151+ */
152+ private GlowWormDevice createDefaultDevice () {
153+ GlowWormDevice gwDevice = new GlowWormDevice ();
154+ gwDevice .setDeviceName (Constants .USB_DEVICE );
155+ gwDevice .setDeviceIP (MainSingleton .getInstance ().serial .getSystemPortName ());
156+ gwDevice .setDhcpInUse (false );
157+ gwDevice .setWifi (Constants .DASH );
158+ gwDevice .setDeviceVersion (Constants .DASH );
159+ gwDevice .setDeviceBoard (Constants .DASH );
160+ gwDevice .setMac (Constants .DASH );
161+ gwDevice .setGpio (Constants .DASH );
162+ gwDevice .setNumberOfLEDSconnected (Constants .DASH );
163+ gwDevice .setLastSeen (MainSingleton .getInstance ().formatter .format (new Date ()));
164+ gwDevice .setFirmwareType (Constants .DASH );
165+ gwDevice .setBaudRate (Constants .DASH );
166+ gwDevice .setMqttTopic (Constants .DASH );
167+ gwDevice .setColorMode (Constants .DASH );
168+ gwDevice .setColorOrder (Enums .ColorOrder .GRB_GRBW .name ());
169+ gwDevice .setLdrValue (Constants .DASH );
170+ gwDevice .setRelayPin (Constants .DASH );
171+ gwDevice .setRelayInvertedPin (false );
172+ gwDevice .setSbPin (Constants .DASH );
173+ gwDevice .setLdrPin (Constants .DASH );
174+ gwDevice .setGpioClock (Constants .DASH );
175+ return gwDevice ;
176+ }
177+
153178 /**
154179 * Add a listener on USB ports
155180 */
0 commit comments