-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialhub.cpp
165 lines (146 loc) · 6.79 KB
/
serialhub.cpp
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
#include "serialhub.h"
/**
* @brief SerialHub::SerialHub create a new SerialHub object containing all connected battery testers
* (determined by pinging each serial port and checking the response)
*/
SerialHub::SerialHub() {
for(QSerialPortInfo port: QSerialPortInfo::availablePorts()){
//qDebug()<<port.portName();
if(port.portName().contains("ttyUSB") || port.portName().contains("ttyACM") || port.portName().contains("cu")){
qDebug()<<"contains USB or ACM"<<port.portName();
Serial serial(("/dev/"+port.portName().toStdString()).c_str());
serial.send("tester,\n");
QByteArray reply = serial.read();
qDebug()<<"Reply: "<<reply;
if(reply.contains("yes")){
QStringList testID=QString(reply).split(",");
deviceInfoList.push_back(new deviceInfo(("/dev/"+port.portName().toStdString()).c_str(),testID[1].toStdString(), "", 1.5));
devices.push_back(new SerialThread(("/dev/"+port.portName().toStdString()).c_str()));
QObject::connect(&*devices[devices.size()-1], &SerialThread::testerStateChange,
this, &SerialHub::on_testerStateChange);
QObject::connect(&*devices[devices.size()-1], &SerialThread::testEnded,
this, &SerialHub::on_testEnded);
QObject::connect(&*devices[devices.size()-1], &SerialThread::error,
this, &SerialHub::on_error);
QObject::connect(this, &SerialHub::errorDecisionMade,
&*devices[devices.size()-1], &SerialThread::handleErrorDecision);
QObject::connect(this, &SerialHub::signalStartTest,
&*devices[devices.size()-1], &SerialThread::startTest);
QObject::connect(&*devices[devices.size()-1], &SerialThread::finished,
&*devices[devices.size()-1], &SerialThread::deleteLater);
QObject::connect(&*devices[devices.size()-1], &SerialThread::voltageChange,
this, &SerialHub::on_voltageChange);
devices[devices.size()-1]->start();
}
}
}
}
void SerialHub::startTest(const char* testerID, const char* batteryID, double current) {
qDebug()<<"startTest() 1 in SerialHub";
int deviceIndex = getIndex(testerID);
if(deviceIndex != -1) {
deviceInfoList[deviceIndex]->batteryID = batteryID;
deviceInfoList[deviceIndex]->discharge_current = current;
deviceInfoList[deviceIndex]->running = true;
qDebug()<<"startTest() 2 in SerialHub";
emit signalStartTest(deviceInfoList[deviceIndex]->devicePort, batteryID, current);
}
}
/**
* @brief SerialHub::getIndex Get index of device in deviceInfoList with the specified testerID
* @param testerID The testerID of the device to find
* @return The index of the device in deviceInfoList with a testerID matching the provided string; if not found, -1 is returned
*/
int SerialHub::getIndex(std::string testerID) {
for(int i = 0 ; i < deviceInfoList.size() ; i ++) {
if(deviceInfoList[i]->testerID == testerID) {
return i;
}
}
return -1;
}
/**
* @brief SerialHub::getDeviceConfig Get device from deviceInfoList using the provided testerID
* @param testerID The testerID of the desired device
* @return The device with a testerID matching the provided string; If not found, an empty device is returned
*/
deviceInfo* SerialHub::getDeviceConfig(std::string testerID) {
for(int i = 0 ; i < deviceInfoList.size() ; i++) {
if(deviceInfoList[i]->testerID == testerID) {
return deviceInfoList[i];
}
}
deviceInfo *deviceInformation = new deviceInfo();
deviceInformation->batteryID = "DNE";
deviceInformation->testerID = "";
deviceInformation->discharge_current = 1.5;
return deviceInformation;
}
/**
* @brief SerialHub::on_testerStateChange Handle testerStatusChange signal from SerialThread. Notifies MainWindow of status change
* @param newState The state into which the tester has transitioned
* @param port The port of the tester whose state has changed
*/
void SerialHub::on_testerStateChange(int newState, std::string port) {
for (int i = 0 ; i < devices.size() ; i++) {
if(deviceInfoList[i]->devicePort == port) {
deviceInfoList[i]->testerState = newState;
deviceInfoList[i]->batReady = newState != IDLE; // TODO Check against finish state as well once finish state is added
}
}
if(newState == READY) { // TODO Tester Ready Mouse
emit testerMovedToReady(port); // TODO Tester Ready Mouse
} // TODO Tester Ready Mouse
emit statusChange();
}
/**
* @brief SerialHub::on_testEnded Handle testEnded signal from SerialThread. Notifies MainWindow of status change
* @param port String indicating the port of the tester whose test ended
*/
void SerialHub::on_testEnded(std::string port) {
for (int i = 0 ; i < devices.size() ; i++) {
if(deviceInfoList[i]->devicePort == port) {
deviceInfoList[i]->running = false;
deviceInfoList[i]->batteryID = "";
deviceInfoList[i]->discharge_current = 1.5;
deviceInfoList[i]->batReady = devices[i]->batReady;
deviceInfoList[i]->testerState = IDLE; // TODO Change to FINISH when finish state is added (to account for possible wait in firmware)
}
}
emit statusChange();
}
/**
* @brief SerialHub::on_error Handle error signal from SerialThread
* @param port Serial port of the tester throwing an error
* @param message Error message from tester
* @param prevState State tester was in when error was thrown
*/
void SerialHub::on_error(std::string port, QString message, int prevState) {
QString title = "";
for (int i = 0 ; i < devices.size() ; i++){
if(deviceInfoList[i]->devicePort == port) {
title += QString::fromStdString(deviceInfoList[i]->testerID);
switch(prevState) {
case IDLE:
title += ": Idle";
break;
case READY:
title += ": Ready";
break;
case CHARGE:
title += ": Charging";
break;
case DISCHARGE:
title += ": Discharging";
break;
default:
//qDebug()<<"SerialHub: Error was from state other than idle, ready, charging, or discharging";
break;
}
deviceInfoList[i]->errorTitle = title;
deviceInfoList[i]->errorMessage = message;
deviceInfoList[i]->error = true;
}
}
emit statusChange();
}