コード例 #1
0
        public async Task <IActionResult> Create(PeopleViewModel model)
        {
            if (ModelState.IsValid)
            {
                var newperson = Mapper.Map <Person>(model);
                _context.Add(newperson);
                await _context.SaveChangesAsync();

                byte[] result = null;
                using (var memoryStream = new System.IO.MemoryStream())

                {
                    await model.AvatarImage.CopyToAsync(memoryStream);

                    result = memoryStream.ToArray();
                }

                newperson.Uri = await _uploadBlobService.UploadFile(result, $"{newperson.IdPerson}/{model.AvatarImage.FileName}");

                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> PutBadge(long id, Badge badge)
        {
            if (id != badge.Id)
            {
                return(BadRequest());
            }

            _context.Entry(badge).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BadgeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #3
0
        public async Task <IActionResult> Create(MachinesViewModel model)
        {
            if (ModelState.IsValid)
            {
                var newmachine = Mapper.Map <Machine>(model);
                _context.Add(newmachine);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(model));
        }
コード例 #4
0
        public async Task <IActionResult> Create(int idperson, BadgesViewModel model)
        {
            if (ModelState.IsValid)
            {
                var newswipes = Mapper.Map <PopulateBadge>(model);
                _context.Add(newswipes);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", new { peopleid = idperson }));
            }
            return(View(model));
        }
コード例 #5
0
        public async Task <IActionResult> Create(int idperson, SwipesViewModel model)
        {
            if (ModelState.IsValid)
            {
                var newswipes    = Mapper.Map <Swipe>(model);
                var badgeCurrent = await _context.FindAsync <PopulateBadge>(newswipes.NomeBadge);

                var machineCurrent = await _context.FindAsync <Machine>(newswipes.MachineName);

                newswipes.Badge   = badgeCurrent;
                newswipes.Machine = machineCurrent;
                _context.Add(newswipes);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", new { peopleid = idperson }));
            }

            return(View(model));
        }
コード例 #6
0
        /// <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);
                }
            }
        }