timespan 廃止。不要なコメント削減。

parent 58387adf
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
mc:Ignorable="d" mc:Ignorable="d"
Title="MainWindow" Height="350" Width="938.836"> Title="MainWindow" Height="350" Width="938.836">
<Grid> <Grid>
<TextBox x:Name="listBox" Margin="0,95,75,0.5" TextWrapping="Wrap" Text="around list" IsReadOnly="True"/> <TextBox x:Name="listBox" Margin="0,95,75,0.5" TextWrapping="Wrap" Text="" IsReadOnly="True"/>
<TextBox x:Name="RUNUBox" Margin="0,35,607,251.5" TextWrapping="Wrap" Text="" IsReadOnly="True"/> <TextBox x:Name="RUNUBox" Margin="0,35,607,251.5" TextWrapping="Wrap" Text="" IsReadOnly="True"/>
<TextBox x:Name="textBox" Margin="332,35,394,251.5" TextWrapping="Wrap" Text="" IsReadOnly="True"/> <TextBox x:Name="textBox" Margin="332,35,394,251.5" TextWrapping="Wrap" Text="" IsReadOnly="True"/>
<TextBox x:Name="pass" HorizontalAlignment="Left" Height="36" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Margin="621,35,0,0"/> <TextBox x:Name="pass" HorizontalAlignment="Left" Height="36" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Margin="621,35,0,0"/>
......
...@@ -25,16 +25,17 @@ namespace iBeaconScanner ...@@ -25,16 +25,17 @@ namespace iBeaconScanner
/* パラメータ */ /* パラメータ */
public static int A_thresh = 2;//認証可能人数 public static int A_thresh = 2;//認証可能人数
public static double B_thresh = 0.3;//RU率 public static double B_thresh = 0.3;//RU率
public static TimeSpan TIMEOUT = new TimeSpan(0,1,0);//3秒間離れてたら近くにいないと判断 public static TimeSpan LIMIT_TIME = new TimeSpan(0, 0, 8);//n秒パケットが取れなければ終了
public static TimeSpan LIMIT_TIME = new TimeSpan(0, 0, 5);//3秒パケットが取れなければ終了 public static short RSSI = -50;//探索範囲 大きいほど狭い
static int RUcnt, NUcnt; static int RUcnt, NUcnt;
Stopwatch sw = new Stopwatch(); Stopwatch sw = new Stopwatch();
static HashSet<string> RU_uuid = new HashSet<string>(); //RUのハッシュセット static HashSet<string> RU_uuid = new HashSet<string>(); //RUのハッシュセット
static Dictionary<string,DateTimeOffset> Around_uuid = new Dictionary<string, DateTimeOffset>(); //周囲のuuidたち static private HashSet<string> All_uuid = new HashSet<string>(); //RUのハッシュセット
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
...@@ -43,37 +44,28 @@ namespace iBeaconScanner ...@@ -43,37 +44,28 @@ namespace iBeaconScanner
RU_uuid.Add("48534442-4c45-4144-1234-dcba12345678"); RU_uuid.Add("48534442-4c45-4144-1234-dcba12345678");
RU_uuid.Add("48534442-4c45-4144-1234-abcd12345678"); RU_uuid.Add("48534442-4c45-4144-1234-abcd12345678");
RU_uuid.Add("48534442-4c45-4144-1234-abcd12345671"); RU_uuid.Add("48534442-4c45-4144-1234-abcd12345671");
RU_uuid.Add("48534442-4c45-4144-80c0-1800ffffffff"); RU_uuid.Add("48534442-4c45-4144-80c0-1800ffffffff");//deafault
this.advWatcher = new BluetoothLEAdvertisementWatcher(); this.advWatcher = new BluetoothLEAdvertisementWatcher();
// https://blogs.msdn.microsoft.com/shozoa/2016/02/28/windows-10-bluetooth/ // https://blogs.msdn.microsoft.com/shozoa/2016/02/28/windows-10-bluetooth/
// インターバルがゼロのままだと、CPU負荷が高くなりますので、適切な間隔(SDK サンプルでは 1秒)に指定しないと、アプリの動作に支障をきたすことになります // インターバルがゼロのままだと、CPU負荷が高い。適切な間隔(SDK サンプルでは 1秒)に指定しないと、アプリの動作に支障をきたすことになる
this.advWatcher.SignalStrengthFilter.SamplingInterval = TimeSpan.FromMilliseconds(1000); this.advWatcher.SignalStrengthFilter.SamplingInterval = TimeSpan.FromMilliseconds(1000);
// rssi >= -60のときスキャンする // rssi >= RSSIのときスキャンする
//this.advWatcher.SignalStrengthFilter.InRangeThresholdInDBm = -60; this.advWatcher.SignalStrengthFilter.InRangeThresholdInDBm = RSSI;//-50 で3m強
this.advWatcher.Received += this.Watcher_Received; this.advWatcher.Received += this.Watcher_Received;
sw.Start(); sw.Start();
// スキャン開始 // スキャン開始
this.advWatcher.Start(); this.advWatcher.Start();
} }
private static Mutex mutex = new Mutex(); private static Mutex mutex = new Mutex();
private void Button1_Click(object sender, EventArgs e)
{
//★★★TextBoxのテキストを取得★★★
string strText = pass.Text;
if (strText.Equals("1234")) {
this.textBox.Text = "OK!" + "\r\n";
}
Debug.WriteLine($"InSWARD");
}
private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args) private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{ {
...@@ -82,27 +74,16 @@ namespace iBeaconScanner ...@@ -82,27 +74,16 @@ namespace iBeaconScanner
mutex.WaitOne(); mutex.WaitOne();
Debug.WriteLine($"{sw.Elapsed}");// Debug.WriteLine($"{sw.Elapsed}");//経過時間
if (sw.Elapsed > LIMIT_TIME) if (sw.Elapsed > LIMIT_TIME)
{ {
Debug.WriteLine($"#############");
advWatcher.Stop(); advWatcher.Stop();
Debug.WriteLine($"Input PASSWARD");
this.textBox.Text = "Input PASSWARD" + "\r\n";
} }
foreach (var manufacturerData in args.Advertisement.ManufacturerData) foreach (var manufacturerData in args.Advertisement.ManufacturerData)
{ {
/* mutex.WaitOne();
Debug.WriteLine($"{args.Timestamp} {args.Advertisement.LocalName}:{args.BluetoothAddress}");
Debug.WriteLine($"RSSI={args.RawSignalStrengthInDBm}");
Debug.WriteLine($"length:{manufacturerData.Data.Length}");
Debug.WriteLine($"companyId :{manufacturerData.CompanyId}");
Debug.WriteLine($"Ad type :{args.AdvertisementType}");
Debug.WriteLine($"--------------");
*/
//mutex.WaitOne();
if (manufacturerData.CompanyId != CompanyID_Apple) if (manufacturerData.CompanyId != CompanyID_Apple)
{ {
...@@ -117,6 +98,7 @@ namespace iBeaconScanner ...@@ -117,6 +98,7 @@ namespace iBeaconScanner
Debug.WriteLine($"{args.Timestamp} RSSI={args.RawSignalStrengthInDBm} BD:{args.BluetoothAddress}"); Debug.WriteLine($"{args.Timestamp} RSSI={args.RawSignalStrengthInDBm} BD:{args.BluetoothAddress}");
// Debug.WriteLine($"{args.AdvertisementType}, {manufacturerData.CompanyId}"); // Debug.WriteLine($"{args.AdvertisementType}, {manufacturerData.CompanyId}");
/* All_uuidリストを作成するフェーズ */
using (var reader = DataReader.FromBuffer(manufacturerData.Data)) using (var reader = DataReader.FromBuffer(manufacturerData.Data))
{ {
reader.ByteOrder = ByteOrder.BigEndian;// BigEndian:順方向にパケットを保存 reader.ByteOrder = ByteOrder.BigEndian;// BigEndian:順方向にパケットを保存
...@@ -129,7 +111,7 @@ namespace iBeaconScanner ...@@ -129,7 +111,7 @@ namespace iBeaconScanner
continue; //iBeacon でないAdvertise Packet continue; //iBeacon でないAdvertise Packet
} }
Debug.WriteLine("#####This is iBeacon Packet!!!"); Debug.WriteLine(" This is iBeacon Packet!!!");
// Parse advertise packet. // Parse advertise packet.
var uuidA = reader.ReadInt32(); var uuidA = reader.ReadInt32();
...@@ -142,72 +124,53 @@ namespace iBeaconScanner ...@@ -142,72 +124,53 @@ namespace iBeaconScanner
var power = reader.ReadByte(); var power = reader.ReadByte();
var proximity_uuid = new Guid(uuidA, uuidB, uuidC, uuidD); var proximity_uuid = new Guid(uuidA, uuidB, uuidC, uuidD);
Debug.WriteLine($"UUID:{proximity_uuid}, major: {major}, minor: {minor}, power: {power}"); Debug.WriteLine($"UUID:{proximity_uuid}, major: {major}, minor: {minor}, power: {power}");
/* 周囲のuuidのDictionaryを作成 */ /* 周囲のuuidの hash set を作成 */
if (!Around_uuid.ContainsKey(proximity_uuid.ToString())) //新しいUUIDなら追加 if (!All_uuid.Contains(proximity_uuid.ToString())) //新しいUUIDなら追加
{ {
sw.Restart(); All_uuid.Add(proximity_uuid.ToString());
DateTime timeA = DateTime.Now;
Around_uuid.Add(proximity_uuid.ToString(),args.Timestamp);
Debug.WriteLine($" It's New UUID!"); Debug.WriteLine($" It's New UUID!");
this.listBox.Text = this.listBox.Text + proximity_uuid + "\r\n";
} }
else //既にあるUUIDなら時間更新
{
Around_uuid[proximity_uuid.ToString()] = args.Timestamp;
Debug.WriteLine($" Time Refreshed...");
}
/* 周囲にあるUUIDを表示 */
this.listBox.Text = this.listBox.Text + "---------------" + "\r\n";
foreach (var some in Around_uuid)
{
//Debug.WriteLine($"%%%{some.Value} UUID:{some.Key}");
this.listBox.Text = this.listBox.Text +" Arround uuid:"+ some.Value + ":::" + some.Key +"\r\n";
}//
}
/* Timestamp が古いものを削除 */
DateTime dt = DateTime.Now;
var removeTargetLst = Around_uuid.Where(kv => (dt - kv.Value) > TIMEOUT).ToList();
foreach (var item in removeTargetLst)
{
Around_uuid.Remove(item.Key); // ここで削除する。Keyを指定して削除出来る。
} }
/* 判定フェーズ↓ */ /* 判定フェーズ↓ */
RUcnt = 0; RUcnt = 0;
foreach (string N in RU_uuid) foreach (string N in RU_uuid) //RUリストが大きいときは逆の入れ子の方がいいかも
{ {
if (Around_uuid.ContainsKey(N)) if (All_uuid.Contains(N))
{ {
RUcnt++; RUcnt++;
continue; continue;
} }
} }
NUcnt = Around_uuid.Count - RUcnt; NUcnt = All_uuid.Count - RUcnt;
this.RUNUBox.Text = "RU:" + RUcnt + "人, NU" + NUcnt + "人" + "\r\n"; this.RUNUBox.Text = "RU:" + RUcnt + "人, NU:" + NUcnt + "人" + "\r\n";
if (RUcnt >= A_thresh && RUcnt / (RUcnt + NUcnt) >= B_thresh)//安全かどうかの判定 if (RUcnt >= A_thresh && 1.0 * RUcnt / (RUcnt + NUcnt) >= B_thresh)//安全かどうかの判定
{ {
this.textBox.Text = "You are ACCEPTED!!" + "\r\n"; this.textBox.Text = "You are ACCEPTED!!";
advWatcher.Stop();
Debug.WriteLine($"You are ACCEPTED!!"); Debug.WriteLine($"You are ACCEPTED!!");
} }
else
{
this.textBox.Text = "Input Password";
Debug.WriteLine($"Input Password");
}
/* 判定フェーズ↑ */ /* 判定フェーズ↑ */
mutex.ReleaseMutex(); mutex.ReleaseMutex();
} }
}); });
} }
} }
} }
#pragma checksum "..\..\MainWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "1FF327C0534BE6044F07318BB02F5A77C7094ADD" #pragma checksum "..\..\MainWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "9C402D27C8DAE609477A515BE5E3E11AED27EC8B"
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// <auto-generated> // <auto-generated>
// このコードはツールによって生成されました。 // このコードはツールによって生成されました。
......
#pragma checksum "..\..\MainWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "1FF327C0534BE6044F07318BB02F5A77C7094ADD" #pragma checksum "..\..\MainWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "9C402D27C8DAE609477A515BE5E3E11AED27EC8B"
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// <auto-generated> // <auto-generated>
// このコードはツールによって生成されました。 // このコードはツールによって生成されました。
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment