// -*- mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; coding: utf-8-unix -*- // ***** BEGIN LICENSE BLOCK ***** ////////////////////////////////////////////////////////////////////////// // Copyright (c) 2011-2014 RALOVICH, Kristóf // // // // 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 3 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. // // // ////////////////////////////////////////////////////////////////////////// // ***** END LICENSE BLOCK ***** #include "DeviceSettings.hpp" #include #include // memset #include "common.hpp" #include #include #include #include "Log.hpp" namespace antpm { DeviceSettings::DeviceSettings(const char *devId) : mDevId(devId) { loadDefaultValues(); } void DeviceSettings::loadDefaultValues() { MaxFileDownloads = 1000; struct tm y2k; // 2000-01-01T00:00:00Z 946684800 y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0; y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1; y2k.tm_isdst = -1; LastUserProfileTime = ::mktime(&y2k) - timezone; LastTransferredTime = ::mktime(&y2k) - timezone; SerialWriteDelayMs = 3; } const std::string DeviceSettings::getConfigFileName() const { return getFolder() + "/config.ini"; } const std::string DeviceSettings::getFolder() const { return getConfigFolder() + "/" + mDevId + "/"; } /// Both inpout and output are represented in GMT/UTC. std::time_t DeviceSettings::str2time(const char* from) { if(!from) return 0; struct tm tm; memset(&tm, 0, sizeof(struct tm)); //strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm); #ifndef _WIN32 char* rv = ::strptime(from, "%Y-%m-%dT%H:%M:%SZ", &tm); bool ok = rv==from+::strlen(from); if(!ok) return 0; #else //std::string ts("2002-01-20 23:59:59.000"); boost::posix_time::ptime t(boost::posix_time::time_from_string(from)); tm = boost::posix_time::to_tm( t ); #endif return ::mktime(&tm) - timezone; } /// Both input and output are represented in GMT/UTC. const std::string DeviceSettings::time2str(const std::time_t t) { char outstr[256]; memset(outstr, 0, sizeof(outstr)); struct tm tm; memset(&tm, 0, sizeof(struct tm)); #ifndef _WIN32 ::gmtime_r(&t, &tm); #else gmtime_s(&tm, &t); #endif if(::strftime(outstr, sizeof(outstr), "%Y-%m-%dT%TZ", &tm) == 0) return ""; return outstr; } bool DeviceSettings::saveToFile(const char *fname) { boost::property_tree::ptree pt; pt.put("antpm.MaxFileDownloads", MaxFileDownloads); pt.put("antpm.LastUserProfileTime", time2str(LastUserProfileTime)); pt.put("antpm.LastTransferredTime", time2str(LastTransferredTime)); pt.put("antpm.SerialWriteDelayMs", 3); try { boost::property_tree::ini_parser::write_ini(fname, pt); } catch(boost::property_tree::ini_parser_error& ipe) { LOG(LOG_WARN) << "DeviceSettings::saveToFile: " << ipe.message() << std::endl; return false; } return true; } bool DeviceSettings::loadFromFile(const char *fname) { boost::property_tree::ptree pt; try { boost::property_tree::ini_parser::read_ini(fname, pt); } catch(boost::property_tree::ini_parser_error& ipe) { LOG(LOG_WARN) << "DeviceSettings::loadFromFile: " << ipe.message() << std::endl; return false; } LOG_VAR(pt.get("antpm.MaxFileDownloads")); LOG_VAR(pt.get("antpm.LastUserProfileTime")); LOG_VAR(pt.get("antpm.LastTransferredTime")); LOG_VAR(pt.get("antpm.SerialWriteDelayMs")); MaxFileDownloads = pt.get("antpm.MaxFileDownloads"); LastUserProfileTime = str2time(pt.get("antpm.LastUserProfileTime").c_str()); LastTransferredTime = str2time(pt.get("antpm.LastTransferredTime").c_str()); SerialWriteDelayMs = pt.get("antpm.SerialWriteDelayMs"); return true; } /// /// \param t expected to be represented as local time void DeviceSettings::mergeLastUserProfileTime(const std::time_t gmt) { //std::time_t gmt = t + timezone; LOG(LOG_DBG) << "LastUserProfileTime: " << time2str(LastUserProfileTime) << " => " << time2str(gmt) << "\n"; LOG(LOG_DBG) << "LastUserProfileTime: " << LastUserProfileTime << " => " << gmt << "\n"; if(gmt > LastUserProfileTime) { LastUserProfileTime = gmt; } } /// /// \param t expected to be represented as local time void DeviceSettings::mergeLastTransferredTime(const std::time_t gmt) { LOG(LOG_DBG) << "LastTransferredTime: " << time2str(LastTransferredTime) << " => " << time2str(gmt) << "\n"; //std::time_t gmt = t + timezone; LastTransferredTime = std::max(LastTransferredTime, gmt); } }