Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Threading;
- namespace Program5
- {
- public partial class Form1 : Form
- {
- Pogodynka pogod;
- string[] jednostki = { "°C", "°F", "hPa", "mmHg" };
- int[] jednostka = new int[2];
- string[] pliki = { @"dane.txt", @"historia_pogody.txt"};
- public delegate void WypiszDane(string value);
- public delegate void UpdateList(object value);
- public WypiszDane[] WyslijDane = new WypiszDane[5];
- public Form1()
- {
- InitializeComponent();
- }
- public void WyswietlTemperature(string value)
- {
- /*if (pogod != null)
- {
- if (fahrenhiet.Checked)
- {
- jednostka[0] = 1;
- temperatura.Text = pogod.Temperatura("F");
- }
- else
- {
- jednostka[0] = 0;
- temperatura.Text = pogod.Temperatura();
- }
- groupTemp.Text = string.Format("Temperatura [{0}]", jednostki[jednostka[0]]);
- }*/
- temperatura.Text = value;
- }
- public void WyswietlCisnienie(string value){
- /*if (pogod != null)
- {
- if (mmhg.Checked)
- {
- jednostka[1] = 3;
- cisnienie.Text = pogod.Cisnienie("mmHg");
- }
- else
- {
- jednostka[1] = 2;
- cisnienie.Text = pogod.Cisnienie();
- }
- groupCisn.Text = string.Format("Ciśnienie [{0}]", jednostki[jednostka[1]]);
- }*/
- cisnienie.Text = value;
- }
- public bool JednTemp()
- {
- return fahrenhiet.Checked;
- }
- public bool JednCisn()
- {
- return mmhg.Checked;
- }
- public bool Run()
- {
- return autoread.Checked;
- }
- public void WyswietlWilgotnosc(string value)
- {
- /*if (pogod != null)
- {
- wilgotnosc.Text = pogod.Wilgotnosc();
- }
- */
- wilgotnosc.Text = value;
- }
- public void UstawTytul(string value)
- {
- panelGlowny.Text = string.Format("Aktualne dane z: {0}", value);
- }
- private void DodajDoListy(string value)
- {
- string[] s = value.Split(',');
- ListViewItem item = new ListViewItem(s);
- listView1.Items.Insert(listView1.Items.Count, item);
- chart1.Series[0].Points.AddXY(s[0], s[1]);
- chart1.Series[1].Points.AddXY(s[0], s[2]);
- chart1.Series[2].Points.AddXY(s[0], s[3]);
- label1.Visible = false;
- label2.Visible = false;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (File.Exists(pliki[0]))
- {
- WyslijDane[0] = new WypiszDane(WyswietlTemperature);
- WyslijDane[1] = new WypiszDane(WyswietlCisnienie);
- WyslijDane[2] = new WypiszDane(WyswietlWilgotnosc);
- WyslijDane[3] = new WypiszDane(UstawTytul);
- WyslijDane[4] = new WypiszDane(DodajDoListy);
- fahrenhiet.Enabled = true;
- mmhg.Enabled = true;
- pogod = new Pogodynka(pliki, this);
- Thread t = new Thread(new ThreadStart(pogod.ThreadProc));
- t.Start();
- }
- else
- {
- MessageBox.Show("Brak pliku z danymi.","Uwaga!");
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- if (File.Exists(pliki[1]))
- {
- FileStream historia = File.Open(pliki[1], FileMode.Open, FileAccess.Read);
- string[] s = { };
- using (StreamReader dane = new StreamReader(historia))
- {
- while (!dane.EndOfStream)
- {
- DodajDoListy(dane.ReadLine());
- }
- }
- }
- else
- {
- label1.Visible = true;
- label2.Visible = true;
- }
- fahrenhiet.Enabled = false;
- mmhg.Enabled = false;
- }
- private void autoread_CheckedChanged(object sender, EventArgs e)
- {
- if (autoread.Checked)
- {
- readData.PerformClick();
- readData.Enabled = false;
- }
- else
- {
- readData.Enabled = true;
- }
- }
- private void fahrenhiet_CheckedChanged(object sender, EventArgs e)
- {
- if (!autoread.Checked)
- {
- try
- {
- if (fahrenhiet.Checked)
- {
- temperatura.Text = Przelicz.Fahrenhiet(int.Parse(temperatura.Text)).ToString();
- }
- else
- {
- temperatura.Text = Przelicz.Celsjusz(int.Parse(temperatura.Text)).ToString();
- }
- }
- catch (Exception) { }
- }
- }
- private void mmhg_CheckedChanged(object sender, EventArgs e)
- {
- if (!autoread.Checked)
- {
- try
- {
- if (mmhg.Checked)
- {
- cisnienie.Text = Przelicz.mmHg(int.Parse(cisnienie.Text)).ToString();
- }
- else
- {
- cisnienie.Text = Przelicz.hPa(int.Parse(cisnienie.Text)).ToString();
- }
- }
- catch (Exception) { }
- }
- }
- }
- public class Przelicz
- {
- public static int Fahrenhiet(int temperaturaC)
- {
- return (int)((temperaturaC * 1.8) + 32);
- }
- public static int Celsjusz(int temperaturaF)
- {
- return (int)((temperaturaF - 32) / 1.8) + 1;
- }
- public static int mmHg(int hpa)
- {
- return (int)(hpa * 0.75);
- }
- public static int hPa(int mmhg)
- {
- return (int)(mmhg / 0.75);
- }
- }
- public class Pogodynka
- {
- private string dataCzas;
- private int temperatura;
- private int cisnienie;
- private int wilgotnosc;
- private Form1 myForm;
- private string[] s = { };
- private string[] paths = { };
- public Pogodynka(string[] paths, Form1 myForm)
- {
- this.paths = paths;
- this.myForm = myForm;
- }
- public void ThreadProc()
- {
- do
- {
- FileStream path = new FileStream(paths[0], FileMode.Open, FileAccess.Read);
- FileStream backup = new FileStream(paths[1], FileMode.Append, FileAccess.Write);
- lock (path)
- {
- using (StreamReader dane = new StreamReader(path))
- {
- s = dane.ReadLine().Split(',');
- dataCzas = s[0];
- temperatura = int.Parse(s[1]);
- cisnienie = int.Parse(s[2]);
- wilgotnosc = int.Parse(s[3]);
- }
- }
- lock (backup)
- {
- using (StreamWriter daneHist = new StreamWriter(backup))
- {
- string linia = string.Format("{0},{1},{2},{3}", s);
- daneHist.WriteLine(linia);
- }
- }
- WyswietlDane();
- Thread.Sleep(1000);
- }while(myForm.Run());
- }
- private void WyswietlDane()
- {
- myForm.Invoke(myForm.WyslijDane[3], s[0]);
- if (myForm.JednTemp())
- {
- myForm.Invoke(myForm.WyslijDane[0], Temperatura("F"));
- }
- else
- {
- myForm.Invoke(myForm.WyslijDane[0], Temperatura());
- }
- if (myForm.JednCisn())
- {
- myForm.Invoke(myForm.WyslijDane[1], Cisnienie("mmHg"));
- }
- else
- {
- myForm.Invoke(myForm.WyslijDane[1], Cisnienie());
- }
- myForm.Invoke(myForm.WyslijDane[2], Wilgotnosc());
- myForm.Invoke(myForm.WyslijDane[4], string.Format("{0},{1},{2},{3}", s));
- }
- private string Temperatura(string jednostka = "")
- {
- if (jednostka.Equals("F"))
- {
- int stF = Przelicz.Fahrenhiet(temperatura);
- return stF.ToString();
- }
- else
- {
- return temperatura.ToString();
- }
- }
- private string Cisnienie(string jednostka = "")
- {
- if (jednostka.Equals("mmHg"))
- {
- int mmHg = Przelicz.mmHg(cisnienie);
- return mmHg.ToString();
- }
- else
- {
- return cisnienie.ToString();
- }
- }
- private string Wilgotnosc()
- {
- return wilgotnosc.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement