Browse Source

last functions

master
gituser 10 months ago
parent
commit
12f8f78b0e

+ 1
- 1
src/main/java/ch/spherICIT/communication/ConnectionState.java View File

@@ -1,5 +1,5 @@
package ch.spherICIT.communication;

public enum ConnectionState {
READY, CONNECTING, CONNECTED, TIMEOUT
READY, CONNECTING, CONNECTED, TIMEOUT, WRONG_SW_VERSION
}

+ 24
- 0
src/main/java/ch/spherICIT/communication/SerialCommHandler.java View File

@@ -26,6 +26,12 @@ public class SerialCommHandler {
return serializedMsg;
}

public static String sendCamStatusCommand(SerialPort serialPort) {
String serializedMsg = getSerializedCmdString(Message.CMD_STATUS_CAM, null);
send(serialPort, serializedMsg);
return serializedMsg;
}

public static String sendCamZeroCommand(SerialPort serialPort) {
String serializedMsg = getSerializedCmdString(Message.CMD_ZERO_CAM, "0");
send(serialPort, serializedMsg);
@@ -50,6 +56,24 @@ public class SerialCommHandler {
return serializedMsg;
}

public static String sendCamVelocityStatusCommand(SerialPort serialPort) {
String serializedMsg = getSerializedCmdString(Message.CMD_STATUS_VELOCITY_CAM,"");
send(serialPort, serializedMsg);
return serializedMsg;
}

public static String sendRotateCommand(SerialPort serialPort, String val) {
String serializedMsg = getSerializedCmdString(Message.CMD_AZIMUT_CAM, val);
send(serialPort, serializedMsg);
return serializedMsg;
}

public static String sendActualAzimutCommand(SerialPort serialPort) {
String serializedMsg = getSerializedCmdString(Message.CMD_ACT_AZIMUT_CAM, "");
send(serialPort, serializedMsg);
return serializedMsg;
}

private static void send(SerialPort serialPort, String serializedMsg) {
if (serialPort.isOpen()) {
serialPort.writeBytes(serializedMsg.getBytes(), serializedMsg.length());

+ 38
- 0
src/main/java/ch/spherICIT/communication/StatusLoopWorker.java View File

@@ -0,0 +1,38 @@
package ch.spherICIT.communication;

import com.fazecast.jSerialComm.SerialPort;

import javax.swing.SwingWorker;

public class StatusLoopWorker extends SwingWorker<Void, Void> {

private SerialPort serialPort;

public StatusLoopWorker(SerialPort serialPort) {
this.serialPort = serialPort;
}

@Override
protected Void doInBackground() throws Exception {

long counter = 0;

while (counter < 4) {
if (isCancelled()) {
break;
}
if (counter == 0) {
SerialCommHandler.sendRelaisStatusCommand(this.serialPort);
} else if (counter == 1) {
SerialCommHandler.sendCamStatusCommand(this.serialPort);
} else if (counter == 2) {
SerialCommHandler.sendCamVelocityStatusCommand(this.serialPort);
} else {
SerialCommHandler.sendActualAzimutCommand(this.serialPort);
}
counter++;
Thread.sleep(3000);
}
return null;
}
}

+ 9
- 1
src/main/java/ch/spherICIT/components/statusbar/FSStatusBar.java View File

@@ -1,8 +1,8 @@
package ch.spherICIT.components.statusbar;

import ch.spherICIT.communication.ConnectionState;
import ch.spherICIT.communication.IConnectionState;
import ch.spherICIT.communication.ConnectionStateInfo;
import ch.spherICIT.communication.IConnectionState;
import com.formdev.flatlaf.ui.FlatLineBorder;
import com.formdev.flatlaf.util.StringUtils;

@@ -27,6 +27,8 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;

import static ch.spherICIT.main.MainPanel.MIN_ARDUINO_VERSION;

public class FSStatusBar implements IConnectionState {


@@ -64,6 +66,7 @@ public class FSStatusBar implements IConnectionState {
this.reconnectBtn.setVisible(false);
this.portValueLbl.setText(" ");
this.versionValueLbl.setText(" ");
this.versionValueLbl.setForeground(Color.BLACK);
this.connectionStatePrgBar.setValue(0);
this.connectionStatePrgBar.setIndeterminate(false);
if (connectionStateInfo.connectionState().equals(ConnectionState.READY)) {
@@ -79,6 +82,11 @@ public class FSStatusBar implements IConnectionState {
this.connectionStatePrgBar.setValue(100);
this.portValueLbl.setText(StringUtils.isEmpty(connectionStateInfo.port()) ? " " : connectionStateInfo.port());
this.versionValueLbl.setText(StringUtils.isEmpty(connectionStateInfo.version()) ? " " : connectionStateInfo.version());
} else if (connectionStateInfo.connectionState().equals(ConnectionState.WRONG_SW_VERSION)) {
this.connectionStateLbl.setText("Falsche SW-Version!");
this.connectionStatePrgBar.setString(MIN_ARDUINO_VERSION);
this.versionValueLbl.setText(connectionStateInfo.version());
this.versionValueLbl.setForeground(Color.RED);
} else {
this.connectionStateLbl.setText("Zeitüberschreitung!");
this.connectionStatePrgBar.setString("Nicht verbunden");

+ 33
- 2
src/main/java/ch/spherICIT/components/switchbtn/EventSwitchSelected.java View File

@@ -1,6 +1,37 @@
package ch.spherICIT.components.switchbtn;

public interface EventSwitchSelected {
public abstract class EventSwitchSelected {

public void onSelected(boolean selected);
private boolean suspended;

public void onSelected(boolean selected) {
if (!isSuspended()) {
onSwitchSelected(selected);
}
}

/**
* Checks if this listenrer is in suspended mode.
*
* @return True if <code>suspended</code>, otherwise false
*/
public synchronized boolean isSuspended() {
return suspended;
}

/**
* Sets this listener to suspended mode, means the value of <code>suspended</code> is set to true.
*/
public synchronized void suspend() {
this.suspended = true;
}

/**
* Resumes this listener from suspended mode, means the value of <code>suspended</code> is set to false.
*/
public synchronized void resume() {
this.suspended = false;
}

public abstract void onSwitchSelected(boolean selected);
}

+ 4
- 0
src/main/java/ch/spherICIT/components/switchbtn/SwitchButton.java View File

@@ -138,4 +138,8 @@ public class SwitchButton extends JComponent {
public void addEventSelected(EventSwitchSelected event) {
events.add(event);
}

public EventSwitchSelected getFirstSwitchListener() {
return this.events.getFirst();
}
}

+ 62
- 1
src/main/java/ch/spherICIT/main/MainPanel.form View File

@@ -4,7 +4,9 @@
<constraints>
<xy x="20" y="20" width="814" height="542"/>
</constraints>
<properties/>
<properties>
<opaque value="true"/>
</properties>
<border type="none"/>
<children>
<hspacer id="6aa04">
@@ -368,6 +370,65 @@
<text value=""/>
</properties>
</component>
<component id="74466" class="javax.swing.JSlider" binding="rotationSlider">
<constraints>
<grid row="2" column="3" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="3" indent="0" use-parent-layout="false"/>
<gridbag top="0" left="10" bottom="0" right="10" weightx="0.0" weighty="0.0"/>
</constraints>
<properties>
<font name="Bahnschrift" size="14" style="0"/>
<majorTickSpacing value="60"/>
<maximum value="180"/>
<minimum value="-180"/>
<minorTickSpacing value="30"/>
<paintLabels value="true"/>
<paintTicks value="true"/>
<snapToTicks value="false"/>
<value value="0"/>
</properties>
</component>
<grid id="dd15a" layout-manager="GridBagLayout">
<constraints>
<grid row="3" column="3" row-span="2" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e73fe" class="javax.swing.JTextField" binding="rotationValTxtFld">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
<properties>
<background color="-16777216"/>
<caretColor color="-14690702"/>
<disabledTextColor color="-14690702"/>
<editable value="false"/>
<enabled value="false"/>
<font name="Bahnschrift" size="14" style="0"/>
<foreground color="-14690702"/>
<horizontalAlignment value="0"/>
<preferredSize width="100" height="36"/>
<selectedTextColor color="-14690702"/>
<text value="0°"/>
</properties>
</component>
<component id="d53d9" class="javax.swing.JButton" binding="rotateBtn">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<gridbag top="10" left="10" bottom="10" right="10" weightx="1.0" weighty="0.0"/>
</constraints>
<properties>
<icon value="rotate.png"/>
<preferredSize width="85" height="70"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

+ 126
- 10
src/main/java/ch/spherICIT/main/MainPanel.java View File

@@ -4,6 +4,7 @@ import ch.spherICIT.communication.ConnectionState;
import ch.spherICIT.communication.ConnectionStateInfo;
import ch.spherICIT.communication.IConnectionState;
import ch.spherICIT.communication.SerialCommHandler;
import ch.spherICIT.communication.StatusLoopWorker;
import ch.spherICIT.components.led.Led;
import ch.spherICIT.components.switchbtn.EventSwitchSelected;
import ch.spherICIT.components.switchbtn.SwitchButton;
@@ -19,7 +20,11 @@ import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.FontUIResource;
import javax.swing.text.StyleContext;
import java.awt.Color;
@@ -48,6 +53,7 @@ import static ch.spherICIT.main.LogWindow.SendDirection.OUT;
public class MainPanel {

public static final long COMM_TIMEOUT = 3000L;
public static final String MIN_ARDUINO_VERSION = "V1.3.0";

private JPanel mainPanel;
private JLabel serPortLbl;
@@ -61,6 +67,9 @@ public class MainPanel {
private Led relaisPwrLed;
private Led camStepperPwrLed;
private JButton logWindowBtn;
private JSlider rotationSlider;
private JTextField rotationValTxtFld;
private JButton rotateBtn;
private final LogWindow logWindow;

private Set<IConnectionState> connectionStateListeners;
@@ -69,6 +78,7 @@ public class MainPanel {
private SerialPort selectedSerialPort;
private FSSerialPortMessageListener serialPortMessageListener;
private Timer timer;
private StatusLoopWorker statusLoopWorker;

/**
* Konstruktor.
@@ -115,6 +125,10 @@ public class MainPanel {
camVelocityModeBtn.setEnabled(false);
camVelocityModeBtn.setSelected(false);

if (statusLoopWorker != null) {
statusLoopWorker.cancel(true);
}

for (IConnectionState listener : connectionStateListeners) {
listener.connectionStateChanged(new ConnectionStateInfo(ConnectionState.READY, null, null));
}
@@ -126,6 +140,8 @@ public class MainPanel {
@Override
public void actionPerformed(ActionEvent e) {
logWindow.addLogEntry(SerialCommHandler.sendCamZeroCommand(selectedSerialPort), OUT);
rotationSlider.setValue(0);
rotationValTxtFld.setText("0°");
}
});

@@ -138,25 +154,38 @@ public class MainPanel {

this.relaisPwrBtn.addEventSelected(new EventSwitchSelected() {
@Override
public void onSelected(boolean selected) {
public void onSwitchSelected(boolean selected) {
logWindow.addLogEntry(SerialCommHandler.sendRelaisPwrCommand(selectedSerialPort, selected ? Message.CMD_VAL_ON : Message.CMD_VAL_OFF), OUT);
}
});

this.camStepperPwrBtn.addEventSelected(new EventSwitchSelected() {
@Override
public void onSelected(boolean selected) {
public void onSwitchSelected(boolean selected) {
logWindow.addLogEntry(SerialCommHandler.sendCamPwrCommand(selectedSerialPort, selected ? Message.CMD_VAL_ON : Message.CMD_VAL_OFF), OUT);
}
});

this.camVelocityModeBtn.addEventSelected(new EventSwitchSelected() {
@Override
public void onSelected(boolean selected) {
public void onSwitchSelected(boolean selected) {
logWindow.addLogEntry(SerialCommHandler.sendCamVelocityCommand(selectedSerialPort, selected ? Message.CMD_VELOCITY_FAST : Message.CMD_VELOCITY_SLOW), OUT);
}
});

this.rotationSlider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
rotationValTxtFld.setText(Integer.toString(rotationSlider.getValue()) + "°");
}
});

this.rotateBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
logWindow.addLogEntry(SerialCommHandler.sendRotateCommand(selectedSerialPort, Integer.toString(rotationSlider.getValue())), OUT);
}
});
}

public void registerConnectionStateListener(IConnectionState connectionStateListener) {
@@ -326,22 +355,52 @@ public class MainPanel {
if (deserializedMsg.getSender().equals(Message.CMD_SENDER_AN) && deserializedMsg.getCmd().equals(Message.CMD_VER)) {
// connected
timer.cancel();
SwingUtilities.invokeLater(() -> {
for (IConnectionState listener : connectionStateListeners) {
listener.connectionStateChanged(new ConnectionStateInfo(ConnectionState.CONNECTED, selectedSerialPort.getSystemPortName(), deserializedMsg.getVal()));
if (deserializedMsg.getVal().equals(MIN_ARDUINO_VERSION)) {
SwingUtilities.invokeLater(() -> {
for (IConnectionState listener : connectionStateListeners) {
listener.connectionStateChanged(new ConnectionStateInfo(ConnectionState.CONNECTED, selectedSerialPort.getSystemPortName(), deserializedMsg.getVal()));
}
});
relaisPwrBtn.setEnabled(true);
camStepperPwrBtn.setEnabled(true);
camVelocityModeBtn.setEnabled(true);
if (statusLoopWorker != null) {
statusLoopWorker.cancel(true);
}
});
relaisPwrBtn.setEnabled(true);
camStepperPwrBtn.setEnabled(true);
camVelocityModeBtn.setEnabled(true);
statusLoopWorker = new StatusLoopWorker(selectedSerialPort);
statusLoopWorker.execute();
} else {
SwingUtilities.invokeLater(() -> {
for (IConnectionState listener : connectionStateListeners) {
listener.connectionStateChanged(new ConnectionStateInfo(ConnectionState.WRONG_SW_VERSION, selectedSerialPort.getSystemPortName(), deserializedMsg.getVal()));
}
});
}
} else if (deserializedMsg.getSender().equals(Message.CMD_SENDER_AN) && deserializedMsg.getCmd().equals(Message.CMD_STATUS_RELAY)) {
//Power relais status
String val = deserializedMsg.getVal();
relaisPwrBtn.getFirstSwitchListener().suspend();
relaisPwrBtn.setSelected(val.equals(Message.CMD_VAL_ON));
relaisPwrBtn.getFirstSwitchListener().resume();
relaisPwrLed.doSwitch(val);
} else if (deserializedMsg.getSender().equals(Message.CMD_SENDER_AN) && deserializedMsg.getCmd().equals(Message.CMD_STATUS_CAM)) {
//Cam relais status
String val = deserializedMsg.getVal();
camStepperPwrBtn.getFirstSwitchListener().suspend();
camStepperPwrBtn.setSelected(val.equals(Message.CMD_VAL_ON));
camStepperPwrBtn.getFirstSwitchListener().resume();
camStepperPwrLed.doSwitch(val);
} else if (deserializedMsg.getSender().equals(Message.CMD_SENDER_AN) && deserializedMsg.getCmd().equals(Message.CMD_STATUS_VELOCITY_CAM)) {
//Cam velocity status
String val = deserializedMsg.getVal();
camVelocityModeBtn.getFirstSwitchListener().suspend();
camVelocityModeBtn.setSelected(val.equals(Message.CMD_VAL_ON));
camVelocityModeBtn.getFirstSwitchListener().resume();
} else if (deserializedMsg.getSender().equals(Message.CMD_SENDER_AN) && deserializedMsg.getCmd().equals(Message.CMD_ACT_AZIMUT_CAM)) {
//Actual azimut status
String val = deserializedMsg.getVal();
rotationSlider.setValue(Integer.parseInt(val));
rotationValTxtFld.setText(val + "°");
}
} else if (serialPortEvent.getEventType() == SerialPort.LISTENING_EVENT_DATA_WRITTEN) {
System.out.println("serialEvent: LISTENING_EVENT_DATA_WRITTEN");
@@ -361,6 +420,7 @@ public class MainPanel {
createUIComponents();
mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
mainPanel.setOpaque(true);
final JPanel spacer1 = new JPanel();
GridBagConstraints gbc;
gbc = new GridBagConstraints();
@@ -726,6 +786,62 @@ public class MainPanel {
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.insets = new Insets(10, 10, 10, 10);
mainPanel.add(logWindowBtn, gbc);
rotationSlider = new JSlider();
Font rotationSliderFont = this.$$$getFont$$$("Bahnschrift", Font.PLAIN, 14, rotationSlider.getFont());
if (rotationSliderFont != null) rotationSlider.setFont(rotationSliderFont);
rotationSlider.setMajorTickSpacing(60);
rotationSlider.setMaximum(180);
rotationSlider.setMinimum(-180);
rotationSlider.setMinorTickSpacing(30);
rotationSlider.setPaintLabels(true);
rotationSlider.setPaintTicks(true);
rotationSlider.setSnapToTicks(false);
rotationSlider.setValue(0);
gbc = new GridBagConstraints();
gbc.gridx = 3;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(0, 10, 0, 10);
mainPanel.add(rotationSlider, gbc);
final JPanel panel5 = new JPanel();
panel5.setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.gridx = 3;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.gridheight = 2;
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(panel5, gbc);
rotationValTxtFld = new JTextField();
rotationValTxtFld.setBackground(new Color(-16777216));
rotationValTxtFld.setCaretColor(new Color(-14690702));
rotationValTxtFld.setDisabledTextColor(new Color(-14690702));
rotationValTxtFld.setEditable(false);
rotationValTxtFld.setEnabled(false);
Font rotationValTxtFldFont = this.$$$getFont$$$("Bahnschrift", Font.PLAIN, 14, rotationValTxtFld.getFont());
if (rotationValTxtFldFont != null) rotationValTxtFld.setFont(rotationValTxtFldFont);
rotationValTxtFld.setForeground(new Color(-14690702));
rotationValTxtFld.setHorizontalAlignment(0);
rotationValTxtFld.setPreferredSize(new Dimension(100, 36));
rotationValTxtFld.setSelectedTextColor(new Color(-14690702));
rotationValTxtFld.setText("0°");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
panel5.add(rotationValTxtFld, gbc);
rotateBtn = new JButton();
rotateBtn.setIcon(new ImageIcon(getClass().getResource("/rotate.png")));
rotateBtn.setPreferredSize(new Dimension(85, 70));
rotateBtn.setText("");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(10, 10, 10, 10);
panel5.add(rotateBtn, gbc);
}

/**

+ 1
- 0
src/main/java/ch/spherICIT/main/Message.java View File

@@ -12,6 +12,7 @@ public class Message {
public static String CMD_AZIMUT_CAM = "Azimut_Cam";
public static String CMD_ACT_AZIMUT_CAM = "Actual_Azimut_Cam";
public static String CMD_VELOCITY_CAM = "Velocity_Cam";
public static String CMD_STATUS_VELOCITY_CAM = "Status_Velocity_Cam";
public static String CMD_ZERO_CAM = "Zero_Cam";
public static String CMD_VAL_ON = "On";
public static String CMD_VAL_OFF = "Off";

+ 3
- 0
src/main/resources/META-INF/MANIFEST.MF View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: ch.spherICIT.main.MainApp


BIN
src/main/resources/rotate.png View File


Loading…
Cancel
Save