class Measurement { public: Measurement(); Measurement(std::string datetime, float val); void setDatetime(std::string datetime); void setVal(float val); std::string getDatetime(void) const; float getVal(void) const; private: std::string m_datetime; float m_val; }; #include "measurement.hpp" Measurement::Measurement() { } Measurement::Measurement(std::string datetime, float val) { m_datetime = datetime; m_val = val; } void Measurement::setDatetime(std::string datetime) { m_datetime = datetime; } void Measurement::setVal(float val) { m_val = val; } std::string Measurement::getDatetime(void) const { return m_datetime; } float Measurement::getVal(void) const { return m_val; } class Statistics { public: void addMeasurement(std::string datetime, float val); void clearAll(void); Measurement getMaxMeasurement(void) const; Measurement getMinMeasurement(void) const; Measurement getMeanMeasurement(void) const; int queryMeasurement(std::string filename, std::string location, std::string sensor); void printVector(void) const; //void sortone(void); private: std::vector breakSpaces(const std::string& str) const; std::vector m_mv; }; #include #include #include #include #include #include #include "statistics.hpp" void Statistics::addMeasurement(std::string datetime, float val) { m_mv.push_back(Measurement(datetime, val)); } void Statistics::clearAll(void) { m_mv.clear(); } Measurement Statistics::getMaxMeasurement(void) const { Measurement maxM; int i; if (m_mv.empty()) { // error, do sth for god shake :P } else { maxM = m_mv[0]; } for (i = 1; i < m_mv.size(); i++) if (maxM.getVal() < m_mv[i].getVal()) maxM = m_mv[i]; return maxM; } Measurement Statistics::getMinMeasurement(void) const { Measurement minM; int i; if (m_mv.empty()) { // error, do sth for god shake :P } else { minM = m_mv[0]; } for (i = 1; i < m_mv.size(); i++) if (minM.getVal() > m_mv[i].getVal()) minM = m_mv[i]; return minM; } Measurement Statistics::getMeanMeasurement(void) const { float mean; int i; if (m_mv.empty()) { // error, do sth for god shake :P } mean = 0.0; for (i = 0; i < m_mv.size(); i++) mean += m_mv[i].getVal(); return Measurement("none", mean/m_mv.size()); } /* * `getMeasurement' searches the data file and fills in a vector of Measurements * that fulfill the searching criteria, namely the location and the sensor type. */ int Statistics::queryMeasurement(std::string filename, std::string location, std::string sensor) { std::ifstream infile(filename.c_str()); std::string readline; std::vector vs; int i; Measurement m; if (!infile.is_open()) { return -1; } while (!infile.eof()) { std::getline(infile, readline); vs = breakSpaces(readline); if (vs[0] == location) { m.setDatetime(vs[1] + " " + vs[2] + " " + vs[3] + " " + vs[4] + " " + vs[5]); for (i = 6; i < vs.size(); i++) { if (vs[i] == sensor) { std::stringstream ss; ss << vs[i + 1]; float f; ss >> f; m.setVal(f); m_mv.push_back(m); break; } } } } return 0; } std::vector Statistics::breakSpaces(const std::string& str) const { std::vector vs; std::string::size_type startpos, endpos, len; int i, j; len = str.size(); for (i = 0; i < len; i++) { for (j = i; j < len, str[j] == ' '; j++) ; startpos = j; for (j = startpos + 1; j <= len, str[j] != ' ' && str[j] != '\0'; j++) ; endpos = j - 1; if (startpos != len) vs.push_back(str.substr(startpos, endpos - startpos + 1)); i = endpos + 1; } return vs; } /* * Prints all the items of the vector. * Useful for debugging purposes. */ void Statistics::printVector(void) const { int i; for (i = 0; i < m_mv.size(); i++) std::cout << "Datetime = " << m_mv[i].getDatetime() << " " << "Val = " << m_mv[i].getVal() << std::endl; } //attempt to sort /*void Statistics::sortone(void) { if (m_mv.empty()) { // error, do sth for god shake :P } sort(m_mv.begin(), m_mv.end()); Statistics::printVector(); } */ class Mainframe { public: Mainframe(std::string filename); std::string getFilename(void) const; void addNode(Node *node); void removeNode(std::string location); Node *getNodeByName(std::string location); unsigned int getNumOfNodes(void) const; void printAll(std::ostream& stream) const; Node *getNodeByIndex(unsigned int index); Measurement getMaxMeasurement(std::string sensorType); Measurement getMinMeasurement(std::string sensorType); Measurement getMeanMeasurement(std::string location, std::string sensorType); //void Mainframe::sortone(std::string sensorType); private: std::vector getMeasurement(std::string location, std::string sensor) const; std::vector m_pnv; std::string m_filename; Statistics m_stat; }; #include #include #include #include #include "mainframe.hpp" #include "node.hpp" Mainframe::Mainframe(std::string filename) { m_filename = filename; } std::string Mainframe::getFilename(void) const { return m_filename; } void Mainframe::addNode(Node *node) { m_pnv.push_back(node); } /* * Remove a node from the mainframe's list provided that * there is a match as far the location name is concerned. */ void Mainframe::removeNode(std::string location) { std::vector::iterator it; for (it = m_pnv.begin(); it != m_pnv.end(); it++) if ((*it)->getLocation() == location) { m_pnv.erase(it); break; } } Node* Mainframe::getNodeByName(std::string location) { std::vector::iterator it; for (it = m_pnv.begin(); it != m_pnv.end(); it++) if ((*it)->getLocation() == location) break; return *it; } unsigned int Mainframe::getNumOfNodes(void) const { return m_pnv.size(); } /* * `printAll' prints all sensors' data for all nodes * in the output `stream'. */ void Mainframe::printAll(std::ostream& stream) const { unsigned int i; std::vector::const_iterator it; for (it = m_pnv.begin(); it != m_pnv.end(); it++) { stream << std::setw(15) << std::left << (*it)->getLocation() << " " << (*it)->getDateTime() << " "; for (i = 0; i < (*it)->getNumOfSensors(); i++) { stream << (*it)->getSensor(i)->getType() << " " << (*it)->getSensor(i)->getVal() << " " ; } stream << std::endl; } stream << std::endl; } Measurement Mainframe::getMaxMeasurement(std::string sensorType) { Measurement maxM; int i; for (i = 0; i < m_pnv.size(); i++) m_stat.queryMeasurement(m_filename, m_pnv[i]->getLocation(), sensorType); maxM = m_stat.getMaxMeasurement(); m_stat.clearAll(); return maxM; } Measurement Mainframe::getMinMeasurement(std::string sensorType) { Measurement minM; int i; for (i = 0; i < m_pnv.size(); i++) m_stat.queryMeasurement(m_filename, m_pnv[i]->getLocation(), sensorType); minM = m_stat.getMinMeasurement(); m_stat.clearAll(); return minM; } Measurement Mainframe::getMeanMeasurement(std::string location, std::string sensorType) { Measurement meanM; m_stat.queryMeasurement(m_filename, location, sensorType); meanM = m_stat.getMeanMeasurement(); m_stat.clearAll(); return meanM; } Node* Mainframe::getNodeByIndex(unsigned int index) { return m_pnv[index]; } //sort /*void Mainframe::sortone(std::string sensorType) { int i; for (i = 0; i < m_pnv.size(); i++) m_stat.queryMeasurement(m_filename,m_pnv[i]->getLocation(),sensorType); m_stat.sortone(); m_stat.clearAll(); } */