Esempio n. 1
0
        public void SendViaTCP(string host, int port, string key, string val)
        {
            using TcpClient _tcpClient = new TcpClient();
            string data = key + "|" + escapeString(val);

            while (true)
            {
                try
                {
                    ConsoleMessage.Write("Подключаюсь к VizEngine...");
                    _tcpClient.Connect(host, port);
                    ConsoleMessage.Write("Подключение успешно установлено");
                    _tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                    _tcpClient.Client.Send(Encoding.UTF8.GetBytes(data + "\0"));
                    ConsoleMessage.Write("Данные успешно переданы в VizEngine \n");
                }
                catch (Exception e)
                {
                    ConsoleMessage.Write("Ошибка подключения к VizEngine. Попробую снова...", e);
                    Thread.Sleep(10000);
                    continue;
                }

                if (_tcpClient.Connected)
                {
                    _tcpClient.Close();
                    break;
                }
                ;
            }
        }
Esempio n. 2
0
 public XmlReadItem(string path)
 {
     try
     {
         XmlReader reader = XmlReader.Create(path);
         item = new List <Item>();
         while (reader.Read())
         {
             if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "record"))
             {
                 if (reader.HasAttributes)
                 {
                     Item i = new Item();
                     i.Index = Convert.ToInt32(reader.GetAttribute("index"));
                     i.City  = reader.GetAttribute("city");
                     i.Lat   = Convert.ToDouble(reader.GetAttribute("lat"));
                     i.Lon   = Convert.ToDouble(reader.GetAttribute("lon"));
                     i.Popul = Convert.ToInt32(reader.GetAttribute("popul"));
                     item.Add(i);
                 }
             }
         }
         reader.Close();
     }
     catch (Exception e)
     {
         ConsoleMessage.Write("Ошибка при чтении xml файла", e);
     }
 }
Esempio n. 3
0
        public Weather(string lat, string lon)
        {
            try
            {
                lat = lat.Replace(",", ".");
                lon = lon.Replace(",", ".");
                string         url     = ($"https://api.weather.yandex.ru/v2/forecast?lat={lat}&lon={lon}");
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Headers.Add("X-Yandex-API-Key: " + System.Configuration.ConfigurationManager.AppSettings["X-Yandex-API-Key"]);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                ConsoleMessage.Write("Получен ответ от сервера погоды");
                string strResponse;
                using (StreamReader streamRead = new StreamReader(response.GetResponseStream()))
                {
                    strResponse = streamRead.ReadToEnd();
                }
                ConsoleMessage.Write("Поток успешно прочитан");

                yandexWeatherAPI = JsonConvert.DeserializeObject <YandexWeatherAPI>(strResponse);
            }
            catch (Exception e)
            {
                ConsoleMessage.Write("Ошибка запроса на сервер погоды", e);
            }
        }
Esempio n. 4
0
 static void Task()
 {
     try
     {
         Console.ForegroundColor = ConsoleColor.White;
         Console.WriteLine(System.DateTime.Now);
         SendToVizEngine sendToVizEngine = new SendToVizEngine();
         XmlReadItem     xmlReadItem     = new XmlReadItem(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\city_list.xml");
         List <Item>     item_list       = xmlReadItem.ReturnItemList();
         ConsoleMessage.Write("Список городов считан из файла xml");
         Item   item = new Item();
         string lat  = "";
         string lon  = "";
         string val  = "";
         for (int i = 0; i < item_list.Count; i++) //
         {
             item = item_list.Find(a => a.Index == i);
             string city = item.City;
             lat = item.Lat.ToString();
             lon = item.Lon.ToString();
             Weather weather   = new Weather(lat, lon);
             int     temp      = weather.WeatherTemp();
             string  condition = weather.WeatherCondition();
             ConsoleMessage.Write("Передаю данные");
             ConsoleMessage.Write(i + "\t" + lat + " " + lon + "\t" + temp + "\t" + city.Trim() + " \t \t " + condition.Trim());
             val = city + "*" + temp + "*" + condition;
             sendToVizEngine.SendViaTCP(host, port, "key" + i, val);
         }
         sendToVizEngine.SendViaTCP(host, port, "city_number", item_list.Count.ToString()); //
         DateTime now = DateTime.Now;
         Console.WriteLine((now.Hour * 60 + now.Minute).ToString());
         sendToVizEngine.SendViaTCP(host, port, "data_freshness", (now.Hour * 60 + now.Minute).ToString());
     }
     catch (Exception e)
     {
         ConsoleMessage.Write("Произошла ошибка", e);
     }
 }