示例#1
0
        public Mt100Message GetMt100Message(string s)
        {
            var message = new Mt100Message();

            //"$MGV002,860719021429240,,R,240415,001631,V,5241.51296,N,02522.36540,E,00,00,00,0.97,0.069,,157.6,,257,02,007D,1CFD,21,,,,,,,,,00,100,Sos;!"
            if (!String.IsNullOrEmpty(s) && s.IndexOf(',') > -1)
            {
                var messageArr = s.Split(',');
                if (messageArr.Length > 3 && messageArr[0].StartsWith("STX"))
                {
                    if (messageArr[2].StartsWith("$GPRMC"))
                    {
                        message.Imei = messageArr[17].Remove(0, (messageArr[17].IndexOf(':') + 1));
                        //message.DeviceName = messageArr[2];
                        message.PositioningStatus = messageArr[15]; // F или L
                        string formatString = "dd''MM''yy''HH''mm''ss";
                        var    sampleData   = messageArr[11] + messageArr[3].Remove(messageArr[3].IndexOf('.'));
                        //  System date, format: DDMMYY.  System time, format: HHMMSS.
                        message.DateAndTime = DateTime.ParseExact(sampleData, formatString, null);
                        //message.GPSFix = messageArr[6]; //‘A’ means GPS fix successfully, ‘V’ means GPS can not fix.
                        var latitude = messageArr[5].Replace('.', ',');
                        // Latitude (degrees & minutes), format: DDMM.MMMM.
                        var indicatorNorthSouth = messageArr[6];
                        var longitude           = messageArr[7].Replace('.', ',');
                        //Longitude (degrees & minutes), format: DDDMM.MMMMM.
                        var indicatorEastWest = messageArr[8];
                        message.Latitude =
                            ((indicatorNorthSouth == "S" ? -1 : 1) *
                             Math.Round(Convert.ToDecimal(latitude.Remove(2), new CultureInfo("ru-RU")) +
                                        (Convert.ToDecimal(latitude.Remove(0, 2), new CultureInfo("ru-RU")) / 60), 6))
                            .ToString().Replace(',', '.');
                        message.Longitude =
                            ((indicatorEastWest == "W" ? -1 : 1) *
                             Math.Round(Convert.ToDecimal(longitude.Remove(3), new CultureInfo("ru-RU")) +
                                        (Convert.ToDecimal(longitude.Remove(0, 3), new CultureInfo("ru-RU")) / 60), 6))
                            .ToString().Replace(',', '.');

                        var batteryTmp = messageArr[20].Remove(0, messageArr[20].IndexOf('=') + 1);

                        message.Battery   = Convert.ToInt32(batteryTmp.Remove(batteryTmp.IndexOf('%')));
                        message.EventCode = messageArr[16];
                        Console.WriteLine(message.EventCode);
                    }
                }
            }
            return(message);
        }
示例#2
0
 public DeviceModel SaveMessage(Mt100Message message)
 {
     using (var conn = new SqlConnection(_connectionString))
     {
         conn.Open();
         var result = conn.Query <DeviceModel>(@"Mt100AddMessage",
                                               new
         {
             Protocol = 2,
             message.Imei,
             message.EventCode,
             message.Latitude,
             message.Longitude,
             message.DateAndTime,
             message.PositioningStatus,
             message.Ip,
             message.Battery,
         }, commandType: CommandType.StoredProcedure).FirstOrDefault();
         return(result ?? new DeviceModel());
     }
 }
