123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- #include "otaclient.h"
- #include <iostream>
- #include <QFile>
- #include <QDir>
- #include <thread>
- #include "md5file.h"
- #include "xmlparam.h"
- #ifndef UBUNTU1604
- #include "ivlog.h"
- extern iv::Ivlog * givlog;
- #endif
- extern std::string gstrserverip;//"123.57.212.138";
- extern std::string gstrserverport;//"9000";
- extern std::string gstrwaitnettime;
- otaclient::otaclient()
- {
- mstrserverip = gstrserverip;
- mstrserverport = gstrserverport;
- iv::xmlparam::Xmlparam xp("./vin.xml");
- std::string strvin = xp.GetParam("VIN","AAAAAAAAAAAAAAAAA");
- std::string strvehicletype = xp.GetParam("VehicleType","");
- if(strvehicletype == "")
- {
- mbInitOK = false;
- std::cout<<"xml not found"<<std::endl;
- #ifndef UBUNTU1604
- givlog->warn("xml not found");
- #endif
- return;
- }
- mstrVIN = strvin;
- mstrVehicleType = strvehicletype;
- std::cout<<"vin:"<<mstrVIN<<std::endl;
- std::cout<<"VehicleType:"<<mstrVehicleType<<std::endl;
- QFile xFile;
- QByteArray ba;
- xFile.setFileName("./version");
- if(xFile.open(QIODevice::ReadOnly))
- {
- ba = xFile.readAll();
- mstrVersion = ba.toStdString();
- while(mstrVersion.size()>0)
- {
- if(mstrVersion.at(mstrVersion.size() -1) == '\n')
- {
- mstrVersion.erase(mstrVersion.size()-1,1);
- }
- else
- {
- break;
- }
- }
- std::cout<<"version:"<<mstrVersion<<std::endl;
- }
- else
- {
- std::cout<<"version file is not exist."<<std::endl;
- // givlog->warn("version file not found");
- mbInitOK = false;
- }
- xFile.close();
- }
- void otaclient::run()
- {
- if(mbInitOK == false)
- {
- std::cout<<"Init Fail. so not connect server."<<std::endl;
- #ifndef UBUNTU1604
- givlog->warn("Init Fail. so not connect server.");
- #endif
- return;
- }
- int mswait = atoi(gstrwaitnettime.data());
- msleep(mswait);
- std::string target_str =mstrserverip +":";
- target_str = target_str + mstrserverport ;//std::to_string()
- auto cargs = grpc::ChannelArguments();
- cargs.SetMaxReceiveMessageSize(1024 * 1024 * 1024); // 1 GB
- cargs.SetMaxSendMessageSize(1024 * 1024 * 1024);
- std::shared_ptr<Channel> channel = grpc::CreateCustomChannel(
- target_str, grpc::InsecureChannelCredentials(),cargs);
- std::unique_ptr<iv::OTA::Stub> stub_ = iv::OTA::NewStub(channel);
- iv::ota::queryReply xReply;
- iv::ota::queryreq xReq;
- xReq.set_strversion(mstrVersion.data(),mstrVersion.size());
- xReq.set_strvehicletype(mstrVehicleType.data(),mstrVehicleType.size());
- xReq.set_strvin(mstrVIN.data(),mstrVIN.size());
- ClientContext context ;
- bool bNeedUpdate = false;
- Status status = stub_->query(&context, xReq, &xReply);
- if (status.ok()) {
- std::cout<<" query successfully"<<std::endl;
- if(xReply.bupdate() == 1)
- {
- bNeedUpdate = true;
- }
- } else {
- std::cout << status.error_code() << ": " << status.error_message()
- << std::endl;
- std::cout<<"RPC failed"<<std::endl;
- #ifndef UBUNTU1604
- givlog->warn("ota RPC Failed");
- #endif
- std::this_thread::sleep_for(std::chrono::milliseconds(900));
- return;
- }
- if(bNeedUpdate == false)
- {
- std::cout<<"Not Need Update."<<std::endl;
- return;
- }
- std::cout<<"version:"<<xReply.strversion()<<std::endl;
- std::cout<<" file size : "<<xReply.nfilesize()<<std::endl;
- std::cout<<" md5: "<<xReply.strmd5()<<std::endl;
- QByteArray baFile;
- iv::ota::Filereq xFilereq;
- iv::ota::FileReply xFileReply;
- xFilereq.set_strversion(mstrVersion.data(),mstrVersion.size());
- xFilereq.set_strvehicletype(mstrVehicleType.data(),mstrVehicleType.size());
- xFilereq.set_strvin(mstrVIN.data(),mstrVIN.size());
- const qint64 nFilePacMaxSize = 10000000;//10M per packet.
- bool bComplete = false;
- qint64 nPos = 0;
- while(bComplete == false)
- {
- ClientContext context2;
- xFilereq.set_nsize(nFilePacMaxSize);
- xFilereq.set_npos(nPos);
- Status status2 = stub_->downfile(&context2, xFilereq, &xFileReply);
- if (status2.ok()) {
- // std::cout<<" down successfully"<<std::endl;
- if(xFileReply.bupdate() == 1)
- {
- std::cout<<"file ok. size: "<<xFileReply.xdata().size()<<" pos:"<<nPos<<std::endl;
- #ifndef UBUNTU1604
- givlog->verbose("file ok.size: %ld pos: %ld",xFileReply.xdata().size(),nPos);
- #endif
- nPos = nPos + xFileReply.nsize();
- baFile.append(xFileReply.xdata().data(),xFileReply.xdata().size());
- if(xFileReply.blastpac())
- {
- bComplete = true;
- }
- }
- else
- {
- break;
- }
- } else {
- std::cout << status2.error_code() << ": " << status2.error_message()
- << std::endl;
- std::cout<<"RPC failed"<<std::endl;
- #ifndef UBUNTU1604
- givlog->warn("ota download file RPC Failed");
- #endif
- std::this_thread::sleep_for(std::chrono::milliseconds(900));
- break;
- }
- }
- QDir xDir;
- if(bComplete)
- {
- if((xFileReply.nfilesize() == nPos)&&(baFile.size() == nPos))
- {
- qDebug("down file succesfully.");
- QFile xFile;
- std::string strfilepath = "./../temp.zip";
- std::string strfileupdate = "./../update.zip";
- xFile.setFileName(strfilepath.data());
- if(xFile.open(QIODevice::ReadWrite))
- {
- xFile.write(baFile);
- xFile.close();
- std::string strfilemd5 = getFileMD5(strfilepath);
- std::cout<<" calc md5 is "<<strfilemd5<<std::endl;
- if(strfilemd5 == xFileReply.strmd5())
- {
- qDebug("file md5 ok");
- #ifndef UBUNTU1604
- givlog->verbose("file md5 ok");
- #endif
- QString oldname = strfilepath.data();
- QString newname = strfileupdate.data();
- QFile xFileold;
- xFileold.setFileName(newname);
- bool bCanRename = true;
- if(xFileold.exists())
- {
- if(xDir.remove(newname))
- {
- }
- else
- {
- bCanRename = false;
- qDebug("can't remove old file. FAIL.");
- #ifndef UBUNTU1604
- givlog->warn("can't remove old file. FAIL.");
- #endif
- }
- }
- if(bCanRename)
- {
- bool bRename = xDir.rename(oldname,newname);
- if(bRename)
- {
- qDebug("Successfully rename to update.zip.");
- #ifndef UBUNTU1604
- givlog->verbose("Successfully rename to update.zip.");
- #endif
- }
- else
- {
- qDebug("Fail rename to update.zip.");
- #ifndef UBUNTU1604
- givlog->verbose("Fail rename to update.zip.");
- #endif
- }
- }
- }
- else
- {
- qDebug("file md5 fail. calc md5 is %s server md5 is %s ",
- strfilemd5.data(),xFileReply.strmd5().data());
- #ifndef UBUNTU1604
- givlog->warn("file md5 fail. calc md5 is %s server md5 is %s ",
- strfilemd5.data(),xFileReply.strmd5().data());
- #endif
- }
- }
- else
- {
- xFile.close();
- qDebug("save down file error.");
- #ifndef UBUNTU1604
- givlog->warn("save down file error.");
- #endif
- }
- }
- else
- {
- qDebug("nFileSize is %ld pos is %ld baFile size is %ld",xFileReply.nfilesize(),nPos,
- baFile.size());
- #ifndef UBUNTU1604
- givlog->verbose("nFileSize is %ld pos is %ld baFile size is %ld",xFileReply.nfilesize(),nPos,
- baFile.size());
- #endif
- }
- }
- else
- {
- qDebug("DownLoad File Error.");
- #ifndef UBUNTU1604
- givlog->warn("DownLoad File Error.");
- #endif
- }
- }
|