예제 #1
0
        private void httpServer_OnPOSTRequest(HttpProcessor p, StreamReader inputData)
        {
            switch (p.http_url)
            {
                case "/settings":
                    {
                        string data = inputData.ReadToEnd();
                        var dataList = GetDataList(data);
                        var command = (from item in dataList where item.Key == "command" select item).SingleOrDefault().Value;

                        p.writeSuccess();
                        Settings.Default.Save();
                        p.outputStream.WriteLine("<html><body><h3>Balance Checker settings saved</h3>");
                        p.outputStream.WriteLine("<a href=/>main</a><p>");
                    }
                    break;
                case "/sipgsmsettings":
                    {
                        string data = inputData.ReadToEnd();
                        var dataList = GetDataList(data);
                        p.outputStream.WriteLine("<html><body><h3>Balance Checker settings saved</h3>");
                        p.outputStream.WriteLine("<a href=/>main</a><p>");
                    }
                    break;
                case "/conf":
                    Log.Write("Получение конфигурации с сервера");
                    string postData = inputData.ReadToEnd();
                    try
                    {
                        Log.Write("Запись конфигурации в файл");
                        File.WriteAllText(@"../Cfg/conf.xml", postData);
                    }
                    catch (Exception ex)
                    {
                        Log.Write("[Ошибка] :: " + ex.Message);
                    }
                    break;
                default:
                    break;
            }
        }
예제 #2
0
        private void UpdateSipGSMConfig(HttpProcessor p)
        {
            StopService(Settings.Default.SipGsmServiceName, 1000);

            Log.Write("Обновление списка модемов");
            modemList = null;
            modemList = Modem.GetList();
            int i = 1;

            Log.Write("Найдено модемов :: " + modemList.Count);
            p.outputStream.WriteLine(string.Format("<h5>Найдено модемов :: {0}</h5>", i));
            foreach (var item in modemList)
            {
                Log.Write(string.Format("{0} ::  {1}", i, item.IMEI));
                p.outputStream.WriteLine(string.Format("<h5>{0} ::  {1}</h5>", i, item.IMEI));
                i++;
            }

            DeleteConfigFiles(p);

            Thread.Sleep(5000);
            i = 1;
            foreach (UsbDevice item in modemList)
            {
                Log.Write("Чтение настроек сервера");

                using (var connection = PostgreSQL.Get())
                {

                    try
                    {
                        Log.Write("Открытие соединения");
                        connection.Open();
                        NpgsqlCommand dbcmd = connection.CreateCommand();
            #if DEBUG
                        string imei = "354183026627980";
            #else
                    string imei = item.IMEI;
            #endif
                        dbcmd.Parameters.AddWithValue("@imei", imei);

                        dbcmd.CommandText = @"
            SELECT
            id,
            port
            FROM " + "\"Getaways\"" +
            @"
            WHERE imei = @imei
            ";
                        Log.Write("Чтение данных с сервера");
                        NpgsqlDataReader reader = dbcmd.ExecuteReader();
                        if(reader.Read())
                        {
                            string id = reader[0].ToString();
                            string port = reader[1].ToString();
                            Log.Write(string.Format("Добавление конфигурации для {0} :: p:{1}, id:{2}", imei, port, id));
                            WriteConfigFile(imei, port, id);
                            p.outputStream.WriteLine(string.Format("<h5>Добавление конфигурации для {0} :: p:{1}, id:{2}<h5>", imei, port, id));
                        }
                    }
                    catch (NpgsqlException ex)
                    {
                        Log.Write(ex.Message);
                    }
                    finally
                    {
                        Log.Write("Закрытие соединения");
                        connection.Close();
                    }
                }
                i++;
            }
            StartService(Settings.Default.SipGsmServiceName, 1000);
        }
