-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.cs
More file actions
103 lines (85 loc) · 3.25 KB
/
Network.cs
File metadata and controls
103 lines (85 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Collections.Generic;
namespace OneWireAPI
{
public class Network
{
private Session _session;
public Dictionary<string, Device> Devices { get; private set; }
public Network(Session session)
{
_session = session;
Devices = new Dictionary<string, Device>();
}
public delegate void DeviceEventDelegate(Device device);
public event DeviceEventDelegate DeviceAdded;
private void LoadDevices()
{
// Get the first device on the network
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
var id = new short[8];
// Get the ROM from the device
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
var deviceId = new Identifier(id);
// Create a new device object
Device device;
switch (deviceId.Family)
{
case 0x10:
device = new DeviceFamily10(_session, id);
break;
case 0x1D:
device = new DeviceFamily1D(_session, id);
break;
case 0x20:
device = new DeviceFamily20(_session, id);
break;
case 0x26:
device = new DeviceFamily26(_session, id);
break;
case 0x12:
device = new DeviceFamily12(_session, id);
break;
case 0xFF:
device = new DeviceFamilyFF(_session, id);
break;
default:
device = new Device(_session, id);
break;
}
// Check if we've seen this device before
if (!Devices.ContainsKey(device.Id.Name))
{
// Add the device to the device list
Devices[device.Id.Name] = device;
// Raise the device added event
if (DeviceAdded != null)
DeviceAdded(device);
}
}
// Try to get the next device
nResult = TMEX.TMNext(_session.SessionHandle, _session.StateBuffer);
}
}
public void Initialize()
{
// Load the device list
LoadDevices();
}
public void Terminate()
{
// Get rid of the device list
Devices.Clear();
Devices = null;
// Get rid of the session
_session = null;
}
}
}