Rise
The Vieneo Province
scanDb.cpp
Go to the documentation of this file.
1 #include "scanDb.h"
2 
3 scanDb::scanDb(Logger* prmLogger)
4 {
5  logger = prmLogger;
6  Reset();
7 }
8 
10 {
11  ZeroMemory(memory, sizeof Sscan * MAX_SHIPINMEMORY);
12 }
13 
14 void scanDb::ClearChildren(unsigned short parentId)
15 {
16  for (int i = 0; i < MAX_SHIPINMEMORY; i++)
17  {
18  if (memory[i].initialized && memory[i].scandata.parentId == parentId)
19  memory[i].scandata.parentId = 0;
20  }
21 }
22 
23 void scanDb::InsertUpdateModeAC(USHORT vehicleId, SmodeAC modeAC)
24 {
25  if (memory[vehicleId].initialized)
26  {
27  // so if this is a PODS that used to be a child of another record we can update his old parent as assume he is no longer a child!
28  const USHORT parentId = memory[vehicleId].scandata.parentId;
29  if (parentId != 0)
30  {
31  logger->Log("Got mode AC information for someone that used to have a parent, clearing...");
32  memory[vehicleId].scandata.parentId = 0;
33  UpdateChildren(parentId);
34  }
35  }
36  else
37  {
38  memory[vehicleId].initialized = true;
39  }
40 
41  memory[vehicleId].scandata.modeAC = modeAC;
42 }
43 
44 void scanDb::UpdateChildren(USHORT parentId)
45 {
46  memory[parentId].children.clear();
47 
48  for (int i = 1; i < MAX_SHIPINMEMORY; i++) // for each vessel
49  {
50  if (i == parentId) continue; // except the parent
51 
52  if (memory[i].scandata.parentId == parentId) // if this vehicle's parent is the one we are worried about
53  {
54  memory[parentId].children.emplace_back(memory[i].scandata); // add to parent's child list
55  }
56  }
57 }
58 
59 void scanDb::InsertUpdateModeX(USHORT vehicleId, SmodeX modeX)
60 {
61  if (!memory[vehicleId].initialized)
62  {
63  logger->Log("Got mode X information before mode AC was received!", Logger::Warn);
64  }
65 
66  memory[vehicleId].scandata.modeX = modeX;
67 }
68 
69 void scanDb::InsertUpdateParent(USHORT vehicleId, USHORT parentId)
70 {
71  if (!memory[vehicleId].initialized)
72  {
73  logger->Log("Got mode S information before mode AC was received!", Logger::Warn);
74  }
75 
76  if (memory[vehicleId].scandata.parentId == parentId) return;
77 
78  const int oldParentId = memory[vehicleId].scandata.parentId;
79  memory[vehicleId].scandata.parentId = parentId;
80 
81  if (oldParentId != 0) // had old parent
82  UpdateChildren(oldParentId);
83  if (parentId != 0) // has new parent
84  UpdateChildren(parentId);
85 }
86 
87 Sscan scanDb::Get(USHORT vehicleId) const
88 {
89  return memory[vehicleId];
90 }
std::vector< Sscandata > children
Definition: scanDb.h:50
Sscan memory[MAX_SHIPINMEMORY]
Definition: scanDb.h:57
Definition: Logger.h:9
scanDb()=default
void InsertUpdateParent(USHORT vehicleId, USHORT parentId)
Definition: scanDb.cpp:69
unsigned short parentId
Definition: scanDb.h:42
void InsertUpdateModeAC(USHORT vehicleId, SmodeAC modeAC)
Definition: scanDb.cpp:23
void UpdateChildren(USHORT parentId)
Definition: scanDb.cpp:44
void ClearChildren(unsigned short parentId)
Definition: scanDb.cpp:14
Logger * logger
Definition: scanDb.h:55
void InsertUpdateModeX(USHORT vehicleId, SmodeX modeX)
Definition: scanDb.cpp:59
Definition: scanDb.h:29
void Log(const char *msg, Level level=Info, int errorCode=0)
Definition: Logger.cpp:11
Definition: scanDb.h:13
SmodeX modeX
Definition: scanDb.h:43
Sscandata scandata
Definition: scanDb.h:49
SmodeAC modeAC
Definition: scanDb.h:41
Definition: scanDb.h:46
Sscan Get(USHORT vehicleId) const
Definition: scanDb.cpp:87
bool initialized
Definition: scanDb.h:48
void Reset()
Definition: scanDb.cpp:9