summaryrefslogtreecommitdiff
path: root/src/DeviceSettings.cpp
blob: 2b6f68bf8b96cc6a3c4adec9bb461ae0732ac396 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// -*- 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 <ctime>
#include <cstring> // memset
#include "common.hpp"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#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<unsigned int>("antpm.MaxFileDownloads"));
  LOG_VAR(pt.get<std::string>("antpm.LastUserProfileTime"));
  LOG_VAR(pt.get<std::string>("antpm.LastTransferredTime"));
  LOG_VAR(pt.get<std::string>("antpm.SerialWriteDelayMs"));
  MaxFileDownloads    = pt.get<unsigned int>("antpm.MaxFileDownloads");
  LastUserProfileTime = str2time(pt.get<std::string>("antpm.LastUserProfileTime").c_str());
  LastTransferredTime = str2time(pt.get<std::string>("antpm.LastTransferredTime").c_str());
  SerialWriteDelayMs = pt.get<size_t>("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_DBG2) << "LastUserProfileTime: " << time2str(LastUserProfileTime) << " => " << time2str(gmt) << "\n";
  LOG(LOG_DBG2) << "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_DBG2) << "LastTransferredTime: " << time2str(LastTransferredTime) << " => " << time2str(gmt) << "\n";
  //std::time_t gmt = t + timezone;
  LastTransferredTime = std::max(LastTransferredTime, gmt);
}

}