private async void ReadingData(CancellationToken token) { while (_device != null && !token.IsCancellationRequested) { try { string workingString = await _device.ReadLineAsync(token); //System.Diagnostics.Debug.WriteLine(workingString); BeaconInfoData info = BeaconInfoData.FromString(workingString); //System.Diagnostics.Debug.WriteLine(info); DataReceived?.Invoke(this, new BeaconInfoEventArgs(info, workingString)); } catch (TaskCanceledException) { _device.Reset(); } } _device.Reset(); }
public static BeaconInfoData FromString(string str) { //Console.WriteLine("parseString(" + str + ")"); string[] subStrings = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (subStrings.Length < MIN_COUNT) { System.Diagnostics.Debug.WriteLine(" parseString() error:'" + str + "'"); return(null); } //2 06 9027E45EA88D 12E69160 -079 WiFi Alexander BeaconInfoData info = new BeaconInfoData(); try { info.wifiChan = Convert.ToByte(subStrings[1], 10); info.mac = subStrings[0]; info.diff = Convert.ToDouble(subStrings[2]); info.level = Convert.ToDouble(subStrings[3]); info.ssid = subStrings[4]; for (int i = 5; i < subStrings.Length; i++) { info.ssid += " "; info.ssid += subStrings[i]; } } catch (FormatException e) { System.Diagnostics.Debug.WriteLine("Parse Error: " + e.StackTrace); return(null); } //info.print(); return(info); }
public bool HandleInfo(BeaconInfoData data) { //data.print(); if (data.ssid == ssid && data.mac == mac) { levels.AddLast(data.level); diffs.AddLast(data.diff); while (levels.Count > avgCount) { levels.RemoveFirst(); } while (diffs.Count > avgCount) { diffs.RemoveFirst(); } needRecalc = true; print(); } else { //Console.WriteLine("LevelCalculator.HandleInfo() skip ssid " + data.ssid); } return(needRecalc); }
protected override void OnResume() { base.OnResume(); usbHandler = new Handler( (message) => { //switch ((USBDeviceStatus)message.What) //{ // case USBDeviceStatus.UsbReading: // break; // case USBDeviceStatus.DeviceConnectionClosed: // break; //} if (message.What == 111) { string raw = message.Data.GetString("raw"); _usb_DataReceived(this, new BeaconInfoEventArgs(BeaconInfoData.FromString(raw), raw)); } }); _usb = new USBCommunicator(this, usbHandler); _usb.DataReceived += _usb_DataReceived; _usb.Connect(); }
public void Run() { if (!_scanMode) { System.Diagnostics.Debug.WriteLine($"Setting channel to {_channel + 1}"); Write(GetChannelByte(0)); Write(GetChannelByte((byte)(_channel + 1))); } byte[] buffer = new byte[100]; while (!_token.IsCancellationRequested) { //Changing channel if (_scanMode && (!_sw.IsRunning || _sw.ElapsedMilliseconds > 2500)) { System.Diagnostics.Debug.WriteLine($"Setting channel to {_channel + 1}"); Write(GetChannelByte(0)); Write(GetChannelByte((byte)(_channel + 1))); _channel = ++_channel % 14; _sw.Restart(); } //Reading data int count = _connection.BulkTransfer(_readEndpoint, buffer, buffer.Length, 1000); if (count > 0 && !_token.IsCancellationRequested) { dataString += Encoding.ASCII.GetString(buffer, 0, count); int termPos1, termPos2; do { //Check if string contains the terminator termPos1 = dataString.IndexOf((char)_terminator1); termPos2 = dataString.IndexOf((char)_terminator2); //Console.WriteLine("RAW: '" + tString + "'" + // " termPoses=" + termPos1 + " " + termPos2 + // " len " + tString.Length ); if (termPos2 > -1 && termPos2 > -1) { string workingString = dataString.Substring(0, termPos2); dataString = dataString.Substring(termPos2 + 1); //Console.WriteLine("NEXT: '" + tString + "'" + " len " + tString.Length); workingString = workingString.Trim(trimChars); //Console.WriteLine("RAW: '" + workingString + "'"); //System.Diagnostics.Debug.WriteLine(workingString); try { BeaconInfoData info = BeaconInfoData.FromString(workingString); System.Diagnostics.Debug.WriteLine(info); var msg = _handler.ObtainMessage(111); msg.Data.PutString("raw", workingString); _handler.SendMessage(msg); if (!_scanMode && info.wifiChan != _channel) { System.Diagnostics.Debug.WriteLine($"Setting channel to {_channel}"); Write(GetChannelByte(0)); Write(GetChannelByte((byte)_channel)); } } catch { } } } while (termPos1 > -1 && termPos2 > -1); } } }
public BeaconInfoEventArgs(BeaconInfoData info, string raw) { Info = info; Raw = raw; }