/[ThermoControl]/trunk/libThermoControl/ACState.cs
ViewVC logotype

Contents of /trunk/libThermoControl/ACState.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 61 - (show annotations) (download)
Thu Aug 7 07:56:35 2014 UTC (5 years, 10 months ago) by william
File size: 27203 byte(s)

1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.IO;
5 using System.Linq;
6 using System.Runtime.Serialization;
7 using System.Runtime.Serialization.Formatters.Binary;
8 using System.Text;
9
10 namespace libThermoControl
11 {
12 public enum PowerState
13 {
14 ON,
15 OFF,
16 }
17 public enum SleepState
18 {
19 OFF,
20 ON,
21 }
22 public enum TimerState
23 {
24 OFF,
25 ON,
26 }
27 public enum FanModeState
28 {
29 COOL,
30 ENERGYSAVE,
31 FANONLY,
32 }
33 public enum FanSpeedState
34 {
35 AUTO,
36 LOW,
37 MEDIUM,
38 HIGH,
39 RESET,
40 }
41 public enum FanOnlySpeedState
42 {
43 LOW,
44 MEDIUM,
45 HIGH,
46 RESET,
47 }
48 #region Range class
49 public interface IRange<T>
50 {
51 T Minimum { get; }
52 T Maximum { get; }
53 string ToString();
54 }
55 [Serializable]
56 public class Range<T> : IRange<T>
57 {
58 public Range(): this(default(T), default(T)) { }
59 public Range(T minimum, T maximum) { this.Minimum = minimum; this.Maximum = maximum; }
60 #region IRange<T> members
61 public T Minimum { get; private set; }
62 public T Maximum { get; private set; }
63 public override string ToString()
64 {
65 return string.Format("min: {0} max: {1}", Minimum.ToString(), Maximum.ToString());
66 }
67 #endregion
68 }
69 #endregion
70
71 #region Range class
72 public interface ISegmentedRange<T>
73 {
74 int IndexOf(T value);
75 T GetAtIndex(int index);
76 T Minimum { get; }
77 T Maximum { get; }
78 bool Contains(T value);
79 T GetClosestValue(T value, out int index);
80 string ToString();
81 }
82 [Serializable]
83 public class SegmentedRange<T> : ISegmentedRange<T>
84 where T: IConvertible
85 {
86 public SegmentedRange() : this(default(T)) { }
87 public SegmentedRange(params T[] values)
88 {
89 if (values == null || values.Length == 0)
90 {
91 throw new InvalidOperationException("values cannot be null or an empty array");
92 }
93 this.values = new Dictionary<int, T>();
94 int count = 0;
95 foreach (var value in values)
96 {
97 this.values.Add(count, value);
98 count++;
99 }
100 }
101 #region ISegmentedRange<T> members
102 private Dictionary<int, T> values = new Dictionary<int, T>();
103
104 public int IndexOf(T value)
105 {
106 return this.values.Values.ToList().IndexOf(value);
107 }
108 public T GetAtIndex(int index)
109 {
110 T value = default(T);
111 value = this.values[index];
112 return value;
113 }
114 public T Minimum { get; private set; }
115 public T Maximum { get; private set; }
116 public bool Contains(T value) { return this.values.Values.Contains(value); }
117 public T GetClosestValue(T value, out int index)
118 {
119 index = 0;
120 if (!this.Contains(value))
121 {
122 List<T> list = this.values.Values.ToList();
123
124 //list.OrderBy(d => Math.Abs(Convert.ToInt64(d) - Convert.ToInt64(value))).ElementAt(0);
125 var c = list.OrderBy(d => Math.Abs(Convert.ToSingle(d) - Convert.ToSingle(value))).ToList();
126
127 if (c.Count == 0)
128 {
129 throw new InvalidOperationException(string.Format("Unable to find a valid/approxiamge value for: {0}", value.ToString()));
130 }
131 else
132 {
133 var t = c.ElementAt(0);
134 if (t == null)
135 {
136 throw new InvalidOperationException(string.Format("Unable to find a valid/approxiamge value for: {0}", value.ToString()));
137 }
138 else
139 {
140 index = this.values.Values.ToList().IndexOf(t);
141 return t;
142 }
143 }
144 }
145 else
146 {
147 index = this.values.Values.ToList().IndexOf(value);
148 return value;
149 }
150 }
151 public override string ToString()
152 {
153 return base.ToString();
154 }
155 #endregion
156 }
157 #endregion
158 public interface IACState
159 {
160 string ThermistatDevice { get; }
161 string WeatherUrl { get; }
162 PowerState PowerState { get; }
163 SleepState SleepState { get; }
164 TimerState TimerState { get; }
165 FanModeState FanMode { get; }
166 FanSpeedState FanSpeed { get; }
167 FanOnlySpeedState FanOnlySpeed { get; }
168 int TempValue { get; }
169 float TimerValue { get; }
170
171 bool IsFanOnly();
172 }
173 [Serializable]
174 public class ACState : IACState
175 {
176 #region default constants
177
178 const string STATE_FILE = "ACSTATE.bin";
179
180 const string DEFAULT_THERMISTAT_DEVICE = "AC_THERMOSTAT";
181 const string DEFAULT_WEATHER_URL = "http://www.accuweather.com/en/us/chattanooga-tn/37421/current-weather/331086";
182 const PowerState DEFAULT_POWER_STATE = PowerState.ON;
183 const SleepState DEFAULT_SLEEP_STATE = SleepState.OFF;
184 const TimerState DEFAULT_TIMER_STATE = TimerState.OFF;
185 const FanModeState DEFAULT_FAN_MDOE = FanModeState.COOL;
186 const FanSpeedState DEFAULT_FAN_SPEED = FanSpeedState.AUTO;
187 const FanOnlySpeedState DEFAULT_FANONLY_SPEED = FanOnlySpeedState.LOW;
188 const int DEFAULT_TEMP_VALUE = 70;
189 const float DEFAULT_TIMER_VALUE = 0.5f;
190
191 #region valid value ranges
192 static readonly Range<int> VALID_TEMP_RANGE = new Range<int>(60, 90);
193 static readonly SegmentedRange<float> VALID_TIMER_RANGE =
194 new SegmentedRange<float>(0.50f, 1.00f, 1.50f, 2.00f, 2.50f, 3.00f, 3.50f,
195 4.00f, 4.50f, 5.00f, 5.50f, 6.00f, 6.50f, 7.00f,
196 7.50f, 8.00f, 8.50f, 9.00f, 9.50f, 10.0f, 11.0f,
197 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f,
198 19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f);
199 #endregion
200
201 #endregion
202 public ACState()
203 {
204 _WeatherUrl = DEFAULT_WEATHER_URL;
205 _ThermistatDevice = DEFAULT_THERMISTAT_DEVICE;
206 _PowerState = DEFAULT_POWER_STATE;
207 _SleepState = DEFAULT_SLEEP_STATE;
208 _TimerState = DEFAULT_TIMER_STATE;
209 _FanMode = DEFAULT_FAN_MDOE;
210 _FanSpeed = DEFAULT_FAN_SPEED;
211 _FanOnlySpeed = DEFAULT_FANONLY_SPEED;
212 _TempValue = DEFAULT_TEMP_VALUE;
213 _TimerValue = DEFAULT_TIMER_VALUE;
214 }
215 #region IACState members
216
217 private string _WeatherUrl = DEFAULT_WEATHER_URL;
218 private string _ThermistatDevice = DEFAULT_THERMISTAT_DEVICE;
219 private PowerState _PowerState = DEFAULT_POWER_STATE;
220 private SleepState _SleepState = DEFAULT_SLEEP_STATE;
221 private TimerState _TimerState = DEFAULT_TIMER_STATE;
222 private FanModeState _FanMode = DEFAULT_FAN_MDOE;
223 private FanSpeedState _FanSpeed = DEFAULT_FAN_SPEED;
224 private FanOnlySpeedState _FanOnlySpeed = DEFAULT_FANONLY_SPEED;
225
226 private int _TempValue = DEFAULT_TEMP_VALUE;
227 private float _TimerValue = DEFAULT_TIMER_VALUE;
228
229
230 public bool IsFanOnly() { return this.FanMode == FanModeState.FANONLY; }
231 public bool IsTimerActive() { return this.TimerState == libThermoControl.TimerState.ON; }
232
233 public string WeatherUrl { get { return _WeatherUrl; } internal set { if (_WeatherUrl != value) { OnWeatherUrlChanged(_WeatherUrl,value); } } }
234 public string ThermistatDevice { get { return _ThermistatDevice; } internal set { if (_ThermistatDevice != value) { OnThermistateDeviceChanged(_ThermistatDevice,value); } } }
235 public PowerState PowerState { get { return _PowerState; } internal set { if (_PowerState != value) { OnPowerStateChanged(_PowerState, value); } } }
236 public SleepState SleepState { get { return _SleepState; } internal set { if (_SleepState != value || !IsDeviceOn()) { OnSleepStateChanged(_SleepState, value); } } }
237 public TimerState TimerState { get { return _TimerState; } internal set { if (_TimerState != value || !IsDeviceOn()) { OnTimerStateChanged(_TimerState, value); } } }
238 public FanModeState FanMode { get { return _FanMode; } internal set { if (_FanMode != value || !IsDeviceOn()) { OnFanModeChanged(_FanMode, value); } } }
239 public FanSpeedState FanSpeed { get { return _FanSpeed; } internal set { if (_FanSpeed != value || !IsDeviceOn()) { OnFanSpeedChanged(_FanSpeed, value); } } }
240 public FanOnlySpeedState FanOnlySpeed { get { return _FanOnlySpeed; } internal set { if (_FanOnlySpeed != value || !IsDeviceOn()) { OnFanOnlySpeedChanged(_FanOnlySpeed, value); } } }
241 public int TempValue { get { return _TempValue; } internal set { if (_TempValue != value || !IsDeviceOn()) { OnTempValueChanged(_TempValue, value); } } }
242 public float TimerValue { get { return _TimerValue; } internal set { if (_TimerValue != value || !IsDeviceOn()) { OnTimerValueChanged(_TimerValue, value); } } }
243 #endregion
244
245 [Conditional("CONFIG_UNITTEST")]
246 public static void UnitTest()
247 {
248 }
249
250 private bool IsDeviceOn() { return PowerState == libThermoControl.PowerState.ON; }
251
252 private void ExecuteCommand(ACControls control)
253 {
254 if (!IsDeviceOn()) { _PowerState = libThermoControl.PowerState.ON; Blaster.BlastCommand(ThermistatDevice, ACControls.POWER.ToString()); }
255 Blaster.BlastCommand(ThermistatDevice, control.ToString());
256 }
257 private void OnValueChanged()
258 {
259 ACState.Save(this);
260 }
261 #region state change methods
262
263 #region
264 private void DoPowerOnEvents()
265 {
266 if (this.PowerState == libThermoControl.PowerState.ON)
267 {
268 if (this.TimerState == libThermoControl.TimerState.ON)
269 {
270 // timer is off by default when unit is powered-on
271 ExecuteCommand(ACControls.TIMER);
272 // update timer value to the current value
273 var current_timer_value = this.TimerValue;
274 _TimerValue = DEFAULT_TIMER_VALUE;
275 this.TimerValue = current_timer_value;
276 }
277 if (this.SleepState == libThermoControl.SleepState.ON)
278 {
279 // sleep is off by default when unit is powered-on
280 ExecuteCommand(ACControls.SLEEP);
281 }
282 }
283 }
284 #endregion
285
286 #region private void OnWeatherUrlChanged(string old_value, string new_value)
287 private void OnWeatherUrlChanged(string old_value, string new_value)
288 {
289 _WeatherUrl = new_value;
290 OnValueChanged();
291 }
292 #endregion
293 #region private void OnThermistateDeviceChanged(string old_value, string new_value)
294 private void OnThermistateDeviceChanged(string old_value, string new_value)
295 {
296 _ThermistatDevice = new_value;
297 OnValueChanged();
298 }
299 #endregion
300 #region private void OnPowerStateChanged(PowerState old_value, PowerState new_value)
301 private void OnPowerStateChanged(PowerState old_value, PowerState new_value)
302 {
303 ExecuteCommand(ACControls.POWER);
304 if (old_value == libThermoControl.PowerState.OFF && new_value == libThermoControl.PowerState.ON)
305 {
306 DoPowerOnEvents();
307 }
308 _PowerState = new_value;
309 OnValueChanged();
310 }
311 #endregion
312 #region private void OnSleepStateChanged(SleepState old_value, SleepState new_value)
313 private void OnSleepStateChanged(SleepState old_value, SleepState new_value)
314 {
315 ExecuteCommand(ACControls.SLEEP);
316 _SleepState = new_value;
317 OnValueChanged();
318 }
319 #endregion
320 #region private void OnTimerStateChanged(TimerState old_value, TimerState new_value)
321 private void OnTimerStateChanged(TimerState old_value, TimerState new_value)
322 {
323 ExecuteCommand(ACControls.TIMER);
324 _TimerState = new_value;
325 OnValueChanged();
326 }
327 #endregion
328 #region private void OnFanModeChanged(FanModeState old_value, FanModeState new_value)
329 private void OnFanModeChanged(FanModeState old_value, FanModeState new_value)
330 {
331 switch (new_value)
332 {
333 case FanModeState.COOL:
334 ExecuteCommand(ACControls.COOL);
335 _FanMode = new_value;
336 break;
337 case FanModeState.ENERGYSAVE:
338 ExecuteCommand(ACControls.ENERGY_SAVE);
339 _FanMode = new_value;
340 break;
341 case FanModeState.FANONLY:
342 ExecuteCommand(ACControls.FAN_ONLY);
343 _FanMode = new_value;
344 break;
345 default:
346 Console.WriteLine("Unknown fan mode: {0}", new_value.ToString());
347 _FanMode = old_value;
348 break;
349 }
350 if (this.FanMode == FanModeState.FANONLY && new_value == FanModeState.FANONLY)
351 {
352 var current_fanonly_speed = this.FanOnlySpeed;
353 _FanOnlySpeed = FanOnlySpeedState.RESET;
354 this.FanOnlySpeed = current_fanonly_speed;
355 }
356 if (this.FanMode != FanModeState.FANONLY && new_value != FanModeState.FANONLY)
357 {
358 var current_fan_speed = this.FanSpeed;
359 _FanSpeed = FanSpeedState.RESET;
360 this.FanSpeed = current_fan_speed;
361 }
362 OnValueChanged();
363 }
364 #endregion
365 #region private void OnFanSpeedChanged(FanSpeedState old_value, FanSpeedState new_value)
366 private void OnFanSpeedChanged(FanSpeedState old_value, FanSpeedState new_value)
367 {
368 if (old_value == new_value && this.FanMode == FanModeState.FANONLY)
369 {
370 return;
371 }
372 if( (this.FanMode != FanModeState.COOL && this.FanMode != FanModeState.ENERGYSAVE))
373 {
374 return;
375 }
376
377
378 switch (new_value)
379 {
380 case FanSpeedState.AUTO:
381 ExecuteCommand(ACControls.AUTO_FAN);
382 _FanSpeed = new_value;
383 break;
384 case FanSpeedState.LOW:
385 if (old_value != FanSpeedState.AUTO) { ExecuteCommand(ACControls.AUTO_FAN); }
386 // from AUTO_MODE, send FAN_SPEED_DOWN twice
387 for (int i = 0; i < 3; i++) { ExecuteCommand(ACControls.FAN_SPEED_DOWN); }
388 _FanSpeed = new_value;
389 break;
390 case FanSpeedState.MEDIUM:
391 if (old_value != FanSpeedState.AUTO) { ExecuteCommand(ACControls.AUTO_FAN); }
392 // from AUTO_MODE, send FAN_SPEED_DOWN once
393 for (int i = 0; i < 3; i++) { ExecuteCommand(ACControls.FAN_SPEED_DOWN); }
394 for (int i = 0; i < 1; i++) { ExecuteCommand(ACControls.FAN_SPEED_UP); }
395 _FanSpeed = new_value;
396 break;
397 case FanSpeedState.HIGH:
398 if (old_value != FanSpeedState.AUTO) { ExecuteCommand(ACControls.AUTO_FAN); }
399 // from AUTO_MODE, send FAN_SPEED_DOWN twice, then FAN_SPEED_UP once
400 for (int i = 0; i < 3; i++) { ExecuteCommand(ACControls.FAN_SPEED_DOWN); }
401 for (int i = 0; i < 2; i++) { ExecuteCommand(ACControls.FAN_SPEED_UP); }
402 _FanSpeed = new_value;
403 break;
404 default:
405 Console.WriteLine("Unknown fan speed: {0}", new_value.ToString());
406 break;
407 }
408
409 OnValueChanged();
410 }
411 #endregion
412 #region private void OnFanOnlySpeedChanged(FanSpeedState old_value, FanSpeedState new_value)
413 private void OnFanOnlySpeedChanged(FanOnlySpeedState old_value, FanOnlySpeedState new_value)
414 {
415 if (old_value == new_value && this.FanMode != FanModeState.FANONLY)
416 {
417 return;
418 }
419 else
420 {
421 // get the old_value
422 if (old_value == FanOnlySpeedState.RESET)
423 {
424 switch (this.FanSpeed)
425 {
426 case FanSpeedState.AUTO: old_value = FanOnlySpeedState.LOW; break;
427 case FanSpeedState.LOW: old_value = FanOnlySpeedState.LOW; break;
428 case FanSpeedState.MEDIUM: old_value = FanOnlySpeedState.MEDIUM; break;
429 case FanSpeedState.HIGH: old_value = FanOnlySpeedState.HIGH; break;
430 }
431 }
432 switch (old_value)
433 {
434 case FanOnlySpeedState.LOW:
435 switch (new_value)
436 {
437 #region FROM LOW to MED or HIGH
438 case FanOnlySpeedState.MEDIUM: for (int i = 0; i < 1; i++) { ExecuteCommand(ACControls.FAN_SPEED_UP); } break;
439 case FanOnlySpeedState.HIGH: for (int i = 0; i < 2; i++) { ExecuteCommand(ACControls.FAN_SPEED_UP); } break;
440 #endregion
441 }
442 break;
443 case FanOnlySpeedState.MEDIUM:
444 switch (new_value)
445 {
446 #region FROM MED to LOW or HIGH
447 case FanOnlySpeedState.LOW: for (int i = 0; i < 1; i++) { ExecuteCommand(ACControls.FAN_SPEED_DOWN); } break;
448 case FanOnlySpeedState.HIGH: for (int i = 0; i < 1; i++) { ExecuteCommand(ACControls.FAN_SPEED_UP); } break;
449 #endregion
450 }
451 break;
452 case FanOnlySpeedState.HIGH:
453 switch (new_value)
454 {
455 #region FROM HIGH to LOW or MED
456 case FanOnlySpeedState.LOW: for (int i = 0; i < 2; i++) { ExecuteCommand(ACControls.FAN_SPEED_DOWN); } break;
457 case FanOnlySpeedState.MEDIUM: for (int i = 0; i < 1; i++) { ExecuteCommand(ACControls.FAN_SPEED_DOWN); } break;
458 #endregion
459 }
460 break;
461 }
462 _FanOnlySpeed = new_value;
463 }
464 OnValueChanged();
465 }
466 #endregion
467 #region private void OnTempValueChanged(int old_value, int new_value)
468 private void OnTempValueChanged(int old_value, int new_value)
469 {
470 if (this.TimerState == TimerState.OFF && this.FanMode != FanModeState.FANONLY)
471 {
472 // check that the new value is within the pre-defined range
473 if (new_value < VALID_TEMP_RANGE.Minimum || new_value > VALID_TEMP_RANGE.Maximum)
474 {
475 Console.WriteLine("Temperature value: {0} is out of range ({1}-{2})...using old value: {3}", new_value, VALID_TEMP_RANGE.Minimum, VALID_TEMP_RANGE.Maximum ,old_value);
476 new_value = old_value;
477 }
478
479
480 var t = new_value - old_value;
481 _TempValue = new_value;
482
483 if (Math.Sign(t) == -1)
484 {
485 // negative offset
486 var abs_t = Math.Abs(t);
487 for (int i = 0; i < abs_t; i++)
488 {
489 ExecuteCommand(ACControls.TEMP_TIMER_DOWN);
490 }
491 }
492 else
493 {
494 for (int i = 0; i < t; i++)
495 {
496 ExecuteCommand(ACControls.TEMP_TIMER_UP);
497 }
498 }
499
500 OnValueChanged();
501 }
502 else
503 {
504 if (this.FanMode == FanModeState.FANONLY)
505 {
506 Console.WriteLine("Changing the temperature has no effect when FanMode is not COOL or ENERGYSAVE");
507 return;
508 }
509 if (this.TimerState == libThermoControl.TimerState.ON)
510 {
511 // must turn time mode off
512 this.TimerState = TimerState.OFF;
513 // set the timer value
514 OnTempValueChanged(old_value, new_value);
515 // turn timer mode on (since it was active)
516 this.TimerState = TimerState.ON;
517 OnTimerValueChanged(this.TimerValue,-1);
518 }
519 }
520 }
521 #endregion
522 #region private void OnTimerValueChanged(float old_value, float new_value)
523 private void OnTimerValueChanged(float old_value, float new_value)
524 {
525 bool reset_value = false;
526 if (new_value == -1)
527 {
528 // -1 is a reset flag to restore the old_value between timer actication
529 new_value = old_value;
530 reset_value = true;
531 }
532 if (this.TimerState == TimerState.ON)
533 {
534
535 int new_index = 0;
536 var closet_value = VALID_TIMER_RANGE.GetClosestValue(new_value, out new_index);
537
538 if (closet_value != new_value)
539 {
540 Console.WriteLine("Timer value: {0} was not valid...using the closest value: {1}", new_value, closet_value);
541 new_value = closet_value;
542 }
543
544 var old_index = VALID_TIMER_RANGE.IndexOf(old_value);
545 var t = new_index - old_index;
546 _TimerValue = new_value;
547
548 if (t == 0 && reset_value)
549 {
550 t = old_index;
551 }
552
553 if (Math.Sign(t) == -1)
554 {
555 // negative offset
556 var abs_t = Math.Abs(t);
557 for (int i = 0; i < abs_t; i++)
558 {
559 ExecuteCommand(ACControls.TEMP_TIMER_DOWN);
560 }
561 }
562 else
563 {
564 for (int i = 0; i < t; i++)
565 {
566 ExecuteCommand(ACControls.TEMP_TIMER_UP);
567 }
568 }
569 OnValueChanged();
570 }
571 else
572 {
573 Console.WriteLine("Timer mode is not active -- cannot change timer value");
574 //// must turn time mode on
575 //this.TimerState = TimerState.ON;
576 //// set the timer value
577 //OnTimerValueChanged(old_value, new_value);
578 //// turn timer mode off (since it was not actived)
579 ////this.TimerState = TimerState.OFF;
580 //// don't turn the timer back-off - otherwise it will reset back to the default value
581 }
582 }
583 #endregion
584 #endregion
585
586 #region internal methods
587 internal void SetDefaultTemp()
588 {
589 this.TempValue = DEFAULT_TEMP_VALUE;
590 }
591 internal void SetDefaultTimer()
592 {
593 this.TimerValue = DEFAULT_TIMER_VALUE;
594 }
595 internal void Reset()
596 {
597 this.WeatherUrl = DEFAULT_WEATHER_URL;
598 this.ThermistatDevice = DEFAULT_THERMISTAT_DEVICE;
599 this.PowerState = DEFAULT_POWER_STATE;
600 this.SleepState = DEFAULT_SLEEP_STATE;
601 this.TimerState = DEFAULT_TIMER_STATE;
602 this.FanMode = DEFAULT_FAN_MDOE;
603 this.FanSpeed = DEFAULT_FAN_SPEED;
604 this.FanOnlySpeed = DEFAULT_FANONLY_SPEED;
605 this.TempValue = DEFAULT_TEMP_VALUE;
606 this.TimerValue = DEFAULT_TIMER_VALUE;
607 }
608 #endregion
609
610 #region static members
611 internal static bool Load(out ACState loaded_state)
612 {
613 loaded_state = new ACState();
614 return loaded_state.DeSerialize(STATE_FILE, out loaded_state);
615 }
616 internal static bool Save(ACState state)
617 {
618 return state.Serialize(STATE_FILE);
619 }
620 #endregion
621
622 private bool DeSerialize(string filename, out ACState state)
623 {
624 state = new ACState();
625
626 FileInfo fi = new FileInfo(filename);
627 if (!fi.Exists)
628 {
629 return false;
630 }
631 using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
632 {
633 BinaryFormatter formatter = new BinaryFormatter();
634 try
635 {
636 state = (ACState)formatter.Deserialize(fs);
637 }
638 catch (SerializationException ex)
639 {
640 Debug.WriteLine(ex.ToString());
641 return false;
642 }
643 finally { fs.Close(); }
644 }
645 return true;
646 }
647 private bool Serialize(string filename)
648 {
649 using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
650 {
651 BinaryFormatter formatter = new BinaryFormatter();
652 try
653 {
654 formatter.Serialize(fs, this);
655 }
656 catch (SerializationException ex)
657 {
658 Debug.WriteLine(ex.ToString());
659 return false;
660 }
661 finally { fs.Close(); }
662 }
663 return true;
664 }
665
666 }
667 }
668

  ViewVC Help
Powered by ViewVC 1.1.22