public IHttpActionResult PutMobileDevice(int id, MobileDevice mobileDevice)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != mobileDevice.MobileDeviceId)
            {
                return BadRequest();
            }

            db.Entry(mobileDevice).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MobileDeviceExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        public IHttpActionResult PostMobileDevice(MobileDevice mobileDevice)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.MobileDevices.Add(mobileDevice);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = mobileDevice.MobileDeviceId }, mobileDevice);
        }
Пример #3
0
        // Send email notification.
        public static void SendEmail(int numberOfDevices, MobileDevice mobileDevice, string type)
        {
            logger.Debug("SendMail(LicensingContext, MobileDevice, string) entered ...");

            try
            {
                // Construct the Subject line
                string subject = CreateSubject(mobileDevice, numberOfDevices);

                logger.DebugFormat("Subject : {0}", subject);

                // Construct the email Body
                string body = CreateBody(mobileDevice, type, numberOfDevices);

                // Send email.
                using (MailMessage message = new MailMessage(Instance.Smtp.From, Instance.Smtp.To))
                {
                    message.Subject = subject;
                    message.IsBodyHtml = true;
                    message.Body = body;

                    using (SmtpClient client = new SmtpClient(Instance.Smtp.Server, Instance.Smtp.Port))
                    {
                        // If not using the standard SMTP port then set Network Credentials
                        if (client.Port != 25)
                        {
                            client.UseDefaultCredentials = false;
                            client.Credentials = new NetworkCredential(Instance.Credentials.Username, Instance.Credentials.Password);
                        }

            #if ENABLE_EMAIL
                        client.Send(message);
            #endif
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Failed to send email", ex);
            }
            finally
            {
                logger.Debug("SendMail(LicensingContext, MobileDevice, string) exited");
            }
        }
Пример #4
0
        private static string CreateBody(MobileDevice device, string type, int numberOfDevices)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendFormat("<b>Device {0}</b><br>", type);
            builder.AppendFormat("<b>Company: {0}</b><br>", device.Company.CompanyName);
            builder.Append("<br>");
            builder.AppendFormat("Date requested: {0} {1}<br>", DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString());
            builder.AppendFormat("Colossus reg. no. {0}<br>", device.Company.ColossusRegNo);
            builder.AppendFormat("Device serial no: {0}<br>", device.SerialNo);
            builder.Append("<br>");
            builder.AppendFormat("Device count: {0}<br>", numberOfDevices);
            builder.AppendFormat("License count: {0}<br>", device.Company.ColossusMobileLicences);

            return builder.ToString();
        }
Пример #5
0
        private static string CreateSubject(MobileDevice device, int numberOfDevices)
        {
            if (numberOfDevices > device.Company.ColossusMobileLicences)
            {
                return "Colossus mobile licensing exceeded";
            }

            return "Colossus mobile licensing";
        }
        public IHttpActionResult PostLicense([FromBody] LicenseRequest request)
        {
            logger.Debug("PostLicense(LicenseRequest) entered ...");

            try
            {
                // Validate Company PIN format, if invalid return an error.
                if (request.CompanyPIN.Length != 8)
                {
                    logger.InfoFormat("PIN is incorrect length : {0}, should be 8 characters", request.CompanyPIN.Length);

                    return Ok(new LicenseResponse()
                        {
                            Error = "PIN must be 8 digits",
                            Result = (int)LicenceResultCode.PinIncorrectLength
                        });
                }

                // Attempt to find Company from the PIN.
                Company company = db.Companies.FirstOrDefault(c => c.Pin == request.CompanyPIN);

                // If not found return an error.
                if (company == null)
                {
                    logger.InfoFormat("Failed to find Company with PIN : {0}", request.CompanyPIN);

                    return Ok(new LicenseResponse()
                    {
                        Error = "PIN is invalid",
                        Result = (int)LicenceResultCode.PinNotFound
                    });
                }

                // Check if already listed as a device.
                MobileDevice device = db.MobileDevices.FirstOrDefault(d => d.SerialNo == request.SerialNo);

                // If the device exists return error showing already registered
                if (device != null)
                {
                    logger.InfoFormat("Device with Serial # : {0} already registered", request.SerialNo);

                    return Ok(new LicenseResponse()
                    {
                        Error = "Device already registered",
                        Result = (int)LicenceResultCode.DeviceAlreadyRegistered
                    });
                }

                // Create new device.
                device = new MobileDevice()
                {
                    Company = company,
                    SerialNo = request.SerialNo,
                    Created = DateTime.Now,
                    CompanyId = company.CompanyId,
                };

                // Add to the collection of Mobile Devices ...
                device = db.MobileDevices.Add(device);
                db.SaveChanges();

                logger.DebugFormat("New device with Id : {0} created", device.MobileDeviceId);

                // Send email ...
                int numberOfDevices = db.MobileDevices.Count(d => d.CompanyId == device.MobileDeviceId);
                MailUtilities.SendEmail(numberOfDevices, device, "licensed");

                // Return configuration.
                return Ok(new LicenseResponse()
                {
                    Result = (int)LicenceResultCode.Success,
                    DeviceNo = device.MobileDeviceId,
                    Url = device.Company.ColossusMobileUrl,
                    ConsignorName = device.Company.ConsignorName,
                    ConsignorAdd1 = device.Company.ConsignorAdd1,
                    ConsignorAdd2 = device.Company.ConsignorAdd2,
                    ConsignorAdd3 = device.Company.ConsignorAdd3,
                    Error = string.Empty
                });
            }
            catch (Exception ex)
            {
                logger.Error("Error licensing device", ex);

                return Ok(new LicenseResponse()
                    {
                        Result = (int)LicenceResultCode.Unknown,
                        Error = "Unknown Error",
                        Url = string.Empty
                    });
            }
            finally
            {
                logger.Debug("PostLicense(LicenseRequest) exited");
            }
        }