Code modernization

This commit is contained in:
2015-01-16 17:55:23 -05:00
parent 0b250cd7d3
commit 47db415506
15 changed files with 1097 additions and 1312 deletions

View File

@@ -1,95 +1,76 @@
using System;
using System.Collections.Generic;
namespace OneWireAPI
{
public class owNetwork
{
#region Member variables
private owSession _session; // Current session
private Dictionary<string, owDevice> _deviceList; // List of current devices
#endregion
#region Constructor
public owNetwork(owSession Session)
public owNetwork(owSession session)
{
_session = Session;
_session = session;
_deviceList = new Dictionary<string, owDevice>();
}
#endregion
#region Delegates
public delegate void DeviceEventDelegate(owDevice device);
#endregion
#region Events
public event DeviceEventDelegate DeviceAdded;
#endregion
#region Private methods
private void LoadDevices()
{
owDevice device; // Current device object
// Get the first device on the network
short nResult = TMEX.TMFirst(_session.SessionHandle, _session.StateBuffer);
var nResult = TMEX.TMFirst(_session.SessionHandle, _session.StateBuffer);
// Keep looping while we get good device data
while (nResult == 1)
{
// Create a new device ID buffer
short[] nROM = new short[8];
var id = new short[8];
// Get the ROM from the device
nResult = TMEX.TMRom(_session.SessionHandle, _session.StateBuffer, nROM);
nResult = TMEX.TMRom(_session.SessionHandle, _session.StateBuffer, id);
// If the ROM was read correctly then add the device to the list
if (nResult == 1)
{
// Get the deviceID
owIdentifier deviceID = new owIdentifier(nROM);
var deviceId = new owIdentifier(id);
// Create a new device object
switch (deviceID.Family)
owDevice device;
switch (deviceId.Family)
{
case 0x10:
device = new owDeviceFamily10(_session, nROM);
device = new owDeviceFamily10(_session, id);
break;
case 0x1D:
device = new owDeviceFamily1D(_session, nROM);
device = new owDeviceFamily1D(_session, id);
break;
case 0x20:
device = new owDeviceFamily20(_session, nROM);
device = new owDeviceFamily20(_session, id);
break;
case 0x26:
device = new owDeviceFamily26(_session, nROM);
device = new owDeviceFamily26(_session, id);
break;
case 0x12:
device = new owDeviceFamily12(_session, nROM);
device = new owDeviceFamily12(_session, id);
break;
case 0xFF:
device = new owDeviceFamilyFF(_session, nROM);
device = new owDeviceFamilyFF(_session, id);
break;
default:
device = new owDevice(_session, nROM);
device = new owDevice(_session, id);
break;
}
// Check if we've seen this device before
if (!_deviceList.ContainsKey(device.ID.Name))
if (!_deviceList.ContainsKey(device.Id.Name))
{
// Add the device to the device list
_deviceList[device.ID.Name] = device;
_deviceList[device.Id.Name] = device;
// Raise the device added event
if (DeviceAdded != null)
@@ -102,19 +83,11 @@ namespace OneWireAPI
}
}
#endregion
#region Properties
public Dictionary<string, owDevice> Devices
{
get { return _deviceList; }
}
#endregion
#region Public methods
public void Initialize()
{
// Load the device list
@@ -130,7 +103,5 @@ namespace OneWireAPI
// Get rid of the session
_session = null;
}
#endregion
}
}