public WebNfcListener(string ident) { if (!ident.Contains("@")) { throw new ArgumentException("Invalid ident - missing @"); } var parts = ident.Split('@'); if (parts.Length != 2) { throw new ArgumentException("Invalid ident - invalid length"); } _endpoint = parts[0]; _address = parts[1]; _ioHandler = IO.Socket(_address); _ioHandler.On(Socket.EVENT_CONNECT, (obj) => { OnConnected?.Invoke(); }); _ioHandler.On("scanner.registered", (obj) => { var scanner = NfcScanner.FromJson(obj as string); OnRegistered?.Invoke(scanner); }); _ioHandler.On("scanner.scanned", (obj) => { var scanner = NfcScanner.FromJson(obj as string); }); }
public WebNfcListener(string ident) { if (!ident.Contains("@")) { throw new ArgumentException("Invalid ident - missing @"); } var parts = ident.Split('@'); if (parts.Length != 2) { throw new ArgumentException("Invalid ident - invalid length"); } _endpoint = parts[0]; _address = parts[1]; _ioHandler = IO.Socket($"http://{_address}", new IO.Options { AutoConnect = false, Reconnection = true }); _ioHandler.On(Socket.EVENT_CONNECT, (obj) => { OnConnected?.Invoke(); }); _ioHandler.On("scanner.registered", (obj) => { var scanner = NfcScanner.FromObject(obj); if (!Scanners.Exists(x => x.DeviceId == scanner.DeviceId)) { Scanners.Add(scanner); OnScannerRegistered?.Invoke(scanner); } }); _ioHandler.On("scanner.disconnected", (obj) => { dynamic o = obj; string deviceId = o.device_id; var scanner = Scanners.FirstOrDefault(x => x.DeviceId == deviceId); if (scanner != null) { Scanners.Remove(scanner); } OnScannerDisconnected?.Invoke(scanner); }); _ioHandler.On("scanner.scanned", (obj) => { dynamic o = obj; string deviceId = o.device_id; var scanner = Scanners.FirstOrDefault(x => x.DeviceId == deviceId); var s = new NfcScan { Scanner = scanner, CardId = ((dynamic)obj).card_id }; OnScanned?.Invoke(s); }); }