
#include "mainwindow.h"
#include "TcpClient.h"
#include "common.h"

int main(int argc, char *argv[]) {

    QApplication a(argc, argv);

    QDir dir("shared_key.txt");
    QByteArray line;
    QFile file("shared_key.txt");
    while(!file.exists())
    {
        dir.mkdir("shared_key.txt");//只创建一级子目录，即必须保证上级目录存在
        file.open(QIODevice::WriteOnly|QIODevice::Text);
        QDateTime current_date_time =QDateTime::currentDateTime();
        QString current_date =current_date_time.toString("hh:mm:ss.zzz");//将时间做为key值写入文件一次
        file.write(current_date.toUtf8());
        file.close();
        break;
    }
    file.open(QIODevice::ReadOnly);
    line=file.readLine();
    file.close();
    QString const key=line;
    QSharedMemory shared_memory;
    shared_memory.setKey(key);//设置固定共享内存段的key值
    if(shared_memory.attach())   //尝试将进程附加到该共享内存段
    {
        QMessageBox messageBox(QMessageBox::Warning,QString::fromUtf8("提示"),QString::QString::fromUtf8("当前程序正在运行中..."));
        messageBox.setButtonText(QMessageBox::Ok,QString::QString::fromUtf8("确认！"));
        messageBox.exec();
        return 0;
    }

    MatrixAILog::Log::SetLogLevel("DEBUG");

    if(shared_memory.create(1)) //创建1byte的共享内存段
    {
        SetConsoleOutputCP(CP_UTF8);
        QApplication::setWindowIcon(QIcon("./logo.ico"));
        MainWindow w;
        w.show();

//        TcpClient tcpClient;
//        tcpClient.connectToServer("127.0.0.1",7000);     //与视频车号通信ip端口
        return QApplication::exec();
    }
    return QApplication::exec();
}
