/*************************************************************************** Copyright (C) 2008 by Matthias Lechner This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ***************************************************************************/ #include "freestylemini.h" #include #include #include #include #include FreeStyleMini::FreeStyleMini() { setManufacturer("Abbott"); setModel("FreeStyle Mini"); setDeviceType(Device::BloodSugarMeasurementDevice); setPortType(Device::Serial); m_serialPort = new SerialPort(SerialPort::BPS19200, 8, SerialPort::No, SerialPort::One); } std::vector FreeStyleMini::getAvailablePorts() { return m_serialPort->getAvailablePorts(); } std::vector FreeStyleMini::readMeasurementData() { std::vector dataSets; // check if a port was set if(getPort().empty()) return dataSets; // connect to the device if(m_serialPort->open(getPort())) { try { m_serialPort->write("mem"); std::string rawAnswer = m_serialPort->read(); if(rawAnswer.empty()) throw std::runtime_error("Could not read from device."); // split the answer... std::vector answer; extractLines(rawAnswer, answer); // TODO: think about validation (we should at least // check the length of the answer) dataSets = parseData(answer); } catch (std::runtime_error e) { std::cout << "Exception occurred: " << e.what() << std::endl; } } m_serialPort->close(); return dataSets; } void FreeStyleMini::extractLines(const std::string& data, std::vector& lines) { // lines are separated by this delimiter std::string delimiter("\r\n"); std::string::size_type delimiterSize = delimiter.size(); std::string::size_type lastPosition = 0; std::string::size_type position = data.find(delimiter, lastPosition); while(lastPosition != std::string::npos) { std::string line = data.substr(lastPosition, position - lastPosition); stripNewline(line); if(!line.empty()) lines.push_back(line); lastPosition = ( position == std::string::npos ) ? std::string::npos : position + delimiterSize; position = data.find(delimiter, lastPosition); } } void FreeStyleMini::stripNewline(std::string& data) { data.erase( std::remove(data.begin(),data.end(),'\n') , data.end() ); } std::vector FreeStyleMini::parseData(const std::vector& data) { // extract and print some (mh..) useful device data std::string serialNumber = data.at(0); std::string firmwareRevision = data.at(1); std::cout << "Serial number: " << serialNumber << std::endl; std::cout << "Firmware revision: " << firmwareRevision << std::endl; // get the number of stored records int storedRecords = 0; std::stringstream storedRecordsStream; storedRecordsStream << data.at(3); storedRecordsStream >> storedRecords; // the header takes 4 lines const int headerOffset = 4; const int footerOffset = 1; if(storedRecords != data.size() - headerOffset - footerOffset) throw std::runtime_error("Device reports another record count than we calculated!"); std::vector dataSets; for(int i=headerOffset; i> convertedMeasurementValue; converter.str(""); converter.clear(); // convert month if(month == "Jan") convertedMonth = 1; else if(month == "Feb") convertedMonth = 2; else if(month == "Mar") convertedMonth = 3; else if(month == "Apr") convertedMonth = 4; else if(month == "May") convertedMonth = 5; else if(month == "June") convertedMonth = 6; else if(month == "July") convertedMonth = 7; else if(month == "Aug") convertedMonth = 8; else if(month == "Sep") convertedMonth = 9; else if(month == "Oct") convertedMonth = 10; else if(month == "Nov") convertedMonth = 11; else if(month == "Dec") convertedMonth = 12; else convertedMonth = 0; converter << day; converter >> convertedDay; converter.str(""); converter.clear(); converter << year; converter >> convertedYear; converter.str(""); converter.clear(); converter << hour; converter >> convertedHour; converter.str(""); converter.clear(); converter << minute; converter >> convertedMinute; converter.str(""); converter.clear(); DataSet* dataSet = 0; try { SalusTime time(convertedHour, convertedMinute, 0); SalusDate date(convertedYear, convertedMonth, convertedDay); dataSet = new DataSet(convertedMeasurementValue, date, time); } catch(std::invalid_argument e) { std::cout << "Exception occurred: " << e.what() << std::endl; dataSet = 0; } return dataSet; }