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

Contents of /trunk/libThermoControl/Controller.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 40 - (show annotations) (download)
Wed Aug 6 17:59:25 2014 UTC (5 years, 11 months ago) by william
File size: 5115 byte(s)

1 #define LOG_DEBUG
2
3 using System;
4 using System.Collections.Generic;
5 using System.Diagnostics;
6 using System.Linq;
7 using System.Text;
8
9 namespace libThermoControl
10 {
11 public interface IController
12 {
13 int CurrentTemp { get; }
14 int ExternalTemp { get; }
15 int DefaultTemp { get; }
16
17 void IncreaseTempBy1();
18 void DecreaseTempBy1();
19
20 int GetTemperatureAdjustment();
21 void SetToDefaultTemperature();
22 void SetTemperature(int temp);
23 void SendCommand(ACControls button);
24 void SendCommand(string device, ACControls button);
25 void SendCommand(string button);
26 void SendCommand(string device, string button);
27 }
28 public class Controller : IController
29 {
30 public Controller()
31 {
32
33 }
34 #region IController members
35
36 public void IncreaseTempBy1()
37 {
38 int c = this.CurrentTemp;
39 c += 1;
40 this.SetTemperature(c);
41 }
42 public void DecreaseTempBy1()
43 {
44 int c = this.CurrentTemp;
45 c -= 1;
46 this.SetTemperature(c);
47 }
48
49 public void SendCommand(ACControls button) { SendCommand(Configuration.THERMISTAT_DEVICE, button); }
50 public void SendCommand(string device, ACControls button) { SendCommand(device, ThermistatControls.GetButtonByValue(button).ButtonName); }
51 public void SendCommand(string button) { SendCommand(Configuration.THERMISTAT_DEVICE, button); }
52 public void SendCommand(string device, string button)
53 {
54 Blaster.BlastCommand(device, button);
55 }
56
57 public int CurrentTemp { get { return Configuration.CurrentTemp; } }
58 public int ExternalTemp { get { return Configuration.ExternalTemp; } }
59 public int DefaultTemp { get { return Configuration.DEFAULT_TEMP; } }
60 public int GetTemperatureAdjustment()
61 {
62 return Configuration.GetTemperatureAdjustment();
63 }
64
65 public void SetToDefaultTemperature()
66 {
67 //int temp_current = CurrentTemperature;
68 //int temp_default = Configuration.DEFAULT_TEMP;
69 //int temp_offset = temp_default - temp_current;
70 //LogDebugMessage("Controller.ResetTemperature():");
71 //LogDebugMessage("\tCurrent: {0}", temp_current);
72 //LogDebugMessage("\tDefault: {0}", temp_default);
73 //LogDebugMessage("\tOffset: {0}", temp_offset);
74 SetTemperature(Configuration.DEFAULT_TEMP);
75 }
76 public void SetTemperature(int temp)
77 {
78 if (temp < Configuration.MIN_TEMP || temp > Configuration.MAX_TEMP)
79 {
80 throw new ArgumentOutOfRangeException(string.Format("Cannot set temperature to: '{0}' ... Range is: '{1}' to '{2}'",temp, Configuration.MIN_TEMP, Configuration.MAX_TEMP));
81 }
82 int temp_current = Configuration.CurrentTemp;
83 int temp_offset = temp - temp_current;
84 //LogDebugMessage("Controller.SetTemperature(int temp):");
85 //LogDebugMessage("\tCurrent: {0}", temp_current);
86 //LogDebugMessage("\ttemp: {0}", temp);
87 //LogDebugMessage("\tOffset: {0}", temp_offset);
88 ChangeTemp(temp, temp_offset);
89 }
90 #endregion
91
92 private void ChangeTemp(int temp, int temp_offset)
93 {
94 LogDebugMessage("Controller.ChangeTemp(int temp, int temp_offset):");
95 LogDebugMessage("\tSetting Temperature to: {0} ({1}{2})", temp, this.CurrentTemp, Math.Sign(temp_offset) == -1 ? temp_offset.ToString() : string.Format("+{0}", temp_offset));
96 if (OffsetIsNegative(temp_offset))
97 {
98 //LogDebugMessage("\tOffset is Negative");
99 for (int i = 0; i < Math.Abs(temp_offset); i++)
100 {
101 //System.Hardware.IR.Blaster.LMRemoteKeyMap.Blast(Configuration.THERMISTAT_DEVICE, button);
102 SendCommand(ACControls.TEMP_TIMER_DOWN);
103 }
104 }
105 else
106 {
107 //LogDebugMessage("\tOffset is Positive");
108 for (int i = 0; i < Math.Abs(temp_offset); i++)
109 {
110 //System.Hardware.IR.Blaster.LMRemoteKeyMap.Blast(Configuration.THERMISTAT_DEVICE, button);
111 SendCommand(ACControls.TEMP_TIMER_UP);
112 }
113 }
114 Configuration.CurrentTemp = temp;
115 }
116
117 private bool OffsetIsNegative(int temp_offset)
118 {
119 return (Math.Abs(temp_offset) != temp_offset);
120 }
121
122 [Conditional("LOG_DEBUG")]
123 private void LogDebugMessage(string format)
124 {
125 LogDebugMessage(format, new object[0]);
126 }
127 [Conditional("LOG_DEBUG")]
128 private void LogDebugMessage(string format, params object[] args)
129 {
130 Console.WriteLine(format, args);
131 }
132 }
133 }

  ViewVC Help
Powered by ViewVC 1.1.22