예제 #3
0
        private void httpServer_OnGETRequest(HttpProcessor p)
        {
            switch (p.http_url)
            {
                case "/":
                    {
                        p.outputStream.WriteLine(@"
            <html>
            <head>" +
            "\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n" +
            @"</head>
            <body>
            <h3>Balance Checker</h3>
            </body>
            </html>
            ");
                    }
                    break;
                case "/reconf":
                    {
                        p.writeSuccess();
                        p.outputStream.WriteLine(@"
            <html>
            <head>" +
            "\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n" +
            @"</head>
            <body>
            <h4>Добавлены конфигурации для IMEI :</h4>
            ");
                        UpdateSipGSMConfig(p);
                        p.outputStream.WriteLine(@"
            <h4>Конфигурация обновлена!</h4>
            </body>
            </html>
            ");
                    }
                    break;
                case "/settings":
                    {
                        p.Show("html/settings.html");
                    }
                    break;
                case "/sipgsmsettings":
                    {
                        p.Show("html/sipgsmsettings.html");
                    }
                    break;
                case "/onoff":
                    {
                        OnOff();
                        var result = CheckSipGsm();
                        string htmlText = string.Format(Settings.Default.SipGsmStatusHtml, "Stopped" == result ? "#CC3300" : "#006600", result);
                        p.writeSuccess();
                        p.outputStream.WriteLine(htmlText);

                    }
                    break;
                case "/checksipgsm":
                    {
                        var result = CheckSipGsm();
                        string htmlText = string.Format(Settings.Default.SipGsmStatusHtml, ("Stopped" == result ? "#CC3300" : "#006600"), result);
                        p.writeSuccess();
                        p.outputStream.WriteLine(htmlText);
                    }
                    break;
                case "/check":
                    p.writeSuccess();
                    p.outputStream.WriteLine(@"
            <html>
            <head>" +
            "\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n" +
            @"<title>Checking Balance</title>
            </head>
            <body>
            <h3>Processing...</h3>
            ");
                    StartCheckBalance();
                    p.outputStream.WriteLine(@"
            </body>
            </html>
            ");
                    break;
                case "/favicon.ico" :
                    Stream fs = File.Open("html/favicon.ico", FileMode.Open);
                    p.writeSuccess("image/x-icon");
                    fs.CopyTo (p.outputStream.BaseStream);
                    p.outputStream.BaseStream.Flush ();
                    break;
                default:
                    p.writeSuccess();
                    p.outputStream.WriteLine("<html><body><h5>Command \"" + p.http_url + "\" not founded</h5>");
                    break;
            }
        }
예제 #4
0
        private void DeleteConfigFiles(HttpProcessor p)
        {
            Log.Write("Удаление старых файлов конфигурации");
            int i = 0;
            if (Directory.Exists(SipGSMPath))
            {
                foreach (string path in Directory.GetFiles(SipGSMPath))
                {
                    try
                    {
                        File.Delete(path);
                        i++;
                    }
                    catch (Exception e)
                    {
                        p.outputStream.WriteLine(string.Format("<h5>{0} ::  {1}</h5>", "Ошибка", e.Message));
                    }

                }
            }
            Log.Write(string.Format("Удалено файлов :: {0} ", i));
        }
 public void listen()
 {
     listener = new TcpListener(IPAddress.Any, port);
     listener.Start();
     if (null != OnHTTPServerStarted)
     {
         OnHTTPServerStarted.Invoke(listener);
     }
     while (is_active)
     {
         TcpClient s = listener.AcceptTcpClient();
         HttpProcessor processor = new HttpProcessor(s, this);
         Thread thread = new Thread(new ThreadStart(processor.process));
         thread.Start();
         Thread.Sleep(1);
     }
 }
 public virtual void handlePOSTRequest(HttpProcessor p, StreamReader inputData)
 {
     if (null != OnPOSTRequest)
     {
         OnPOSTRequest.Invoke(p, inputData);
     }
 }
 public virtual void handleGETRequest(HttpProcessor p)
 {
     if (null != OnGETRequest)
     {
         OnGETRequest.Invoke(p);
     }
 }