示例#1
0
 public DeviceModel SaveMessage(Gc3Message message)
 {
     using (var conn = new SqlConnection(_connectionString))
     {
         conn.Open();
         var result = conn.Query <DeviceModel>(@"Gc3AddMessage",
                                               new
         {
             Protocol = 1,
             message.Phone,
             message.Latitude,
             message.Longitude,
             message.DateAndTime,
             message.PositioningStatus,
             message.Speed,
             message.Battery,
             message.Type,
         }, commandType: CommandType.StoredProcedure).FirstOrDefault();
         return(result);
     }
 }
示例#2
0
        // Конструктор класса. Ему нужно передавать принятого клиента от TcpListener
        public Client(string aOut)
        {
            var s = aOut;

            Console.WriteLine(aOut);
            if (s.IndexOf('&') > -1)
            {
                var strArr = s.Split('&');
                if (strArr.Length > 0)
                {
                    var phoneStr = "";
                    if (strArr[0].IndexOf("phone=", StringComparison.Ordinal) > -1)
                    {
                        phoneStr = strArr[0].Remove(0, 6);
                    }
                    string phone = Regex.Replace(phoneStr, @"[^0-9]", string.Empty);

                    if (!String.IsNullOrEmpty(phone))
                    {
                        if (strArr[1].IndexOf("mes=", StringComparison.Ordinal) > -1)
                        {
                            (new FileHelper()).SaveMessage("output.txt", "mes==" + strArr[1]);

                            var device = (new Gc3Repository()).GetDeviceByPhone(phone);
                            if (device.Type == DeviceTypeEnum.Gc3)
                            {
                                var mes        = strArr[1].Remove(0, 4);
                                var gc3Message = new Gc3Message();
                                Console.WriteLine(mes);
                                gc3Message.Battery = 100;

                                var sendDb = false;

                                if (mes.IndexOf("SOS%20-%20Ende", StringComparison.Ordinal) > -1)
                                {
                                    //SOS Modus ist deaktiviert.

                                    //gc3Message.PositioningStatus = "A";
                                    //sendDb = true;
                                    //gc3Message.Type = 15;
                                    //Console.WriteLine("Position");
                                }
                                else if (mes.IndexOf("SOS%20Modus%20ist%20deaktiviert", StringComparison.Ordinal) >
                                         -1)
                                {
                                    //SOS Modus ist deaktiviert.

                                    //gc3Message.PositioningStatus = "A";
                                    //sendDb = true;
                                    //gc3Message.Type = 15;
                                    //Console.WriteLine("Position");
                                }
                                else if (mes.IndexOf("SOS", StringComparison.Ordinal) > -1)
                                {
                                    var date       = DateTime.UtcNow;
                                    var sosEnabled = ((date - device.SosCloseUpdateTime).Minutes > 2);

                                    if (mes.IndexOf("Letzte%20Position", StringComparison.Ordinal) > -1)
                                    {
                                        gc3Message.PositioningStatus = "V";
                                        sendDb = true;

                                        if (!device.SosActive && device.SosClose && sosEnabled)
                                        {
                                            var messageSms =
                                                HttpUtility.UrlEncode(
                                                    "На " + device.Name +
                                                    " сработал SOS. Последние координаты https://maps.google.com/maps?q=" +
                                                    device.LastCoordinates, Encoding.UTF8);
                                            Addressee(device, messageSms);
                                            gc3Message.Type = 1;
                                            (new Gc3Repository()).SosClose(device.Id, 0);
                                        }
                                        else
                                        {
                                            //if ((date - device.SosTime).Minutes > 15)
                                            //{
                                            //var message = "sosaus";
                                            //var smsSender = new SmsSenderRabbitMQ()
                                            //{
                                            //    Sender = ConfigHelper.GetConfigValueByKey("SmsMt90Sender"),
                                            //    Phone = phone, //device.Phone,
                                            //    Message = message,
                                            //    CrateDate = DateTime.UtcNow
                                            //};
                                            //(new Gc3Repository()).SosClose(device.Id, 1);
                                            //var json = new JavaScriptSerializer().Serialize(smsSender);
                                            //var serviceBus = new ServiceBusHelper();
                                            //serviceBus.SendMessages("SmsSender", json);
                                            //}

                                            gc3Message.Type = 2;
                                        }
                                    }
                                    else if (mes.IndexOf("Aktualisierte%20Position", StringComparison.Ordinal) >
                                             -1)
                                    {
                                        gc3Message.Type = !device.SosActive && sosEnabled ? 1 : 2;

                                        var messageSms =
                                            HttpUtility.UrlEncode(
                                                String.Format(
                                                    "Абонент {0} попал в экстренную ситуацию. Его актуальные координаты  https://maps.google.com/maps?q={1}",
                                                    device.Name, device.LastCoordinates), Encoding.UTF8);
                                        Addressee(device, messageSms);

                                        gc3Message.PositioningStatus = "A";
                                        //var message = "sosaus";
                                        //var smsSender = new SmsSenderRabbitMQ()
                                        //{
                                        //    Sender = ConfigHelper.GetConfigValueByKey("SmsMt90Sender"),
                                        //    Phone = phone, //device.Phone,
                                        //    Message = message,
                                        //    CrateDate = DateTime.UtcNow
                                        //};
                                        //(new Gc3Repository()).SosClose(device.Id, 1);
                                        //var json = new JavaScriptSerializer().Serialize(smsSender);
                                        //var serviceBus = new ServiceBusHelper();
                                        //serviceBus.SendMessages("SmsSender", json);
                                        sendDb = true;
                                    }
                                }
                                else if (mes.IndexOf("Akkukapazitaet", StringComparison.Ordinal) > -1)
                                {
                                    gc3Message.PositioningStatus = "A";
                                    sendDb          = false;
                                    gc3Message.Type = 15;
                                    Console.WriteLine("Position");
                                }

                                else if (mes.IndexOf("%20ist%0A", StringComparison.Ordinal) > -1)
                                {
                                    gc3Message.PositioningStatus = "A";
                                    sendDb          = true;
                                    gc3Message.Type = 15;

                                    var messageSms =
                                        HttpUtility.UrlEncode(
                                            "Актуальные координаты " + device.Name +
                                            " https://maps.google.com/maps?q=" + device.LastCoordinates,
                                            Encoding.UTF8);
                                    Addressee(device, messageSms);

                                    Console.WriteLine("Position");
                                }
                                else if (
                                    mes.IndexOf("Kein%20GPS-Signal%20verfuegbar", StringComparison.Ordinal) >
                                    -1)
                                {
                                    var message   = "position";
                                    var smsSender = new SmsSenderModel()
                                    {
                                        Sender    = ConfigHelper.GetConfigValueByKey("SmsGc3Sender"),
                                        Phone     = phone, //device.Phone,
                                        Message   = message,
                                        CrateDate = DateTime.UtcNow
                                    };
                                    var json       = new JavaScriptSerializer().Serialize(smsSender);
                                    var serviceBus = new ServiceBusHelper();
                                    serviceBus.SendMessages("SmsSender", json);
                                }

                                var strDecode = HttpUtility.UrlDecode(mes);

                                (new FileHelper()).SaveMessage("output.txt", strDecode);

                                if (sendDb)
                                {
                                    #region  обработка сообщения

                                    var coordinatesIndexStart  = mes.IndexOf("geocare%40", StringComparison.Ordinal);
                                    var coordinatesTmp         = mes.Remove(0, coordinatesIndexStart + 10);
                                    var coordinatesIndexFinish = coordinatesTmp.IndexOf("%26z%3D",
                                                                                        System.StringComparison.Ordinal);
                                    var coordinates =
                                        HttpUtility.UrlDecode(coordinatesTmp.Remove(coordinatesIndexFinish));

                                    gc3Message.Phone       = phone;
                                    gc3Message.Latitude    = coordinates.Split(',')[0];
                                    gc3Message.Longitude   = coordinates.Split(',')[1];
                                    gc3Message.DateAndTime = DateTime.Now;
                                    gc3Message.Speed       = "0";


                                    (new Gc3Repository()).SaveMessage(gc3Message);


                                    //Console.WriteLine(coordinates);
                                    Console.WriteLine(strDecode);

                                    #endregion

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

                                    try
                                    {
                                        using (var hubConnection = new HubConnection("http://crm.hranitel24.ru:180")
                                               )
                                        {
                                            var hubProxy = hubConnection.CreateHubProxy("deviceHub");
                                            hubConnection.Start().Wait();
                                            hubProxy.Invoke("UpdateDevice",
                                                            (new JavaScriptSerializer()).Serialize(devicea));
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        (new FileHelper()).SaveMessage("error.txt", "crm==" + e.ToString());
                                    }
                                }
                            }
                        }
                    }
                    //&id=21470454&to=79023500131&time=1427289490&sent=1427289490&smsc=
                }
            }
        }