private async Task ReadAsync() { var mfrc = new Mfrc522(); await mfrc.InitIOAsync(); await ResetAsync(); while (true) { try { if (mfrc.IsTagPresent()) { Uid uid = mfrc.ReadUid(); if (uid.IsValid) { await CheckingAsync(uid); DataBadge badge = new DataBadge { Orario = DateTime.Now, Id = uid.FullUid, Posizione = "Villafranca" }; _pinBlueLed.SetDriveMode(GpioPinDriveMode.Output); _pinBlueLed.Write(GpioPinValue.High); var messageString = JsonConvert.SerializeObject(badge); var message = new Microsoft.Azure.Devices.Client.Message(Encoding.ASCII.GetBytes(messageString)); message.Properties.Add("DeviceId", DeviceId); await _client.SendEventAsync(message); } else { await ErrorReadAsync(); } mfrc.HaltTag(); } } catch (Exception ex) { await _client.CloseAsync(); var e = ex; } } }
/// <summary> /// Ascolta i messaggi dei dispositivi /// </summary> /// <param name="partitionId"></param> /// <returns></returns> /// <remarks> /// Ogni volta che arriva un payload di dati: /// 1) deserializzo l'oggetto /// 2) invoco un metodo registrato sul device (un particolare device Id) /// 3) invio il messaggio a un device (un particolare device Id). Il messaggio inviato ha richiesto esplicitamente la risposta dal device tramite ACK. /// </remarks> /// public async Task RicezioneMessaggiDaDispositivi(string partitionId) { var eventHubReceiver = HubClient.GetDefaultConsumerGroup().CreateReceiver(partitionId, DateTime.UtcNow); while (true) { EventData eventData = await eventHubReceiver.ReceiveAsync(); if (eventData == null) { continue; } string data = Encoding.UTF8.GetString(eventData.GetBytes()); DataBadge nuovoDatoRicevuto = JsonConvert.DeserializeObject <DataBadge>(data); try { bool thereisperson = context.Badges .Any(x => x.Array == nuovoDatoRicevuto.Id); var property = eventData.Properties.FirstOrDefault(x => x.Key == "DeviceId"); if (!thereisperson) { Console.WriteLine("Errore non esiste la persona"); string messageToSend = JsonConvert.SerializeObject(null); await _serverSender.InviaAsync(property.Value.ToString(), messageToSend); } else { Console.WriteLine("La persona è presente"); PopulateBadge currentBadge = await context.Badges .Where(x => x.Array == nuovoDatoRicevuto.Id) .FirstOrDefaultAsync(); //TODO associare una persona a un badge a una macchina. Machine machine = await context.Machines.FirstOrDefaultAsync(); Swipe swipe1 = new Swipe() { Badge = currentBadge, Machine = machine, NomeBadge = currentBadge.NomeBadge, MachineName = machine.Name, Orario = nuovoDatoRicevuto.Orario, PosPersona = nuovoDatoRicevuto.Posizione }; context.Swipe.Add(swipe1); await context.SaveChangesAsync(); Console.WriteLine($"Dato scodato dal server: {nuovoDatoRicevuto}"); // Faccio scattare il metodo registrato dal device Person currentPerson = await context.People.FindAsync(currentBadge.IdPerson); currentPerson.Badge = null; string messageToSend = JsonConvert.SerializeObject(currentPerson); await _serverSender.InviaAsync(property.Value.ToString(), messageToSend); } } catch (Exception ex) { //TODO: Log Console.WriteLine(ex.Message); } } }