예제 #1
0
        public IActionResult Create([FromBody] SnapBoxItem item)
        {
            if (item == null)
            {
                return(BadRequest());
            }

            if (item.PowerLevel == null)
            {
                return(BadRequest("Powerlevel null"));
            }
            if (item.MailReceived && item.ReceiverEmail == null)
            {
                return(BadRequest("No ReceiverEmail specified"));
            }

            _context.SnapBoxItems.Add(item);
            _context.SaveChanges();

            if (item.MailReceived)
            {
                //MailSender ms = new MailSender(_context);
                //ms.SendSnapBoxMail(item);
            }

            return(CreatedAtRoute("GetSnapBox", new { id = item.Id }, item));
        }
예제 #2
0
        public void SetUp()
        {
            _stubSnapBoxItem1 = new SnapBoxItem()
            {
                Checksum      = "12345",
                MailReceived  = true,
                PowerLevel    = "9001",
                SnapBoxId     = "007",
                ReceiverEmail = "*****@*****.**"
            };
            _stubSnapBoxItem2 = new SnapBoxItem()
            {
                Checksum      = "54321",
                MailReceived  = false,
                PowerLevel    = "003",
                SnapBoxId     = "03",
                ReceiverEmail = "nobody"
            };

            _hub = Substitute.For <IHubContext <DevicesHub> >();

            _connection = new SqliteConnection("DataSource=:memory:");
            _connection.Open();

            _options = new DbContextOptionsBuilder <FwpsDbContext>()
                       .UseSqlite(_connection)
                       .EnableSensitiveDataLogging()
                       .Options;
        }
예제 #3
0
        /////////////////////////////////////////////////
        /// Message handler for Poomba. SnapBox only receives
        /// one kind of message. This message is then split
        /// using a RegEx, such that 2 values are extracted.
        /// These values will then decide if mail is received,
        /// and what the powerlevel of the battery is.
        /// These values are then posted to the WebApi
        /////////////////////////////////////////////////
        public void HandleMessage(string message, string topic = "")
        {
            Console.WriteLine("Snap Box message: {0}, Topic: {1}", message, topic);

            var data = Regex.Split(message, @"\D+");

            // Length might be 3 or 2, depending on the Regex implementation...

            for (int i = 0; i < data.Length; i++)
            {
                data[i] = data[i].Trim();
            }

            if (data.Length == 3 && string.IsNullOrEmpty(data[0]))
            {
                if (data[1] == "0" && data[2] == "0")
                {
                    return;                                   // False message from I2C stutter
                }
                var snapboxitem = new SnapBoxItem()
                {
                    MailReceived = data[1] == "1", PowerLevel = data[2], ReceiverEmail = "*****@*****.**"
                };

                var webapi = new WebApiConnector();

                webapi.PostItem("snapbox/", JsonConvert.SerializeObject(snapboxitem));
            }
            else if (data.Length == 2)
            {
                if (data[0] == "0" && data[1] == "0")
                {
                    return;                                   // False message from I2C stutter
                }
                var snapboxitem = new SnapBoxItem()
                {
                    MailReceived = data[0] == "1", PowerLevel = data[1], ReceiverEmail = "*****@*****.**"
                };

                var webapi = new WebApiConnector();

                webapi.PostItem("snapbox/", JsonConvert.SerializeObject(snapboxitem));
            }
            else
            {
                throw new ArgumentException(string.Format("SnapBox Received Wrong Input -- Data length: {0} -- Data: {1}", data.Length, data.ToString()));
            }
        }
예제 #4
0
        public void SetUp()
        {
            _snapBoxId     = "007";
            _powerLevel    = "9001";
            _mailReceived  = true;
            _receiverEmail = "BOB";
            _checkSum      = "902309jifje091nd2d@@]£";

            _uut = new SnapBoxItem()
            {
                Checksum      = _checkSum,
                MailReceived  = _mailReceived,
                PowerLevel    = _powerLevel,
                ReceiverEmail = _receiverEmail,
                SnapBoxId     = _snapBoxId,
            };
        }
예제 #5
0
        public void SendSnapBoxMail(SnapBoxItem item)
        {
            MailMessage mail = new MailMessage("*****@*****.**", item.ReceiverEmail);

            mail.Body    = string.Format("You have received mail\nAlso your current power level is: {0}", item.PowerLevel);
            mail.Subject = "New mail in SnapBox";

            MailItem mailCopy = new MailItem()
            {
                To      = item.ReceiverEmail,
                From    = "*****@*****.**",
                Body    = string.Format("You have received mail\nAlso your current power level is: {0}", item.PowerLevel),
                Subject = "New mail in SnapBox"
            };

            _context.MailItems.Add(mailCopy);
            _context.SaveChanges();

            //Send mail here
            SmtpClient client = new SmtpClient();

            client.UseDefaultCredentials = false;
            client.Port           = 587;
            client.EnableSsl      = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials    = new System.Net.NetworkCredential("*****@*****.**", "Kalle210");
            client.Host           = "smtp.gmail.com";

            try
            {
                client.Send(mail);
            }
            catch (Exception e)
            {
                mailCopy.Subject = "FAILED TO SEND MAIL";
                _context.SaveChanges();
                Console.WriteLine("Failure to send email -- ", e.Message);
            }
        }
예제 #6
0
 public void SendSnapBoxMail(SnapBoxItem item) => throw new SendSnapBoxMailExecutedException();