Skip to content

Commit

Permalink
Updated to version 1.01
Browse files Browse the repository at this point in the history
  • Loading branch information
gitRaiku authored Jun 1, 2020
1 parent bfac31f commit 4f3c7f0
Show file tree
Hide file tree
Showing 33 changed files with 310 additions and 20 deletions.
8 changes: 4 additions & 4 deletions Snmp-Ignition-Module/snmp-build/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
</projectScope>
</projectScopes>

<moduleId>allnetauto.ca.snmp</moduleId>
<moduleId>ca.allnetauto.common.snmp</moduleId>
<moduleName>${project.parent.name}</moduleName>
<moduleDescription>${project.description}</moduleDescription>
<moduleVersion>1.0.0</moduleVersion>
Expand All @@ -80,15 +80,15 @@
<hooks>
<hook>
<scope>C</scope>
<hookClass>allnetauto.ca.client.ClientHook</hookClass>
<hookClass>ca.allnetauto.client.ClientHook</hookClass>
</hook>
<hook>
<scope>D</scope>
<hookClass>allnetauto.ca.designer.DesignerHook</hookClass>
<hookClass>ca.allnetauto.designer.DesignerHook</hookClass>
</hook>
<hook>
<scope>G</scope>
<hookClass>allnetauto.ca.GatewayHook</hookClass>
<hookClass>ca.allnetauto.gateway.GatewayHook</hookClass>
</hook>
</hooks>

Expand Down
Binary file not shown.
Binary file modified Snmp-Ignition-Module/snmp-build/target/snmp-build-1.0.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ca.allnetauto.client;

import com.inductiveautomation.ignition.common.script.ScriptManager;
import com.inductiveautomation.ignition.common.script.hints.PropertiesFileDocProvider;
import com.inductiveautomation.vision.api.client.AbstractClientModuleHook;

