81 lines
2.0 KiB
C++
81 lines
2.0 KiB
C++
#include "FaultManager.hpp"
|
|
|
|
FaultManager::FaultManager(QObject *parent) : QObject(parent)
|
|
{
|
|
bool res;
|
|
|
|
res = OpenDatabase();
|
|
|
|
if (!res) {
|
|
qCritical() << "Failed to open database connection.";
|
|
exit(15);
|
|
}
|
|
|
|
res = OpenSerial();
|
|
|
|
if (!res) {
|
|
qCritical() << "Failure to open GPS serial device.";
|
|
exit(16);
|
|
}
|
|
|
|
StartGPS();
|
|
}
|
|
|
|
FaultManager::~FaultManager()
|
|
{
|
|
db.close();
|
|
gps->stopUpdates();
|
|
serial->close();
|
|
}
|
|
|
|
bool FaultManager::OpenDatabase()
|
|
{
|
|
db = QSqlDatabase::addDatabase("QMYSQL");
|
|
db.setHostName("localhost");
|
|
db.setDatabaseName("FaultDatabase");
|
|
db.setUserName("user");
|
|
db.setPassword("faultfinder");
|
|
return db.open();
|
|
}
|
|
|
|
bool FaultManager::OpenSerial()
|
|
{
|
|
serial = new QSerialPort();
|
|
serial->setPortName("ttyUSB0");
|
|
serial->setBaudRate(QSerialPort::Baud4800);
|
|
serial->setDataBits(QSerialPort::Data8);
|
|
serial->setParity(QSerialPort::NoParity);
|
|
serial->setStopBits(QSerialPort::OneStop);
|
|
serial->setFlowControl(QSerialPort::NoFlowControl);
|
|
return serial->open(QIODevice::ReadOnly);
|
|
}
|
|
|
|
void FaultManager::StartGPS()
|
|
{
|
|
gps = new QNmeaPositionInfoSource(QNmeaPositionInfoSource::RealTimeMode);
|
|
gps->setDevice((QIODevice *)serial);
|
|
gps->startUpdates();
|
|
}
|
|
|
|
void FaultManager::SaveImage(QByteArray *imageBuffer)
|
|
{
|
|
QSqlQuery query;
|
|
|
|
// Get GPS Location
|
|
QGeoPositionInfo position = gps->lastKnownPosition(true);
|
|
QGeoCoordinate coorinate = position.coordinate();
|
|
|
|
query.prepare("INSERT INTO Faults (latitude, longitude, altitude, image) "
|
|
"VALUES (:latitude, :longitude, :altitude, :image)");
|
|
if (coorinate.isValid())
|
|
{
|
|
// This probably wont work without proper converstions
|
|
query.bindValue(":latitude", coorinate.latitude());
|
|
query.bindValue(":longitude", coorinate.longitude());
|
|
query.bindValue(":altitude", coorinate.altitude());
|
|
}
|
|
query.bindValue(":image", *imageBuffer);
|
|
|
|
// Save to Database
|
|
query.exec();
|
|
} |