Skip to content

Commit 8d293a7

Browse files
committed
fixes bug due to different behavior between Map.Get/Dictionary.Get and string formatting
1 parent 30f80d0 commit 8d293a7

File tree

5 files changed

+30
-34
lines changed

5 files changed

+30
-34
lines changed

XBeeLibrary/AbstractXBeeDevice.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -764,9 +764,9 @@ protected ATCommandResponse SendATCommand(ATCommand command)
764764
packet = new ATCommandQueuePacket(GetNextFrameID(), command.Command, command.Parameter);
765765
}
766766
if (command.Parameter == null)
767-
logger.DebugFormat(ToString() + "Sending AT command '{}'.", command.Command);
767+
logger.DebugFormat(ToString() + "Sending AT command '{0}'.", command.Command);
768768
else
769-
logger.DebugFormat(ToString() + "Sending AT command '{} {}'.", command.Command, HexUtils.PrettyHexString(command.Parameter));
769+
logger.DebugFormat(ToString() + "Sending AT command '{0} {1}'.", command.Command, HexUtils.PrettyHexString(command.Parameter));
770770
try
771771
{
772772
// Send the packet and build the corresponding response depending on if the device is local or remote.
@@ -781,7 +781,7 @@ protected ATCommandResponse SendATCommand(ATCommand command)
781781
response = new ATCommandResponse(command, ((RemoteATCommandResponsePacket)answerPacket).getCommandValue(), ((RemoteATCommandResponsePacket)answerPacket).Status);
782782

783783
if (response != null && response.Response != null)
784-
logger.DebugFormat(ToString() + "AT command response: {}.", HexUtils.PrettyHexString(response.Response));
784+
logger.DebugFormat(ToString() + "AT command response: {0}.", HexUtils.PrettyHexString(response.Response));
785785
else
786786
logger.Debug(ToString() + "AT command response: null.");
787787
}
@@ -1113,7 +1113,7 @@ private bool isSamePacket(XBeePacket sentPacket, XBeePacket receivedPacket)
11131113
* @see com.digi.xbee.api.packet.XBeePacket
11141114
*/
11151115
private void WritePacket(XBeePacket packet)/*throws IOException */{
1116-
logger.DebugFormat(ToString() + "Sending XBee packet: \n{}", packet.ToPrettyString());
1116+
logger.DebugFormat(ToString() + "Sending XBee packet: \n{0}", packet.ToPrettyString());
11171117
// Write bytes with the required escaping mode.
11181118
switch (operatingMode)
11191119
{

XBeeLibrary/Connection/Serial/AbstractSerialPort.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,16 @@ public override string ToString()
255255
flowControl = "H";
256256
else if (parameters.FlowControl == Handshake.XOnXOff)
257257
flowControl = "S";
258-
return "[" + port + " - " + baudRate + "/" + parameters.DataBits +
259-
"/" + parity + "/" + parameters.StopBits + "/" + flowControl + "] ";
258+
return string.Format("[{0} - {1}/{2}/{3}/{4}/{5}] ",
259+
port,
260+
baudRate,
261+
parameters.DataBits,
262+
parity,
263+
parameters.StopBits,
264+
flowControl);
260265
}
261266
else
262-
return "[" + port + " - " + baudRate + "/8/N/1/N] ";
267+
return string.Format("[{0} - {1}/8/N/1/N] ", port, baudRate);
263268
}
264269

265270
public SerialPort SerialPort { get; protected set; }

