Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JSON format output #59

Merged
merged 2 commits into from
Oct 20, 2021
Merged

Added JSON format output #59

merged 2 commits into from
Oct 20, 2021

Conversation

Roethenbach
Copy link

I added some code for a JSON formatted output, as proposed in issue #45. I have not much experience with C++ so the code may be very ugly...

@jontio
Copy link
Owner

jontio commented Oct 1, 2021

Thanks very much for that. I'll have a look at it and test it out soon.

@jontio
Copy link
Owner

jontio commented Oct 19, 2021

Sorry it's taken so long. I had a better look. I haven't had much to do with json. The things like "char* nonacars = "false";" shouldn't be used. The output has the time appended before the json text. There is no escaping of things like cr and " (I think json can do this). I think arincparser.arincmessage.info should be used rather than or in addition to acarsitem.message. Qt has a json parser so that will take care of the escaping issue and make things easier. I should have time this afternoon to change it over to the qt json object and get something working. Currently I just tried connecting the json output of your code after I removed the prepeneded time to node red...
Untitled

@jontio
Copy link
Owner

jontio commented Oct 20, 2021

Getting there...

    if(settingsdialog->msgdisplayformat=="4")
    {
        ui->inputwidget->setLineWrapMode(QPlainTextEdit::NoWrap);
        QString message=acarsitem.message;
        message.replace('\r','\n');
        message.replace("\n\n","\n");
        if(message.right(1)=="\n")message.chop(1);
        if(message.left(1)=="\n")message.remove(0,1);
        message.replace("\n","\n\t");

        QString utcdate = QDateTime::currentDateTime().toUTC().toString("yyyy-MM-dd hh:mm:ss");
        if(acarsitem.TAK==0x15)TAKstr=((QString)"!").toLatin1();
        uchar label1=acarsitem.LABEL[1];
        if((uchar)acarsitem.LABEL[1]==127)label1='d';

        //json object creation
        QJsonObject json;
        //add database lookup info if available
        if(acarsitem.dblookupresult.size()==QMetaEnum::fromType<DataBaseTextUser::DataBaseSchema>().keyCount())
        {
            json["DB_MANUFACTURER"]=acarsitem.dblookupresult[DataBaseTextUser::DataBaseSchema::Manufacturer].trimmed();
            json["DB_TYPE"]=acarsitem.dblookupresult[DataBaseTextUser::DataBaseSchema::Type].trimmed();
            json["DB_OWNERS"]=acarsitem.dblookupresult[DataBaseTextUser::DataBaseSchema::RegisteredOwners].trimmed();
        }
        //add common things
        json["TIME"]=utcdate;
        json["NONACARS"]="false";
        json["AESID"]=((QString)"").sprintf("%06X",acarsitem.isuitem.AESID);
        json["GESID"]=((QString)"").sprintf("%02X",acarsitem.isuitem.GESID);
        json["QNO"]=((QString)"").sprintf("%02X",acarsitem.isuitem.QNO);
        json["REFNO"]=((QString)"").sprintf("%02X",acarsitem.isuitem.REFNO);
        json["REG"]=(QString)acarsitem.PLANEREG;
        //add acars message things
        if(!acarsitem.nonacars)
        {
            json["MODE"]=(QString)acarsitem.MODE;
            json["TAK"]=(QString)TAKstr;
            json["LABEL"]=(((QString)"").sprintf("%c%c",(uchar)acarsitem.LABEL[0],label1));
            json["BI"]=(QString)acarsitem.BI;
        }
        //if there is a message then add it and any parsing using arincparser
        if(!message.isEmpty())
        {
            json["MESSAGE"]=message;
            if(!arincparser.downlinkheader.flightid.isEmpty())json["FLIGHT"]=arincparser.downlinkheader.flightid;
            if(arincparser.arincmessage.info.size()>2)json["ARINCPARSER_MESSAGE_INFO"]=arincparser.arincmessage.info;
        }
        //convert json object to string
        humantext=QJsonDocument(json).toJson(QJsonDocument::Compact);

        //output result to udp and screen
        if((!settingsdialog->dropnontextmsgs)||(!message.isEmpty()&&(!acarsitem.nonacars)))
        {
            if(settingsdialog->udp_for_decoded_messages_enabled)
            {
                //send bottom text window to all udp sockects
                for(int ii=0;ii<udpsockets_bottom_textedit.size();ii++)
                {
                    if(ii>=settingsdialog->udp_for_decoded_messages_address.size())continue;
                    if(ii>=settingsdialog->udp_for_decoded_messages_port.size())continue;
                    QUdpSocket *sock=udpsockets_bottom_textedit[ii].data();
                    if((!sock->isOpen())||(!sock->isWritable()))
                    {
                        sock->close();
                        sock->connectToHost(settingsdialog->udp_for_decoded_messages_address[ii], settingsdialog->udp_for_decoded_messages_port[ii]);
                    }
                    if((sock->isOpen())&&(sock->isWritable()))sock->write((humantext+"\n").toLatin1().data());
                }
            }
            ui->inputwidget->appendPlainText(humantext);
            log(humantext);
        }
    }

image

Now "\n" and " are handled correctly. DB info is added to the json object. It uses QJsonObject. And parsing done so "ARINCPARSER_MESSAGE_INFO" is added and derived from "MESSAGE"

@jontio jontio changed the base branch from master to dev_json October 20, 2021 19:53
@jontio jontio merged commit 8ed7b27 into jontio:dev_json Oct 20, 2021
@jontio
Copy link
Owner

jontio commented Oct 20, 2021

I stuffed up the merge. I was working on my main branch and not your fork. I then forced a push after I had merged your pull request. That deleted your work from the commit history. I tried to undo the forced commit but your commit history didn't appear. I then tried to revert the pull request but that just forced me to make another pull request and that was in my name not yours. I'm not that good with git when things go wrong. The changes are in the dev_json branch and it seems to work as far as I can tell. If you want to test it out that would be good. I'll leave it there for a day or two for you and others to test, after that I'll merge that branch into master and delete the dev_json branch. If you want to be part of the commit history resend me your pull request and I'll try again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants