diff --git a/src/bitbyte/MyJFrame.java b/src/bitbyte/MyJFrame.java
index 8023bfb..0aea87c 100644
--- a/src/bitbyte/MyJFrame.java
+++ b/src/bitbyte/MyJFrame.java
@@ -3,31 +3,21 @@
import javax.swing.JFrame;
import rtu_location.TableCableColor;
import rtu_location.TableRTU;
-import utils.ListenerRaw;
import utils.MeuListener;
/**
- * * @author menon
+ * @author menon
*/
public class MyJFrame extends javax.swing.JFrame {
public MyJFrame() {
- /**
- * * Creates new form MyJFrame
- */
initComponents();
- btPTNO.addActionListener(new MeuListener("btPTNO", t1, t3));
- btBitByte.addActionListener(new MeuListener("btBitByte", t2, t4));
- btLimparTudo.addActionListener(new MeuListener("btLimparPTNO", t1, t3));
- btLimparTudo.addActionListener(new MeuListener("btLimparBit", t2, t4));
- //Raw
- }
-
- /**
- * * This method is called from within the constructor to initialize the
- * form. WARNING: Do NOT modify this code. The content of this method is
- * always regenerated by the Form Editor.
- */
+ btPTNO.addActionListener(new MeuListener("btPTNO", bitByteInput, ptnoOutput, errorLabel));
+ btBitByte.addActionListener(new MeuListener("btBitByte", ptnoInput, bitByteOutput, errorLabel));
+ btLimparTudo.addActionListener(new MeuListener("btLimparPTNO", bitByteInput, ptnoOutput, errorLabel));
+ btLimparTudo.addActionListener(new MeuListener("btLimparBit", ptnoInput, bitByteOutput, errorLabel));
+ }
+
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {
@@ -39,20 +29,21 @@ private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
- t1 = new javax.swing.JTextField();
+ bitByteInput = new javax.swing.JTextField();
btPTNO = new javax.swing.JButton();
- t3 = new javax.swing.JTextField();
+ ptnoOutput = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
- t2 = new javax.swing.JTextField();
+ ptnoInput = new javax.swing.JTextField();
btBitByte = new javax.swing.JButton();
- t4 = new javax.swing.JTextField();
+ bitByteOutput = new javax.swing.JTextField();
btLimparTudo = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jLabel5 = new javax.swing.JLabel();
ShowUTR1 = new javax.swing.JButton();
jLabel9 = new javax.swing.JLabel();
jButton_show_CColor = new javax.swing.JButton();
+ errorLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 255));
@@ -74,40 +65,19 @@ private void initComponents() {
jLabel2.setText("BitByte");
btPTNO.setText("Calcular PTNO");
- btPTNO.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btPTNOActionPerformed(evt);
- }
- });
- t3.setEditable(false);
- t3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- t3ActionPerformed(evt);
- }
- });
+ ptnoOutput.setEditable(false);
jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel6.setText("Conversor PTNO --> BitByte ");
jLabel3.setText("PTNO");
- t2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- t2ActionPerformed(evt);
- }
- });
-
btBitByte.setText("Calcular BitByte");
- t4.setEditable(false);
+ bitByteOutput.setEditable(false);
btLimparTudo.setText("Limpar");
- btLimparTudo.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btLimparTudoActionPerformed(evt);
- }
- });
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
@@ -121,21 +91,21 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
- .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(bitByteInput, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btPTNO))
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(t3, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
- .addComponent(t4))
+ .addComponent(ptnoOutput, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
+ .addComponent(bitByteOutput))
.addGap(57, 57, 57))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(23, 23, 23)
- .addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addComponent(ptnoInput, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
@@ -153,9 +123,9 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
- .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(bitByteInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btPTNO)
- .addComponent(t3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addComponent(ptnoOutput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
@@ -164,9 +134,9 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(ptnoInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btBitByte)
- .addComponent(t4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
+ .addComponent(bitByteOutput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(17, Short.MAX_VALUE))
);
@@ -181,11 +151,6 @@ public void mouseClicked(java.awt.event.MouseEvent evt) {
ShowUTR1MouseClicked(evt);
}
});
- ShowUTR1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- ShowUTR1ActionPerformed(evt);
- }
- });
jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel9.setText("Código de cores dos fios das UTRs (Preto e Branco, Coloridos, 8 ou 9 fios)");
@@ -196,11 +161,6 @@ public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton_show_CColorMouseClicked(evt);
}
});
- jButton_show_CColor.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton_show_CColorActionPerformed(evt);
- }
- });
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
@@ -265,61 +225,37 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addGap(24, 24, 24)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
+ .addComponent(errorLabel)
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel4))
);
pack();
}// //GEN-END:initComponents
- private void btLimparTudoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLimparTudoActionPerformed
- }//GEN-LAST:event_btLimparTudoActionPerformed
-
- private void t2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_t2ActionPerformed
- }//GEN-LAST:event_t2ActionPerformed
-
- private void ShowUTR1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ShowUTR1ActionPerformed
- // TODO add your handling code here:
- }//GEN-LAST:event_ShowUTR1ActionPerformed
-
- private void ShowUTR1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ShowUTR1MouseClicked
+ private void ShowUTR1MouseClicked(java.awt.event.MouseEvent evt) {
JFrame frame = new JFrame("Tabela de Localização das Remotas (UTR)");
- TableRTU newContentPane = new TableRTU(); //Create and set up the content pane.
- newContentPane.setOpaque(true); //content panes must be opaque
+ TableRTU newContentPane = new TableRTU();
+ newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
- frame.pack(); //Display the window.
+ frame.pack();
frame.setVisible(true);
- }//GEN-LAST:event_ShowUTR1MouseClicked
-
- private void jButton_show_CColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_show_CColorActionPerformed
- // TODO add your handling code here:
- }//GEN-LAST:event_jButton_show_CColorActionPerformed
+ }
- private void jButton_show_CColorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton_show_CColorMouseClicked
+ private void jButton_show_CColorMouseClicked(java.awt.event.MouseEvent evt) {
JFrame frame = new JFrame("Cores de Fios (6006DC07199E)");
- TableCableColor newContentPane = new TableCableColor(); //Create and set up the content pane.
- newContentPane.setOpaque(true); //content panes must be opaque
+ TableCableColor newContentPane = new TableCableColor();
+ newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
- frame.pack(); //Display the window.
- frame.setVisible(true); // TODO add your handling code here:
- }//GEN-LAST:event_jButton_show_CColorMouseClicked
-
- private void t3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_t3ActionPerformed
-
- }//GEN-LAST:event_t3ActionPerformed
+ frame.pack();
+ frame.setVisible(true);
+ }
- private void btPTNOActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPTNOActionPerformed
- // TODO add your handling code here:
- }//GEN-LAST:event_btPTNOActionPerformed
+ public javax.swing.JLabel getErrorLabel() {
+ return errorLabel;
+ }
- /**
- * @param args the command line arguments
- */
public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
@@ -327,29 +263,22 @@ public static void main(String args[]) {
break;
}
}
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(MyJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(MyJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(MyJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
+ } catch (Exception ex) {
java.util.logging.Logger.getLogger(MyJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
- //
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new MyJFrame().setVisible(true);
- }
+ java.awt.EventQueue.invokeLater(() -> {
+ new MyJFrame().setVisible(true);
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton ShowUTR1;
+ private javax.swing.JTextField bitByteInput;
+ private javax.swing.JTextField bitByteOutput;
private javax.swing.JButton btBitByte;
private javax.swing.JButton btLimparTudo;
private javax.swing.JButton btPTNO;
+ private javax.swing.JLabel errorLabel;
private javax.swing.JButton jButton_show_CColor;
private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JLabel jLabel1;
@@ -363,9 +292,7 @@ public void run() {
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
- private javax.swing.JTextField t1;
- private javax.swing.JTextField t2;
- private javax.swing.JTextField t3;
- private javax.swing.JTextField t4;
+ private javax.swing.JTextField ptnoInput;
+ private javax.swing.JTextField ptnoOutput;
// End of variables declaration//GEN-END:variables
}
diff --git a/src/operacoes/Calculos.java b/src/operacoes/Calculos.java
index 69dad4e..7015b55 100644
--- a/src/operacoes/Calculos.java
+++ b/src/operacoes/Calculos.java
@@ -1,218 +1,168 @@
package operacoes;
-import javax.swing.JOptionPane;
-
public class Calculos {
- public static int calcula_1(String t1) {
- int n1 = 0, result = 0, error = 0;
- try {
- n1 = Integer.parseInt(t1);
-
- if (n1 >= 0 && n1 <= 36096) {
-
- if (n1 >= 0 && n1 <= 2047) { //2W without TimeStamp
- result = n1;
- String message = String.format("Calculadora para SOSTAT, verifique a SOANLG para pontos analógicos");
- JOptionPane.showMessageDialog(null, message, "Erro", JOptionPane.ERROR_MESSAGE);
- error = 0;
- }
-
- if (n1 >= 10000 && n1 <= 11023) { //2W without TimeStamp
- result = ((n1 - 10000) * 2);
- JOptionPane.showMessageDialog(null, "Resultado para um ponto 2WAY sem TimeStamp");
- error = 0;
- }
-
- if (n1 >= 15000 && n1 <= 16023) { //2W with TimeStamp
- result = (((n1 - 15000) * 2) + 2048);
- error = 0;
- } //intervalo entre 16025 e 16814 normalmente SCC (BD SCADA)
-
- if (n1 >= 25000 && n1 <= 25063) { //4W with TimeStamp //25063 <-> 4727 (bloco 5)
- result = (((n1 - 25000) / 8) * 16) + (((n1 - 25000) % 8) + 4608);
- error = 0;
- }
-
- if (n1 >= 36000 && n1 <= 36063) { //Latch with TimeStamp //36095 <-> 5815 (bloco 5)
- result = (((n1 - 36000) / 8) * 16) + (((n1 - 36000) % 8) + 5632);
- error = 0;
- }
-
- if (n1 >= 36088 && n1 <= 36095) { //Latch with TimeStamp //36095 <-> 5815 (bloco 6, parcial)
- result = (((n1 - 36064) / 8) * 16) + (((n1 - 36064) % 8) + 5760);
- JOptionPane.showMessageDialog(null, result);
- error = 0;
- }
-
- if ((n1 >= 2048 && n1 <= 9999)|| (n1 >= 11024 && n1 <= 14999) || (n1 >= 16024 && n1 <= 24999) || (n1 >= 25064 && n1 <= 35999) || (n1 >= 36064 && n1 <= 36087)) {
- error = 1;
- }
- }
-
- if (n1 < 0 || n1 > 36096) {
- error = 1;
- }
-
- if (error == 1) {
- String message = String.format("Verifique valores e intervalos válidos na documentação");
- JOptionPane.showMessageDialog(null, message, "Erro:", JOptionPane.ERROR_MESSAGE);
- result = 0;
- }
-
- return result;
- } catch (NumberFormatException erro) {
- return -1;
- }
- }
-
- public static int calcula_2(String t2) {
- int n2 = 0, result = 0, error = 0;
-
- try {
- n2 = Integer.parseInt(t2);
-
- if (n2 >= 0 && n2 <= 8192) { //intervalo definido para SOSTAT.PNTNO
+ //
+ private static final int PTNO_MAX_INPUT = 36096;
- if (n2 >= 0 && n2 <= 2047) { //2WAY without TimeStamp
- result = ((n2 / 2) + 10000);
- String message = String.format("Cuidado, pode ser ponto Analógico");
- JOptionPane.showMessageDialog(null, message);
- }
+ // SOSTAT Range (Analog points)
+ private static final int SOSTAT_RANGE_START = 0;
+ private static final int SOSTAT_RANGE_END = 2047;
- if (n2 >= 2048 && n2 <= 4095) { //2WAY with TimeStamp
+ // 2-Way without Timestamp Range
+ private static final int PTNO_2W_NO_TS_START = 10000;
+ private static final int PTNO_2W_NO_TS_END = 11023;
+ private static final int PTNO_2W_NO_TS_OFFSET = 10000;
- if (n2 % 2 != 0) {
- String message = String.format("PTNO deve ser um numero par");
- JOptionPane.showMessageDialog(null, message);
- error = 1;
- } else {
- result = ((n2 + 27952) / 2);
- }
- }
+ // 2-Way with Timestamp Range
+ private static final int PTNO_2W_TS_START = 15000;
+ private static final int PTNO_2W_TS_END = 16023;
+ private static final int PTNO_2W_TS_OFFSET = 15000;
+ private static final int PTNO_2W_TS_RESULT_OFFSET = 2048;
- if (n2 >= 4096 && n2 <= 4607) { //Intervalo não utilizado
- String message = String.format("Intervalo não utilizado");
- JOptionPane.showMessageDialog(null, message, "Erro", JOptionPane.WARNING_MESSAGE);
- error = 1;
- }
- if (n2 >= 4608 && n2 <= 5119) { //4WAY with TimeStamp
+ // 4-Way with Timestamp Range
+ private static final int PTNO_4W_TS_START = 25000;
+ private static final int PTNO_4W_TS_END = 25063;
+ private static final int PTNO_4W_TS_OFFSET = 25000;
+ private static final int PTNO_4W_TS_RESULT_OFFSET = 4608;
- if (n2 >= 4608 && n2 <= 4615) {
- result = (n2 + 20392);
- }
- if (n2 >= 4624 && n2 <= 4631) {
- result = (n2 + 20392) - 8;
- }
+ // Latch with Timestamp Range
+ private static final int PTNO_LATCH_TS_START = 36000;
+ private static final int PTNO_LATCH_TS_END = 36063;
+ private static final int PTNO_LATCH_TS_OFFSET = 36000;
+ private static final int PTNO_LATCH_TS_RESULT_OFFSET = 5632;
- if (n2 >= 4640 && n2 <= 4647) {
- result = (n2 + 20392) - 16;
- }
- if (n2 >= 4656 && n2 <= 4663) {
- result = (n2 + 20392) - 24;
- }
+ // Partial Latch with Timestamp Range
+ private static final int PTNO_LATCH_TS_PARTIAL_START = 36088;
+ private static final int PTNO_LATCH_TS_PARTIAL_END = 36095;
+ private static final int PTNO_LATCH_TS_PARTIAL_OFFSET = 36064;
+ private static final int PTNO_LATCH_TS_PARTIAL_RESULT_OFFSET = 5760;
- if (n2 >= 4672 && n2 <= 4679) {
- result = (n2 + 20392) - 32;
- }
+ // Unused ranges for PTNO
+ private static final int UNUSED_PTNO_1_START = 2048;
+ private static final int UNUSED_PTNO_1_END = 9999;
+ private static final int UNUSED_PTNO_2_START = 11024;
+ private static final int UNUSED_PTNO_2_END = 14999;
+ private static final int UNUSED_PTNO_3_START = 16024;
+ private static final int UNUSED_PTNO_3_END = 24999;
+ private static final int UNUSED_PTNO_4_START = 25064;
+ private static final int UNUSED_PTNO_4_END = 35999;
+ private static final int UNUSED_PTNO_5_START = 36064;
+ private static final int UNUSED_PTNO_5_END = 36087;
- if (n2 >= 4688 && n2 <= 4695) {
- result = (n2 + 20392) - 40;
- }
- if (n2 >= 4696 && n2 <= 5119) { //Intervalo não utilizado
- String message = String.format("Intervalo não utilizado atualmente");
- JOptionPane.showMessageDialog(null, message, "Erro", JOptionPane.WARNING_MESSAGE);
- }
+ // Common calculation constants
+ private static final int DIVISOR_8 = 8;
+ private static final int MULTIPLIER_16 = 16;
+ private static final int MULTIPLIER_2 = 2;
+ //
- if ((n2 >= 4616 && n2 <= 4623) || (n2 >= 4632 && n2 <= 4639) || (n2 >= 4648 && n2 <= 4655) || (n2 >= 4664 && n2 <= 4671) || (n2 >= 4680 && n2 <= 4687)) {
- error = 1;
- }
- }
+ //
+ private static final int BITBYTE_MAX_INPUT = 8192;
- if (n2 >= 5120 && n2 <= 5631) { //Intervalo não utilizado
- String message = String.format("Intervalo não utilizado ");
- JOptionPane.showMessageDialog(null, message, "Erro", JOptionPane.WARNING_MESSAGE);
- error = 1;
- }
+ // 2-Way without Timestamp Range for BitByte
+ private static final int BITBYTE_2W_NO_TS_START = 0;
+ private static final int BITBYTE_2W_NO_TS_END = 2047;
+ private static final int BITBYTE_2W_NO_TS_OFFSET = 10000;
+ // 2-Way with Timestamp Range for BitByte
+ private static final int BITBYTE_2W_TS_START = 2048;
+ private static final int BITBYTE_2W_TS_END = 4095;
+ private static final int BITBYTE_2W_TS_OFFSET = 27952;
- if (n2 >= 5632 && n2 <= 6143) { //LATCH with TimeStamp
- if (n2 >= 5632 && n2 <= 5639) {
- result = (n2 + 30368);
- }
+ // 4-Way with Timestamp Range for BitByte
+ private static final int BITBYTE_4W_TS_START = 4608;
+ private static final int BITBYTE_4W_TS_END = 5119;
+ private static final int BITBYTE_4W_TS_OFFSET = 20392;
- if (n2 >= 5648 && n2 <= 5655) {
- result = (n2 + 30368) - 8;
- }
- if (n2 >= 5664 && n2 <= 5671) {
- result = (n2 + 30368) - 16;
- }
+ // Latch with Timestamp Range for BitByte
+ private static final int BITBYTE_LATCH_TS_START = 5632;
+ private static final int BITBYTE_LATCH_TS_END = 6143;
+ private static final int BITBYTE_LATCH_TS_OFFSET = 30368;
- if (n2 >= 5680 && n2 <= 5687) {
- result = (n2 + 30368) - 24;
- }
- if (n2 >= 5696 && n2 <= 5703) {
- result = (n2 + 30368) - 32;
- }
+ // Pseudo Range
+ private static final int PSEUDO_RANGE_START = 7000;
+ private static final int PSEUDO_RANGE_END = 8192;
- if (n2 >= 5712 && n2 <= 5719) {
- result = (n2 + 30368) - 40;
- }
+ // Unused Ranges
+ private static final int UNUSED_BITBYTE_1_START = 4096;
+ private static final int UNUSED_BITBYTE_1_END = 4607;
+ private static final int UNUSED_BITBYTE_2_START = 5120;
+ private static final int UNUSED_BITBYTE_2_END = 5631;
+ private static final int UNUSED_BITBYTE_3_START = 6144;
+ private static final int UNUSED_BITBYTE_3_END = 6999;
+ //
- if (n2 >= 5728 && n2 <= 5735) {
- result = (n2 + 30368) - 48;
- }
- if (n2 >= 5744 && n2 <= 5751) {
- result = (n2 + 30368) - 56;
- }
+ public static int calcula_1(String t1) {
+ int n1;
+ int result = 0;
- if (n2 >= 5792 && n2 <= 5799) {
- result = (n2 + 30368) - 80;
- }
+ try {
+ n1 = Integer.parseInt(t1);
- if (n2 >= 5808 && n2 <= 5815) {
- result = (n2 + 30368) - 88;
- }
+ if (n1 >= SOSTAT_RANGE_START && n1 <= SOSTAT_RANGE_END) {
+ result = n1;
+ } else if (n1 >= PTNO_2W_NO_TS_START && n1 <= PTNO_2W_NO_TS_END) {
+ result = ((n1 - PTNO_2W_NO_TS_OFFSET) * MULTIPLIER_2);
+ } else if (n1 >= PTNO_2W_TS_START && n1 <= PTNO_2W_TS_END) {
+ result = (((n1 - PTNO_2W_TS_OFFSET) * MULTIPLIER_2) + PTNO_2W_TS_RESULT_OFFSET);
+ } else if (n1 >= PTNO_4W_TS_START && n1 <= PTNO_4W_TS_END) {
+ result = (((n1 - PTNO_4W_TS_OFFSET) / DIVISOR_8) * MULTIPLIER_16) + (((n1 - PTNO_4W_TS_OFFSET) % DIVISOR_8) + PTNO_4W_TS_RESULT_OFFSET);
+ } else if (n1 >= PTNO_LATCH_TS_START && n1 <= PTNO_LATCH_TS_END) {
+ result = (((n1 - PTNO_LATCH_TS_OFFSET) / DIVISOR_8) * MULTIPLIER_16) + (((n1 - PTNO_LATCH_TS_OFFSET) % DIVISOR_8) + PTNO_LATCH_TS_RESULT_OFFSET);
+ } else if (n1 >= PTNO_LATCH_TS_PARTIAL_START && n1 <= PTNO_LATCH_TS_PARTIAL_END) {
+ result = (((n1 - PTNO_LATCH_TS_PARTIAL_OFFSET) / DIVISOR_8) * MULTIPLIER_16) + (((n1 - PTNO_LATCH_TS_PARTIAL_OFFSET) % DIVISOR_8) + PTNO_LATCH_TS_PARTIAL_RESULT_OFFSET);
+ } else {
+ return -1;
+ }
- if ((n2 >= 5640 && n2 <= 5647) || (n2 >= 5656 && n2 <= 5663) || (n2 >= 5672 && n2 <= 5681) || (n2 >= 5688 && n2 <= 5695) || (n2 >= 5704 && n2 <= 5711) || (n2 >= 5720 && n2 <= 5727) || (n2 >= 5736 && n2 <= 5743) || (n2 >= 5752 && n2 <= 5791) || (n2 >= 5800 && n2 <= 5807)) {
- error = 1;
- }
+ return result;
+ } catch (NumberFormatException erro) {
+ return -1;
+ }
+ }
- if (n2 >= 5816 && n2 <= 6143) { //Intervalo não utilizado
- String message = String.format("Intervalo não utilizado atualmente");
- JOptionPane.showMessageDialog(null, message, "Erro", JOptionPane.WARNING_MESSAGE);
- }
+ public static int calcula_2(String t2) {
+ int n2;
+ int result = 0;
- }
+ try {
+ n2 = Integer.parseInt(t2);
- if (n2 >= 6144 && n2 <= 6999) { //Intervalo não utilizado
- String message = String.format("Intervalo não utilizado ");
- JOptionPane.showMessageDialog(null, message, "Erro", JOptionPane.WARNING_MESSAGE);
+ if (n2 >= BITBYTE_2W_NO_TS_START && n2 <= BITBYTE_2W_NO_TS_END) {
+ result = ((n2 / 2) + BITBYTE_2W_NO_TS_OFFSET);
+ } else if (n2 >= BITBYTE_2W_TS_START && n2 <= BITBYTE_2W_TS_END) {
+ if (n2 % 2 != 0) {
+ return -1;
+ } else {
+ result = ((n2 + BITBYTE_2W_TS_OFFSET) / 2);
}
-
- if (n2 >= 7000 && n2 <= 8192) { //PSEUDO
- result = 0;
- String message = String.format("Todo Pseudo point tem BITBYTE nulo");
- JOptionPane.showMessageDialog(null, message, "Atenção:", JOptionPane.INFORMATION_MESSAGE);
+ } else if (n2 >= UNUSED_BITBYTE_1_START && n2 <= UNUSED_BITBYTE_1_END) {
+ return -1;
+ } else if (n2 >= BITBYTE_4W_TS_START && n2 <= BITBYTE_4W_TS_END) {
+ if ((n2 % 16) > 7) { // Check for unused gaps
+ return -1;
+ } else {
+ int block = (n2 - BITBYTE_4W_TS_START) / 16;
+ result = (n2 + BITBYTE_4W_TS_OFFSET) - (8 * block);
}
- }
-
- if (n2 < 0 || n2 > 8192) {
- error = 1;
- }
-
- if (error == 1) {
- String message = String.format("Verifique valores e intervalos válidos na documentação");
- JOptionPane.showMessageDialog(null, message, "Erro:", JOptionPane.ERROR_MESSAGE);
+ } else if (n2 >= UNUSED_BITBYTE_2_START && n2 <= UNUSED_BITBYTE_2_END) {
+ return -1;
+ } else if (n2 >= BITBYTE_LATCH_TS_START && n2 <= BITBYTE_LATCH_TS_END) {
+ result = calculaLatchComTimestamp(n2);
+ } else if (n2 >= UNUSED_BITBYTE_3_START && n2 <= UNUSED_BITBYTE_3_END) {
+ return -1;
+ } else if (n2 >= PSEUDO_RANGE_START && n2 <= PSEUDO_RANGE_END) {
result = 0;
+ } else {
+ return -1;
}
return result;
@@ -222,21 +172,30 @@ public static int calcula_2(String t2) {
}
}
+ private static int calculaLatchComTimestamp(int n2) {
+ if ((n2 % 16) > 7) { // Check for unused gaps
+ return -1;
+ }
+
+ int block = (n2 - BITBYTE_LATCH_TS_START) / 16;
+ int offset = 0;
+
+ if (block >= 10) { // Special cases for blocks 10 and 11
+ offset = (2 * block) + (2 * (block - 9));
+ } else {
+ offset = 8 * block;
+ }
+
+ return (n2 + BITBYTE_LATCH_TS_OFFSET) - offset;
+ }
+
public static int calcula_raw(String tvmin) {
- int tr = 1, bias = 0, scale = 0, error = 0, result =0;
- int vmin = 0, vmax = 0;
+ int vmin;
try {
vmin = Integer.parseInt(tvmin);
- result = vmin;
- String message = String.format("Sem Erro");
- JOptionPane.showMessageDialog(null, message, "Sem Erro ", JOptionPane.ERROR_MESSAGE);
- error = 0;
-
- return result;
+ return vmin;
} catch (NumberFormatException erro) {
return -1;
}
}
-
}
-
diff --git a/src/utils/MeuListener.java b/src/utils/MeuListener.java
index b79adf2..f6f8156 100644
--- a/src/utils/MeuListener.java
+++ b/src/utils/MeuListener.java
@@ -2,78 +2,63 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
-import javax.swing.JOptionPane;
+import javax.swing.JLabel;
import javax.swing.JTextField;
import operacoes.Calculos;
-/** Esta classe tem finalidade de agrupar os Listener's dos botoes.
- *obs: Sera preciso criar novos construtores dependendo do tipo funcao a executar */
+/**
+ * Esta classe tem finalidade de agrupar os Listener's dos botoes.
+ * obs: Sera preciso criar novos construtores dependendo do tipo funcao a executar
+ */
public class MeuListener implements ActionListener {
- private String opcode;
- private JTextField t1, t2;
+ private final String opcode;
+ private final JTextField inputField;
+ private final JTextField outputField;
+ private final JLabel errorLabel;
- /* @param opcode: o switch ira se basear de acordo com o botao clicado.
- * @param t1: campo JTextField
- * @param t2: campo JTextField */
- public MeuListener(String opcode, JTextField t1, JTextField t2) {
- this.opcode = opcode;
- this.t1 = t1;
- this.t2 = t2;
- }
-
- /**
- *
- * @param opcode
- * @param tvmin
- * @param tvmax
- */
+ public MeuListener(String opcode, JTextField inputField, JTextField outputField, JLabel errorLabel) {
+ this.opcode = opcode;
+ this.inputField = inputField;
+ this.outputField = outputField;
+ this.errorLabel = errorLabel;
+ }
- @Override
- public void actionPerformed(ActionEvent e) {
-
- int result;
-
- switch (opcode) {
- case "btLimparBit":
- t1.setText("");
- t2.setText("");
- break;
-
- case "btLimparPTNO":
- t1.setText("");
- t2.setText("");
- break;
-
-
- case "btLimparTudo": //usado na novo Frame, anteriores legados do codigo antigo
- t1.setText("");
- t2.setText("");
- break;
-
- case "btPTNO":
- result = Calculos.calcula_1(t1.getText());
- if (result >= 0) {
- t2.setText("" + result);
- } else {
- t2.setText("Preencha Bit/Byte");
- }
- break;
-
- case "btBitByte":
- result = Calculos.calcula_2(t1.getText());
- if (result >= 0) {
- t2.setText("" + result);
- } else {
- t2.setText("Preencha PTNO");
- }
- break;
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ errorLabel.setText(""); // Clear the error label on each action
+ int result;
- default:
- JOptionPane.showMessageDialog(null, "Este botao nao foi configurado ainda.");
- break;
- }
- }
-}
+ switch (opcode) {
+ case "btLimparBit":
+ case "btLimparPTNO":
+ case "btLimparTudo":
+ inputField.setText("");
+ outputField.setText("");
+ break;
+
+ case "btPTNO":
+ result = Calculos.calcula_1(inputField.getText());
+ if (result >= 0) {
+ outputField.setText(String.valueOf(result));
+ } else {
+ errorLabel.setText("Por favor, preencha o campo Bit/Byte com um número válido.");
+ }
+ break;
+ case "btBitByte":
+ result = Calculos.calcula_2(inputField.getText());
+ if (result >= 0) {
+ outputField.setText(String.valueOf(result));
+ } else {
+ errorLabel.setText("Por favor, preencha o campo PTNO com um número válido.");
+ }
+ break;
+
+ default:
+ errorLabel.setText("Este botão ainda não foi configurado.");
+ break;
+ }
+ }
+}
diff --git a/test/operacoes/CalculosTest.java b/test/operacoes/CalculosTest.java
new file mode 100644
index 0000000..d338b92
--- /dev/null
+++ b/test/operacoes/CalculosTest.java
@@ -0,0 +1,77 @@
+package operacoes;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class CalculosTest {
+
+ @Test
+ public void testCalcula_1_sostat() {
+ assertEquals(100, Calculos.calcula_1("100"));
+ }
+
+ @Test
+ public void testCalcula_1_2w_no_ts() {
+ assertEquals(200, Calculos.calcula_1("10100"));
+ }
+
+ @Test
+ public void testCalcula_1_2w_ts() {
+ assertEquals(2248, Calculos.calcula_1("15100"));
+ }
+
+ @Test
+ public void testCalcula_1_4w_ts() {
+ assertEquals(4624, Calculos.calcula_1("25008"));
+ }
+
+ @Test
+ public void testCalcula_1_latch_ts() {
+ assertEquals(5648, Calculos.calcula_1("36008"));
+ }
+
+ @Test
+ public void testCalcula_1_invalid_input() {
+ assertEquals(-1, Calculos.calcula_1("invalid"));
+ }
+
+ @Test
+ public void testCalcula_1_out_of_range() {
+ assertEquals(-1, Calculos.calcula_1("40000"));
+ }
+
+ @Test
+ public void testCalcula_2_2w_no_ts() {
+ assertEquals(10100, Calculos.calcula_2("200"));
+ }
+
+ @Test
+ public void testCalcula_2_2w_ts() {
+ assertEquals(15100, Calculos.calcula_2("2248"));
+ }
+
+ @Test
+ public void testCalcula_2_4w_ts() {
+ assertEquals(25008, Calculos.calcula_2("4624"));
+ }
+
+ @Test
+ public void testCalcula_2_latch_ts() {
+ assertEquals(36008, Calculos.calcula_2("5648"));
+ }
+
+ @Test
+ public void testCalcula_2_invalid_input() {
+ assertEquals(-1, Calculos.calcula_2("invalid"));
+ }
+
+ @Test
+ public void testCalcula_2_out_of_range() {
+ assertEquals(-1, Calculos.calcula_2("9000"));
+ }
+
+ @Test
+ public void testCalcula_2_odd_ptno() {
+ assertEquals(-1, Calculos.calcula_2("2049"));
+ }
+}