//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RiaTimeHistoryCurveResampler, Test_Resampling_Year)
{
    std::vector<QString> timeStrings(
        {
            "2014-06-06",
            "2015-12-02",
            "2018-02-07"
        }
    );

    std::vector<double> dataValues(
        {
            0.0,
            0.0,
            0.0
        }
    );

    RiaTimeHistoryCurveResampler resampler;
    resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
    resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::YEAR);

    EXPECT_EQ(5, (int)resampler.resampledTimeSteps().size());
    EXPECT_EQ(toTime_t("2015-01-01"), resampler.resampledTimeSteps()[0]);
    EXPECT_EQ(toTime_t("2016-01-01"), resampler.resampledTimeSteps()[1]);
    EXPECT_EQ(toTime_t("2017-01-01"), resampler.resampledTimeSteps()[2]);
    EXPECT_EQ(toTime_t("2018-01-01"), resampler.resampledTimeSteps()[3]);
    EXPECT_EQ(toTime_t("2019-01-01"), resampler.resampledTimeSteps()[4]);
}
Esempio n. 2
0
/*----------------------------------------------------------------------*
 * Calculate the DST and standard time change points for the given      *
 * given year as local and UTC time_t values.                           *
 *----------------------------------------------------------------------*/
void Timezone::calcTimeChanges(int yr)
{
    _dstLoc = toTime_t(_dst, yr);
    _stdLoc = toTime_t(_std, yr);
    _dstUTC = _dstLoc - _std.offset * SECS_PER_MIN;
    _stdUTC = _stdLoc - _dst.offset * SECS_PER_MIN;
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RiaTimeHistoryCurveResampler, Test_Resampling_Week)
{
    std::vector<QString> timeStrings(
        {
            "2017-11-02",
            "2017-12-24",
            "2018-01-07"
        }
    );

    std::vector<double> dataValues(
        {
            0.0,
            0.0,
            0.0
        }
    );

    RiaTimeHistoryCurveResampler resampler;
    resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
    resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::WEEK);

    EXPECT_EQ(10, (int)resampler.resampledTimeSteps().size());
    EXPECT_EQ(toTime_t("2017-11-06"), resampler.resampledTimeSteps()[0]);
    EXPECT_EQ(toTime_t("2017-11-13"), resampler.resampledTimeSteps()[1]);
    EXPECT_EQ(toTime_t("2017-11-20"), resampler.resampledTimeSteps()[2]);
    EXPECT_EQ(toTime_t("2017-11-27"), resampler.resampledTimeSteps()[3]);
    EXPECT_EQ(toTime_t("2017-12-04"), resampler.resampledTimeSteps()[4]);
    EXPECT_EQ(toTime_t("2017-12-11"), resampler.resampledTimeSteps()[5]);
    EXPECT_EQ(toTime_t("2017-12-18"), resampler.resampledTimeSteps()[6]);
    EXPECT_EQ(toTime_t("2017-12-25"), resampler.resampledTimeSteps()[7]);
    EXPECT_EQ(toTime_t("2018-01-01"), resampler.resampledTimeSteps()[8]);
    EXPECT_EQ(toTime_t("2018-01-08"), resampler.resampledTimeSteps()[9]);
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RiaTimeHistoryCurveResampler, Test_PeriodEndValues_SamplesStartAndEndMatchPeriod)
{
    std::vector<QString> timeStrings(
        {
            "2018-02-01",
            "2018-02-10",
            "2018-03-01"
        }
    );

    std::vector<double> dataValues(
        {
            3.0,
            5.0,
            7.0
        }
    );

    RiaTimeHistoryCurveResampler resampler;
    resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
    resampler.resampleAndComputePeriodEndValues(DateTimePeriod::MONTH);

    EXPECT_EQ(2, (int)resampler.resampledTimeSteps().size());
    EXPECT_EQ(toTime_t("2018-02-01"), resampler.resampledTimeSteps().front());
    EXPECT_EQ(toTime_t("2018-03-01"), resampler.resampledTimeSteps().back());

    EXPECT_NEAR(3.0, resampler.resampledValues()[0], 1e-12);
    EXPECT_NEAR(7.0, resampler.resampledValues()[1], 1e-12);
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RiaTimeHistoryCurveResampler, Test_Resampling_Decade)
{
    std::vector<QString> timeStrings(
        {
            "1989-02-03",
            "2005-06-06",
            "2012-02-07"
        }
    );

    std::vector<double> dataValues(
        {
            0.0,
            0.0,
            0.0
        }
    );

    RiaTimeHistoryCurveResampler resampler;
    resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
    resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::DECADE);

    EXPECT_EQ(4, (int)resampler.resampledTimeSteps().size());
    EXPECT_EQ(toTime_t("1990-01-01"), resampler.resampledTimeSteps()[0]);
    EXPECT_EQ(toTime_t("2000-01-01"), resampler.resampledTimeSteps()[1]);
    EXPECT_EQ(toTime_t("2010-01-01"), resampler.resampledTimeSteps()[2]);
    EXPECT_EQ(toTime_t("2020-01-01"), resampler.resampledTimeSteps()[3]);
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RiaTimeHistoryCurveResampler, Test_WeightedMean_Days)
{
    std::vector<QString> timeStrings(
        {
            "2018-02-03",
            "2018-02-07"
        }
    );

    std::vector<double> dataValues(
        {
            3.0,
            5.0
        }
    );

    RiaTimeHistoryCurveResampler resampler;
    resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
    resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::DAY);

    EXPECT_EQ(5, (int)resampler.resampledTimeSteps().size());
    EXPECT_EQ(toTime_t("2018-02-03"), resampler.resampledTimeSteps()[0]);
    EXPECT_EQ(toTime_t("2018-02-04"), resampler.resampledTimeSteps()[1]);
    EXPECT_EQ(toTime_t("2018-02-05"), resampler.resampledTimeSteps()[2]);
    EXPECT_EQ(toTime_t("2018-02-06"), resampler.resampledTimeSteps()[3]);
    EXPECT_EQ(toTime_t("2018-02-07"), resampler.resampledTimeSteps()[4]);

    EXPECT_NEAR(3.0, resampler.resampledValues()[0], 1e-12);
    EXPECT_NEAR(5.0, resampler.resampledValues()[1], 1e-12);
    EXPECT_NEAR(5.0, resampler.resampledValues()[2], 1e-12);
    EXPECT_NEAR(5.0, resampler.resampledValues()[3], 1e-12);
    EXPECT_NEAR(5.0, resampler.resampledValues()[4], 1e-12);
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RiaTimeHistoryCurveResampler, Test_WeightedMean_SamplesStartBeforePeriod)
{
    std::vector<QString> timeStrings(
        {
            "2018-01-20",
            "2018-01-29",
            "2018-02-03",
            "2018-02-27",
            "2018-03-02"
        }
    );

    std::vector<double> dataValues(
        {
            3.0,
            5.0,
            7.0,
            11.0,
            13.0
        }
    );
    
    time_t tp0 = toTime_t("2018-02-01");
    time_t tp1 = toTime_t("2018-03-01");
    time_t tp2 = toTime_t("2018-04-01");

    RiaTimeHistoryCurveResampler resampler;
    resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
    resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::MONTH);

    EXPECT_EQ(3, (int)resampler.resampledTimeSteps().size());
    EXPECT_EQ(tp0, resampler.resampledTimeSteps()[0]);
    EXPECT_EQ(tp1, resampler.resampledTimeSteps()[1]);
    EXPECT_EQ(tp2, resampler.resampledTimeSteps()[2]);

    double value0 =
        (5.0 * 9 +
         7.0 * 3) * SECS_PER_DAY / (tp0 - toTime_t("2018-01-01"));

    double value1 =
        (7.0 * 2 +
         11.0 * 24 +
         13.0 * 2) * SECS_PER_DAY / (tp1 - tp0);

    double value2 = 13.0 * 1 * SECS_PER_DAY / (tp2 - tp1);

    EXPECT_NEAR(value0, resampler.resampledValues()[0], 1e-12);
    EXPECT_NEAR(value1, resampler.resampledValues()[1], 1e-12);
    EXPECT_NEAR(value2, resampler.resampledValues()[2], 1e-12);
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RiaTimeHistoryCurveResampler, Test_Resampling_NoSampleCrossingPeriodBoundary)
{
    std::vector<QString> timeStrings(
        {
            "2017-01-02",
            "2017-06-15",
            "2017-12-24"
        }
    );

    std::vector<double> dataValues(
        {
            0.0,
            0.0,
            0.0
        }
    );

    RiaTimeHistoryCurveResampler resampler;
    resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
    resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::YEAR);

    EXPECT_EQ(1, (int)resampler.resampledTimeSteps().size());
    EXPECT_EQ(toTime_t("2018-01-01"), resampler.resampledTimeSteps()[0]);
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RiaTimeHistoryCurveResampler, Test_WeightedMean_MultipleSamplesInLastPeriod)
{
    std::vector<QString> timeStrings(
        {
            "2018-02-10",
            "2018-03-02",
            "2018-03-05",
            "2018-03-15"
        }
    );

    std::vector<double> dataValues(
        {
            3.0,
            5.0,
            7.0,
            11.0
        }
    );

    time_t tp0 = toTime_t("2018-03-01");
    time_t tp1 = toTime_t("2018-04-01");

    RiaTimeHistoryCurveResampler resampler;
    resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
    resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::MONTH);

    EXPECT_EQ(2, (int)resampler.resampledTimeSteps().size());
    EXPECT_EQ(tp0, resampler.resampledTimeSteps()[0]);
    EXPECT_EQ(tp1, resampler.resampledTimeSteps()[1]);

    double value0 = 5.0 * 19 * SECS_PER_DAY / (tp0 - toTime_t("2018-02-01"));
    double value1 =
        (0.0 * 17 +
         11.0 * 10 +
         7.0 * 3 +
         5.0 * 1) * SECS_PER_DAY / (tp1 - tp0);

    EXPECT_NEAR(value0, resampler.resampledValues()[0], 1e-12);
    EXPECT_NEAR(value1, resampler.resampledValues()[1], 1e-12);
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RiaTimeHistoryCurveResampler, Test_WeightedMean_SamplesStartAndEndMatchPeriod)
{
    std::vector<QString> timeStrings(
        {
            "2018-02-01",
            "2018-02-10",
            "2018-03-01"
        }
    );

    std::vector<double> dataValues(
        {
            3.0,
            5.0,
            7.0
        }
    );

    RiaTimeHistoryCurveResampler resampler;
    resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
    resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::MONTH);

    EXPECT_EQ(2, (int)resampler.resampledTimeSteps().size());
    EXPECT_EQ(toTime_t("2018-02-01"), resampler.resampledTimeSteps().front());
    EXPECT_EQ(toTime_t("2018-03-01"), resampler.resampledTimeSteps().back());

    time_t timePeriod = toTime_t("2018-03-01") - toTime_t("2018-02-01");

    double value1 = 3.0;
    double value2 =
        (5.0 * 9 +
         7.0 * 19) * SECS_PER_DAY / timePeriod;

    EXPECT_NEAR(value1, resampler.resampledValues()[0], 1e-12);
    EXPECT_NEAR(value2, resampler.resampledValues()[1], 1e-12);
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RiaTimeHistoryCurveResampler, Test_WeightedMean_Decade)
{
    std::vector<QString> timeStrings(
        {
            "1999-02-03",
            "2005-06-06",
            "2012-02-07"
        }
    );

    std::vector<double> dataValues(
        {
            3.0,
            5.0,
            7.0
        }
    );

    time_t t0 = toTime_t("1999-02-03");
    time_t t1 = toTime_t("2005-06-06");
    time_t t2 = toTime_t("2012-02-07");
    time_t tp0 = toTime_t("2000-01-01");
    time_t tp1 = toTime_t("2010-01-01");
    time_t tp2 = toTime_t("2020-01-01");

    RiaTimeHistoryCurveResampler resampler;
    resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
    resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::DECADE);

    EXPECT_EQ(3, (int)resampler.resampledTimeSteps().size());
    EXPECT_EQ(tp0, resampler.resampledTimeSteps()[0]);
    EXPECT_EQ(tp1, resampler.resampledTimeSteps()[1]);
    EXPECT_EQ(tp2, resampler.resampledTimeSteps()[2]);

    double value0 = 5.0 * (tp0 - t0) / (tp0 - toTime_t("1990-01-01"));
    double value1 = (5.0 * (t1 - tp0) + 7.0 * (tp1 - t1)) / (tp1 - tp0);
    double value2 = 7.0 * (t2 - tp1) / (tp2 - tp1);

    EXPECT_NEAR(value0, resampler.resampledValues()[0], 1e-12);
    EXPECT_NEAR(value1, resampler.resampledValues()[1], 1e-12);
    EXPECT_NEAR(value2, resampler.resampledValues()[2], 1e-12);
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RiaTimeHistoryCurveResampler, Test_PeriodEndValues_SingleSample)
{
    std::vector<QString> timeStrings(
        {
            "2018-02-10"
        }
    );

    std::vector<double> dataValues(
        {
            3.0
        }
    );

    RiaTimeHistoryCurveResampler resampler;
    resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
    resampler.resampleAndComputePeriodEndValues(DateTimePeriod::MONTH);

    EXPECT_EQ(1, (int)resampler.resampledTimeSteps().size());
    EXPECT_EQ(toTime_t("2018-03-01"), resampler.resampledTimeSteps()[0]);

    EXPECT_NEAR(3.0, resampler.resampledValues()[0], 1e-12);
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RiaTimeHistoryCurveResampler, Test_Resampling_NoPeriod)
{
    std::vector<QString> timeStrings(
        {
            "2018-02-03",
            "2018-02-27"
        }
    );

    std::vector<double> dataValues(
        {
            3.0,
            5.0
        }
    );

    RiaTimeHistoryCurveResampler resampler;
    resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
    resampler.resampleAndComputeWeightedMeanValues(DateTimePeriod::MONTH);

    EXPECT_EQ(1, (int)resampler.resampledTimeSteps().size());
    EXPECT_EQ(toTime_t("2018-03-01"), resampler.resampledTimeSteps()[0]);
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RiaTimeHistoryCurveResampler, Test_PeriodEndValues_SamplesStartBeforePeriod)
{
    std::vector<QString> timeStrings(
        {
            "2018-01-30",
            "2018-02-10",
            "2018-03-05",
            "2018-03-02"
        }
    );

    std::vector<double> dataValues(
        {
            3.0,
            5.0,
            7.0,
            11.0
        }
    );

    time_t t0 = toTime_t("2018-01-30");
    time_t t1 = toTime_t("2018-02-10");
    time_t t2 = toTime_t("2018-03-05");
    time_t tp0 = toTime_t("2018-02-01");
    time_t tp1 = toTime_t("2018-03-01");
    time_t tp2 = toTime_t("2018-04-01");

    RiaTimeHistoryCurveResampler resampler;
    resampler.setCurveData(dataValues, toTime_tVector(timeStrings));
    resampler.resampleAndComputePeriodEndValues(DateTimePeriod::MONTH);

    EXPECT_EQ(3, (int)resampler.resampledTimeSteps().size());
    EXPECT_EQ(tp0, resampler.resampledTimeSteps()[0]);
    EXPECT_EQ(tp1, resampler.resampledTimeSteps()[1]);
    EXPECT_EQ(tp2, resampler.resampledTimeSteps()[2]);

    double value0 = (5.0 - 3.0) * (tp0 - t0) / (t1 - t0) + 3.0;
    double value1 = (7.0 - 5.0) * (tp1 - t1) / (t2 - t1) + 5.0;
    double value2 = 11.0;

    EXPECT_NEAR(value0, resampler.resampledValues()[0], 1e-12);
    EXPECT_NEAR(value1, resampler.resampledValues()[1], 1e-12);
    EXPECT_NEAR(value2, resampler.resampledValues()[2], 1e-12);
}
Esempio n. 15
0
QVariant ScheduleModel::data(const QModelIndex &index, int role) const
{
    QVariant res;

    if(index.row() != rowCount()-1)
    {
        auto dtCome = m_times[index.row()].first;
        auto dtLeave = m_times[index.row()].second;

        if(!(dtCome == dtLeave && dtCome.time() == QTime(0,0)))
        {
            //measurement error fix
            dtCome = dtCome.addSecs(-5*60);
            dtLeave = dtLeave.addSecs(5*60);
        }

        auto diffSecs = dtCome.secsTo(dtLeave);
        QTime diffTime = QTime(0,0).addSecs(diffSecs);
        QTime needToWork;
        auto holidayData = m_holidays.find(dtCome.date());
        if(holidayData != m_holidays.end())
        {
            if(holidayData.value() == HOLIDAY)
                needToWork = QTime(0,0);
            else if(holidayData.value() == SHORT_DAY)
                needToWork = QTime(7,30);
            else
                needToWork = QTime(8,30);
        }
        else
        {
            if(dtCome.date().dayOfWeek() == 6 || dtCome.date().dayOfWeek() == 7)
                needToWork = QTime(0,0);
            else
                needToWork = QTime(8,30);
        }

        QString resultString;
        if(index.column() == COL_RESULT)
        {
            //SKIP -> current day, no result yet
            if(dtCome.date() == QDateTime::currentDateTime().date())
                return res;
        }


        if(role == Qt::DisplayRole)
        {
            switch(index.column())
            {
            case COL_COME:
                if(dtCome.isValid())
                    res = dtCome.toString();
                break;
            case COL_LEAVE:
                if(dtLeave.isValid())
                    res = dtLeave.toString();
                break;
            case COL_DIFF:
                if(dtCome.isValid() && dtLeave.isValid())
                {
                    res = diffTime.toString();
                }
                break;
            case COL_RESULT:

                if(diffTime < needToWork)
                {
                    diffTime = QTime(0,0).addSecs(QTime(0,0).secsTo(needToWork) - QTime(0,0).secsTo(diffTime));
                    resultString += "-";
                }
                else if(diffTime > needToWork)
                {
                    diffTime = QTime(0,0).addSecs(QTime(0,0).secsTo(diffTime) - QTime(0,0).secsTo(needToWork));
                }
                else
                    diffTime = QTime(0,0);
                res = resultString + diffTime.toString();
            }
        }
        else
        if(index.column() == COL_DIFF && role == ROLE_DIFF)
        {            
            res = diffSecs;
        }
        else
        if(index.column() == COL_RESULT && role == ROLE_RESULT)
        {            
            auto resSecs = QTime(0,0).secsTo(needToWork) - diffSecs;
            res = resSecs;
        }
        else
        if((index.column() == COL_COME || index.column() == COL_LEAVE) && role == ROLE_DATETIME)
        {
            if(index.column() == COL_COME)
                res = dtCome.toTime_t();
            else
                res = dtLeave.toTime_t();
        }
    }
    else
    {
        if(role == Qt::DisplayRole)
        {
            unsigned int sum = 0, temp = 0;
            int result = 0, tempResult = 0;
            bool ok = false;
            switch(index.column())
            {
            //Last row
            case COL_LEAVE:
                res = tr("Sum:");
                break;
            case COL_DIFF:
            {
                for(auto i = 0; i < rowCount() - 1; ++i)
                {
                    temp = data(this->index(i, COL_DIFF), ROLE_DIFF).toUInt(&ok);
                    if(ok)
                        sum += temp;
                }                
                res = secondsToString(sum);
                break;
            }
            case COL_RESULT:
                for(auto i = 0; i < rowCount() - 1; ++i)
                {
                    tempResult = data(this->index(i, COL_RESULT), ROLE_RESULT).toInt(&ok);
                    if(ok)
                        result += tempResult;
                }
                //if result < 0 it's overwork
                result *= -1;

                res = secondsToString(result);
                break;

            }
        }
    }
//    else
//        res = QAbstractItemModel::data(index, role);
    return res;
}
static std::vector<time_t> toTime_tVector(const std::vector<QString>& timeStrings)
{
    std::vector<time_t> tv;
    for (auto& ts : timeStrings) tv.push_back(toTime_t(ts));
    return tv;
}
Esempio n. 17
0
File: usb.cpp Progetto: LazyT/obpm
void usbDialog::on_pushButton_import_clicked()
{
	HEALTHDATA set;

	((MainWindow*)parent())->cfg.hem7322u = radioButton_hem7322u->isChecked();

	mem = radioButton_hem7322u->isChecked() ? 100 : 60;

	if(!readRawData())
	{
		return;
	}

	user1 = 0;
	user2 = 0;

	for(int i = 0; i < 14*mem; i += 14)
	{
		if((payload[i] != 0xFF) & (payload[i + 1] != 0xFF) && (payload[i + 2] != 0xFF))
		{
			user1++;
		}

		if((payload[i + 14*mem] != 0xFF) & (payload[i + 1 + 14*mem] != 0xFF) && (payload[i + 2 + 14*mem] != 0xFF))
		{
			user2++;
		}
	}

	for(int i = 0; i < 14*user1; i += 14)
	{
		set.time = QDateTime(QDate(2000 + payload[i + 2], (payload[i + 4]>>2) & 0x0F, ((payload[i + 4]<<8 | payload[i + 5])>>5) & 0x1F), QTime(payload[i + 5] & 0x1F, ((payload[i + 6]<<8 | payload[i + 7])>>6) & 0x3F)).toTime_t();
		set.sys = payload[i + 1] + 25;
		set.dia = payload[i];
		set.bpm = payload[i + 3];

		((MainWindow*)parent())->healthdata[0].append(set);
	}

	for(int i = 0; i < 14*user2; i += 14)
	{
		set.time = QDateTime(QDate(2000 + payload[i + 2 + 14*mem], (payload[i + 4 + 14*mem]>>2) & 0x0F, ((payload[i + 4 + 14*mem]<<8 | payload[i + 5 + 14*mem])>>5) & 0x1F), QTime(payload[i + 5 + 14*mem] & 0x1F, ((payload[i + 6 + 14*mem]<<8 | payload[i + 7 + 14*mem])>>6) & 0x3F)).toTime_t();
		set.sys = payload[i + 1 + 14*mem] + 25;
		set.dia = payload[i + 14*mem];
		set.bpm = payload[i + 3 + 14*mem];

		((MainWindow*)parent())->healthdata[1].append(set);
	}

	accept();
}
Esempio n. 18
0
AuthenticationResultPtr OAuth2Response::parseTokenResponse(TokenResponsePtr tokenResponse, CallStatePtr/* callState*/)
{
    Logger::info(Tag(), "parseTokenResponse");
    AuthenticationResultPtr result = nullptr;

    if (!tokenResponse->accessToken.empty())
    {
        auto curTimeUts = QDateTime::currentDateTimeUtc();
        Logger::info(Tag(), "curTimeUTC: %; expiresIn: %", curTimeUts.toString("HH:mm:ss MM.dd.yy").toStdString(), tokenResponse->expiresIn);

        auto expiresUtc = curTimeUts.addSecs(tokenResponse->expiresIn);
        DateTimeOffset expiresOn = expiresUtc.toTime_t();

        Logger::info(Tag(), "tonken expiresOn: % (%)", expiresUtc.toString("HH:mm:ss MM.dd.yy").toStdString(), expiresOn);

        result = std::make_shared<AuthenticationResult>(tokenResponse->tokenType, tokenResponse->accessToken, tokenResponse->refreshToken, expiresOn);
        // This is only needed for AcquireTokenByAuthorizationCode in which parameter resource is optional and we need
        // to get it from the STS response.
        result->resource(tokenResponse->resource);
        result->isMultipleResourceRefreshToken(!tokenResponse->refreshToken.empty() && !tokenResponse->resource.empty());

        IdTokenPtr idToken = OAuth2Response::parseIdToken(tokenResponse->idToken);
        if (idToken != nullptr)
        {
            String tenantId = idToken->tenantId;
            String uniqueId;
            String displayableId;

            if (!idToken->objectId.empty())
            {
                uniqueId = idToken->objectId;
            }
            else if (!idToken->subject.empty())
            {
                uniqueId = idToken->subject;
            }

            if (!idToken->UPN.empty())
            {
                displayableId = idToken->UPN;
            }
            else if (!idToken->email.empty())
            {
                displayableId = idToken->email;
            }

            String givenName = idToken->givenName;
            String familyName = idToken->familyName;
            String identityProvider = idToken->identityProvider.empty() ? idToken->issuer : idToken->identityProvider;
            DateTimeOffset passwordExpiresOn = 0;
            if (idToken->passwordExpiration > 0)
            {
                passwordExpiresOn = QDateTime::currentDateTimeUtc().addSecs(idToken->passwordExpiration).toTime_t();
            }

            String changePasswordUri;
            if (!idToken->passwordChangeUrl.empty())
            {
                changePasswordUri = idToken->passwordChangeUrl;
            }

            auto userInfo = std::make_shared<UserInfo>();
            userInfo->uniqueId(uniqueId);
            userInfo->displayableId(displayableId);
            userInfo->givenName(givenName);
            userInfo->familyName(familyName);
            userInfo->identityProvider(identityProvider);
            userInfo->passwordExpiresOn(passwordExpiresOn);
            userInfo->passwordChangeUrl(changePasswordUri);

            result->updateTenantAndUserInfo(tenantId, tokenResponse->idToken, userInfo );
        }
    }
    else if (!tokenResponse->error.empty())
    {
        Logger::error(Tag(), "error: %, description: %", tokenResponse->error, tokenResponse->errorDescription);
        throw RmsauthException(tokenResponse->error, tokenResponse->errorDescription);
    }
    else
    {
        throw RmsauthException(Constants::rmsauthError().Unknown);
    }

    return result;
}