Avionics
Dropship Simulator
CAS.cpp
Go to the documentation of this file.
1 #include "Module.h"
2 #include <algorithm> // for std::sort
3 
4 
5 
6 void Cas::FrameMove(float fElapsed)
7 {
9  bool oldpowered = powered;
10  powered = *casGetFloatPtr > 0;
11  if (!oldpowered) // was unpowered
12  {
13  if (powered) // now powered, make it look like it was just switched on!
14  {
15  secsincelast = -2.0f; // 2 second intro delay
16  }
17  }
18  else
19  {
20  if (secsincelast < 0.0f) // 2 second power-up delay
21  {
22  secsincelast += fElapsed;
23  if (secsincelast >= 0.0f)
24  {
25  secsincelast = 2.0f;
26  UnacknowledgeAllMessages(); // reset on power-up
27  secsincereset = 0.0f; // will delay sending emails for 2 seconds
28  }
29  }
30  else
31  {
32  secsincelast += fElapsed;
33  }
34  }
35 
36  secsincereset += fElapsed;
37  if (secsincereset > 500.0f)
38  secsincereset = 500.0f;
39 
41 
42  for (UINT i = 0; i < bus->commandStream.size(); i++)
43  {
44  Command command = bus->commandStream.at(i);
45  if (command.delay != 0.0f) continue;
46 
47  if (command.name == "AcknowledgeAllMessages")
48  {
50  bus->commandStream.erase(bus->commandStream.begin() + i);
51  }
52  }
53 
54 
55 #pragma region Watches
56  for (UINT i = 0; i < watches.size(); i++)
57  {
58  if (!watches.at(i).currentState)
59  watches.at(i).currentState = bus->GetComponentCurrentStatePtr(watches.at(i).guid);
60  if (!watches.at(i).faultState)
61  watches.at(i).faultState = bus->GetComponentFaultStatePtr(watches.at(i).guid);
62 
63  bool removedOne = false, addedOne = false, breakOut = false;
64  for (UINT j = 0; j < watches.at(i).conditions.size(); j++)
65  {
66  watches.at(i).conditions.at(j).secondsSinceLast += fElapsed;
67  if (!addedOne && watches.at(i).conditions.at(j).fault.is_set() && watches.at(i).conditions.at(j).fault.get() & *watches.at(i).faultState)
68  {
69  if (!watches.at(i).conditions.at(j).messageOnStack)
70  {
71  AddMessage(watches.at(i).conditions.at(j).message, watches.at(i).conditions.at(j).level, watches.at(i).sendEmail);
72  watches.at(i).conditions.at(j).messageOnStack = true;
73  breakOut = true;
74  }
75  addedOne = true;
76  }
77  else if (!addedOne && watches.at(i).conditions.at(j).below.is_set() && *watches.at(i).currentState < watches.at(i).conditions.at(j).below)
78  {
79  bool reTrip = watches.at(i).conditions.at(j).repeatBelow.is_set() && *watches.at(i).currentState < watches.at(i).conditions.at(j).repeatBelow && watches.at(i).conditions.at(j).secondsSinceLast > 60.0f;
80  if (!watches.at(i).conditions.at(j).messageOnStack || reTrip)
81  {
82  AddMessage(watches.at(i).conditions.at(j).message, watches.at(i).conditions.at(j).level, watches.at(i).sendEmail);
83  watches.at(i).conditions.at(j).secondsSinceLast = 0.0f;
84  watches.at(i).conditions.at(j).messageOnStack = true;
85  breakOut = true;
86  }
87  addedOne = true;
88  }
89  else if (!addedOne && watches.at(i).conditions.at(j).above.is_set() && *watches.at(i).currentState > watches.at(i).conditions.at(j).above)
90  {
91  bool reTrip = watches.at(i).conditions.at(j).repeatAbove.is_set() && *watches.at(i).currentState > watches.at(i).conditions.at(j).repeatAbove && watches.at(i).conditions.at(j).secondsSinceLast > 60.0f;
92  if (!watches.at(i).conditions.at(j).messageOnStack || reTrip)
93  {
94  AddMessage(watches.at(i).conditions.at(j).message, watches.at(i).conditions.at(j).level, watches.at(i).sendEmail);
95  watches.at(i).conditions.at(j).secondsSinceLast = 0.0f;
96  watches.at(i).conditions.at(j).messageOnStack = true;
97  breakOut = true;
98  }
99  addedOne = true;
100  }
101  else if (addedOne || watches.at(i).conditions.at(j).messageOnStack)
102  {
103  breakOut = RemoveMessage(watches.at(i).conditions.at(j).message);
104  watches.at(i).conditions.at(j).messageOnStack = false;
105  removedOne = true;
106  }
107  }
108 
109  if (!addedOne && removedOne && !watches.at(i).onClear.empty())
110  {
111  bool quiet = false;
112  if (watches.at(i).quietStart.is_set() && watches.at(i).quietEnd.is_set())
113  {
114  SYSTEMTIME st;
115  GetLocalTime(&st);
116  if (st.wHour > watches.at(i).quietStart.get().wHour || (st.wHour == watches.at(i).quietStart.get().wHour && st.wMinute >= watches.at(i).quietStart.get().wMinute))
117  quiet = true;
118  if (st.wHour < watches.at(i).quietEnd.get().wHour || (st.wHour == watches.at(i).quietEnd.get().wHour && st.wMinute < watches.at(i).quietEnd.get().wMinute))
119  quiet = true;
120  }
121  if (!quiet && watches.at(i).sendEmail)
122  {
123  Command command;
124  command.name = "SendEmail";
125  strcpy_s(command.svalue, 64, watches.at(i).onClear.c_str());
126  bus->commandStream.push_back(command);
127  }
128  else
129  {
130  char msg[199];
131  sprintf_s(msg, 199, "Cas::FrameMove Did not send clear message due to quiet time: %s", watches.at(i).onClear.c_str());
132  logger->Log(msg);
133  }
134  }
135  if (breakOut) break;
136  }
137 #pragma endregion
138 
139 
141  bus->MasterWarning = false;
142  for (UINT i = 0; i < bus->messages.size(); i++)
143  {
144  if (!bus->messages.at(i).acknowledged)
145  {
146  if (bus->messages.at(i).level == Advisory)
147  {
149  if ((bus->ProgramTime - bus->messages.at(i).time) > 5.0f)
150  {
151  bus->messages.at(i).acknowledged = true;
152  }
153  }
154  if (bus->messages.at(i).level == Warning)
155  {
156  bus->MasterWarning = true;
157  }
158  }
159  }
160 }
161 
162 Cas::Cas(Bus* prmBus, Logger* prmLogger, std::vector<Watch> prmWatches, float* prmGetFloatPtr) : Module(prmBus)
163 {
164  logger = prmLogger;
165  watches = prmWatches;
166  casGetFloatPtr = prmGetFloatPtr;
167 }
168 
170 {
171  for (UINT i = 0; i < bus->messages.size(); i++)
172  {
173  bus->messages.at(i).acknowledged = true;
174  }
175  logger->Log("CAS::AcknowledgeAllMessages Completed!");
176 }
177 
178 bool Cas::IsMessageOnStack(std::wstring text, bool renew) const
179 {
180  for (UINT i = 0; i < bus->messages.size(); i++)
181  {
182  if (bus->messages.at(i).text == text)
183  {
184  if (renew)
185  {
186  bus->messages.at(i).acknowledged = false; // trip again
187  bus->messages.at(i).time = bus->ProgramTime;
188  char msg[99];
189  sprintf_s(msg, 99, "CAS::IsMessageOnStack Retripped: %S", bus->messages.at(i).text.c_str());
190  logger->Log(msg);
191 
193  std::sort(bus->messages.begin(), bus->messages.end());
194  }
195  return true;
196  }
197  }
198  return false;
199 }
200 
201 bool Cas::RemoveMessage(std::wstring text) const
202 {
203  for (UINT i = 0; i < bus->messages.size(); i++)
204  {
205  if (bus->messages.at(i).text == text)
206  {
207  bus->messages.erase(bus->messages.begin() + i);
208  char msg[99];
209  sprintf_s(msg, 99, "CAS::RemoveMessage Removed: %S", text.c_str());
210  logger->Log(msg);
211  return true;
212  }
213  }
214  return false;
215 }
216 
217 void Cas::AddMessage(std::wstring text, MessageLevel level, bool sendEmail)
218 {
219  if (level == Environmental && sendEmail && secsincereset >= 2.0f)
220  {
221  Command command;
222  command.name = "SendEmail";
223  sprintf_s(command.svalue, 64, "%S", text.c_str());
224  bus->commandStream.push_back(command);
225  }
226 
227  if (!IsMessageOnStack(text, true))
228  {
229  Message message;
230  message.acknowledged = false;
231  message.text = text;
232  message.level = level;
233  message.time = bus->ProgramTime;
234  bus->messages.push_back(message);
235  char msg[99];
236  sprintf_s(msg, 99, "CAS::AddMessage Added: %S (%i)", text.c_str(), level);
237  logger->Log(msg);
238 
240  std::sort(bus->messages.begin(), bus->messages.end());
241 
242  if (secsincelast >= 2.0f) // this or VMU?
243  {
244  if (level == Warning)
245  {
246  Command newCommand;
247  newCommand.name = "CASMasterWarningChime";
248  bus->commandStream.push_back(newCommand);
249  logger->Log("CAS::AddMessage MasterWarningChime requested!");
250  secsincelast = 0.0f;
251  return;
252  }
253  if (level == Environmental || level == Caution)
254  {
255  Command newCommand;
256  newCommand.name = "CASMasterCautionChime";
257  bus->commandStream.push_back(newCommand);
258  logger->Log("CAS::AddMessage MasterCautionChime requested!");
259  secsincelast = 0.0f;
260  }
261  }
262  }
263 }
264 
266 {
267  bus->messages.clear();
268 
269  for (UINT w = 0; w < watches.size(); w++)
270  {
271  for (UINT c = 0; c < watches.at(w).conditions.size(); c++)
272  {
273  watches.at(w).conditions.at(c).messageOnStack = false;
274  }
275  }
276 
277  logger->Log("CAS::UnacknowledgeAllMessages Completed!");
278 }
std::vector< Message > messages
Definition: Bus.h:23
void UnacknowledgeAllMessages()
Definition: CAS.cpp:265
double ProgramTime
Identified a need for these in modules.
Definition: Bus.h:289
std::vector< Command > commandStream
Definition: Bus.h:20
Definition: Logger.h:5
Systems::Fault * GetComponentFaultStatePtr(std::string guidStr)
Definition: Bus.cpp:141
Cas(Bus *prmBus, Logger *prmLogger, std::vector< Watch > watches, float *prmCasGetFloatPtr)
Definition: CAS.cpp:162
bool IsMessageOnStack(std::wstring text, bool renew) const
Definition: CAS.cpp:178
char svalue[64]
Definition: Command.h:24
void FrameMove(float fElapsedTime) override
Definition: CAS.cpp:6
Definition: Message.h:8
MessageLevel level
Definition: Message.h:18
Definition: Bus.h:12
std::string name
command name
Definition: Command.h:11
std::vector< Watch > watches
Definition: Module.h:156
bool acknowledged
Definition: Message.h:19
bool RemoveMessage(std::wstring text) const
Definition: CAS.cpp:201
Abstract base class for modules By definition, instruments don&#39;t do any of the work (they don&#39;t modif...
Definition: Module.h:11
float * casGetFloatPtr
Definition: Module.h:158
Definition: Command.h:5
bool MasterWarning
Definition: Bus.h:148
void Log(const char *msg, Level level=Info, int errorCode=0)
These have to be in this order.
Definition: Logger.cpp:16
Bus * bus
Definition: Module.h:17
std::wstring text
Definition: Message.h:17
bool powered
Definition: Module.h:159
double time
Definition: Message.h:20
void AddMessage(std::wstring text, MessageLevel level, bool sendEmail)
Definition: CAS.cpp:217
float delay
wait number of seconds before executing command
Definition: Command.h:8
float secsincelast
Definition: Module.h:153
float secsincereset
2 second power up delay
Definition: Module.h:154
void AcknowledgeAllMessages() const
Definition: CAS.cpp:169
MessageLevel
Definition: Message.h:5
Definition: Message.h:9
Logger * logger
2 second application start delay
Definition: Module.h:155