Intal version with working trainoverlord
This commit is contained in:
parent
a1f9fa172b
commit
872cc04a54
23 changed files with 992 additions and 46 deletions
|
|
@ -3,20 +3,87 @@
|
|||
#include <QIODevice>
|
||||
#include <QTcpSocket>
|
||||
#include <iostream>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QStandardPaths>
|
||||
#include <QFile>
|
||||
#include <QTimer>
|
||||
#include <signal.h>
|
||||
|
||||
#include "microcontroller.h"
|
||||
#include "nfcuid.h"
|
||||
#include "overlorditemstore.h"
|
||||
#include "train.h"
|
||||
#include "turnout.h"
|
||||
#include "signal.h"
|
||||
#include "layout.h"
|
||||
|
||||
void gotTag(NfcUid uid)
|
||||
void sigHandler(int sig)
|
||||
{
|
||||
std::cout<<"Got tag from "<<uid.reader<<" uid ";
|
||||
for(size_t i = 0; i < uid.length; ++i)
|
||||
QCoreApplication::quit();
|
||||
}
|
||||
|
||||
QJsonObject getJsonObjectFromDisk(const QString& filePath, bool* error)
|
||||
{
|
||||
QFile file;
|
||||
|
||||
if(filePath.size() > 0)
|
||||
file.setFileName(filePath);
|
||||
else
|
||||
file.setFileName(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/trainoverloard.json");
|
||||
|
||||
file.open(QIODevice::ReadOnly);
|
||||
if(!file.isOpen())
|
||||
{
|
||||
std::cout<<(int)uid.bytes[i]<<(i == uid.length-1 ? "" : ":");
|
||||
std::cerr<<"Can not open config file: "<<filePath.toLatin1().data()<<std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
QJsonParseError qerror;
|
||||
QJsonDocument document(QJsonDocument::fromJson(file.readAll(), &qerror));
|
||||
file.close();
|
||||
if(qerror.error != QJsonParseError::NoError)
|
||||
{
|
||||
qDebug()<<filePath<<" "<<qerror.errorString();
|
||||
if(error)
|
||||
(*error) = true;
|
||||
}
|
||||
return document.object();
|
||||
}
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
bool storeJsonObjectToDisk(const QJsonObject& json, QString filePath = QString())
|
||||
{
|
||||
if(filePath.size() == 0)
|
||||
{
|
||||
filePath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/trainoverloard.json";
|
||||
}
|
||||
QFile file(filePath + ".out");
|
||||
|
||||
qDebug()<<"config file: "<<filePath;
|
||||
file.open(QIODevice::WriteOnly);
|
||||
if(!file.isOpen())
|
||||
{
|
||||
std::cerr<<"Can not open config file: "<<filePath.toLatin1().data()<<std::endl;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
QJsonDocument document(json);
|
||||
file.write(document.toJson());
|
||||
file.close();
|
||||
QFile::remove(filePath);
|
||||
file.rename(filePath);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void getItemsCb(QTimer* timer, ItemStore* items, Microcontroller* micro)
|
||||
{
|
||||
if(items->getItems()->empty())
|
||||
{
|
||||
micro->requestState();
|
||||
timer->start(1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -24,6 +91,8 @@ int main(int argc, char *argv[])
|
|||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
|
||||
signal(SIGINT, &sigHandler);
|
||||
|
||||
//set info
|
||||
QCoreApplication::setOrganizationName("UVOS");
|
||||
QCoreApplication::setOrganizationDomain("uvos.xyz");
|
||||
|
|
@ -60,9 +129,12 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
OverlordItemStore items;
|
||||
Layout layout;
|
||||
|
||||
QObject::connect(&items, &OverlordItemStore::itemAdded, &layout, &Layout::itemAdded);
|
||||
QObject::connect(&items, &OverlordItemStore::trainArrivedAtReader, &layout, &Layout::trainArrivedAtReader);
|
||||
|
||||
Microcontroller micro(µSocket);
|
||||
QObject::connect(µ, &Microcontroller::gotTag, gotTag);
|
||||
QObject::connect(µ, &Microcontroller::gotTag, &items, &OverlordItemStore::gotNfcTag);
|
||||
QObject::connect(µ, &Microcontroller::gotItemList, &items, &OverlordItemStore::addItems);
|
||||
QObject::connect(µ, &Microcontroller::itemChanged, &items, &OverlordItemStore::itemStateChanged);
|
||||
|
|
@ -71,5 +143,27 @@ int main(int argc, char *argv[])
|
|||
Turnout::micro = µ
|
||||
Signal::micro = µ
|
||||
|
||||
return a.exec();
|
||||
QTimer timer;
|
||||
timer.setSingleShot(false);
|
||||
QObject::connect(&timer, &QTimer::timeout, &timer, [µ, &timer, &items](){getItemsCb(&timer, &items, µ);});
|
||||
getItemsCb(&timer, &items, µ);
|
||||
|
||||
{
|
||||
bool err = false;
|
||||
QJsonObject json = getJsonObjectFromDisk(QString(), &err);
|
||||
if(err)
|
||||
{
|
||||
std::cerr<<"Could not load config file\n";
|
||||
return -1;
|
||||
}
|
||||
layout.load(json);
|
||||
}
|
||||
|
||||
int ret = a.exec();
|
||||
/*
|
||||
QJsonObject jsonStore;
|
||||
layout.store(jsonStore);
|
||||
storeJsonObjectToDisk(jsonStore);
|
||||
*/
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue