Advertisement
rav1989

Untitled

Apr 30th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.83 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Threading;
  12.  
  13. namespace Program5
  14. {
  15.  
  16.     public partial class Form1 : Form
  17.     {
  18.         Pogodynka pogod;
  19.         string[] jednostki = { "°C", "°F", "hPa", "mmHg" };
  20.         int[] jednostka = new int[2];
  21.         string[] pliki = { @"dane.txt", @"historia_pogody.txt"};
  22.         public delegate void WypiszDane(string value);
  23.         public delegate void UpdateList(object value);
  24.         public WypiszDane[] WyslijDane = new WypiszDane[5];
  25.         public Form1()
  26.         {
  27.             InitializeComponent();
  28.         }
  29.  
  30.         public void WyswietlTemperature(string value)
  31.         {
  32.             /*if (pogod != null)
  33.             {
  34.                 if (fahrenhiet.Checked)
  35.                 {
  36.                     jednostka[0] = 1;
  37.                     temperatura.Text = pogod.Temperatura("F");
  38.                 }
  39.                 else
  40.                 {
  41.                     jednostka[0] = 0;
  42.                     temperatura.Text = pogod.Temperatura();
  43.                 }
  44.  
  45.                 groupTemp.Text = string.Format("Temperatura [{0}]", jednostki[jednostka[0]]);
  46.             }*/
  47.             temperatura.Text = value;
  48.         }
  49.  
  50.         public void WyswietlCisnienie(string value){
  51.             /*if (pogod != null)
  52.             {
  53.                 if (mmhg.Checked)
  54.                 {
  55.                     jednostka[1] = 3;
  56.                     cisnienie.Text = pogod.Cisnienie("mmHg");
  57.                 }
  58.                 else
  59.                 {
  60.                     jednostka[1] = 2;
  61.                     cisnienie.Text = pogod.Cisnienie();
  62.                 }
  63.                
  64.                 groupCisn.Text = string.Format("Ciśnienie [{0}]", jednostki[jednostka[1]]);
  65.             }*/
  66.             cisnienie.Text = value;
  67.         }
  68.  
  69.         public bool JednTemp()
  70.         {
  71.             return fahrenhiet.Checked;
  72.         }
  73.  
  74.         public bool JednCisn()
  75.         {
  76.             return mmhg.Checked;
  77.         }
  78.         public bool Run()
  79.         {
  80.             return autoread.Checked;
  81.         }
  82.  
  83.         public void WyswietlWilgotnosc(string value)
  84.         {
  85.             /*if (pogod != null)
  86.             {
  87.                 wilgotnosc.Text = pogod.Wilgotnosc();
  88.             }
  89.             */
  90.             wilgotnosc.Text = value;
  91.         }
  92.  
  93.         public void UstawTytul(string value)
  94.         {
  95.             panelGlowny.Text = string.Format("Aktualne dane z: {0}", value);
  96.         }
  97.  
  98.         private void DodajDoListy(string value)
  99.         {
  100.             string[] s = value.Split(',');
  101.             ListViewItem item = new ListViewItem(s);
  102.             listView1.Items.Insert(listView1.Items.Count, item);
  103.             chart1.Series[0].Points.AddXY(s[0], s[1]);
  104.             chart1.Series[1].Points.AddXY(s[0], s[2]);
  105.             chart1.Series[2].Points.AddXY(s[0], s[3]);
  106.             label1.Visible = false;
  107.             label2.Visible = false;
  108.         }
  109.  
  110.         private void button1_Click(object sender, EventArgs e)
  111.         {
  112.             if (File.Exists(pliki[0]))
  113.             {
  114.                 WyslijDane[0] = new WypiszDane(WyswietlTemperature);
  115.                 WyslijDane[1] = new WypiszDane(WyswietlCisnienie);
  116.                 WyslijDane[2] = new WypiszDane(WyswietlWilgotnosc);
  117.                 WyslijDane[3] = new WypiszDane(UstawTytul);
  118.                 WyslijDane[4] = new WypiszDane(DodajDoListy);
  119.  
  120.                 fahrenhiet.Enabled = true;
  121.                 mmhg.Enabled = true;
  122.  
  123.                 pogod = new Pogodynka(pliki, this);
  124.                 Thread t = new Thread(new ThreadStart(pogod.ThreadProc));
  125.                 t.Start();
  126.             }
  127.             else
  128.             {
  129.                 MessageBox.Show("Brak pliku z danymi.","Uwaga!");
  130.             }
  131.            
  132.         }
  133.  
  134.         private void Form1_Load(object sender, EventArgs e)
  135.         {
  136.             if (File.Exists(pliki[1]))
  137.             {
  138.                 FileStream historia = File.Open(pliki[1], FileMode.Open, FileAccess.Read);
  139.                 string[] s = { };
  140.  
  141.                 using (StreamReader dane = new StreamReader(historia))
  142.                 {
  143.                     while (!dane.EndOfStream)
  144.                     {
  145.                         DodajDoListy(dane.ReadLine());
  146.                     }
  147.                 }            
  148.             }
  149.             else
  150.             {
  151.                 label1.Visible = true;
  152.                 label2.Visible = true;
  153.             }
  154.             fahrenhiet.Enabled = false;
  155.             mmhg.Enabled = false;
  156.         }
  157.  
  158.         private void autoread_CheckedChanged(object sender, EventArgs e)
  159.         {
  160.             if (autoread.Checked)
  161.             {
  162.                 readData.PerformClick();
  163.                 readData.Enabled = false;
  164.             }
  165.             else
  166.             {
  167.                 readData.Enabled = true;
  168.             }
  169.         }
  170.  
  171.         private void fahrenhiet_CheckedChanged(object sender, EventArgs e)
  172.         {
  173.             if (!autoread.Checked)
  174.             {
  175.                 try
  176.                 {
  177.                     if (fahrenhiet.Checked)
  178.                     {
  179.                         temperatura.Text = Przelicz.Fahrenhiet(int.Parse(temperatura.Text)).ToString();
  180.                     }
  181.                     else
  182.                     {
  183.                         temperatura.Text = Przelicz.Celsjusz(int.Parse(temperatura.Text)).ToString();
  184.                     }
  185.                 }
  186.                 catch (Exception) { }
  187.             }
  188.         }
  189.  
  190.         private void mmhg_CheckedChanged(object sender, EventArgs e)
  191.         {
  192.             if (!autoread.Checked)
  193.             {
  194.                 try
  195.                 {
  196.                     if (mmhg.Checked)
  197.                     {
  198.                         cisnienie.Text = Przelicz.mmHg(int.Parse(cisnienie.Text)).ToString();
  199.                     }
  200.                     else
  201.                     {
  202.                         cisnienie.Text = Przelicz.hPa(int.Parse(cisnienie.Text)).ToString();
  203.                     }
  204.                 }
  205.                 catch (Exception) { }
  206.             }
  207.         }
  208.  
  209.     }
  210.  
  211.     public class Przelicz
  212.     {
  213.         public static int Fahrenhiet(int temperaturaC)
  214.         {
  215.             return (int)((temperaturaC * 1.8) + 32);
  216.         }
  217.  
  218.         public static int Celsjusz(int temperaturaF)
  219.         {
  220.             return (int)((temperaturaF - 32) / 1.8) + 1;
  221.         }
  222.  
  223.         public static int mmHg(int hpa)
  224.         {
  225.             return (int)(hpa * 0.75);
  226.         }
  227.  
  228.         public static int hPa(int mmhg)
  229.         {
  230.             return (int)(mmhg / 0.75);
  231.         }
  232.     }
  233.  
  234.     public class Pogodynka
  235.     {
  236.         private string dataCzas;
  237.         private int temperatura;
  238.         private int cisnienie;
  239.         private int wilgotnosc;
  240.         private Form1 myForm;
  241.         private string[] s = { };
  242.         private string[] paths = { };
  243.  
  244.         public Pogodynka(string[] paths, Form1 myForm)
  245.         {
  246.             this.paths = paths;
  247.             this.myForm = myForm;
  248.         }
  249.  
  250.         public void ThreadProc()
  251.         {
  252.             do
  253.             {
  254.                 FileStream path = new FileStream(paths[0], FileMode.Open, FileAccess.Read);
  255.                 FileStream backup = new FileStream(paths[1], FileMode.Append, FileAccess.Write);
  256.                 lock (path)
  257.                 {
  258.                     using (StreamReader dane = new StreamReader(path))
  259.                     {
  260.                         s = dane.ReadLine().Split(',');
  261.                         dataCzas = s[0];
  262.                         temperatura = int.Parse(s[1]);
  263.                         cisnienie = int.Parse(s[2]);
  264.                         wilgotnosc = int.Parse(s[3]);
  265.                     }
  266.                 }
  267.                 lock (backup)
  268.                 {
  269.                     using (StreamWriter daneHist = new StreamWriter(backup))
  270.                     {
  271.                         string linia = string.Format("{0},{1},{2},{3}", s);
  272.                         daneHist.WriteLine(linia);
  273.                     }
  274.                 }
  275.                     WyswietlDane();
  276.                     Thread.Sleep(1000);
  277.             }while(myForm.Run());
  278.         }
  279.  
  280.  
  281.         private void WyswietlDane()
  282.         {
  283.             myForm.Invoke(myForm.WyslijDane[3], s[0]);
  284.             if (myForm.JednTemp())
  285.             {
  286.                 myForm.Invoke(myForm.WyslijDane[0], Temperatura("F"));
  287.             }
  288.             else
  289.             {
  290.                 myForm.Invoke(myForm.WyslijDane[0], Temperatura());
  291.             }
  292.             if (myForm.JednCisn())
  293.             {
  294.                 myForm.Invoke(myForm.WyslijDane[1], Cisnienie("mmHg"));
  295.             }
  296.             else
  297.             {
  298.                 myForm.Invoke(myForm.WyslijDane[1], Cisnienie());
  299.             }
  300.             myForm.Invoke(myForm.WyslijDane[2], Wilgotnosc());
  301.             myForm.Invoke(myForm.WyslijDane[4], string.Format("{0},{1},{2},{3}", s));
  302.         }
  303.  
  304.         private string Temperatura(string jednostka = "")
  305.         {
  306.             if (jednostka.Equals("F"))
  307.             {
  308.                 int stF = Przelicz.Fahrenhiet(temperatura);
  309.                 return stF.ToString();
  310.             }
  311.             else
  312.             {
  313.                 return temperatura.ToString();
  314.             }
  315.         }
  316.  
  317.         private string Cisnienie(string jednostka = "")
  318.         {
  319.             if (jednostka.Equals("mmHg"))
  320.             {
  321.                 int mmHg = Przelicz.mmHg(cisnienie);
  322.                 return mmHg.ToString();
  323.             }
  324.             else
  325.             {
  326.                 return cisnienie.ToString();
  327.             }
  328.         }
  329.         private string Wilgotnosc()
  330.         {
  331.             return wilgotnosc.ToString();
  332.         }
  333.     }
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement