public DeviceModel SaveMessage(Mt70Message message) { using (var conn = new SqlConnection(_connectionString)) { conn.Open(); var result = conn.Query <DeviceModel>(@"Mt70AddMessage", 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()); } }
public Mt70Message GetMt70Message(string s) { var message = new Mt70Message(); //"$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("$MGV002")) { message.Imei = messageArr[1]; //message.DeviceName = messageArr[2]; message.PositioningStatus = messageArr[3]; // R или S string formatString = "dd''MM''yy''HH''mm''ss"; var sampleData = messageArr[4] + messageArr[5]; // 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[7].Replace('.', ','); // Latitude (degrees & minutes), format: DDMM.MMMM. var indicatorNorthSouth = messageArr[8]; var longitude = messageArr[9].Replace('.', ','); //Longitude (degrees & minutes), format: DDDMM.MMMMM. var indicatorEastWest = messageArr[10]; 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(',', '.'); message.Battery = Convert.ToInt32(messageArr[33]); message.EventCode = messageArr[34].Remove(messageArr[34].IndexOf(';')); } } return(message); }
private void SendWebClients(Mt70Message message) { var deviceOld = repository.GetDeviceByImei(message.Imei); var device = repository.SaveMessage(message); if (device != null) { if (message.EventCode == "Sos") { try { var messageSms = message.PositioningStatus == "R" ? HttpUtility.UrlEncode( String.Format( "Абонент {0} попал в экстренную ситуацию. Его актуальные координаты https://maps.google.com/maps?q={1}", device.Name, device.LastCoordinates), Encoding.UTF8) : HttpUtility.UrlEncode( String.Format( "Абонент {0} попал в экстренную ситуацию. Его последние координаты https://maps.google.com/maps?q={1}", device.Name, device.LastCoordinates), Encoding.UTF8); Addressee(device, messageSms); } catch (Exception e) { (new FileHelper()).SaveMessage("error.txt", e.ToString()); } } if (message.EventCode == "Timer") { try { if (message.PositioningStatus == "R" && !deviceOld.LastCoordinatesValid) { var messageSms = HttpUtility.UrlEncode( String.Format( "Абонент {0} попал в экстренную ситуацию. Его актуальные координаты https://maps.google.com/maps?q={1}", device.Name, 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()); } }