public class ClientHook extends AbstractClientModuleHook {

@Override
public void initializeScriptManager(ScriptManager manager) {
super.initializeScriptManager(manager);

manager.addScriptModule(
"system.snmp",
new ClientScriptModule(),
new PropertiesFileDocProvider()
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ca.allnetauto.client;

import ca.allnetauto.common.AbstractScriptModule;
import ca.allnetauto.common.FunctionInterface;
import com.inductiveautomation.ignition.client.gateway_interface.ModuleRPCFactory;

public class ClientScriptModule extends AbstractScriptModule {

private final FunctionInterface rpc;

public ClientScriptModule() {
rpc = ModuleRPCFactory.create(
"allnetauto.ca.snmp-1.0.0",
FunctionInterface.class
);
}

@Override
protected String[] getImpl(String addr, int port, String[] OIDS, String... params){
return rpc.get(addr, port, OIDS, params);
}

}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
allnetauto\ca\client\ClientHook.class
allnetauto\ca\client\ClientScriptModule.class
ca\allnetauto\client\ClientHook.class
ca\allnetauto\client\ClientScriptModule.class
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
F:\Sebi\Idea\snmp-1.0.0\snmp-client\src\main\java\allnetauto\ca\client\ClientScriptModule.java
F:\Sebi\Idea\snmp-1.0.0\snmp-client\src\main\java\allnetauto\ca\client\ClientHook.java
F:\Sebi\Idea\Snmp-Ignition-Module\snmp-client\src\main\java\ca\allnetauto\client\ClientHook.java
F:\Sebi\Idea\Snmp-Ignition-Module\snmp-client\src\main\java\ca\allnetauto\client\ClientScriptModule.java
Binary file modified Snmp-Ignition-Module/snmp-client/target/snmp-client-1.0.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ca.allnetauto.common;

import com.inductiveautomation.ignition.common.BundleUtil;
import com.inductiveautomation.ignition.common.script.hints.ScriptArg;
import com.inductiveautomation.ignition.common.script.hints.ScriptFunction;

public abstract class AbstractScriptModule implements FunctionInterface {

static {
BundleUtil.get().addBundle(
AbstractScriptModule.class.getSimpleName(),
AbstractScriptModule.class.getClassLoader(),
AbstractScriptModule.class.getName().replace('.', '/')
);
}

@Override
@ScriptFunction
public String[] get(
@ScriptArg("address") String addr,
@ScriptArg("port") int port,
@ScriptArg("OID") String[] OIDS,
@ScriptArg("Others") String... params)
{
return snmp.snmpGet(addr, port, OIDS, params);
}

protected abstract String[] getImpl(String addr, int port, String[] OIDS, String... params);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ca.allnetauto.common;

public interface FunctionInterface {
public String[] get(String addr, int port, String[] OIDS, String... params);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
Code taken from:
https://sites.google.com/site/mullais/logic/java/how-to-run-a-simple-snmp-get-program-using-java-with-eclipse?tmpl=%2Fsystem%2Fapp%2Ftemplates%2Fprint%2F&showPrintDialog=1
*/

package ca.allnetauto.common;

import java.io.IOException;
import java.util.ArrayList;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class snmp {

public static final int DEFAULT_VERSION = SnmpConstants.version2c;
public static final String DEFAULT_PROTOCOL = "udp";
public static final long DEFAULT_TIMEOUT = 3000L;
public static final int DEFAULT_RETRY = 1;

public static CommunityTarget createDefault(String ip, String community, int port, String[] params) {
Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip + "/" + port);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(address);

target.setVersion(DEFAULT_VERSION);
target.setTimeout(DEFAULT_TIMEOUT);
target.setRetries(DEFAULT_RETRY);

for(String param : params) {
String[] value = param.split("=");
if(value[0].equalsIgnoreCase("version")){
target.setVersion(getVersion(value[1]));
} else if(value[0].equalsIgnoreCase("timeout")){
target.setTimeout(Integer.parseInt(value[1]));
} else if(value[0].equalsIgnoreCase("retry")){
target.setRetries(Integer.parseInt(value[1]));
}
}
return target;
}

private static int getVersion(String s) {
if(s.equalsIgnoreCase("1")){
return SnmpConstants.version1;
} else if (s.equalsIgnoreCase("3")){
return SnmpConstants.version3;
} else {
return SnmpConstants.version2c;
}

}

public static String[] snmpGet(String ip, int port, String[] oids, String[] params) { /// if anyone knows how to get rid of the first item from an array please let me know
String community = params[0];
CommunityTarget target = createDefault(ip, community, port, params);
PDU pdu = new PDU();
pdu.addAll(getBindings(oids));
return get(pdu, target);
}

private static VariableBinding[] getBindings(String[] oids) {
ArrayList<VariableBinding> vars = new ArrayList<>();
for (String oid : oids) {
vars.add(new VariableBinding(new OID(oid)));
}

return vars.toArray(new VariableBinding[0]);
}

private static String[] get(PDU pdu, CommunityTarget target) {

Snmp snmp = null;
ArrayList<String> res = new ArrayList<>();
try {
DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
snmp.listen();
pdu.setType(PDU.GET);
ResponseEvent respEvent = snmp.send(pdu, target);
PDU response = respEvent.getResponse();

if (response == null) {
res.add("Error: no Response");
} else {
for (int i = 0; i < response.size(); i++) {
VariableBinding vb = response.get(i);
res.add(String.valueOf(vb.getVariable()));
}
}
return res.toArray(new String[0]);
} catch (Exception e) {
res.add("Error: " + e.getMessage());
return res.toArray(new String[0]);
} finally {
if (snmp != null) {
try {
snmp.close();
} catch (IOException ignored) {
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
get.desc=Performs a snmp get operation
get.param.address=IP Address
get.param.port=Port
get.param.OID=A list with all the OIDs to get (eg. ['OID'] or ['OID1', 'OID2', ...])
get.param.params=First the community and then specifications such as 'version=2c', 'timeout=3000', retry='1'
get.returns=An string array with all the requested OIDs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
get.desc=Performs a snmp get operation
get.param.address=IP Address
get.param.port=Port
get.param.OID=A list with all the OIDs to get (eg. ['OID'] or ['OID1', 'OID2', ...])
get.param.params=First the community and then specifications such as 'version=2c', 'timeout=3000', retry='1'
get.returns=An string array with all the requested OIDs
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
allnetauto\ca\AbstractScriptModule.class
allnetauto\ca\FunctionInterface.class
allnetauto\ca\snmp.class
ca\allnetauto\common\FunctionInterface.class
ca\allnetauto\common\AbstractScriptModule.class
ca\allnetauto\common\snmp.class
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
F:\Sebi\Idea\snmp-1.0.0\snmp-common\src\main\java\allnetauto\ca\snmp.java
F:\Sebi\Idea\snmp-1.0.0\snmp-common\src\main\java\allnetauto\ca\AbstractScriptModule.java
F:\Sebi\Idea\snmp-1.0.0\snmp-common\src\main\java\allnetauto\ca\FunctionInterface.java
F:\Sebi\Idea\Snmp-Ignition-Module\snmp-common\src\main\java\ca\allnetauto\common\AbstractScriptModule.java
F:\Sebi\Idea\Snmp-Ignition-Module\snmp-common\src\main\java\ca\allnetauto\common\snmp.java
F:\Sebi\Idea\Snmp-Ignition-Module\snmp-common\src\main\java\ca\allnetauto\common\FunctionInterface.java
Binary file modified Snmp-Ignition-Module/snmp-common/target/snmp-common-1.0.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ca.allnetauto.designer;

import ca.allnetauto.client.ClientScriptModule;
import com.inductiveautomation.ignition.common.script.ScriptManager;
import com.inductiveautomation.ignition.common.script.hints.PropertiesFileDocProvider;
import com.inductiveautomation.ignition.designer.model.AbstractDesignerModuleHook;

public class DesignerHook extends AbstractDesignerModuleHook {

@Override
public void initializeScriptManager(ScriptManager manager) {
super.initializeScriptManager(manager);

manager.addScriptModule("system.snmp",
new ClientScriptModule(),
new PropertiesFileDocProvider());
}

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
allnetauto\ca\designer\DesignerHook.class
ca\allnetauto\designer\DesignerHook.class
Original file line number Diff line number Diff line change
@@ -1 +1 @@
F:\Sebi\Idea\snmp-1.0.0\snmp-designer\src\main\java\allnetauto\ca\designer\DesignerHook.java
F:\Sebi\Idea\Snmp-Ignition-Module\snmp-designer\src\main\java\ca\allnetauto\designer\DesignerHook.java
Binary file modified Snmp-Ignition-Module/snmp-designer/target/snmp-designer-1.0.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ca.allnetauto.gateway;

import com.inductiveautomation.ignition.common.licensing.LicenseState;
import com.inductiveautomation.ignition.common.script.ScriptManager;
import com.inductiveautomation.ignition.common.script.hints.PropertiesFileDocProvider;
import com.inductiveautomation.ignition.gateway.clientcomm.ClientReqSession;
import com.inductiveautomation.ignition.gateway.model.AbstractGatewayModuleHook;
import com.inductiveautomation.ignition.gateway.model.GatewayContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GatewayHook extends AbstractGatewayModuleHook {

private final Logger logger = LoggerFactory.getLogger(getClass());

private final GatewayScriptModule scriptModule = new GatewayScriptModule();

@Override
public void setup(GatewayContext gatewayContext) {
logger.info("setup()");
}

@Override
public void startup(LicenseState licenseState) {
logger.info("startup()");
}

@Override
public void shutdown() {
logger.info("shutdown()");
}

@Override
public void initializeScriptManager(ScriptManager manager) {
super.initializeScriptManager(manager);

manager.addScriptModule(
"system.snmp",
scriptModule,
new PropertiesFileDocProvider());
}

@Override
public boolean isFreeModule(){
return true;
}

@Override
public Object getRPCHandler(ClientReqSession session, String projectName) {
return scriptModule;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ca.allnetauto.gateway;

import ca.allnetauto.common.AbstractScriptModule;
import ca.allnetauto.common.snmp;

public class GatewayScriptModule extends AbstractScriptModule {

@Override
protected String[] getImpl(String addr, int port, String[] OIDS, String... params) {
return snmp.snmpGet(addr, port, OIDS, params);
}

}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
allnetauto\ca\GatewayHook.class
allnetauto\ca\GatewayScriptModule.class
ca\allnetauto\gateway\GatewayHook.class
ca\allnetauto\gateway\GatewayScriptModule.class
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
F:\Sebi\Idea\snmp-1.0.0\snmp-gateway\src\main\java\allnetauto\ca\GatewayHook.java
F:\Sebi\Idea\snmp-1.0.0\snmp-gateway\src\main\java\allnetauto\ca\GatewayScriptModule.java
F:\Sebi\Idea\Snmp-Ignition-Module\snmp-gateway\src\main\java\ca\allnetauto\gateway\GatewayHook.java
F:\Sebi\Idea\Snmp-Ignition-Module\snmp-gateway\src\main\java\ca\allnetauto\gateway\GatewayScriptModule.java
Binary file modified Snmp-Ignition-Module/snmp-gateway/target/snmp-gateway-1.0.0.jar
Binary file not shown.

0 comments on commit 4f3c7f0

Please sign in to comment.