use of org.openmuc.jmbus.SecondaryAddress in project OpenMUC by isc-konstanz.
the class DriverConnectionTest method newConnection.
private DriverConnection newConnection(String mBusAdresse) throws Exception {
MBusConnection con = mock(MBusConnection.class);
VariableDataStructure vds = new VariableDataStructure(NZR_ANSWER, 6, NZR_ANSWER.length - 6, null, null);
vds.decode();
PowerMockito.when(con.read(anyInt())).thenReturn(vds);
ConnectionInterface serialIntervace = new ConnectionInterface(con, mBusAdresse, delay, interfaces);
serialIntervace.increaseConnectionCounter();
String[] deviceAddressTokens = mBusAdresse.trim().split(":");
Integer mBusAddress;
SecondaryAddress secondaryAddress = null;
if (deviceAddressTokens[1].length() == 16) {
mBusAddress = 0xfd;
byte[] addressData = Helper.hexToBytes(deviceAddressTokens[1]);
secondaryAddress = SecondaryAddress.newFromLongHeader(addressData, 0);
} else {
mBusAddress = Integer.decode(deviceAddressTokens[1]);
}
DriverConnection mBusConnection = new DriverConnection(serialIntervace, mBusAddress, secondaryAddress, delay);
return mBusConnection;
}
use of org.openmuc.jmbus.SecondaryAddress in project OpenMUC by isc-konstanz.
the class Driver method connect.
@Override
public Connection connect(String deviceAddress, String settingsString) throws ArgumentSyntaxException, ConnectionException {
String[] deviceAddressTokens = deviceAddress.trim().split(":");
String serialPortName = "";
boolean isTCP = false;
String host = "";
int port = 0;
int offset;
if (deviceAddressTokens[0].equalsIgnoreCase(TCP)) {
host = deviceAddressTokens[1];
try {
port = Integer.parseInt(deviceAddressTokens[2]);
} catch (NumberFormatException e) {
throw new ArgumentSyntaxException("Could not parse port.");
}
isTCP = true;
offset = 2;
} else {
if (deviceAddressTokens.length != 2) {
throw new ArgumentSyntaxException("The device address does not consist of two parameters.");
}
offset = 0;
serialPortName = deviceAddressTokens[0 + offset];
}
Integer mBusAddress;
SecondaryAddress secondaryAddress = null;
try {
if (deviceAddressTokens[1 + offset].length() == 16) {
mBusAddress = 0xfd;
byte[] saData = Helper.hexToBytes(deviceAddressTokens[1 + offset]);
secondaryAddress = SecondaryAddress.newFromLongHeader(saData, 0);
} else {
mBusAddress = Integer.decode(deviceAddressTokens[1 + offset]);
}
} catch (Exception e) {
throw new ArgumentSyntaxException("Settings: mBusAddress (" + deviceAddressTokens[1 + offset] + ") is not a number between 0 and 255 nor a 16 sign long hexadecimal secondary address");
}
ConnectionInterface connectionInterface;
Settings settings = new Settings(settingsString, false);
synchronized (this) {
synchronized (interfaces) {
connectionInterface = setConnectionInterface(deviceAddressTokens, serialPortName, isTCP, host, port, offset, settings);
}
synchronized (connectionInterface) {
if (strictConnectionTest) {
try {
testConnection(mBusAddress, secondaryAddress, connectionInterface, settings);
} catch (IOException e) {
connectionInterface.close();
throw new ConnectionException(e);
}
}
connectionInterface.increaseConnectionCounter();
}
}
DriverConnection driverCon = new DriverConnection(connectionInterface, mBusAddress, secondaryAddress, settings.delay);
driverCon.setResetLink(settings.resetLink);
driverCon.setResetApplication(settings.resetApplication);
return driverCon;
}
use of org.openmuc.jmbus.SecondaryAddress in project OpenMUC by isc-konstanz.
the class Driver method parseSecondaryAddress.
private SecondaryAddress parseSecondaryAddress(String secondaryAddressAsString) throws ArgumentSyntaxException, DecoderException {
SecondaryAddress secondaryAddress;
try {
byte[] bytes = Hex.decodeHex(secondaryAddressAsString);
secondaryAddress = SecondaryAddress.newFromWMBusHeader(bytes, 0);
} catch (NumberFormatException e) {
throw new ArgumentSyntaxException("The SecondaryAddress: " + secondaryAddressAsString + " could not be converted to a byte array.");
}
return secondaryAddress;
}
use of org.openmuc.jmbus.SecondaryAddress in project OpenMUC by isc-konstanz.
the class Driver method connect.
@Override
public Connection connect(String deviceAddress, String settings) throws ArgumentSyntaxException, ConnectionException {
String[] deviceAddressTokens = deviceAddress.trim().split(":");
if (deviceAddressTokens.length != 2) {
throw new ArgumentSyntaxException("The device address does not consist of two parameters.");
}
String connectionPort = deviceAddressTokens[0];
String secondaryAddressAsString = "";
String host = "";
int port = 0;
boolean isTCP = false;
if (connectionPort.equalsIgnoreCase("TCP")) {
isTCP = true;
host = deviceAddressTokens[0];
try {
port = Integer.decode(deviceAddressTokens[1]);
} catch (NumberFormatException e) {
throw new ArgumentSyntaxException("TCP port is not a number.");
}
secondaryAddressAsString = deviceAddressTokens[2].toLowerCase();
} else {
secondaryAddressAsString = deviceAddressTokens[1].toLowerCase();
}
SecondaryAddress secondaryAddress = null;
try {
secondaryAddress = parseSecondaryAddress(secondaryAddressAsString);
} catch (DecoderException e) {
e.printStackTrace();
}
String[] settingsTokens = splitSettingsToken(settings);
String transceiverString = settingsTokens[0];
String modeString = settingsTokens[1];
String keyString = null;
if (settingsTokens.length == 3) {
keyString = settingsTokens[2];
}
WMBusInterface wmBusInterface;
if (isTCP) {
synchronized (this) {
wmBusInterface = WMBusInterface.getTCPInstance(host, port, transceiverString, modeString);
try {
return wmBusInterface.connect(secondaryAddress, keyString);
} catch (DecoderException e) {
e.printStackTrace();
}
}
} else {
synchronized (this) {
wmBusInterface = WMBusInterface.getSerialInstance(connectionPort, transceiverString, modeString);
try {
return wmBusInterface.connect(secondaryAddress, keyString);
} catch (DecoderException e) {
e.printStackTrace();
}
}
}
return null;
}
use of org.openmuc.jmbus.SecondaryAddress in project solarnetwork-node by SolarNetwork.
the class WMBusNetworkTests method prepareConnection.
/**
* Create and prime the connection with test data
*
* @throws IOException
* @throws DecodingException
*/
@BeforeClass
public static void prepareConnection() throws IOException, DecodingException {
final byte[] bytes = copyToByteArray(WMBusNetworkTests.class.getResourceAsStream("wmbus-message.bin"));
final byte[] key = { (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB };
final SecondaryAddress address = SecondaryAddress.newFromWMBusHeader(bytes, 2);
keyMap.put(address, key);
conn = network.createConnection(JMBusConversion.from(address), key);
}
Aggregations