|
From: <to...@us...> - 2008-07-02 05:06:16
|
Revision: 249
http://gearbox.svn.sourceforge.net/gearbox/?rev=249&view=rev
Author: tobasco
Date: 2008-07-01 22:06:14 -0700 (Tue, 01 Jul 2008)
Log Message:
-----------
Alex's comments II
Modified Paths:
--------------
gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserver.cpp
gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserver.h
gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.cpp
gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.h
gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.h
gearbox/trunk/submitted/gbxsmartbatteryacfr/test/test.cpp
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserver.cpp
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserver.cpp 2008-07-02 05:03:13 UTC (rev 248)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserver.cpp 2008-07-02 05:06:14 UTC (rev 249)
@@ -13,21 +13,30 @@
#include "oceanserver.h"
namespace gbxsmartbatteryacfr {
+
+static const int MAX_EXCEPTIONS_ROW = 10;
OceanServer::OceanServer( const std::string &port,
gbxutilacfr::Tracer &tracer)
- : tracer_(tracer)
+ : tracer_(tracer),
+ exceptionCounter_(0)
{
reader_.reset(new gbxsmartbatteryacfr::OceanServerReader( port, tracer_ ));
}
-void
-OceanServer::read()
+const gbxsmartbatteryacfr::OceanServerSystem&
+OceanServer::getData()
{
try
{
+ // read new data, this may throw
gbxsmartbatteryacfr::OceanServerSystem data;
reader_->read(data);
+
+ // if successful, reset counter
+ exceptionCounter_ = 0;
+
+ // update internal (full) record
gbxsmartbatteryacfr::updateWithNewData( data, data_ );
}
catch ( gbxsmartbatteryacfr::ParsingException &e )
@@ -37,12 +46,19 @@
<< e.what() << endl
<< "This can happen sometimes. Will continue regardless.";
tracer_.info( ss.str() );
+
+ exceptionCounter_++;
+ if (exceptionCounter_ >= MAX_EXCEPTIONS_ROW)
+ {
+ ss.str("");
+ ss << "OceanServer: " << __func__ << ": Caught " << MAX_EXCEPTIONS_ROW
+ << " ParsingExceptions in a row. Something must be wrong";
+ throw gbxutilacfr::Exception( ERROR_INFO, ss.str() );
+ }
}
-}
-
-const gbxsmartbatteryacfr::OceanServerSystem&
-OceanServer::getData() const
-{
+
+ // return updated internal storage
+ // if there was an exception on read, we just return the previous record
return data_;
}
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserver.h
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserver.h 2008-07-02 05:03:13 UTC (rev 248)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserver.h 2008-07-02 05:06:14 UTC (rev 249)
@@ -33,18 +33,19 @@
OceanServer( const std::string &port,
gbxutilacfr::Tracer &tracer);
- //! Reads data from OceanServer and incrementally updates internal storage
- void read();
+ //! Reads data from OceanServer, incrementally updates internal storage
+ //! Returns a reference to the internal storage
+ //! May throw gbxutilacfr::Exception
+ const gbxsmartbatteryacfr::OceanServerSystem& getData();
- //! Returns a reference to the current (incrementally updated) OceanServer data
- const gbxsmartbatteryacfr::OceanServerSystem& getData() const;
-
private:
gbxsmartbatteryacfr::OceanServerSystem data_;
gbxutilacfr::Tracer& tracer_;
auto_ptr<gbxsmartbatteryacfr::OceanServerReader> reader_;
+ int exceptionCounter_;
+
};
} //namespace
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.cpp
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.cpp 2008-07-02 05:03:13 UTC (rev 248)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.cpp 2008-07-02 05:06:14 UTC (rev 249)
@@ -18,16 +18,45 @@
namespace gbxsmartbatteryacfr {
-// baudrate is fixed
-static const int BAUDRATE = 19200;
+namespace {
+
+ // baudrate is fixed
+ static const int BAUDRATE = 19200;
+
+ // timeout for reading from the serial port
+ static const int TIMEOUT_SEC = 2;
+
+ // the maximum number of lines to read until we are confident that we are not
+ // connected to an OceanServer system
+ static const int MAX_TRIES = 50;
+
+ // Returns true if 100% sure that we are connected to an OceanServer system, otherwise false
+ bool isOceanServerSystem( const string &candidateString )
+ {
+ // number of characters which we require to match
+ // if they match, we are pretty sure we have an OceanServerSystem
+ unsigned int numCharRequired = 8;
+ if ( candidateString.size()<numCharRequired ) return false;
+
+ // some menu entries from the OceanServer system - used to recognize whether
+ // we are connected to the right device
+ std::vector<std::string> oceanServerStrings;
+ oceanServerStrings.push_back(" S - Setup Controller");
+ oceanServerStrings.push_back(" B - Battery Status");
+ oceanServerStrings.push_back(" X - Host HEX");
+ oceanServerStrings.push_back(" H - Help");
+ oceanServerStrings.push_back(" www.ocean-server.com");
+
+ for (unsigned int i=0; i<oceanServerStrings.size(); i++)
+ {
+ if ( strncmp(oceanServerStrings[i].c_str(),candidateString.c_str(),numCharRequired)==0 )
+ return true;
+ }
+ return false;
+ }
-// timeout for reading from the serial port
-static const int TIMEOUT_SEC = 2;
+}
-// the maximum number of lines to read until we are confident that we are not
-// connected to an OceanServer system
-static const int MAX_TRIES = 50;
-
OceanServerReader::OceanServerReader( const string &serialPort,
gbxutilacfr::Tracer &tracer )
@@ -35,15 +64,7 @@
tracer_(tracer),
parser_(tracer),
firstTime_(true)
-{
- // some menu entries from the OceanServer system - used to recognize whether
- // we are connected to the right device
- oceanServerStrings_.push_back(" S - Setup Controller");
- oceanServerStrings_.push_back(" B - Battery Status");
- oceanServerStrings_.push_back(" X - Host HEX");
- oceanServerStrings_.push_back(" H - Help");
- oceanServerStrings_.push_back(" www.ocean-server.com");
-
+{
checkConnection();
// send the command to start reading data
@@ -52,23 +73,6 @@
serial_.write(&startReading, 1);
}
-bool
-OceanServerReader::isOceanServerSystem( string &oceanServerString )
-{
- // number of characters which we require to match
- // if they match, we are pretty sure we have an OceanServerSystem
- unsigned int numCharRequired = 8;
-
- if ( oceanServerString.size()<numCharRequired ) return false;
-
- for (unsigned int i=0; i<oceanServerStrings_.size(); i++)
- {
- if ( strncmp(oceanServerStrings_[i].c_str(),oceanServerString.c_str(),numCharRequired)==0 )
- return true;
- }
- return false;
-}
-
void
OceanServerReader::checkConnection()
{
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.h
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.h 2008-07-02 05:03:13 UTC (rev 248)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.h 2008-07-02 05:06:14 UTC (rev 249)
@@ -42,11 +42,6 @@
private:
- // Returns true if 100% sure that we are connected to an OceanServer system, otherwise false
- bool isOceanServerSystem( std::string &oceanServerString );
-
- std::vector<std::string> oceanServerStrings_;
-
gbxserialacfr::Serial serial_;
gbxutilacfr::Tracer& tracer_;
gbxsmartbatteryacfr::OceanServerParser parser_;
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.h
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.h 2008-07-02 05:03:13 UTC (rev 248)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.h 2008-07-02 05:06:14 UTC (rev 249)
@@ -47,8 +47,8 @@
int minToEmpty;
std::string messageToSystem;
- //! Battery module states
- //! Each vector is always of size 8 because oceanserver system has 8 slots
+ //! Battery module states.
+ //! Each vector is always of size 8 because OceanServer's MP08 battery management module has 8 slots
std::vector<bool> availableBatteries;
std::vector<bool> chargingStates;
std::vector<bool> supplyingPowerStates;
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/test/test.cpp
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/test/test.cpp 2008-07-02 05:03:13 UTC (rev 248)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/test/test.cpp 2008-07-02 05:06:14 UTC (rev 249)
@@ -39,7 +39,6 @@
for (unsigned int i=0; i<=numRecords; i++)
{
- oceanserver.read();
gbxsmartbatteryacfr::OceanServerSystem data = oceanserver.getData();
cout << "TRACE(test): Reading record " << i << ": " << endl
@@ -54,11 +53,18 @@
<< "This shouldn't happen!" << endl;
return 1;
}
+ catch ( gbxutilacfr::Exception &e )
+ {
+ cout << "ERROR(test): Caught a gbxutilacfr::Exception: "
+ << e.what() << endl
+ << "This shouldn't happen!" << endl;
+ return 1;
+ }
catch ( std::exception &e )
{
cout << "ERROR(test): Caught an unknown exception: "
- << e.what() << endl
- << "This shouldn't happen!" << endl;
+ << e.what() << endl
+ << "This shouldn't happen!" << endl;
return 1;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|