Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. //Eric Liu
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. // observable collections
  9. using System.Collections.ObjectModel;
  10.  
  11. // debug output
  12. using System.Diagnostics;
  13.  
  14. // timer, sleep
  15. using System.Threading;
  16.  
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows;
  20.  
  21. // hi res timer
  22. //using PrecisionTimers;
  23. using System.Windows.Threading;
  24. // Rectangle
  25. // Must update References manually
  26. using System.Drawing;
  27.  
  28. // INotifyPropertyChanged
  29. using System.ComponentModel;
  30.  
  31. using TimeDataDLL;
  32. using System.Net.Sockets;
  33. using System.Net;
  34. using System.Runtime.Serialization.Formatters.Binary;
  35. using System.IO;
  36.  
  37. namespace Final
  38. {
  39. public partial class Model : INotifyPropertyChanged
  40. {
  41.  
  42. public event PropertyChangedEventHandler PropertyChanged;
  43. private void OnPropertyChanged(string propertyName)
  44. {
  45. if (PropertyChanged != null)
  46. {
  47. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  48. }
  49. }
  50.  
  51.  
  52.  
  53. //public DispatcherTimer timer = new DispatcherTimer();
  54. //int count = 0;
  55.  
  56. private System.Windows.Threading.DispatcherTimer timer;
  57. public ObservableCollection<LED> LEDCollection;
  58.  
  59. //
  60. private Thread _synchWithSetter;
  61. private static UdpClient _dataSocket;
  62. private static UInt32 _localPort = 5001;
  63. private static String _localIPAddress = "127.0.0.1";
  64. private static UInt32 _remotePort = 5000;
  65. private static String _remoteIPAddress = "127.0.0.1";
  66. private bool _connected = false;
  67. private Thread _receiveDataThread;
  68. TimeDataDLL.TimeData.StructTimeData tD;
  69. TimeDataDLL.TimeData.StructTimeData alarm;
  70. private System.Windows.Threading.DispatcherTimer _elapsedTimeTimer;
  71.  
  72. //
  73.  
  74. public Model()
  75. {
  76. DateTime now = DateTime.Now;
  77. LEDCollection = new ObservableCollection<LED>();
  78. tD.hour = now.Hour;
  79. tD.minute = now.Minute;
  80. tD.second = now.Second;
  81. tD.is24HourTime = false;
  82. tD.isAlarmTime = false;
  83.  
  84. int offset = 10;
  85. for(int i = 0; i < 6; i++)
  86. {
  87. LEDCollection.Add(new LED()
  88. {
  89. LEDValue = Convert.ToUInt32(0),
  90. LEDLeft = offset,
  91. LEDTop = 50.0
  92. });
  93. offset += 50;
  94. }
  95.  
  96. display();
  97.  
  98.  
  99. try
  100. {
  101. _dataSocket = new UdpClient((int)_localPort);
  102. }
  103.  
  104. catch (Exception ex)
  105. {
  106. Debug.Write(ex.ToString());
  107. }
  108.  
  109. ThreadStart threadFunction;
  110. threadFunction = new ThreadStart(SynchWithSetter);
  111. _synchWithSetter = new Thread(threadFunction);
  112. _synchWithSetter.Start();
  113.  
  114. try
  115. {
  116. _elapsedTimeTimer = new System.Windows.Threading.DispatcherTimer();
  117. _elapsedTimeTimer.Tick += new EventHandler(elapsedTimerHandler);
  118. _elapsedTimeTimer.Interval = TimeSpan.FromMilliseconds(1000);
  119. _elapsedTimeTimer.Start();
  120. }
  121. catch
  122. {
  123. Console.WriteLine("Failed to create elapsed time timer.");
  124. }
  125. }
  126.  
  127. private void elapsedTimerHandler(object source, EventArgs e)
  128. {
  129. tD.second++;
  130. if(tD.second >= 60)
  131. {
  132. tD.second = 0;
  133. tD.minute++;
  134. if(tD.minute >= 60)
  135. {
  136. tD.minute = 0;
  137. tD.hour++;
  138. if (tD.is24HourTime)
  139. {
  140. if(tD.hour >= 25)
  141. {
  142. tD.hour = 0;
  143. }
  144. }
  145. else
  146. {
  147. if(tD.hour >= 13)
  148. {
  149. tD.hour = 1;
  150. if(AMPM == "AM")
  151. {
  152. AMPM = "PM";
  153. }
  154. else
  155. {
  156. AMPM = "AM";
  157. }
  158. }
  159. }
  160. }
  161. }
  162. display();
  163. }
  164.  
  165. private void SynchWithSetter()
  166. {
  167. Byte[] data = new Byte[1];
  168. IPEndPoint endPointSend = new IPEndPoint(IPAddress.Parse(_remoteIPAddress), (int)_remotePort);
  169. IPEndPoint endPointRecieve = new IPEndPoint(IPAddress.Any, 0);
  170.  
  171. UdpClient synchSocket = new UdpClient((int)_localPort + 10);
  172.  
  173. _dataSocket.Client.ReceiveTimeout = 1000;
  174.  
  175. while (true)
  176. {
  177. try
  178. {
  179. Console.WriteLine("Trying to connect!");
  180. synchSocket.Send(data, data.Length, endPointSend);
  181. _dataSocket.Receive(ref endPointRecieve);
  182. _connected = true;
  183. Console.WriteLine("Connected to the Setter!");
  184. break;
  185. }
  186. catch (SocketException ex)
  187. {
  188. if (ex.ErrorCode == (int)SocketError.TimedOut)
  189. {
  190. Debug.Write(ex.ToString());
  191. }
  192. else
  193. {
  194. synchSocket.Close();
  195. return;
  196. }
  197. }
  198. catch (System.ObjectDisposedException ex)
  199. {
  200. Console.WriteLine(ex.ToString());
  201. synchSocket.Close();
  202. return;
  203. }
  204.  
  205. }
  206.  
  207. synchSocket.Send(data, data.Length, endPointSend);
  208.  
  209. synchSocket.Close();
  210.  
  211. _dataSocket.Client.ReceiveTimeout = 0;
  212.  
  213. ThreadStart threadFunction = new ThreadStart(ReceiveThreadFunction);
  214. _receiveDataThread = new Thread(threadFunction);
  215. _receiveDataThread.Start();
  216. }
  217.  
  218. private void ReceiveThreadFunction()
  219. {
  220. IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
  221. while (true)
  222. {
  223. try
  224. {
  225. Byte[] receiveData = _dataSocket.Receive(ref endPoint);
  226. if (receiveData.Length < 2)
  227. continue;
  228.  
  229. TimeDataDLL.TimeData.StructTimeData tD1;
  230. BinaryFormatter formatter = new BinaryFormatter();
  231. MemoryStream stream = new MemoryStream();
  232. stream = new System.IO.MemoryStream(receiveData);
  233. tD1 = (TimeDataDLL.TimeData.StructTimeData)formatter.Deserialize(stream);
  234. if (!tD.isAlarmTime)
  235. {
  236. tD = tD1;
  237. display();
  238. }
  239. else
  240. {
  241. alarm = tD1;
  242. }
  243. }
  244. catch (SocketException ex)
  245. {
  246. Console.WriteLine(ex.ToString());
  247. return;
  248. }
  249. catch (Exception ex)
  250. {
  251. Console.Write(ex.ToString());
  252. }
  253.  
  254. }
  255. }
  256.  
  257. public void display()
  258. {
  259. LEDCollection[0].LEDValue = (uint)tD.hour / 10;
  260. LEDCollection[1].LEDValue = (uint)tD.hour % 10;
  261.  
  262. LEDCollection[2].LEDValue = (uint)tD.minute / 10;
  263. LEDCollection[3].LEDValue = (uint)tD.minute % 10;
  264.  
  265. LEDCollection[4].LEDValue = (uint)tD.second / 10;
  266. LEDCollection[5].LEDValue = (uint)tD.second % 10;
  267.  
  268. if (tD.is24HourTime)
  269. {
  270. if(tD.hour >= 12)
  271. {
  272. AMPM = "PM";
  273. }
  274. else
  275. {
  276. AMPM = "AM";
  277. }
  278. }
  279. else
  280. {
  281. AMPM = "";
  282. }
  283.  
  284. checkAlarm();
  285. }
  286.  
  287. private void checkAlarm()
  288. {
  289.  
  290. }
  291.  
  292. private String _ampm;
  293. public String AMPM
  294. {
  295. get { return _ampm; }
  296. set
  297. {
  298. _ampm = value;
  299. OnPropertyChanged("AMPM");
  300. }
  301. }
  302.  
  303. private String _alarm;
  304. public String Alarm
  305. {
  306. get { return _alarm; }
  307. set
  308. {
  309. _ampm = value;
  310. OnPropertyChanged("Alarm");
  311. }
  312. }
  313. }
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement