-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
251 lines (249 loc) · 10 KB
/
Program.cs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using static System.Net.Dns;
using System.Net.Sockets;
using static System.Text.Encoding;
using System.Threading;
using System.Linq;
namespace AssetManagementSystem
{
class Beacon
{
private double distance;
private double rssi;
private int txPower;
private string address;
public double Distance { get => distance; set => distance = value; }
public double Rssi { get => rssi; }
public int TxPower { get => txPower; }
public string Address { get => address; }
public Beacon(string address, int txPower, double rssi)
{
this.address = address;
this.distance = 0.89976 * Math.Pow(rssi/txPower, 7.7095) + 0.111;
this.txPower = txPower;
this.rssi = rssi;
}
}
class Node
{
private string id;
private Beacon beacon;
public string Id { get => id; }
public Beacon Beacon { get => beacon; }
public Node(string id, string address, int txPower, double rssi)
{
this.id = id;
beacon = new Beacon(address, txPower, rssi);
}
}
class Data
{
private int length;
private byte[] buffer;
private static string[] fields;
private static List<Node> nodes;
private static List<(string, string)> references;
public int Length { get => length; set => length = value; }
public byte[] Buffer { get => buffer; }
public static string[] Fields { get => fields; set => fields = value; }
public static List<Node> Nodes { get => nodes; set => nodes = value; }
public static List<(string, string)> References { get => references; set => references = value; }
public Data()
{
length = 0;
buffer = new byte[30];
fields = null;
nodes = new List<Node>();
references = new List<(string, string)>();
}
public static bool IsReference((string id, string address) reference)
{
return reference.id == Fields[0] && reference.address == Fields[3];
}
}
class Network
{
private int port;
private NetworkStream stream;
private TcpClient client;
private TcpListener connection;
public int Port { get => port; }
public NetworkStream Stream { get => stream; set => stream = value; }
public TcpClient Client { get => client; set => client = value; }
public TcpListener Connection { get => connection; }
public Network(int port)
{
this.port = port;
IPAddress address = GetHostEntry(GetHostName()).AddressList[1];
connection = new TcpListener(address, port);
}
}
static class IndoorLocalization
{
private static double GetCosAngle(double adjacentSide1, double adjacentSide2, double oppositeSide) => Math.Acos(GetAngleRatio(adjacentSide1, adjacentSide2, oppositeSide));
private static double GetSinAngle(double adjacentSide1, double adjacentSide2, double oppositeSide) => Math.Asin(GetAngleRatio(adjacentSide1, adjacentSide2, oppositeSide));
private static double GetAngleRatio(double adjacentSide1, double adjacentSide2, double oppositeSide) => (Math.Pow(adjacentSide1, 2) - Math.Pow(oppositeSide, 2) + Math.Pow(adjacentSide2, 2)) / (2 * adjacentSide1 * adjacentSide2);
private static (double, double) GetBeaconCoordinates()
{
return (1, 2);
}
}
static class Program
{
private static int invokes = 0;
private static void TimeConnection(object s)
{
AutoResetEvent state = (AutoResetEvent)s;
Thread node1 = new Thread(StartConnection);
Thread node2 = new Thread(StartConnection);
Thread node3 = new Thread(StartConnection);
if (node1.IsAlive || node2.IsAlive || node3.IsAlive)
{
Console.Write("\rWaiting for scan threads to finish");
}
else if (invokes == 5)
{
Console.WriteLine("Scanning finally stopped!");
invokes = 0;
state.Set();
}
else
{
Console.WriteLine("Scanning started {0}x!", ++invokes);
node1.Start(new Network(55556));
node2.Start(new Network(55557));
node3.Start(new Network(55558));
}
}
private static void GetProximityStatus(object s)
{
Network server = (Network)s;
while (true)
{
try
{
server.Connection.Start();
}
catch (SocketException)
{
server.Connection.Stop();
return;
}
Console.WriteLine("Waiting for connection @ {0}", server.Port);
server.Client = server.Connection.AcceptTcpClient();
using (server.Stream = server.Client.GetStream())
{
Console.WriteLine("Connected to {0}!", server.Port);
byte[] buffer = new byte[1];
server.Stream.ReadTimeout = 10000;
try
{
while (server.Stream.Read(buffer, 0, buffer.Length) != 0)
{
Console.Write("\rWaiting for proximity...");
if (int.Parse(ASCII.GetString(buffer, 0, buffer.Length)) != 0)
{
Console.WriteLine("\rThe proximity is triggered!");
var state = new AutoResetEvent(false);
var timer = new Timer(TimeConnection, state, 0, 60000);
state.WaitOne();
timer.Dispose();
Console.WriteLine("Connections ended!");
break;
}
}
}
catch (IOException)
{
server.Client.Close();
server.Connection.Stop();
}
}
}
}
private static void StartConnection(object s)
{
Network server = (Network)s;
try
{
server.Connection.Start();
}
catch (SocketException)
{
server.Connection.Stop();
return;
}
Console.WriteLine("Waiting for connection @ {0}", server.Port);
server.Client = server.Connection.AcceptTcpClient();
using (server.Stream = server.Client.GetStream())
{
Console.WriteLine("Connected to {0}!", server.Port);
Data info = new Data();
server.Stream.ReadTimeout = 10000;
try
{
while ((info.Length = server.Stream.Read(info.Buffer, 0, info.Buffer.Length)) != 0)
{
Data.Fields = ASCII.GetString(info.Buffer, 0, info.Length).Split(" ");
Data.Nodes.Add(new Node(id: Data.Fields[0],
txPower: int.Parse(Data.Fields[1]),
rssi: double.Parse(Data.Fields[2]),
address: Data.Fields[3]));
if (!Data.References.Exists(Data.IsReference))
{
Data.References.Add((Data.Fields[0], Data.Fields[3]));
}
}
}
catch (IOException)
{
Console.WriteLine("Creating nodes for {0}", server.Port);
server.Client.Close();
server.Connection.Stop();
foreach ((string id, string address) reference in Data.References)
{
List<Node> results = Data.Nodes.FindAll(delegate (Node node)
{
return node.Id == reference.id && node.Beacon.Address == reference.address;
});
if (results.Count > 1)
{
List<double> rssis = new List<double>();
rssis.AddRange(from Node result in results select result.Beacon.Rssi);
Node mean = new Node(id: reference.id,
txPower: results[0].Beacon.TxPower,
rssi: rssis.Average(),
address: reference.address);
foreach (Node result in results)
{
Data.Nodes.Remove(result);
}
Data.Nodes.Add(mean);
}
}
DisplayNodes(new List<Node>(Data.Nodes));
Console.WriteLine("Connection to {0} ended!", server.Port);
}
}
}
public static void DisplayNodes(List<Node> updatedNodes)
{
foreach (Node node in updatedNodes)
{
Console.WriteLine("Node: {0}\tAddress: {1}\tRSSI: {2:F2}\tTX Power: {3}\tDistance: {4:F4} m",
node.Id,
node.Beacon.Address,
node.Beacon.Rssi,
node.Beacon.TxPower,
node.Beacon.Distance);
}
}
static void Main(string[] args)
{
GetProximityStatus(new Network(55555));
}
}
}