Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
LordVeovis committed Jul 2, 2015
1 parent cef6687 commit 45bbf58
Show file tree
Hide file tree
Showing 20 changed files with 157 additions and 178 deletions.
32 changes: 12 additions & 20 deletions XBeeLibrary/AbstractXBeeDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ public abstract class AbstractXBeeDevice

protected DataReader dataReader = null;

protected XBeeProtocol xbeeProtocol = XBeeProtocol.UNKNOWN;

protected OperatingMode operatingMode = OperatingMode.UNKNOWN;

protected XBee16BitAddress xbee16BitAddress = XBee16BitAddress.UNKNOWN_ADDRESS;
Expand Down Expand Up @@ -188,6 +186,7 @@ public AbstractXBeeDevice(IConnectionInterface connectionInterface)
{
Contract.Requires<ArgumentNullException>(connectionInterface != null, "ConnectionInterface cannot be null.");

XBeeProtocol = XBeeProtocol.UNKNOWN;
this.connectionInterface = connectionInterface;
this.logger = LogManager.GetLogger(this.GetType());
logger.DebugFormat(ToString() + "Using the connection interface {0}.",
Expand Down Expand Up @@ -257,6 +256,7 @@ public AbstractXBeeDevice(XBeeDevice localXBeeDevice, XBee64BitAddress addr64,
if (localXBeeDevice.IsRemote)
throw new ArgumentException("The given local XBee device is remote.");

XBeeProtocol = XBeeProtocol.UNKNOWN;
this.localXBeeDevice = localXBeeDevice;
this.connectionInterface = localXBeeDevice.GetConnectionInterface();
this.xbee64BitAddress = addr64;
Expand Down Expand Up @@ -346,12 +346,12 @@ public void readDeviceInfo() /*throws TimeoutException, XBeeException*/ {
firmwareVersion = HexUtils.ByteArrayToHexString(response);

// Obtain the device protocol.
xbeeProtocol = XBeeProtocol.UNKNOWN.DetermineProtocol(hardwareVersion, firmwareVersion);
XBeeProtocol = XBeeProtocol.UNKNOWN.DetermineProtocol(hardwareVersion, firmwareVersion);

// Get the 16-bit address. This must be done after obtaining the protocol because
// DigiMesh and Point-to-Multipoint protocols don't have 16-bit addresses.
if (GetXBeeProtocol() != XBeeProtocol.DIGI_MESH
&& GetXBeeProtocol() != XBeeProtocol.DIGI_POINT)
if (XBeeProtocol != XBeeProtocol.DIGI_MESH
&& XBeeProtocol != XBeeProtocol.DIGI_POINT)
{
response = GetParameter("MY");
xbee16BitAddress = new XBee16BitAddress(response);
Expand Down Expand Up @@ -405,19 +405,11 @@ protected OperatingMode GetOperatingMode()
return operatingMode;
}

/**
* Returns the XBee Protocol of this XBee device.
*
* <p>To refresh this value use the {@link #readDeviceInfo()} method.</p>
*
* @return The XBee device protocol.
*
* @see com.digi.xbee.api.models.XBeeProtocol
*/
public virtual XBeeProtocol GetXBeeProtocol()
{
return xbeeProtocol;
}
/// <summary>
/// Gets the XBee protocol of this XBee device.
/// </summary>
/// <remarks>To refresh this value, use the <see cref="ReadDeviceInfo"/> method.</remarks>
public virtual XBeeProtocol XBeeProtocol { get; protected set; }

/// <summary>
/// Gets or sets the node identifier of thix XBee device.
Expand Down Expand Up @@ -1800,7 +1792,7 @@ public IOSample readIOSample()/*throws TimeoutException, XBeeException */{

// The response to the IS command in local 802.15.4 devices is empty,
// so we have to create a packet listener to receive the IO sample.
if (!IsRemote && GetXBeeProtocol() == XBeeProtocol.RAW_802_15_4)
if (!IsRemote && XBeeProtocol == XBeeProtocol.RAW_802_15_4)
{
ExecuteParameter("IS");
samplePayload = receiveRaw802IOPacket();
Expand Down Expand Up @@ -2125,7 +2117,7 @@ protected void Set16BitAddress(XBee16BitAddress xbee16BitAddress)/*throws Timeou
* @see #setPANID(byte[])
*/
public byte[] GetPANID()/*throws TimeoutException, XBeeException */{
switch (GetXBeeProtocol())
switch (XBeeProtocol)
{
case XBeeProtocol.ZIGBEE:
return GetParameter("OP");
Expand Down
21 changes: 7 additions & 14 deletions XBeeLibrary/Connection/DataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ private RemoteXBeeDevice createRemoteXBeeDevice(XBee64BitAddress addr64,
{
RemoteXBeeDevice device = null;

switch (xbeeDevice.GetXBeeProtocol())
switch (xbeeDevice.XBeeProtocol)
{
case XBeeProtocol.ZIGBEE:
device = new RemoteZigBeeDevice(xbeeDevice, addr64, addr16, ni);
Expand Down Expand Up @@ -751,14 +751,9 @@ private void NotifyModemStatusReceived(ModemStatusEvent modemStatusEvent)
}
}

/**
* Returns whether this Data reader is running or not.
*
* @return {@code true} if the Data reader is running, {@code false}
* otherwise.
*
* @see #stopReader()
*/
/// <summary>
/// Indicates whether the data reader is running or not.
/// </summary>
public bool IsRunning
{
get
Expand All @@ -767,11 +762,9 @@ public bool IsRunning
}
}

/**
* Stops the Data reader thread.
*
* @see #isRunning()
*/
/// <summary>
/// Stops the data reader thread.
/// </summary>
public void StopReader()
{
running = false;
Expand Down
11 changes: 7 additions & 4 deletions XBeeLibrary/DigiMeshDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public DigiMeshDevice(IConnectionInterface connectionInterface)
public override void Open()/*throws XBeeException */{
base.Open();

if (xbeeProtocol != XBeeProtocol.DIGI_MESH)
throw new XBeeDeviceException("XBee device is not a " + GetXBeeProtocol().GetDescription() + " device, it is a " + xbeeProtocol.GetDescription() + " device.");
if (base.XBeeProtocol != XBeeProtocol.DIGI_MESH)
throw new XBeeDeviceException("XBee device is not a " + XBeeProtocol.GetDescription() + " device, it is a " + base.XBeeProtocol.GetDescription() + " device.");
}

public override XBeeNetwork GetNetwork()
Expand All @@ -112,9 +112,12 @@ public override XBeeNetwork GetNetwork()
return network;
}

public override XBeeProtocol GetXBeeProtocol()
public override XBeeProtocol XBeeProtocol
{
return XBeeProtocol.DIGI_MESH;
get
{
return XBeeProtocol.DIGI_MESH;
}
}
}
}
15 changes: 5 additions & 10 deletions XBeeLibrary/DigiMeshNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ namespace Kveer.XBeeApi
*/
public class DigiMeshNetwork : XBeeNetwork
{

/**
* Instantiates a new DigiMesh Network object.
*
* @param device Local DigiMesh device to get the network from.
*
* @throws ArgumentNullException if {@code device == null}.
*
* @see DigiMeshDevice
*/
/// <summary>
/// Initializes a new instance of <see cref="DigiMeshNetwork"/>.
/// </summary>
/// <param name="device">A local DigiMesh device to get the network from.</param>
/// <exception cref="ArgumentNullException">if <paramref name="device"/> is null.</exception>
public DigiMeshNetwork(DigiMeshDevice device)
: base(device)
{
Expand Down
18 changes: 9 additions & 9 deletions XBeeLibrary/DigiPointDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ public DigiPointDevice(IConnectionInterface connectionInterface)

public override void Open()/*throws XBeeException */{
base.Open();

if (IsRemote)
return;
if (xbeeProtocol != XBeeProtocol.DIGI_POINT)
throw new XBeeDeviceException("XBee device is not a " + GetXBeeProtocol().GetDescription() + " device, it is a " + xbeeProtocol.GetDescription() + " device.");

if (base.XBeeProtocol != XBeeProtocol.DIGI_POINT)
throw new XBeeDeviceException("XBee device is not a " + XBeeProtocol.GetDescription() + " device, it is a " + base.XBeeProtocol.GetDescription() + " device.");
}

/*
Expand All @@ -113,14 +115,12 @@ public override XBeeNetwork GetNetwork()
return network;
}

/*
* (non-Javadoc)
* @see com.digi.xbee.api.XBeeDevice#getXBeeProtocol()
*/
//@Override
public override XBeeProtocol GetXBeeProtocol()
public override XBeeProtocol XBeeProtocol
{
return XBeeProtocol.DIGI_POINT;
get
{
return XBeeProtocol.DIGI_POINT;
}
}
}
}
15 changes: 5 additions & 10 deletions XBeeLibrary/DigiPointNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ namespace Kveer.XBeeApi
*/
public class DigiPointNetwork : XBeeNetwork
{

/**
* Instantiates a new DigiPoint Network object.
*
* @param device Local DigiPoint device to get the network from.
*
* @throws ArgumentNullException if {@code device == null}.
*
* @see DigiPointDevice
*/
/// <summary>
/// Initializes a new instance of <see cref="DigiPointNetwork"/>.
/// </summary>
/// <param name="device">A local DigiPoint device to get the network from.</param>
/// <exception cref="ArgumentNullException">if <see cref="device"/> is null.</exception>
public DigiPointNetwork(DigiPointDevice device)
: base(device)
{
Expand Down
51 changes: 25 additions & 26 deletions XBeeLibrary/NodeDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ public List<RemoteXBeeDevice> discoverDevices(IList<string> ids)/*throws XBeeExc
* @throws InterfaceNotOpenException if the device is not open.
* @throws ArgumentNullException if {@code listeners == null}.
*
* @see #isRunning()
* @see #stopDiscoveryProcess()
* @see #IsRunning()
* @see #StopDiscoveryProcess()
*/
public void startDiscoveryProcess(IList<IDiscoveryListener> listeners)
{
Expand Down Expand Up @@ -211,26 +211,25 @@ public void startDiscoveryProcess(IList<IDiscoveryListener> listeners)
/**
* Stops the discovery process if it is running.
*
* @see #isRunning()
* @see #IsRunning()
* @see #startDiscoveryProcess(List)
*/
public void stopDiscoveryProcess()
public void StopDiscoveryProcess()
{
discovering = false;
}

/**
* Retrieves whether the discovery process is running.
*
* @return {@code true} if the discovery process is running, {@code false}
* otherwise.
*
* @see #startDiscoveryProcess(List)
* @see #stopDiscoveryProcess()
*/
public bool isRunning()
/// <summary>
/// Indicates whether the discovery process is running.
/// </summary>
/// <see cref="StartDiscoveryProcess"/>
/// <see cref="StopDiscoveryProcess"/>
public bool IsRunning
{
return running;
get
{
return running;
}
}

/**
Expand Down Expand Up @@ -292,7 +291,7 @@ public async void PacketReceived(XBeePacket receivedPacket)
_node.deviceList.Add(rdevice);
}
// If the local device is 802.15.4 wait until the 'end' command is received.
if (_node.xbeeDevice.GetXBeeProtocol() != XBeeProtocol.RAW_802_15_4)
if (_node.xbeeDevice.XBeeProtocol != XBeeProtocol.RAW_802_15_4)
_node.discovering = false;
}
}
Expand Down Expand Up @@ -327,12 +326,12 @@ private void DiscoverDevicesAPI(IList<IDiscoveryListener> listeners, string id)/

// In 802.15.4 devices, the discovery finishes when the 'end' command
// is received, so it's not necessary to calculate the timeout.
if (xbeeDevice.GetXBeeProtocol() != XBeeProtocol.RAW_802_15_4)
if (xbeeDevice.XBeeProtocol != XBeeProtocol.RAW_802_15_4)
deadLine += CalculateTimeout(listeners);

sendNodeDiscoverCommand(id);

if (xbeeDevice.GetXBeeProtocol() != XBeeProtocol.RAW_802_15_4)
if (xbeeDevice.XBeeProtocol != XBeeProtocol.RAW_802_15_4)
{
// Wait for scan timeout.
while (discovering)
Expand Down Expand Up @@ -411,17 +410,17 @@ private long CalculateTimeout(IList<IDiscoveryListener> listeners)
// In DigiMesh/DigiPoint the network discovery timeout is NT + the
// network propagation time. It means that if the user sends an AT
// command just after NT ms, s/he will receive a timeout exception.
if (xbeeDevice.GetXBeeProtocol() == XBeeProtocol.DIGI_MESH)
if (xbeeDevice.XBeeProtocol == XBeeProtocol.DIGI_MESH)
{
timeout += 3000;
}
else if (xbeeDevice.GetXBeeProtocol() == XBeeProtocol.DIGI_POINT)
else if (xbeeDevice.XBeeProtocol == XBeeProtocol.DIGI_POINT)
{
timeout += 8000;
}
}

if (xbeeDevice.GetXBeeProtocol() == XBeeProtocol.DIGI_MESH)
if (xbeeDevice.XBeeProtocol == XBeeProtocol.DIGI_MESH)
{
try
{
Expand Down Expand Up @@ -513,7 +512,7 @@ private async Task<RemoteXBeeDevice> ParseDiscoveryAPIData(byte[] data, XBeeDevi
addr64 = new XBee64BitAddress(await ByteUtils.ReadBytes(8, inputStream));


switch (localDevice.GetXBeeProtocol())
switch (localDevice.XBeeProtocol)
{
case XBeeProtocol.ZIGBEE:
case XBeeProtocol.DIGI_MESH:
Expand All @@ -537,7 +536,7 @@ private async Task<RemoteXBeeDevice> ParseDiscoveryAPIData(byte[] data, XBeeDevi
manufacturerID = await ByteUtils.ReadBytes(2, inputStream);

logger.DebugFormat("{0}Discovered {1} device: 16-bit[{2}], 64-bit[{3}], id[{4}], parent[{5}], profile[{6}], manufacturer[{7}].",
xbeeDevice.ToString(), localDevice.GetXBeeProtocol().GetDescription(), addr16,
xbeeDevice.ToString(), localDevice.XBeeProtocol.GetDescription(), addr16,
addr64, id, parentAddress, HexUtils.ByteArrayToHexString(profileID),
HexUtils.ByteArrayToHexString(manufacturerID));

Expand All @@ -549,18 +548,18 @@ private async Task<RemoteXBeeDevice> ParseDiscoveryAPIData(byte[] data, XBeeDevi
id = ByteUtils.ReadString(inputStream);

logger.DebugFormat("{0}Discovered {1} device: 16-bit[{2}], 64-bit[{3}], id[{4}], rssi[{5}].",
xbeeDevice.ToString(), localDevice.GetXBeeProtocol().GetDescription(), addr16, addr64, id, signalStrength);
xbeeDevice.ToString(), localDevice.XBeeProtocol.GetDescription(), addr16, addr64, id, signalStrength);

break;
case XBeeProtocol.UNKNOWN:
default:
logger.DebugFormat("{0}Discovered {1} device: 16-bit[{2}], 64-bit[{3}].",
xbeeDevice.ToString(), localDevice.GetXBeeProtocol().GetDescription(), addr16, addr64);
xbeeDevice.ToString(), localDevice.XBeeProtocol.GetDescription(), addr16, addr64);
break;
}

// Create device and fill with parameters.
switch (localDevice.GetXBeeProtocol())
switch (localDevice.XBeeProtocol)
{
case XBeeProtocol.ZIGBEE:
device = new RemoteZigBeeDevice(localDevice, addr64, addr16, id/*, role*/);
Expand Down
Loading

0 comments on commit 45bbf58

Please sign in to comment.