示例#3
0
        private void SendWebClients(Mt100Message message)
        {
            var device = repository.SaveMessage(message);

            if (device != null)
            {
                if (message.EventCode == "Help")
                {
                    try
                    {
                        var messageSms = HttpUtility.UrlEncode("На " + device.Name + " сработал SOS. https://maps.google.com/maps?q=" + device.LastCoordinates, Encoding.UTF8);
                        Addressee(device, messageSms);
                    }
                    catch (Exception e)
                    {
                        (new FileHelper()).SaveMessage("error.txt", e.ToString());
                    }
                }

                if (message.EventCode == "Geo out")
                {
                    try
                    {
                        var messageSms = HttpUtility.UrlEncode(device.Name + " покинуло гео-зону. https://maps.google.com/maps?q=" + device.LastCoordinates, Encoding.UTF8);
                        Addressee(device, messageSms);
                    }
                    catch (Exception e)
                    {
                        (new FileHelper()).SaveMessage("error.txt", e.ToString());
                    }
                }

                if (message.EventCode == "VIB")
                {
                    try
                    {
                        var messageSms = HttpUtility.UrlEncode("На " + device.Name + " зафиксировано падение. https://maps.google.com/maps?q=" + device.LastCoordinates, Encoding.UTF8);
                        Addressee(device, messageSms);
                    }
                    catch (Exception e)
                    {
                        (new FileHelper()).SaveMessage("error.txt", e.ToString());
                    }
                }
                if (message.EventCode == "LowBattery")
                {
                    try
                    {
                        var messageSms = HttpUtility.UrlEncode(device.Name + " села батарейка. https://maps.google.com/maps?q=" + device.LastCoordinates, Encoding.UTF8);
                        var serviceBus = new ServiceBusHelper();
                        if (!String.IsNullOrEmpty(device.Addressee1) && device.Addressee1LowBattery)
                        {
                            var smsSender = new SmsSenderModel()
                            {
                                Sender    = ConfigurationManager.AppSettings["SmsSender"].ToString(),
                                Phone     = device.Addressee1,
                                Message   = messageSms,
                                CrateDate = DateTime.UtcNow
                            };

                            var json = new JavaScriptSerializer().Serialize(smsSender);
                            serviceBus.SendMessages("SmsSender", json);
                        }
                        if (!String.IsNullOrEmpty(device.Addressee2) && device.Addressee2LowBattery)
                        {
                            var smsSender = new SmsSenderModel()
                            {
                                Sender    = ConfigurationManager.AppSettings["SmsSender"].ToString(),
                                Phone     = device.Addressee2,
                                Message   = messageSms,
                                CrateDate = DateTime.UtcNow
                            };

                            var json = new JavaScriptSerializer().Serialize(smsSender);
                            serviceBus.SendMessages("SmsSender", json);
                        }
                        if (!String.IsNullOrEmpty(device.Addressee3) && device.Addressee3LowBattery)
                        {
                            var smsSender = new SmsSenderModel()
                            {
                                Sender    = ConfigurationManager.AppSettings["SmsSender"].ToString(),
                                Phone     = device.Addressee3,
                                Message   = messageSms,
                                CrateDate = DateTime.UtcNow
                            };

                            var json = new JavaScriptSerializer().Serialize(smsSender);
                            serviceBus.SendMessages("SmsSender", json);
                        }
                        if (!String.IsNullOrEmpty(device.Addressee4) && device.Addressee4LowBattery)
                        {
                            var smsSender = new SmsSenderModel()
                            {
                                Sender    = ConfigurationManager.AppSettings["SmsSender"].ToString(),
                                Phone     = device.Addressee4,
                                Message   = messageSms,
                                CrateDate = DateTime.UtcNow
                            };

                            var json = new JavaScriptSerializer().Serialize(smsSender);
                            serviceBus.SendMessages("SmsSender", json);
                        }
                    }
                    catch (Exception e)
                    {
                        (new FileHelper()).SaveMessage("error.txt", e.ToString());
                    }
                }
            }

            try
            {
                using (var hubConnection = new HubConnection("http://cb.hranitel24.ru:80"))
                {
                    var hubProxy = hubConnection.CreateHubProxy("deviceHub");
                    hubConnection.Start().Wait();
                    hubProxy.Invoke("UpdateDevice", (new JavaScriptSerializer()).Serialize(device));
                }
            }
            catch (Exception e)
            {
                (new FileHelper()).SaveMessage("error.txt", e.ToString());
            }

            try
            {
                using (var hubConnection = new HubConnection("http://crm.hranitel24.ru:80"))
                {
                    var hubProxy = hubConnection.CreateHubProxy("deviceHub");
                    hubConnection.Start().Wait();
                    hubProxy.Invoke("UpdateDevice", (new JavaScriptSerializer()).Serialize(device));
                }
            }
            catch (Exception e)
            {
                (new FileHelper()).SaveMessage("error.txt", e.ToString());
            }
        }