NotificationController.cs 7.62 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Newtonsoft.Json.Linq;
using ControlPanel.Models;
using System.Runtime.Serialization;
using ControlPanel.LogicLayers;
using System.IO;
using System.Web;
using System.Web.Mvc;



namespace ControlPanel.Controllers
{

  
    public class NotificationController : Controller
    {
       
      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
  
        public JObject GetNotifications(int page, int start, int limit)
        {

            List<Notification> lstNotification = LogicNotification.instance.GetLog( page,  start,  limit);

           
            
            JObject result = JObject.FromObject(new
            {
                notifications = lstNotification,
                success = true,
                total = LogicNotification.instance.Count()
            });
            return result;
        }


        /// <summary>
        /// Изменение  таблицы, без изменения на сервере оповещения
        /// </summary>
        /// <param name="number"></param>
        /// <param name="f"></param>
        /// <returns></returns>
        [HttpPost]
        public JObject UpdateSzs(int number, bool f)
        {
            bool changeservernotification = true;  
                try
                {
                Notification obj = LogicNotification.instance.GetNotificationByNumber(number);

                obj.notification = f;
               
                }
                catch (Exception)
                {
                    changeservernotification = false;
                }

            JObject result = JObject.FromObject(new
            {
                data = changeservernotification
            });
            return result;
        }

    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <param name="notification"></param>
        /// <returns></returns>
        [HttpPost]
        public JObject UpdateNotification(int number, bool fszs,bool fphone, bool fopendoor)
        {
            string changeservernotification = "Изменения приняты!";  // изменили
      
                Notification obj = LogicNotification.instance.GetNotificationByNumber(number);



                obj.notification = fszs;
                obj.notificationphone = fphone;
                obj.opendoor = fopendoor;


                    try
                    {
                      

                        Unchek(number, fszs);
                        LogicNotification.instance.Update(obj);

                    }
                    catch (Exception)
                    {
                        changeservernotification = "Изменения для СЗС не приняты!";
                    }



            

                    ///////////////////Сервер оповещение ////////////////////

                    int ff = 0;
                    if (fphone)
                        ff = 1;

                    var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://46.226.227.171/api/alert_handler.php?post=" + number + "&" + "on=" + ff);
                    httpWebRequest.ContentType = "text/json";
                    httpWebRequest.Method = "POST";

                    try
                    {
                        var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());

                        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                        {
                            var res = streamReader.ReadToEnd();

                            string str = res.ToString();

                            char p = str[0];
                            if (p == '0' || p == '1')
                            {
                                  LogicNotification.instance.Update(obj);





                            }
                            else
                            {
                                changeservernotification = "Сервер оповещений изменений не принял!";
                            }

                        }


                    



                    }
                    catch (Exception)
                    {
                        changeservernotification = "Ошибка!";
                    }


             



            JObject result = JObject.FromObject(new
            {
                data=changeservernotification
            });

            return result;
        }

        public void Unchek(int id_agk, bool flag)
        {

            List<Route> lstRoute = LogicRouts.instance.GetAll();

            Agk agk = LogicAgk.instance.GetByNumber(id_agk);

            foreach (Route route in lstRoute)
            {



                if (route.RefRouteAgk.id == agk.id)
                {
                    Route newRoute = route;
                    newRoute.enabled = flag;
                    LogicRouts.instance.UpdateEnabled(newRoute);
                }

            }



        }



        [HttpPost]
        public JObject Varification()
        {


            var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://46.226.227.171/api/json_info.php?on");
            httpWebRequest.ContentType = "text/json";
            httpWebRequest.Method = "POST";

            try
            {
                var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var res = streamReader.ReadToEnd();

                    string str = res.ToString();

      

                    dynamic obj = JObject.Parse(res);

                    try
                    {

                        var lst = obj["notifications"];

                        foreach (var p in lst)
                        {
                            try
                            {
                                dynamic json = JValue.Parse(p.ToString());

                                int number = json.number;
                                bool notificationphone = json.notification;

                                Notification notification = LogicNotification.instance.GetNotificationByNumber(number);
                                notification.notificationphone = notificationphone;
                                LogicNotification.instance.Update(notification);
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                    catch (Exception)
                    {

                    }
                }

            }
            catch (Exception)
            {
             
            }













            JObject result = JObject.FromObject(new
            {
                data = ""
            });
            return result;
        }







        //

 /*       var request = (HttpWebRequest) HttpWebRequest.Create("http://url");
request.Method = "POST";
const string command = @"Текст POST запроса";
byte[] bytes = Encoding.ASCII.GetBytes(command);
request.ContentLength = bytes.Length;
using (var stream = request.GetRequestStream())
{
    stream.Write(bytes, 0, bytes.Length);
}
using(var stream = new StreamReader(request.GetResponse().GetResponseStream()))
{
    Console.WriteLine(stream.ReadToEnd());
}
        //
        */

    }
}