Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ compileJava.options.forkOptions.executable = 'C:\\Program Files\\Java\\jdk1.8.0_
```

## Run

Single emulator
```sh
# Run the DAQ emulator
java -jar ./build/libs/daqifi-java-api-0.2.0.jar 9760
```

Multiple emulators
Note that the serial number and MAC address are deterministic and based on the port number.
```sh
# starts three device emulations
java -jar ./build/libs/daqifi-java-api-0.2.0.jar 9760 9761 9762
```

## Useful Stuff
Useful Classes:
* Server can be run as a stand-alone Java application
Expand Down
40 changes: 15 additions & 25 deletions src/main/java/com/daqifi/common/devices/DeviceFactory.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Copyright 2013 Marc Bernardini.
package com.daqifi.common.devices;

import com.daqifi.io.messages.DeviceBroadcastMessage;
Expand All @@ -9,13 +8,12 @@
import com.daqifi.common.messages.ProtoMessageV2;
import com.google.protobuf.ByteString;

import java.lang.reflect.InvocationTargetException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

Expand All @@ -30,7 +28,7 @@ public class DeviceFactory {
private static Map<String, Class<? extends DeviceInterface>> deviceClasses = initializeDeviceClasses();

private static Map<String, Class<? extends DeviceInterface>> initializeDeviceClasses() {
Map<String, Class<? extends DeviceInterface>> map = new HashMap<String, Class<? extends DeviceInterface>>();
Map<String, Class<? extends DeviceInterface>> map = new HashMap<>();
map.put("Nyquist 1", Nyquist1.class);
map.put("Nyquist 2", Nyquist2.class);
map.put("Nyquist 3", Nyquist3.class);
Expand All @@ -52,20 +50,12 @@ public static DeviceInterface getDevice(DeviceBroadcastMessage message) {

ProtoMessageV2.DaqifiOutMessage sysinfo = message.getMessage();
if(sysinfo != null) {
switch (sysinfo.getDevicePn()) {
case "Nq1":
device = new Nyquist1();
break;
case "Nq2":
device = new Nyquist2();
break;
case "Nq3":
device = new Nyquist3();
break;
default:
device = new Nyquist1();
break;
}
device = switch (sysinfo.getDevicePn()) {
case "Nq1" -> new Nyquist1();
case "Nq2" -> new Nyquist2();
case "Nq3" -> new Nyquist3();
default -> new Nyquist1();
};
} else {
device = new Nyquist1();
}
Expand Down Expand Up @@ -174,17 +164,17 @@ public static DeviceInterface setDeviceStatus(ProtoMessageV2.DaqifiOutMessage sy

public static DeviceInterface getDevice(String deviceType, String host, int port)
throws InvalidDeviceType {
DeviceInterface device = null;
DeviceInterface device;
try {
Class deviceClass = deviceClasses.get(deviceType);
device = (DeviceInterface) deviceClass.newInstance();
} catch (InstantiationException e) {
throw new InvalidDeviceType(deviceType);
} catch (IllegalAccessException e) {
Class<? extends DeviceInterface> deviceClass = deviceClasses.get(deviceType);
device = deviceClass.getDeclaredConstructor().newInstance();
} catch (InstantiationException |
NoSuchMethodException |
IllegalAccessException |
InvocationTargetException ignore) {
throw new InvalidDeviceType(deviceType);
}


device.setDeviceName(host);
InetSocketAddress address = InetSocketAddress.createUnresolved(host, port);
device.setNetworkAddress(address);
Expand Down
56 changes: 19 additions & 37 deletions src/main/java/com/daqifi/io/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,28 @@
*/
public class Server extends Thread {
private static Logger log = Logger.getLogger(Server.class.getName());
private static final long BASE_SERIAL_NUMBER = 4788544735461581972L;
private final InetAddress ip = initIp();
private int port;
private long serialNumber;
private DataInterpreter clientConnectionInterpreter;
static int SAMPLES_PER_SEC = 100;

private DataThread dt;

public Server(int port, DataInterpreter dataInterpreter) {
this.port = port;
this.clientConnectionInterpreter = dataInterpreter;
this.serialNumber = getSerialNumberForPort(port);

log.info(String.format("Listening on port %d", port));
log.info(String.format("Listening on port %d\nSerial number: %d", port, serialNumber));
start();
}

@Override
public void run() {
try {
ServerSocket sserver = new ServerSocket(port);
BufferedReader in = null;
BufferedReader in;
while (sserver.isBound()) {
try {
Socket clientSocket = sserver.accept();
Expand Down Expand Up @@ -160,7 +163,7 @@ public ProtoMessageV2.DaqifiOutMessage getOutMessage() {
}
builder.setDeviceFwRev("1.0.2");
builder.setDeviceHwRev("1.0");
builder.setDeviceSn(4788544735461581972l);
builder.setDeviceSn(serialNumber);

builder.setDigitalPortNum(Nyquist1.DIGITAL_IO_CHANNELS);
builder.setDigitalPortType(toByteString(0, 1));
Expand All @@ -177,7 +180,6 @@ public ProtoMessageV2.DaqifiOutMessage getOutMessage() {
builder.setTempStatus(1);
//builder.setDacBytes(2);


builder.setDigitalPortDir(toByteString(0, 1));
builder.setHostName(getHostName());
builder.setIpAddr(ByteString.copyFrom(getIpAddress()));
Expand All @@ -186,12 +188,6 @@ public ProtoMessageV2.DaqifiOutMessage getOutMessage() {
builder.setPrimaryDns(ByteString.copyFrom("8.8.8.8".getBytes()));
builder.setSecondaryDns(ByteString.copyFrom("1.1.1.1".getBytes()));

// IPv6
// builder.setIpAddrV6(ByteString.copyFrom(getLocalIpV6().getBytes()));
// builder.setSubPreLengthV6()
// builder.setEui64(ByteString.copyFrom("00:00:00:ff:f0:00:00:00".getBytes()));


builder.setSsid("1111");
builder.setDevicePn("Nq1");
builder.setDevicePort(port);
Expand All @@ -216,31 +212,6 @@ protected int getChannelMask() {
return channelMask;
}

byte[] macAddr = initMacAddr();

protected byte[] initMacAddr() {
try {
Enumeration<NetworkInterface> networks = NetworkInterface
.getNetworkInterfaces();
while (networks.hasMoreElements()) {
NetworkInterface network = networks.nextElement();
byte[] mac = network.getHardwareAddress();
if (mac != null) {
return mac;
}
}
} catch (SocketException e) {
e.printStackTrace();
}

byte[] mac = new byte[6];
SecureRandom random = new SecureRandom();
random.nextBytes(mac);
return mac;
}

private InetAddress ip = initIp();

protected InetAddress initIp() {
try {
return InetAddress.getLocalHost();
Expand Down Expand Up @@ -269,7 +240,14 @@ protected byte[] getIpAddress() {
}

protected byte[] getMacAddress() {
return macAddr;
byte[] mac = new byte[6];
mac[0] = (byte) 0x02;
mac[1] = (byte) 0x00;
mac[2] = (byte) 0x00;
mac[3] = (byte) ((port >> 16) & 0xFF);
mac[4] = (byte) ((port >> 8) & 0xFF);
mac[5] = (byte) (port & 0xFF);
return mac;
}

public class DataThread extends Thread {
Expand Down Expand Up @@ -385,4 +363,8 @@ private void buildDataMessageV2(long time) throws IOException {
out.flush();
}
}

private long getSerialNumberForPort(int port) {
return BASE_SERIAL_NUMBER + port;
}
}