Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Eric Liu
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- // observable collections
- using System.Collections.ObjectModel;
- // debug output
- using System.Diagnostics;
- // timer, sleep
- using System.Threading;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows;
- // hi res timer
- //using PrecisionTimers;
- using System.Windows.Threading;
- // Rectangle
- // Must update References manually
- using System.Drawing;
- // INotifyPropertyChanged
- using System.ComponentModel;
- using TimeDataDLL;
- using System.Net.Sockets;
- using System.Net;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.IO;
- namespace Final
- {
- public partial class Model : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- private void OnPropertyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- //public DispatcherTimer timer = new DispatcherTimer();
- //int count = 0;
- private System.Windows.Threading.DispatcherTimer timer;
- public ObservableCollection<LED> LEDCollection;
- //
- private Thread _synchWithSetter;
- private static UdpClient _dataSocket;
- private static UInt32 _localPort = 5001;
- private static String _localIPAddress = "127.0.0.1";
- private static UInt32 _remotePort = 5000;
- private static String _remoteIPAddress = "127.0.0.1";
- private bool _connected = false;
- private Thread _receiveDataThread;
- TimeDataDLL.TimeData.StructTimeData tD;
- TimeDataDLL.TimeData.StructTimeData alarm;
- private System.Windows.Threading.DispatcherTimer _elapsedTimeTimer;
- //
- public Model()
- {
- DateTime now = DateTime.Now;
- LEDCollection = new ObservableCollection<LED>();
- tD.hour = now.Hour;
- tD.minute = now.Minute;
- tD.second = now.Second;
- tD.is24HourTime = false;
- tD.isAlarmTime = false;
- int offset = 10;
- for(int i = 0; i < 6; i++)
- {
- LEDCollection.Add(new LED()
- {
- LEDValue = Convert.ToUInt32(0),
- LEDLeft = offset,
- LEDTop = 50.0
- });
- offset += 50;
- }
- display();
- try
- {
- _dataSocket = new UdpClient((int)_localPort);
- }
- catch (Exception ex)
- {
- Debug.Write(ex.ToString());
- }
- ThreadStart threadFunction;
- threadFunction = new ThreadStart(SynchWithSetter);
- _synchWithSetter = new Thread(threadFunction);
- _synchWithSetter.Start();
- try
- {
- _elapsedTimeTimer = new System.Windows.Threading.DispatcherTimer();
- _elapsedTimeTimer.Tick += new EventHandler(elapsedTimerHandler);
- _elapsedTimeTimer.Interval = TimeSpan.FromMilliseconds(1000);
- _elapsedTimeTimer.Start();
- }
- catch
- {
- Console.WriteLine("Failed to create elapsed time timer.");
- }
- }
- private void elapsedTimerHandler(object source, EventArgs e)
- {
- tD.second++;
- if(tD.second >= 60)
- {
- tD.second = 0;
- tD.minute++;
- if(tD.minute >= 60)
- {
- tD.minute = 0;
- tD.hour++;
- if (tD.is24HourTime)
- {
- if(tD.hour >= 25)
- {
- tD.hour = 0;
- }
- }
- else
- {
- if(tD.hour >= 13)
- {
- tD.hour = 1;
- if(AMPM == "AM")
- {
- AMPM = "PM";
- }
- else
- {
- AMPM = "AM";
- }
- }
- }
- }
- }
- display();
- }
- private void SynchWithSetter()
- {
- Byte[] data = new Byte[1];
- IPEndPoint endPointSend = new IPEndPoint(IPAddress.Parse(_remoteIPAddress), (int)_remotePort);
- IPEndPoint endPointRecieve = new IPEndPoint(IPAddress.Any, 0);
- UdpClient synchSocket = new UdpClient((int)_localPort + 10);
- _dataSocket.Client.ReceiveTimeout = 1000;
- while (true)
- {
- try
- {
- Console.WriteLine("Trying to connect!");
- synchSocket.Send(data, data.Length, endPointSend);
- _dataSocket.Receive(ref endPointRecieve);
- _connected = true;
- Console.WriteLine("Connected to the Setter!");
- break;
- }
- catch (SocketException ex)
- {
- if (ex.ErrorCode == (int)SocketError.TimedOut)
- {
- Debug.Write(ex.ToString());
- }
- else
- {
- synchSocket.Close();
- return;
- }
- }
- catch (System.ObjectDisposedException ex)
- {
- Console.WriteLine(ex.ToString());
- synchSocket.Close();
- return;
- }
- }
- synchSocket.Send(data, data.Length, endPointSend);
- synchSocket.Close();
- _dataSocket.Client.ReceiveTimeout = 0;
- ThreadStart threadFunction = new ThreadStart(ReceiveThreadFunction);
- _receiveDataThread = new Thread(threadFunction);
- _receiveDataThread.Start();
- }
- private void ReceiveThreadFunction()
- {
- IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
- while (true)
- {
- try
- {
- Byte[] receiveData = _dataSocket.Receive(ref endPoint);
- if (receiveData.Length < 2)
- continue;
- TimeDataDLL.TimeData.StructTimeData tD1;
- BinaryFormatter formatter = new BinaryFormatter();
- MemoryStream stream = new MemoryStream();
- stream = new System.IO.MemoryStream(receiveData);
- tD1 = (TimeDataDLL.TimeData.StructTimeData)formatter.Deserialize(stream);
- if (!tD.isAlarmTime)
- {
- tD = tD1;
- display();
- }
- else
- {
- alarm = tD1;
- }
- }
- catch (SocketException ex)
- {
- Console.WriteLine(ex.ToString());
- return;
- }
- catch (Exception ex)
- {
- Console.Write(ex.ToString());
- }
- }
- }
- public void display()
- {
- LEDCollection[0].LEDValue = (uint)tD.hour / 10;
- LEDCollection[1].LEDValue = (uint)tD.hour % 10;
- LEDCollection[2].LEDValue = (uint)tD.minute / 10;
- LEDCollection[3].LEDValue = (uint)tD.minute % 10;
- LEDCollection[4].LEDValue = (uint)tD.second / 10;
- LEDCollection[5].LEDValue = (uint)tD.second % 10;
- if (tD.is24HourTime)
- {
- if(tD.hour >= 12)
- {
- AMPM = "PM";
- }
- else
- {
- AMPM = "AM";
- }
- }
- else
- {
- AMPM = "";
- }
- checkAlarm();
- }
- private void checkAlarm()
- {
- }
- private String _ampm;
- public String AMPM
- {
- get { return _ampm; }
- set
- {
- _ampm = value;
- OnPropertyChanged("AMPM");
- }
- }
- private String _alarm;
- public String Alarm
- {
- get { return _alarm; }
- set
- {
- _ampm = value;
- OnPropertyChanged("Alarm");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement