Skip to content

Commit

Permalink
Added static tags, redundancy and scripting.
Browse files Browse the repository at this point in the history
  • Loading branch information
SciFiDryer committed Jul 14, 2021
1 parent 7672d37 commit ec50bb4
Show file tree
Hide file tree
Showing 19 changed files with 963 additions and 56 deletions.
3 changes: 1 addition & 2 deletions nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ includes=**
jar.compress=false
javac.classpath=\
${file.reference.jlibmodbus-1.2.9.7.jar}:\
${file.reference.etherip.jar}:\
${file.reference.Nalco3DTDriver-dist}
${file.reference.etherip.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
Expand Down
142 changes: 126 additions & 16 deletions src/protocolwhisperer/BridgeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.net.InetAddress;
import java.util.*;
import javax.swing.*;
import javax.script.*;

/**
*
Expand All @@ -35,6 +36,7 @@ public class BridgeManager{
public ArrayList<ProtocolRecord> dataSourceRecords = new ArrayList();
public ArrayList<BridgeEntryContainer> dataDestinationList = new ArrayList();
public ArrayList<ProtocolRecord> dataDestinationRecords = new ArrayList();
public BridgeOptions options = new BridgeOptions();
ArrayList<ProtocolDriver> driverList = new ArrayList();
boolean firstRun = true;
int restTime = 1000;
Expand All @@ -43,6 +45,7 @@ public class BridgeManager{
public DriverMenuHandler dmh = null;
MainFrame bridgeFrame = null;
boolean headless = false;
long redundancyTimerStart = 0;
public BridgeManager(boolean aHeadless, String fileName)
{
headless = aHeadless;
Expand Down Expand Up @@ -86,13 +89,54 @@ public BridgeManager(boolean aHeadless, String fileName)
startBridge();
}
}
public JComboBox getOutgoingRecordTags(String selectedGuid)
{
ArrayList<String> menu = new ArrayList();
int selectedIndex = 0;
for (int i = 0; i < dataSourceRecords.size(); i++)
{
ProtocolRecord currentRecord = dataSourceRecords.get(i);
for (int j = 0; j < currentRecord.tagRecords.size(); j++)
{
menu.add(currentRecord.tagRecords.get(j).tag);
if (selectedGuid.equals(currentRecord.tagRecords.get(j).guid))
{
selectedIndex = menu.size() - 1;
}
}
}
JComboBox menuBox = new JComboBox(menu.toArray());
if (menu.size() > 0)
{
menuBox.setSelectedIndex(selectedIndex);
}
return menuBox;
}
public String getGuidFromIndex(int index)
{
int counter = 0;
for (int i = 0; i < dataSourceRecords.size(); i++)
{
ProtocolRecord currentRecord = dataSourceRecords.get(i);
for (int j = 0; j < currentRecord.tagRecords.size(); j++)
{
if (index == counter)
{
return currentRecord.tagRecords.get(j).guid;
}
counter++;
}
}
return "";
}
public void loadConfig(File f)
{
try
{
XMLDecoder xmld = new XMLDecoder(new FileInputStream(f));
dataSourceRecords.clear();
dataDestinationRecords.clear();
options = (BridgeOptions)xmld.readObject();
dataSourceRecords = (ArrayList<ProtocolRecord>)xmld.readObject();
dataDestinationRecords = (ArrayList<ProtocolRecord>)xmld.readObject();
xmld.close();
Expand Down Expand Up @@ -150,13 +194,14 @@ public void runBridge()
processProtocolRecord(pr);
}
}
//map the incoming values if the driver needs to
//get incoming values
for (int i = 0; i < driverList.size(); i++)
{
ProtocolDriver currentDriver = driverList.get(i);
currentDriver.getIncomingRecords();
currentDriver.mapIncomingValues();
}
runScripting();
if (!headless)
{
javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel)bridgeFrame.valuesTable.getModel();
Expand All @@ -174,34 +219,94 @@ public void runBridge()
}
}
}
for (int i = 0; i < dataDestinationRecords.size(); i++)
//check if redundancy watchdog has elapsed
if (options.redundancyEnabled)
{
ProtocolRecord currentRecord = dataDestinationRecords.get(i);
for (int j = 0; j < currentRecord.tagRecords.size(); j++)
if (getIncomingRecordByGuid(options.watchdogGuid).getValue() == 1)
{
TagRecord currentTagRecord = currentRecord.tagRecords.get(j);
TagRecord incomingRecord = getIncomingRecordByTag(currentTagRecord.tag);
if (incomingRecord != null)
if (redundancyTimerStart == 0)
{
currentTagRecord.setValue(incomingRecord.getValue());
System.out.println("Redundancy timeout started");
redundancyTimerStart = System.currentTimeMillis();
}
}
else
{
redundancyTimerStart = 0;
}
}
for (int i = 0; i < driverList.size(); i++)
if (!options.redundancyEnabled || (redundancyTimerStart > 0 && System.currentTimeMillis() - redundancyTimerStart > options.redundancyTimeout))
{
ProtocolDriver currentDriver = driverList.get(i);
currentDriver.sendOutgoingRecords();
for (int i = 0; i < dataDestinationRecords.size(); i++)
{
ProtocolRecord currentRecord = dataDestinationRecords.get(i);
for (int j = 0; j < currentRecord.tagRecords.size(); j++)
{
TagRecord currentTagRecord = currentRecord.tagRecords.get(j);
TagRecord incomingRecord = getIncomingRecordByGuid(currentTagRecord.tag);
if (incomingRecord != null)
{
currentTagRecord.setValue(incomingRecord.getValue());
}
}
}
for (int i = 0; i < driverList.size(); i++)
{
ProtocolDriver currentDriver = driverList.get(i);
currentDriver.sendOutgoingRecords();
}
}
firstRun = false;
}
public TagRecord getIncomingRecordByTag(String tag)
public void runScripting()
{
if (options.scriptContent.length() > 0)
{
ScriptEngineManager engineManager = new ScriptEngineManager();
ScriptEngine engine = engineManager.getEngineByName("JavaScript");
String builtin = "function tagByName(name)"+
"{"+
"for (i = 0; i < tags.size(); i++)"+
"{"+
"if (tags.get(i).tag == name)"+
"{"+
"return tags.get(i);"+
"}"+
"}"+
"}";
ArrayList<TagRecord> tags = new ArrayList<TagRecord>();
for (int i = 0; i < dataSourceRecords.size(); i++)
{
ProtocolRecord currentProtocolRecord = dataSourceRecords.get(i);
for (int j = 0; j < currentProtocolRecord.tagRecords.size(); j++)
{
TagRecord currentTagRecord = currentProtocolRecord.tagRecords.get(j);
tags.add(currentTagRecord);
}
}
try
{
engine.put("tags", tags);
engine.eval(builtin);
engine.eval(options.scriptContent);
}
catch(Exception e)
{
if (ProtocolWhisperer.debug)
{
e.printStackTrace();
}
}
}
}
public TagRecord getIncomingRecordByGuid(String guid)
{
for (int i = 0; i < dataSourceRecords.size(); i++)
{
ProtocolRecord currentRecord = dataSourceRecords.get(i);
for (int j = 0; j < currentRecord.tagRecords.size(); j++)
{
if (currentRecord.tagRecords.get(j).tag.equals(tag))
if (currentRecord.tagRecords.get(j).guid.equals(guid))
{
return currentRecord.tagRecords.get(j);
}
Expand All @@ -211,6 +316,13 @@ public TagRecord getIncomingRecordByTag(String tag)
}
public void restoreGuiFromFile()
{
bridgeFrame.enableRedundancy.setSelected(options.redundancyEnabled);
bridgeFrame.tagSelectMenu = getOutgoingRecordTags(options.watchdogGuid);
bridgeFrame.scriptTextArea.setText(options.scriptContent);
bridgeFrame.tagSelectPane.removeAll();
bridgeFrame.tagSelectPane.add(bridgeFrame.tagSelectMenu);
bridgeFrame.repaint();
bridgeFrame.watchdogTimerField.setText(options.redundancyTimeout + "");
bridgeFrame.incomingDataPane.removeAll();
for (int i = 0; i < dataSourceRecords.size(); i++)
{
Expand Down Expand Up @@ -271,7 +383,5 @@ public void shutdown()
{
driverList.get(i).shutdown();
}
}


}
}
27 changes: 27 additions & 0 deletions src/protocolwhisperer/BridgeOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2021 Matt Jamesson <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package protocolwhisperer;

/**
*
* @author Matt Jamesson <[email protected]>
*/
public class BridgeOptions {
public boolean redundancyEnabled = false;
public String watchdogGuid = "";
public int redundancyTimeout = 0;
public String scriptContent = "";
}
74 changes: 74 additions & 0 deletions src/protocolwhisperer/MainFrame.form
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
</Layout>
<SubComponents>
<Container class="javax.swing.JTabbedPane" name="jTabbedPane1">
<Events>
<EventHandler event="stateChanged" listener="javax.swing.event.ChangeListener" parameters="javax.swing.event.ChangeEvent" handler="jTabbedPane1StateChanged"/>
</Events>

<Layout class="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout"/>
<SubComponents>
Expand Down Expand Up @@ -94,6 +97,46 @@
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JPanel" name="jPanel7">

<Layout class="org.netbeans.modules.form.compat2.layouts.DesignFlowLayout"/>
<SubComponents>
<Component class="javax.swing.JCheckBox" name="enableRedundancy">
<Properties>
<Property name="text" type="java.lang.String" value="Enable redundancy"/>
</Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="1"/>
</AuxValues>
</Component>
<Container class="javax.swing.JPanel" name="tagSelectPane">
<AuxValues>
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="1"/>
</AuxValues>

<Layout class="org.netbeans.modules.form.compat2.layouts.DesignFlowLayout"/>
</Container>
<Component class="javax.swing.JLabel" name="jLabel5">
<Properties>
<Property name="text" type="java.lang.String" value="Redundancy timer"/>
</Properties>
</Component>
<Component class="javax.swing.JTextField" name="watchdogTimerField">
<Properties>
<Property name="columns" type="int" value="4"/>
<Property name="text" type="java.lang.String" value="5000"/>
</Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="1"/>
</AuxValues>
</Component>
<Component class="javax.swing.JLabel" name="jLabel6">
<Properties>
<Property name="text" type="java.lang.String" value="ms"/>
</Properties>
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JPanel" name="jPanel3">

<Layout class="org.netbeans.modules.form.compat2.layouts.DesignFlowLayout"/>
Expand Down Expand Up @@ -263,6 +306,37 @@
</Container>
</SubComponents>
</Container>
<Container class="javax.swing.JPanel" name="scriptPane">
<Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout" value="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout$JTabbedPaneConstraintsDescription">
<JTabbedPaneConstraints tabName="Scripting">
<Property name="tabTitle" type="java.lang.String" value="Scripting"/>
</JTabbedPaneConstraints>
</Constraint>
</Constraints>

<Layout class="org.netbeans.modules.form.compat2.layouts.DesignBoxLayout"/>
<SubComponents>
<Container class="javax.swing.JScrollPane" name="jScrollPane4">

<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JTextArea" name="scriptTextArea">
<Properties>
<Property name="columns" type="int" value="20"/>
<Property name="rows" type="int" value="5"/>
</Properties>
<Events>
<EventHandler event="keyReleased" listener="java.awt.event.KeyListener" parameters="java.awt.event.KeyEvent" handler="scriptTextAreaKeyReleased"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="1"/>
</AuxValues>
</Component>
</SubComponents>
</Container>
</SubComponents>
</Container>
<Container class="javax.swing.JPanel" name="viewSourcesPane">
<Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout" value="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout$JTabbedPaneConstraintsDescription">
Expand Down
Loading

0 comments on commit ec50bb4

Please sign in to comment.