|
|
|
|
|
|
|
|
import javax.swing.plaf.FontUIResource; |
|
|
import javax.swing.plaf.FontUIResource; |
|
|
import javax.swing.text.StyleContext; |
|
|
import javax.swing.text.StyleContext; |
|
|
import java.awt.Color; |
|
|
import java.awt.Color; |
|
|
|
|
|
import java.awt.Cursor; |
|
|
import java.awt.Dimension; |
|
|
import java.awt.Dimension; |
|
|
import java.awt.Font; |
|
|
import java.awt.Font; |
|
|
import java.awt.GridBagConstraints; |
|
|
import java.awt.GridBagConstraints; |
|
|
|
|
|
|
|
|
private List<CommPort> commPorts; |
|
|
private List<CommPort> commPorts; |
|
|
private SerialPort selectedSerialPort; |
|
|
private SerialPort selectedSerialPort; |
|
|
private FSSerialPortMessageListener serialPortMessageListener; |
|
|
private FSSerialPortMessageListener serialPortMessageListener; |
|
|
private Timer timer; |
|
|
|
|
|
|
|
|
private Timer timeoutTimer; |
|
|
private StatusLoopWorker statusLoopWorker; |
|
|
private StatusLoopWorker statusLoopWorker; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
|
|
|
|
|
|
this.reconnectActionListener = new ActionListener() { |
|
|
this.reconnectActionListener = new ActionListener() { |
|
|
@Override |
|
|
@Override |
|
|
public void actionPerformed(ActionEvent e) { |
|
|
public void actionPerformed(ActionEvent e) { |
|
|
connect(); |
|
|
|
|
|
|
|
|
initConnection(); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
this.serPortOpenBtn.addActionListener(new ActionListener() { |
|
|
this.serPortOpenBtn.addActionListener(new ActionListener() { |
|
|
@Override |
|
|
@Override |
|
|
public void actionPerformed(ActionEvent e) { |
|
|
public void actionPerformed(ActionEvent e) { |
|
|
if (!selectedSerialPort.isOpen()) { |
|
|
|
|
|
System.out.println("connecten clicked"); |
|
|
|
|
|
selectedSerialPort.openPort(); |
|
|
|
|
|
logWindow.addLogEntry(SerialCommHandler.sendInitCommand(selectedSerialPort), OUT); |
|
|
|
|
|
} |
|
|
|
|
|
connect(); |
|
|
|
|
|
|
|
|
System.out.println("connecten clicked"); |
|
|
|
|
|
initConnection(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onSwitchSelected(boolean selected) { |
|
|
public void onSwitchSelected(boolean selected) { |
|
|
logWindow.addLogEntry(SerialCommHandler.sendCamPwrCommand(selectedSerialPort, selected ? Message.CMD_VAL_ON : Message.CMD_VAL_OFF), OUT); |
|
|
logWindow.addLogEntry(SerialCommHandler.sendCamPwrCommand(selectedSerialPort, selected ? Message.CMD_VAL_ON : Message.CMD_VAL_OFF), OUT); |
|
|
|
|
|
rotateBtn.setEnabled(selected); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return this.serPortCboBox.getSelectedIndex() != -1 ? Optional.of(this.serPortCboBox.getItemAt(this.serPortCboBox.getSelectedIndex())) : Optional.empty(); |
|
|
return this.serPortCboBox.getSelectedIndex() != -1 ? Optional.of(this.serPortCboBox.getItemAt(this.serPortCboBox.getSelectedIndex())) : Optional.empty(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void initConnection() { |
|
|
|
|
|
for (IConnectionState listener : connectionStateListeners) { |
|
|
|
|
|
listener.connectionStateChanged(new ConnectionStateInfo(ConnectionState.CONNECTING, selectedSerialPort.getSystemPortName(), null)); |
|
|
|
|
|
} |
|
|
|
|
|
SwingUtilities.invokeLater(() -> { |
|
|
|
|
|
connect(); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private void connect() { |
|
|
private void connect() { |
|
|
if (this.selectedSerialPort != null) { |
|
|
|
|
|
if (this.selectedSerialPort.openPort()) { |
|
|
|
|
|
this.timer = new Timer(); |
|
|
|
|
|
this.timer.schedule(new TimerTask() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void run() { |
|
|
|
|
|
SwingUtilities.invokeLater(() -> { |
|
|
|
|
|
for (IConnectionState listener : connectionStateListeners) { |
|
|
|
|
|
listener.connectionStateChanged(new ConnectionStateInfo(ConnectionState.TIMEOUT, null, null)); |
|
|
|
|
|
} |
|
|
|
|
|
serPortOpenBtn.setEnabled(true); |
|
|
|
|
|
serPortCloseBtn.setEnabled(false); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}, COMM_TIMEOUT); |
|
|
|
|
|
|
|
|
if (this.selectedSerialPort != null && this.selectedSerialPort.openPort()) { |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
this.mainPanel.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
|
|
|
|
|
this.logWindow.addLogEntry(SerialCommHandler.sendInitCommand(this.selectedSerialPort), OUT); |
|
|
|
|
|
Thread.sleep(3000); |
|
|
|
|
|
} catch (InterruptedException ex) { |
|
|
|
|
|
throw new RuntimeException(ex); |
|
|
|
|
|
} finally { |
|
|
|
|
|
this.mainPanel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
for (IConnectionState listener : this.connectionStateListeners) { |
|
|
|
|
|
listener.connectionStateChanged(new ConnectionStateInfo(ConnectionState.CONNECTING, selectedSerialPort.getSystemPortName(), null)); |
|
|
|
|
|
|
|
|
this.timeoutTimer = new Timer(); |
|
|
|
|
|
this.timeoutTimer.schedule(new TimerTask() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void run() { |
|
|
|
|
|
SwingUtilities.invokeLater(() -> { |
|
|
|
|
|
for (IConnectionState listener : connectionStateListeners) { |
|
|
|
|
|
listener.connectionStateChanged(new ConnectionStateInfo(ConnectionState.TIMEOUT, null, null)); |
|
|
|
|
|
} |
|
|
|
|
|
serPortOpenBtn.setEnabled(true); |
|
|
|
|
|
serPortCloseBtn.setEnabled(false); |
|
|
|
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
}, COMM_TIMEOUT); |
|
|
|
|
|
|
|
|
SwingUtilities.invokeLater(() -> { |
|
|
|
|
|
logWindow.addLogEntry(SerialCommHandler.sendVersionCommand(selectedSerialPort), |
|
|
|
|
|
OUT); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
SwingUtilities.invokeLater(() -> { |
|
|
|
|
|
logWindow.addLogEntry(SerialCommHandler.sendVersionCommand(selectedSerialPort), OUT); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
this.serPortOpenBtn.setEnabled(false); |
|
|
|
|
|
this.serPortCloseBtn.setEnabled(true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
this.serPortOpenBtn.setEnabled(false); |
|
|
|
|
|
this.serPortCloseBtn.setEnabled(true); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
selectedSerialPort = selCommPortOptional.get().getSerialPort().get(); |
|
|
selectedSerialPort = selCommPortOptional.get().getSerialPort().get(); |
|
|
selectedSerialPort.setComPortParameters(9600, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY); |
|
|
selectedSerialPort.setComPortParameters(9600, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY); |
|
|
selectedSerialPort.addDataListener(serialPortMessageListener); |
|
|
selectedSerialPort.addDataListener(serialPortMessageListener); |
|
|
selectedSerialPort.openPort(); |
|
|
|
|
|
SwingUtilities.invokeLater(() -> { |
|
|
|
|
|
logWindow.addLogEntry(SerialCommHandler.sendInitCommand(selectedSerialPort), OUT); |
|
|
|
|
|
}); |
|
|
|
|
|
serPortOpenBtn.setEnabled(true); |
|
|
serPortOpenBtn.setEnabled(true); |
|
|
} else { |
|
|
} else { |
|
|
if (selectedSerialPort != null) { |
|
|
if (selectedSerialPort != null) { |
|
|
|
|
|
|
|
|
logWindow.addLogEntry(input, IN); |
|
|
logWindow.addLogEntry(input, IN); |
|
|
if (deserializedMsg.getSender().equals(Message.CMD_SENDER_AN) && deserializedMsg.getCmd().equals(Message.CMD_VER)) { |
|
|
if (deserializedMsg.getSender().equals(Message.CMD_SENDER_AN) && deserializedMsg.getCmd().equals(Message.CMD_VER)) { |
|
|
// connected |
|
|
// connected |
|
|
timer.cancel(); |
|
|
|
|
|
|
|
|
timeoutTimer.cancel(); |
|
|
if (deserializedMsg.getVal().equals(MIN_ARDUINO_VERSION)) { |
|
|
if (deserializedMsg.getVal().equals(MIN_ARDUINO_VERSION)) { |
|
|
SwingUtilities.invokeLater(() -> { |
|
|
SwingUtilities.invokeLater(() -> { |
|
|
for (IConnectionState listener : connectionStateListeners) { |
|
|
for (IConnectionState listener : connectionStateListeners) { |
|
|
|
|
|
|
|
|
String val = deserializedMsg.getVal(); |
|
|
String val = deserializedMsg.getVal(); |
|
|
camStepperPwrBtn.getFirstSwitchListener().suspend(); |
|
|
camStepperPwrBtn.getFirstSwitchListener().suspend(); |
|
|
camStepperPwrBtn.setSelected(val.equals(Message.CMD_VAL_ON)); |
|
|
camStepperPwrBtn.setSelected(val.equals(Message.CMD_VAL_ON)); |
|
|
|
|
|
rotateBtn.setEnabled(camStepperPwrBtn.isSelected()); |
|
|
camStepperPwrBtn.getFirstSwitchListener().resume(); |
|
|
camStepperPwrBtn.getFirstSwitchListener().resume(); |
|
|
camStepperPwrLed.doSwitch(val); |
|
|
camStepperPwrLed.doSwitch(val); |
|
|
} else if (deserializedMsg.getSender().equals(Message.CMD_SENDER_AN) && deserializedMsg.getCmd().equals(Message.CMD_STATUS_VELOCITY_CAM)) { |
|
|
} else if (deserializedMsg.getSender().equals(Message.CMD_SENDER_AN) && deserializedMsg.getCmd().equals(Message.CMD_STATUS_VELOCITY_CAM)) { |
|
|
//Cam velocity status |
|
|
//Cam velocity status |
|
|
String val = deserializedMsg.getVal(); |
|
|
String val = deserializedMsg.getVal(); |
|
|
camVelocityModeBtn.getFirstSwitchListener().suspend(); |
|
|
camVelocityModeBtn.getFirstSwitchListener().suspend(); |
|
|
camVelocityModeBtn.setSelected(val.equals(Message.CMD_VAL_ON)); |
|
|
|
|
|
|
|
|
camVelocityModeBtn.setSelected(val.equals(Message.CMD_VELOCITY_FAST)); |
|
|
camVelocityModeBtn.getFirstSwitchListener().resume(); |
|
|
camVelocityModeBtn.getFirstSwitchListener().resume(); |
|
|
} else if (deserializedMsg.getSender().equals(Message.CMD_SENDER_AN) && deserializedMsg.getCmd().equals(Message.CMD_ACT_AZIMUT_CAM)) { |
|
|
} else if (deserializedMsg.getSender().equals(Message.CMD_SENDER_AN) && deserializedMsg.getCmd().equals(Message.CMD_ACT_AZIMUT_CAM)) { |
|
|
//Actual azimut status |
|
|
//Actual azimut status |
|
|
|
|
|
|
|
|
gbc.gridy = 0; |
|
|
gbc.gridy = 0; |
|
|
panel5.add(rotationValTxtFld, gbc); |
|
|
panel5.add(rotationValTxtFld, gbc); |
|
|
rotateBtn = new JButton(); |
|
|
rotateBtn = new JButton(); |
|
|
|
|
|
rotateBtn.setEnabled(false); |
|
|
rotateBtn.setIcon(new ImageIcon(getClass().getResource("/rotate.png"))); |
|
|
rotateBtn.setIcon(new ImageIcon(getClass().getResource("/rotate.png"))); |
|
|
rotateBtn.setPreferredSize(new Dimension(85, 70)); |
|
|
rotateBtn.setPreferredSize(new Dimension(85, 70)); |
|
|
rotateBtn.setText(""); |
|
|
rotateBtn.setText(""); |
|
|
|
|
|
|
|
|
gbc.gridy = 1; |
|
|
gbc.gridy = 1; |
|
|
gbc.weightx = 1.0; |
|
|
gbc.weightx = 1.0; |
|
|
gbc.fill = GridBagConstraints.HORIZONTAL; |
|
|
gbc.fill = GridBagConstraints.HORIZONTAL; |
|
|
gbc.insets = new Insets(10, 10, 10, 10); |
|
|
|
|
|
|
|
|
gbc.insets = new Insets(10, 20, 10, 20); |
|
|
panel5.add(rotateBtn, gbc); |
|
|
panel5.add(rotateBtn, gbc); |
|
|
} |
|
|
} |
|
|
|
|
|
|