XBeeLibrary/NodeDiscovery.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public NodeDiscovery(XBeeDevice xbeeDevice)
9090
*
9191
* @see #discoverDevices(List)
9292
*/
93-
public RemoteXBeeDevice discoverDevice(string id)/*throws XBeeException */{
93+
public RemoteXBeeDevice DiscoverDevice(string id)/*throws XBeeException */{
9494
// Check if the connection is open.
9595
if (!xbeeDevice.IsOpen)
9696
throw new InterfaceNotOpenException();
@@ -133,7 +133,7 @@ public RemoteXBeeDevice discoverDevice(string id)/*throws XBeeException */{
133133
*
134134
* @see #discoverDevice(String)
135135
*/
136-
public List<RemoteXBeeDevice> discoverDevices(IList<String> ids)/*throws XBeeException */{
136+
public List<RemoteXBeeDevice> discoverDevices(IList<string> ids)/*throws XBeeException */{
137137
// Check if the connection is open.
138138
if (!xbeeDevice.IsOpen)
139139
throw new InterfaceNotOpenException();

XBeeLibrary/XBeeNetwork.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@
66
using System;
77
using System.Collections.Concurrent;
88
using System.Collections.Generic;
9+
using System.Diagnostics.Contracts;
910
using System.Linq;
1011

1112
namespace Kveer.XBeeApi
1213
{
13-
/**
14-
* This class represents an XBee Network.
15-
*
16-
* <p>The network allows the discovery of remote devices in the same network
17-
* as the local one and stores them.</p>
18-
*/
14+
/// <summary>
15+
/// This class represents an XBee Network.
16+
/// </summary>
17+
/// <remarks>The network allows the discovery of remote devices in the same network as the local one and stores them.</remarks>
1918
public class XBeeNetwork
2019
{
21-
2220
// Variables.
23-
2421
private XBeeDevice localDevice;
2522

2623
private ConcurrentDictionary<XBee64BitAddress, RemoteXBeeDevice> remotesBy64BitAddr;
@@ -79,15 +76,12 @@ public XBeeNetwork(XBeeDevice device)
7976
* @see #getDevice(String)
8077
* @see RemoteXBeeDevice
8178
*/
82-
public RemoteXBeeDevice discoverDevice(String id)/*throws XBeeException */{
83-
if (id == null)
84-
throw new ArgumentNullException("Device identifier cannot be null.");
85-
if (id.Length == 0)
86-
throw new ArgumentException("Device identifier cannot be an empty string.");
79+
public RemoteXBeeDevice DiscoverDevice(string id)/*throws XBeeException */{
80+
Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(id), "Device identifier cannot be null or empty.");
8781

8882
logger.DebugFormat("{0}Discovering '{1}' device.", localDevice.ToString(), id);
8983

90-
return nodeDiscovery.discoverDevice(id);
84+
return nodeDiscovery.DiscoverDevice(id);
9185
}
9286

9387
/**
@@ -115,7 +109,7 @@ public RemoteXBeeDevice discoverDevice(String id)/*throws XBeeException */{
115109
* @see #discoverDevice(String)
116110
* @see RemoteXBeeDevice
117111
*/
118-
public List<RemoteXBeeDevice> discoverDevices(IList<String> ids)/*throws XBeeException */{
112+
public List<RemoteXBeeDevice> discoverDevices(IList<string> ids)/*throws XBeeException */{
119113
if (ids == null)
120114
throw new ArgumentNullException("List of device identifiers cannot be null.");
121115
if (ids.Count == 0)
@@ -411,7 +405,10 @@ public RemoteXBeeDevice GetDevice(XBee64BitAddress address)
411405

412406
logger.DebugFormat("{0}Getting device '{1}' from network.", localDevice.ToString(), address);
413407

414-
return remotesBy64BitAddr[address];
408+
RemoteXBeeDevice result;
409+
remotesBy64BitAddr.TryGetValue(address, out result);
410+
411+
return result;
415412
}
416413

417414
/**
@@ -510,8 +507,7 @@ public RemoteXBeeDevice addRemoteDevice(RemoteXBeeDevice remoteDevice)
510507
if (addr64 != null && !addr64.Equals(XBee64BitAddress.UNKNOWN_ADDRESS))
511508
{
512509
// The device has 64-bit address, so look in the 64-bit map.
513-
devInNetwork = remotesBy64BitAddr[addr64];
514-
if (devInNetwork != null)
510+
if (remotesBy64BitAddr.TryGetValue(addr64, out devInNetwork))
515511
{
516512
// The device exists in the 64-bit map, so update the reference and return it.
517513
logger.DebugFormat("{0}Existing device '{1}' in network.", localDevice.ToString(), devInNetwork.ToString());
@@ -524,8 +520,7 @@ public RemoteXBeeDevice addRemoteDevice(RemoteXBeeDevice remoteDevice)
524520
if (addr16 != null && !addr16.Equals(XBee16BitAddress.UNKNOWN_ADDRESS))
525521
{
526522
// The device has 16-bit address, so look in the 16-bit map.
527-
devInNetwork = remotesBy16BitAddr[addr16];
528-
if (devInNetwork != null)
523+
if (remotesBy16BitAddr.TryGetValue(addr16, out devInNetwork))
529524
{
530525
// The device exists in the 16-bit map, so remove it and add it to the 64-bit map.
531526
logger.DebugFormat("{0}Existing device '{1}' in network.", localDevice.ToString(), devInNetwork.ToString());

XBeeLibrary/ZigBeeDevice.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,13 @@ public override void Open()/*throws XBeeException */{
9595
throw new XBeeDeviceException("XBee device is not a " + GetXBeeProtocol().GetDescription() + " device, it is a " + xbeeProtocol.GetDescription() + " device.");
9696
}
9797

98-
/*
99-
* (non-Javadoc)
100-
* @see com.digi.xbee.api.XBeeDevice#GetNetwork()
101-
*/
102-
//@Override
10398
public override XBeeNetwork GetNetwork()
10499
{
105100
if (!IsOpen)
106101
throw new InterfaceNotOpenException();
107102
if (network == null)
108103
network = new ZigBeeNetwork(this);
104+
109105
return network;
110106
}
111107

0 commit comments

Comments
 (0)