-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcesmc.cpp
More file actions
145 lines (129 loc) · 4.62 KB
/
cesmc.cpp
File metadata and controls
145 lines (129 loc) · 4.62 KB
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
#include "cesmc.h"
#include <iostream>
CESmc::CESmc(MainWindow* window)
{
this->window = window;
battery = Battery();
intensity = new Intensity(); // default value for any session.
connection = new Connection(); //default connection value is off
database = window->getDB();
power = false; // if device is on or off
inSession = false; //the session is not running.
therapyGroup = 0; // how long the session should run for
therapyType = "";
maxIntensityLevel = intensity->getMaxIntensity();
}
void CESmc::togglePower(){
if(power){
if(inSession){ //if the user is in a session soft off
currTime = 1; //move time remaining to 1 left.
softOff(); //soft off the device.
}
}else{
if(battery.getCharge() <= 0.0){
return;
}
}
power = !power;
}
void CESmc::toggleConnection(){ //toggles the connection on and off
connection->setStatus(!connection->testConnection());
}
bool CESmc::InitCES()
{
// CES initialization code under Power button in GUI
// should be transfered to this method.
// better not to repeat writing code.
return true;
}
void CESmc::runSession()
{
if(runConnectionTest()){
beginTherapy();
} else {
window->navigateBack();
}
}
bool CESmc::runConnectionTest(){
/*if(!inSession){ //if the session has started, do not use the UI.
window->initializeConnectionTestUI(connection->testConnection());
}*/
return connection->testConnection();
}
void CESmc::beginTherapy() {
inSession = true;
currentTherapy = new therapy(therapyType, therapyGroup, intensity->getIntensity());
currTime = currentTherapy->getTime();
timeString = QString::number(currTime/60) + ((currTime%60 < 10) ? + ":0" + QString::number(currTime%60) : + ":" + QString::number(currTime%60));
//timeString = QString::number(currTime/60) + ":00";
window->beginTherapyUI(currentTherapy);
window->initializeTimer(currentTherapy->getTimer());
record* newR = new record(currentTherapy->getName(), QDateTime::currentDateTime(), 0, 0);
recordings.append(newR);
maxIntensityLevel = intensity->getIntensity();
}
void CESmc::changeIntensity(bool state){
if(state){
intensity->increaseIntensity();
} else {
intensity->decreaseIntensity();
}
}
void CESmc::updateTimer() {
if(connection->testConnection()){
window->drainBattery();
if (currentTherapy->getTime() == 0) {
currTime += 1;
}
else {
currTime -= 1;
}
timeString = QString::number(currTime/60) + ((currTime%60 < 10) ? + ":0" + QString::number(currTime%60) : + ":" + QString::number(currTime%60));
}
if (currTime == 0 && currentTherapy->getTime() != 0) {
inSession = false;
//Save record
recordings.last()->setDuration(currentTherapy->getTime());
recordings.last()->setPowerLevel(maxIntensityLevel);
//database->addTherapyRecord(recordings.last()->getTreatment(),recordings.last()->getStartTime(),recordings.last()->getPowerLevel(),recordings.last()->getDuration());
allRecordings += recordings.last()->toString();
currTime = -1;
currentTherapy->getTimer()->stop();
currentTherapy->getTimer()->disconnect();
currentTherapy = nullptr;
window->navigateBack();
}
}
void CESmc::drainBattery() {
battery.setCharge( (intensity->getIntensity() == 0) ? battery.getCharge() - 0.05 : qMax(battery.getCharge() - intensity->getIntensity()/10.0, 0.0) );
}
void CESmc::softOff(){
while(getIntensity() > 1){
QTest::qSleep(250);
changeIntensity(false);
window->softOffUpdate();
}
}
bool CESmc::getPower(){return power;}
int CESmc::getIntensity(){return intensity->getIntensity();}
void CESmc::setTherapyGroup(int i){therapyGroup = i;}
void CESmc::setTherapyType(QString type){therapyType = type;}
QString CESmc::getTherapyType(){return therapyType;}
void CESmc::setBatteryCharge(double newLevel){
if (newLevel >= 0.0 && newLevel < 100.0) {
if (newLevel == 0.0 && getPower() == true) {
togglePower();
battery.setCharge(0.0);
}else{
battery.setCharge(newLevel);
}
} else {
if (getPower() == true) {
togglePower();
battery.setCharge(0.0);
}
}
}
double CESmc::getBatteryCharge(){return battery.getCharge();}
bool CESmc::getInSession(){return inSession;}
int CESmc::getCurrTime(){return currTime;}