otaclient.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include "otaclient.h"
  2. #include <iostream>
  3. #include <QFile>
  4. #include <QDir>
  5. #include <thread>
  6. #include "md5file.h"
  7. #include "xmlparam.h"
  8. #ifndef UBUNTU1604
  9. #include "ivlog.h"
  10. extern iv::Ivlog * givlog;
  11. #endif
  12. extern std::string gstrserverip;//"123.57.212.138";
  13. extern std::string gstrserverport;//"9000";
  14. extern std::string gstrwaitnettime;
  15. otaclient::otaclient()
  16. {
  17. mstrserverip = gstrserverip;
  18. mstrserverport = gstrserverport;
  19. iv::xmlparam::Xmlparam xp("./vin.xml");
  20. std::string strvin = xp.GetParam("VIN","AAAAAAAAAAAAAAAAA");
  21. std::string strvehicletype = xp.GetParam("VehicleType","");
  22. if(strvehicletype == "")
  23. {
  24. mbInitOK = false;
  25. std::cout<<"xml not found"<<std::endl;
  26. #ifndef UBUNTU1604
  27. givlog->warn("xml not found");
  28. #endif
  29. return;
  30. }
  31. mstrVIN = strvin;
  32. mstrVehicleType = strvehicletype;
  33. std::cout<<"vin:"<<mstrVIN<<std::endl;
  34. std::cout<<"VehicleType:"<<mstrVehicleType<<std::endl;
  35. QFile xFile;
  36. QByteArray ba;
  37. xFile.setFileName("./version");
  38. if(xFile.open(QIODevice::ReadOnly))
  39. {
  40. ba = xFile.readAll();
  41. mstrVersion = ba.toStdString();
  42. while(mstrVersion.size()>0)
  43. {
  44. if(mstrVersion.at(mstrVersion.size() -1) == '\n')
  45. {
  46. mstrVersion.erase(mstrVersion.size()-1,1);
  47. }
  48. else
  49. {
  50. break;
  51. }
  52. }
  53. std::cout<<"version:"<<mstrVersion<<std::endl;
  54. }
  55. else
  56. {
  57. std::cout<<"version file is not exist."<<std::endl;
  58. // givlog->warn("version file not found");
  59. mbInitOK = false;
  60. }
  61. xFile.close();
  62. }
  63. void otaclient::run()
  64. {
  65. if(mbInitOK == false)
  66. {
  67. std::cout<<"Init Fail. so not connect server."<<std::endl;
  68. #ifndef UBUNTU1604
  69. givlog->warn("Init Fail. so not connect server.");
  70. #endif
  71. return;
  72. }
  73. int mswait = atoi(gstrwaitnettime.data());
  74. msleep(mswait);
  75. std::string target_str =mstrserverip +":";
  76. target_str = target_str + mstrserverport ;//std::to_string()
  77. auto cargs = grpc::ChannelArguments();
  78. cargs.SetMaxReceiveMessageSize(1024 * 1024 * 1024); // 1 GB
  79. cargs.SetMaxSendMessageSize(1024 * 1024 * 1024);
  80. std::shared_ptr<Channel> channel = grpc::CreateCustomChannel(
  81. target_str, grpc::InsecureChannelCredentials(),cargs);
  82. std::unique_ptr<iv::OTA::Stub> stub_ = iv::OTA::NewStub(channel);
  83. iv::ota::queryReply xReply;
  84. iv::ota::queryreq xReq;
  85. xReq.set_strversion(mstrVersion.data(),mstrVersion.size());
  86. xReq.set_strvehicletype(mstrVehicleType.data(),mstrVehicleType.size());
  87. xReq.set_strvin(mstrVIN.data(),mstrVIN.size());
  88. ClientContext context ;
  89. bool bNeedUpdate = false;
  90. Status status = stub_->query(&context, xReq, &xReply);
  91. if (status.ok()) {
  92. std::cout<<" query successfully"<<std::endl;
  93. if(xReply.bupdate() == 1)
  94. {
  95. bNeedUpdate = true;
  96. }
  97. } else {
  98. std::cout << status.error_code() << ": " << status.error_message()
  99. << std::endl;
  100. std::cout<<"RPC failed"<<std::endl;
  101. #ifndef UBUNTU1604
  102. givlog->warn("ota RPC Failed");
  103. #endif
  104. std::this_thread::sleep_for(std::chrono::milliseconds(900));
  105. return;
  106. }
  107. if(bNeedUpdate == false)
  108. {
  109. std::cout<<"Not Need Update."<<std::endl;
  110. return;
  111. }
  112. std::cout<<"version:"<<xReply.strversion()<<std::endl;
  113. std::cout<<" file size : "<<xReply.nfilesize()<<std::endl;
  114. std::cout<<" md5: "<<xReply.strmd5()<<std::endl;
  115. QByteArray baFile;
  116. iv::ota::Filereq xFilereq;
  117. iv::ota::FileReply xFileReply;
  118. xFilereq.set_strversion(mstrVersion.data(),mstrVersion.size());
  119. xFilereq.set_strvehicletype(mstrVehicleType.data(),mstrVehicleType.size());
  120. xFilereq.set_strvin(mstrVIN.data(),mstrVIN.size());
  121. const qint64 nFilePacMaxSize = 10000000;//10M per packet.
  122. bool bComplete = false;
  123. qint64 nPos = 0;
  124. while(bComplete == false)
  125. {
  126. ClientContext context2;
  127. xFilereq.set_nsize(nFilePacMaxSize);
  128. xFilereq.set_npos(nPos);
  129. Status status2 = stub_->downfile(&context2, xFilereq, &xFileReply);
  130. if (status2.ok()) {
  131. // std::cout<<" down successfully"<<std::endl;
  132. if(xFileReply.bupdate() == 1)
  133. {
  134. std::cout<<"file ok. size: "<<xFileReply.xdata().size()<<" pos:"<<nPos<<std::endl;
  135. #ifndef UBUNTU1604
  136. givlog->verbose("file ok.size: %ld pos: %ld",xFileReply.xdata().size(),nPos);
  137. #endif
  138. nPos = nPos + xFileReply.nsize();
  139. baFile.append(xFileReply.xdata().data(),xFileReply.xdata().size());
  140. if(xFileReply.blastpac())
  141. {
  142. bComplete = true;
  143. }
  144. }
  145. else
  146. {
  147. break;
  148. }
  149. } else {
  150. std::cout << status2.error_code() << ": " << status2.error_message()
  151. << std::endl;
  152. std::cout<<"RPC failed"<<std::endl;
  153. #ifndef UBUNTU1604
  154. givlog->warn("ota download file RPC Failed");
  155. #endif
  156. std::this_thread::sleep_for(std::chrono::milliseconds(900));
  157. break;
  158. }
  159. }
  160. QDir xDir;
  161. if(bComplete)
  162. {
  163. if((xFileReply.nfilesize() == nPos)&&(baFile.size() == nPos))
  164. {
  165. qDebug("down file succesfully.");
  166. QFile xFile;
  167. std::string strfilepath = "./../temp.zip";
  168. std::string strfileupdate = "./../update.zip";
  169. xFile.setFileName(strfilepath.data());
  170. if(xFile.open(QIODevice::ReadWrite))
  171. {
  172. xFile.write(baFile);
  173. xFile.close();
  174. std::string strfilemd5 = getFileMD5(strfilepath);
  175. std::cout<<" calc md5 is "<<strfilemd5<<std::endl;
  176. if(strfilemd5 == xFileReply.strmd5())
  177. {
  178. qDebug("file md5 ok");
  179. #ifndef UBUNTU1604
  180. givlog->verbose("file md5 ok");
  181. #endif
  182. QString oldname = strfilepath.data();
  183. QString newname = strfileupdate.data();
  184. QFile xFileold;
  185. xFileold.setFileName(newname);
  186. bool bCanRename = true;
  187. if(xFileold.exists())
  188. {
  189. if(xDir.remove(newname))
  190. {
  191. }
  192. else
  193. {
  194. bCanRename = false;
  195. qDebug("can't remove old file. FAIL.");
  196. #ifndef UBUNTU1604
  197. givlog->warn("can't remove old file. FAIL.");
  198. #endif
  199. }
  200. }
  201. if(bCanRename)
  202. {
  203. bool bRename = xDir.rename(oldname,newname);
  204. if(bRename)
  205. {
  206. qDebug("Successfully rename to update.zip.");
  207. #ifndef UBUNTU1604
  208. givlog->verbose("Successfully rename to update.zip.");
  209. #endif
  210. }
  211. else
  212. {
  213. qDebug("Fail rename to update.zip.");
  214. #ifndef UBUNTU1604
  215. givlog->verbose("Fail rename to update.zip.");
  216. #endif
  217. }
  218. }
  219. }
  220. else
  221. {
  222. qDebug("file md5 fail. calc md5 is %s server md5 is %s ",
  223. strfilemd5.data(),xFileReply.strmd5().data());
  224. #ifndef UBUNTU1604
  225. givlog->warn("file md5 fail. calc md5 is %s server md5 is %s ",
  226. strfilemd5.data(),xFileReply.strmd5().data());
  227. #endif
  228. }
  229. }
  230. else
  231. {
  232. xFile.close();
  233. qDebug("save down file error.");
  234. #ifndef UBUNTU1604
  235. givlog->warn("save down file error.");
  236. #endif
  237. }
  238. }
  239. else
  240. {
  241. qDebug("nFileSize is %ld pos is %ld baFile size is %ld",xFileReply.nfilesize(),nPos,
  242. baFile.size());
  243. #ifndef UBUNTU1604
  244. givlog->verbose("nFileSize is %ld pos is %ld baFile size is %ld",xFileReply.nfilesize(),nPos,
  245. baFile.size());
  246. #endif
  247. }
  248. }
  249. else
  250. {
  251. qDebug("DownLoad File Error.");
  252. #ifndef UBUNTU1604
  253. givlog->warn("DownLoad File Error.");
  254. #endif
  255. }
  256. }