Garmin Fleet Management Controller  2.19.0
SensorList.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 *
3 * MODULE NAME:
4 * SensorList.cpp
5 *
6 * Copyright 2014 by Garmin Ltd. or its subsidiaries.
7 *---------------------------------------------------------------------
8 * $NoKeywords$
9 *********************************************************************/
10 
11 #define SAVE_SENSORS "sensors.dat"
12 
13 #include "stdafx.h"
14 #include "fmi.h"
15 #include "SensorList.h"
16 
17 #if( FMI_SUPPORT_A617 )
18 
21 
22 //----------------------------------------------------------------------
24 //----------------------------------------------------------------------
25 
26 SensorList::SensorList():
27  mSensors( SAVE_SENSORS )
28 {
29  std::map<uint32,SensorListItem>::const_iterator it = mSensors.begin();
30  for( ; it != mSensors.end(); it++ )
31  {
32  if( mSensors.get( it->first ).isValid() )
33  {
34  mSensorIndexInList[ mSensors.get( it->first ).getDisplayIndex() ] = mSensors.get( it->first ).getUniqueId();
35  }
36  }
37 }
38 
39 //----------------------------------------------------------------------
41 //----------------------------------------------------------------------
42 void SensorList::add( SensorListItem item )
43 {
44 std::map<uint8,uint32>::const_iterator it = mSensorIndexInList.find( item.getDisplayIndex() );
45 if( ( it != mSensorIndexInList.end() ) && it->second != item.getUniqueId() && item.isValid() )
46  {
47  std::map<uint8,uint32>::const_iterator curr;
48  std::map<uint8,uint32>::const_iterator end;
49  //should the loop traverse up?
50  bool up = true;
51  //handle rearranging all existing elements
52  if( mSensors.contains( item.getUniqueId() ) )
53  {
54  if( mSensors.get( item.getUniqueId() ).getDisplayIndex() > item.getDisplayIndex() )
55  {
56  curr = mSensorIndexInList.find( mSensors.get( item.getUniqueId() ).getDisplayIndex() );
57  curr--;
58  end = mSensorIndexInList.find( item.getDisplayIndex() );
59  }
60  else if( mSensors.get( item.getUniqueId() ).getDisplayIndex() < item.getDisplayIndex() )
61  {
62  curr = mSensorIndexInList.find( item.getDisplayIndex() );
63  curr++;
64  end = mSensorIndexInList.find( mSensors.get( item.getUniqueId() ).getDisplayIndex() );
65  up = false;
66  }
67  }
68  //add new element
69  else
70  {
71  curr = mSensorIndexInList.end();
72  curr--;
73  end = mSensorIndexInList.find( item.getDisplayIndex() );
74  }
75  uint8 di;
76  while( curr != end )
77  {
78  di = mSensors.get( curr->second ).getDisplayIndex();
79  if( up )
80  ++di;
81  else
82  --di;
83  mSensors.get( curr->second ).setDisplayIndex( di );
84  mSensorIndexInList[ mSensors.get( curr->second ).getDisplayIndex() ] = mSensors.get( curr->second ).getUniqueId();
85  if( up )
86  curr--;
87  else
88  curr++;
89  }
90 
91  di = mSensors.get( curr->second ).getDisplayIndex();
92  if( up )
93  ++di;
94  else
95  --di;
96  mSensors.get( curr->second ).setDisplayIndex( di );
97  mSensorIndexInList[ mSensors.get( curr->second ).getDisplayIndex() ] = mSensors.get( curr->second ).getUniqueId();
98  }
99  //if changing index of an existing sensor or reusing id, get rid of old index entry to avoid double entries
100  if( mSensors.contains( item.getUniqueId() ) && mSensorIndexInList[ mSensors.get( item.getUniqueId() ).getDisplayIndex() ] == item.getUniqueId() )
101  {
102  mSensorIndexInList.erase( mSensors.get( item.getUniqueId() ).getDisplayIndex() );
103  }
104  mSensorIndexInList[ item.getDisplayIndex() ] = item.getUniqueId();
105  mSensors.put( item );
106 }
107 
108 #endif
BOOL isValid() const
Check whether this item is valid.
Data structure to hold details of an A617 sensor that the server needs to keep.
uint8 getDisplayIndex() const
Get display index.
unsigned char uint8
8-bit unsigned integer
Definition: garmin_types.h:62
uint32 getUniqueId() const
Get the unique id.
#define SAVE_SENSORS
Definition: SensorList.cpp:11