Yonka2019

Form1.cs

Apr 13th, 2021
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.06 KB | None | 0 0
  1. // Decompiled with JetBrains decompiler
  2. // Type: WindowsFormsApplication1.Form1
  3. // Assembly: WindowsFormsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  4. // MVID: A0967493-2B5E-45DB-9F7E-6A0B14E2A48A
  5. // Assembly location: C:\Users\yonka\Desktop\WeatherClient\WeatherClient.exe
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Net;
  13. using System.Net.Sockets;
  14. using System.Text;
  15. using System.Threading;
  16. using System.Windows.Forms;
  17. using WindowsFormsApplication1.Properties;
  18.  
  19. namespace WindowsFormsApplication1
  20. {
  21.   public class Form1 : Form
  22.   {
  23.     private const int PORT = 77;
  24.     private string ip;
  25.     private string city;
  26.     private string date;
  27.     private int lastIndex = 0;
  28.     private Dictionary<string, Bitmap> resourcesWeatherDictioanry = new Dictionary<string, Bitmap>();
  29.     private string[] keysHirarchey = new string[7]
  30.     {
  31.       "sky is clear",
  32.       "few clouds",
  33.       "shower rain",
  34.       "thunder storm",
  35.       "clouds",
  36.       "rain",
  37.       "snow"
  38.     };
  39.     private IContainer components = (IContainer) null;
  40.     private PictureBox imgBoxForecast;
  41.     private Label locationLBL;
  42.     private Label dateLBL;
  43.     private Label textLBL;
  44.     private Label tempLBL;
  45.  
  46.     private void start_comm()
  47.     {
  48.       ServerResponse response = (ServerResponse) null;
  49.       this.imgBoxForecast.SizeMode = PictureBoxSizeMode.Zoom;
  50.       Dictionary<string, string> dictionary = this.readConfigFile("config.ini");
  51.       if (dictionary != null)
  52.       {
  53.         this.ip = dictionary["server-ip"];
  54.         this.city = dictionary["city"];
  55.         this.date = dictionary["date"];
  56.         Console.WriteLine(this.ip + ", " + this.city + ", " + this.date);
  57.         response = this.requestResults(this.ip, this.city, this.date);
  58.       }
  59.       this.proccessResponse(response);
  60.     }
  61.  
  62.     public Form1()
  63.     {
  64.       this.InitializeComponent();
  65.       this.InitializeResourcesWeatherDictioanry();
  66.       new Thread(new ThreadStart(this.start_comm)).Start();
  67.     }
  68.  
  69.     private string CalculateChecksum(string city, string date)
  70.     {
  71.       city = city.ToLower();
  72.       int num1 = 0;
  73.       int num2 = 0;
  74.       foreach (char ch in city)
  75.       {
  76.         if (ch >= 'a' && ch <= 'z')
  77.           num1 += (int) ch - 97 + 1;
  78.       }
  79.       foreach (char ch in date)
  80.       {
  81.         if (ch >= '0' && ch <= '9')
  82.           num2 += (int) ch - 48;
  83.       }
  84.       return num1.ToString() + "." + (object) num2;
  85.     }
  86.  
  87.     private ServerResponse requestResults(string ip, string city, string date)
  88.     {
  89.       byte[] buffer = new byte[1024];
  90.       string checksum = this.CalculateChecksum(city, date);
  91.       try
  92.       {
  93.         IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(ip), 77);
  94.         Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  95.         try
  96.         {
  97.           socket.Connect((EndPoint) ipEndPoint);
  98.           socket.Receive(buffer);
  99.           string s = "100:REQUEST:city=" + city + "&date=" + date + "&checksum=" + checksum;
  100.           for (int index = 0; index < 1024; ++index)
  101.             buffer[index] = index >= s.Length ? (byte) 0 : (byte) s[index];
  102.           socket.Send(Encoding.ASCII.GetBytes(s));
  103.           int num = socket.Receive(buffer);
  104.           string raw_response = "";
  105.           for (int index = 0; index < num; ++index)
  106.             raw_response += ((char) buffer[index]).ToString();
  107.           socket.Shutdown(SocketShutdown.Both);
  108.           socket.Close();
  109.           return new ServerResponse(raw_response);
  110.         }
  111.         catch (ArgumentNullException ex)
  112.         {
  113.           Console.WriteLine("ArgumentNullException : {0}", (object) ex.ToString());
  114.         }
  115.         catch (SocketException ex)
  116.         {
  117.           Console.WriteLine("SocketException : {0}", (object) ex.ToString());
  118.         }
  119.         catch (Exception ex)
  120.         {
  121.           Console.WriteLine("Unexpected exception : {0}", (object) ex.ToString());
  122.         }
  123.       }
  124.       catch (Exception ex)
  125.       {
  126.         Console.WriteLine(ex.ToString());
  127.       }
  128.       return (ServerResponse) null;
  129.     }
  130.  
  131.     private static byte[] GetBytes(string str)
  132.     {
  133.       byte[] numArray = new byte[str.Length * 2];
  134.       Buffer.BlockCopy((Array) str.ToCharArray(), 0, (Array) numArray, 0, numArray.Length);
  135.       return numArray;
  136.     }
  137.  
  138.     private static string GetString(byte[] bytes)
  139.     {
  140.       char[] chArray = new char[bytes.Length / 2];
  141.       Buffer.BlockCopy((Array) bytes, 0, (Array) chArray, 0, bytes.Length);
  142.       return new string(chArray);
  143.     }
  144.  
  145.     private Dictionary<string, string> readConfigFile(string fileName)
  146.     {
  147.       Dictionary<string, string> dictionary = new Dictionary<string, string>();
  148.       using (StreamReader streamReader = new StreamReader(fileName))
  149.       {
  150.         string str;
  151.         while ((str = streamReader.ReadLine()) != null)
  152.         {
  153.           string[] strArray = str.Split('=');
  154.           if (strArray.Length == 2)
  155.             dictionary[strArray[0]] = strArray[1];
  156.         }
  157.       }
  158.       return dictionary.Count != 3 || (dictionary["server-ip"] == null || dictionary["city"] == null || dictionary["date"] == null) ? (Dictionary<string, string>) null : dictionary;
  159.     }
  160.  
  161.     private void proccessResponse(ServerResponse response)
  162.     {
  163.       if (response == null)
  164.       {
  165.         int num = (int) MessageBox.Show("ERROR: No Internet Connection / Server Unavailable");
  166.       }
  167.       else
  168.       {
  169.         foreach (string key in this.keysHirarchey)
  170.         {
  171.           if (response.getText().Contains(key))
  172.           {
  173.             this.imgBoxForecast.Image = (Image) this.resourcesWeatherDictioanry[key];
  174.             break;
  175.           }
  176.         }
  177.         this.Invoke((Delegate) (() => this.updateGUI(response)));
  178.       }
  179.     }
  180.  
  181.     private void updateGUI(ServerResponse response)
  182.     {
  183.       this.locationLBL.Text = response.getLocation();
  184.       this.dateLBL.Text = response.getDate();
  185.       this.textLBL.Text = response.getText();
  186.       this.tempLBL.Text = ((int) response.getTemperture()).ToString() + "°";
  187.     }
  188.  
  189.     private void InitializeResourcesWeatherDictioanry()
  190.     {
  191.       this.resourcesWeatherDictioanry["sky is clear"] = new Bitmap((Image) Resources.res_clear_sky_day);
  192.       this.resourcesWeatherDictioanry["few clouds"] = new Bitmap((Image) Resources.res_few_clouds_day);
  193.       this.resourcesWeatherDictioanry["clouds"] = new Bitmap((Image) Resources.res_clouds_day);
  194.       this.resourcesWeatherDictioanry["rain"] = new Bitmap((Image) Resources.res_rain_day);
  195.       this.resourcesWeatherDictioanry["shower rain"] = new Bitmap((Image) Resources.res_shower_rain);
  196.       this.resourcesWeatherDictioanry["snow"] = new Bitmap((Image) Resources.res_snow);
  197.       this.resourcesWeatherDictioanry["thunder storm"] = new Bitmap((Image) Resources.res_thunder_strom);
  198.     }
  199.  
  200.     public void Form1_FormClosing(object sender, FormClosingEventArgs e)
  201.     {
  202.       if (!(string.Concat((object) e.CloseReason) != "UserClosing"))
  203.         return;
  204.       int num = (int) MessageBox.Show(string.Concat((object) e.CloseReason));
  205.     }
  206.  
  207.     protected override void Dispose(bool disposing)
  208.     {
  209.       if (disposing && this.components != null)
  210.         this.components.Dispose();
  211.       base.Dispose(disposing);
  212.     }
  213.  
  214.     private void InitializeComponent()
  215.     {
  216.       this.imgBoxForecast = new PictureBox();
  217.       this.locationLBL = new Label();
  218.       this.dateLBL = new Label();
  219.       this.textLBL = new Label();
  220.       this.tempLBL = new Label();
  221.       ((ISupportInitialize) this.imgBoxForecast).BeginInit();
  222.       this.SuspendLayout();
  223.       this.imgBoxForecast.Location = new Point(12, 243);
  224.       this.imgBoxForecast.Name = "imgBoxForecast";
  225.       this.imgBoxForecast.Size = new Size(360, 216);
  226.       this.imgBoxForecast.TabIndex = 2;
  227.       this.imgBoxForecast.TabStop = false;
  228.       this.locationLBL.AutoSize = true;
  229.       this.locationLBL.Font = new Font("Microsoft Sans Serif", 18f, FontStyle.Regular, GraphicsUnit.Point, (byte) 177);
  230.       this.locationLBL.Location = new Point(10, 43);
  231.       this.locationLBL.Name = "locationLBL";
  232.       this.locationLBL.Size = new Size(105, 29);
  233.       this.locationLBL.TabIndex = 3;
  234.       this.locationLBL.Text = "loading..";
  235.       this.dateLBL.AutoSize = true;
  236.       this.dateLBL.Location = new Point(12, 72);
  237.       this.dateLBL.Name = "dateLBL";
  238.       this.dateLBL.Size = new Size(50, 13);
  239.       this.dateLBL.TabIndex = 4;
  240.       this.dateLBL.Text = "loading...";
  241.       this.textLBL.AutoSize = true;
  242.       this.textLBL.Font = new Font("Microsoft Sans Serif", 12f, FontStyle.Regular, GraphicsUnit.Point, (byte) 177);
  243.       this.textLBL.Location = new Point(12, 220);
  244.       this.textLBL.Name = "textLBL";
  245.       this.textLBL.Size = new Size(68, 20);
  246.       this.textLBL.TabIndex = 5;
  247.       this.textLBL.Text = "loading..";
  248.       this.tempLBL.AutoSize = true;
  249.       this.tempLBL.Font = new Font("Microsoft Sans Serif", 60f, FontStyle.Regular, GraphicsUnit.Point, (byte) 177);
  250.       this.tempLBL.Location = new Point(214, 72);
  251.       this.tempLBL.Name = "tempLBL";
  252.       this.tempLBL.Size = new Size(110, 91);
  253.       this.tempLBL.TabIndex = 6;
  254.       this.tempLBL.Text = "-1";
  255.       this.tempLBL.TextAlign = ContentAlignment.MiddleCenter;
  256.       this.AutoScaleDimensions = new SizeF(6f, 13f);
  257.       this.AutoScaleMode = AutoScaleMode.Font;
  258.       this.ClientSize = new Size(384, 471);
  259.       this.Controls.Add((Control) this.tempLBL);
  260.       this.Controls.Add((Control) this.textLBL);
  261.       this.Controls.Add((Control) this.dateLBL);
  262.       this.Controls.Add((Control) this.locationLBL);
  263.       this.Controls.Add((Control) this.imgBoxForecast);
  264.       this.MaximizeBox = false;
  265.       this.MaximumSize = new Size(400, 510);
  266.       this.MinimumSize = new Size(400, 510);
  267.       this.Name = nameof (Form1);
  268.       this.Text = "Forecast Client - Magshimim";
  269.       this.FormClosing += new FormClosingEventHandler(this.Form1_FormClosing);
  270.       ((ISupportInitialize) this.imgBoxForecast).EndInit();
  271.       this.ResumeLayout(false);
  272.       this.PerformLayout();
  273.     }
  274.   }
  275. }
  276.  
Add Comment
Please, Sign In to add comment