CanModelDevice.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. #include "stdafx.h"
  2. #include <Windows.h>
  3. #include <assert.h>
  4. #include <iostream>
  5. #include <functional>
  6. #include "CanModelDPC.h"
  7. #include "common_api.h"
  8. #include "PacketAnalizer.h"
  9. #include "CanModelDevice.h"
  10. #include "ImagePool.h"
  11. //#include "logger.h"
  12. using namespace std::placeholders;
  13. CanModelDevice::CanModelDevice()
  14. {
  15. m_ImagePool = new ImagePoolEx();
  16. m_DisconnectEvt = 0;
  17. //m_pMqttClient = NULL;
  18. m_bSendIndependently = true;
  19. m_funcRoate = nullptr;
  20. m_funcFlip = nullptr;
  21. m_nRotateAngle = 0;
  22. m_nFlipDirection = 0xFFFF;
  23. }
  24. CanModelDevice::~CanModelDevice()
  25. {
  26. CompleteUnInit();
  27. delete m_ImagePool;
  28. }
  29. bool ConverCmdType_IoLevel2Ccos(ATTRACTION iocmd, PACKET_CMD &ccoscmd)
  30. {
  31. if (iocmd == ATTRACTION_GET)
  32. {
  33. ccoscmd = PACKET_CMD_GET;
  34. }
  35. else if (iocmd == ATTRACTION_SET)
  36. {
  37. ccoscmd = PACKET_CMD_UPDATE;
  38. }
  39. else if (iocmd == ATTRACTION_ADD)
  40. {
  41. ccoscmd = PACKET_CMD_ADD;
  42. }
  43. else if (iocmd == ATTRACTION_DEL)
  44. {
  45. ccoscmd = PACKET_CMD_DEL;
  46. }
  47. else if (iocmd == ATTRACTION_UPDATE)
  48. {
  49. ccoscmd = PACKET_CMD_UPDATE;
  50. }
  51. else if (iocmd == ATTRACTION_DATA)
  52. {
  53. ccoscmd = PACKET_CMD_DATA;
  54. }
  55. else if (iocmd == ATTRACTION_MSG)
  56. {
  57. ccoscmd = PACKET_CMD_MSG;
  58. }
  59. else
  60. {
  61. //wtf??
  62. //mLog::FERROR("unknown iocmd:{$},packetcmd:{$}", (int)iocmd, (int)ccoscmd);
  63. return false;
  64. }
  65. return true;
  66. }
  67. bool ConverCmdType_Ccos2IoLevel(PACKET_CMD ccoscmd, ATTRACTION &iocmd)
  68. {
  69. if (ccoscmd == PACKET_CMD_GET)
  70. {
  71. iocmd = ATTRACTION_GET;
  72. }
  73. else if (ccoscmd == PACKET_CMD_UPDATE)
  74. {
  75. iocmd = ATTRACTION_SET;
  76. }
  77. else if (ccoscmd == PACKET_CMD_ADD)
  78. {
  79. iocmd = ATTRACTION_ADD;
  80. }
  81. else if (ccoscmd == PACKET_CMD_DEL)
  82. {
  83. iocmd = ATTRACTION_DEL;
  84. }
  85. else if (ccoscmd == PACKET_CMD_PART_UPDATE)
  86. {
  87. iocmd = ATTRACTION_UPDATE;
  88. }
  89. else if (ccoscmd == PACKET_CMD_DATA)
  90. {
  91. iocmd = ATTRACTION_DATA;
  92. }
  93. else if (ccoscmd == PACKET_CMD_MSG)
  94. {
  95. iocmd = ATTRACTION_MSG;
  96. }
  97. else
  98. {
  99. //wtf??
  100. //mLog::FERROR("unknown iocmd:{$},packetcmd:{$}", (int)iocmd, (int)ccoscmd);
  101. return false;
  102. }
  103. return true;
  104. }
  105. void CanModelDevice::SubscribeSelf()
  106. {
  107. LogicDevice::SubscribeSelf();
  108. //委托IODevice订阅
  109. m_pMidObject->SubscribeSelf();
  110. }
  111. void CanModelDevice::Init(std::unique_ptr <nsDEV::IODevice>&& pMid, HANDLE DisconnectEvt)
  112. {
  113. //mLog::FINFO("CanModelDevice::Init...");
  114. m_pMidObject = std::move(pMid);
  115. //g_pDPCDeviceObject = this;
  116. //auto a = std::bind(&CanModelDevice::NotifyCallBackEntry, this, _1, _2, _3);
  117. m_pMidObject->EventCenter->OnNotify.Push(this,&CanModelDevice::NotifyCallBackEntry);
  118. //auto b = std::bind(&CanModelDevice::RawDataNotifyCallBackEntry, this, _1, _2, _3, _4, _5, _6);
  119. m_pMidObject->EventCenter->OnDataNotify.Push(this, &CanModelDevice::RawDataNotifyCallBackEntry);
  120. //auto c = std::bind(&CanModelDevice::NotifyCallBackSetBlockSize, this, _1, _2, _3, _4, _5);
  121. m_pMidObject->EventCenter->OnMaxBlockSize.Push(this, &CanModelDevice::NotifyCallBackSetBlockSize);
  122. m_pMidObject->EventCenter->OnSystemLog.Push(this, &CanModelDevice::OnSystemLog);
  123. m_pMidObject->EventCenter->OnLog.Push(this, &CanModelDevice::OnLog);
  124. //mLog::FINFO("CanModelDevice:: ...m_pMidObject->Prepare");
  125. m_pMidObject->Prepare();
  126. CompleteInit();
  127. m_pMidObject->SetClientRootID((m_strClientID + "_IODevice").c_str(), "");
  128. m_pMidObject->CompleteInit();
  129. //regist notify call back
  130. //typedef RET_STATUS(DEVICE_ACTION CanModelDevice::*callbackentry) (ATTRACTION cmdType, string &keyType, string &Context);
  131. //callbackentry func = (callbackentry)&CanModelDevice::CallBackEntry;
  132. //typedef union _funcContext {
  133. // callbackentry func_entry;
  134. // NotifyCALLBACK func_callback;
  135. //}FUNCCONTEXT;
  136. //FUNCCONTEXT fc;
  137. //memset(&fc, 0, sizeof(fc));
  138. //fc.func_entry = func;
  139. //NotifyCALLBACK callback = fc.func_callback;
  140. //std::function<void(const Foo&, int)> fun = std::mem_fn(&Foo::bar);
  141. m_DisconnectEvt = DisconnectEvt;
  142. }
  143. #define CcosImageFul ("ImageFull")
  144. #define CcosImagePrev ("ImagePrev")
  145. #define CcosImageId ("Sharememid")
  146. void DEVICE_ACTION CanModelDevice::NotifyCallBackSetBlockSize(string QueName, DWORD BlockSize, DWORD FulBlockCount, DWORD PrevBlockSize, DWORD PrevBlockCount)
  147. {
  148. //Logger *pDevLog = GetLogHandle ();
  149. //mLog::FINFO ("SetBlockSize. Que:{$},BlockSize:{$},BlockCount:{$},PrevBlockSize:{$},PrevBlockCount:{$}", QueName.c_str(),BlockSize,FulBlockCount,PrevBlockSize,PrevBlockCount);
  150. //mLog::FINFO("Need Check and Set Rotate and Flip configItem");
  151. //if (strItem == "RotateAngle")
  152. //{
  153. // resResponse = to_string(m_nRotateAngle).c_str();
  154. // return RET_SUCCEED;
  155. //}
  156. //else if (strItem == "FlipDirection")
  157. ResDataObject resRotate, resFlip;
  158. resRotate.add("Level", "Public");
  159. resFlip.add("Level", "Public");
  160. resRotate.add("Value", "0");
  161. resFlip.add("Value", "");
  162. ResDataObject resVs;
  163. resVs.add("", "0"); resVs.add("", "90"); resVs.add("", "180"); resVs.add("", "270");
  164. resRotate.add("List", resVs);
  165. resVs.clear();
  166. resVs.add("", ""); resVs.add("", "X"); resVs.add("", "Y");
  167. resFlip.add("List", resVs);
  168. bool changed = false;
  169. if (m_resProperties.GetKeyCount("RotateAngle") <= 0)
  170. {
  171. changed = true;
  172. m_resProperties.add("RotateAngle", resRotate);
  173. m_resProperties.add("FlipDirection", resFlip);
  174. //mLog::FINFO("No Rotate and Flip configItem Add default");
  175. }
  176. else
  177. {
  178. m_nRotateAngle = m_resProperties["RotateAngle"]["Value"];
  179. string dir = m_resProperties["FlipDirection"]["Value"];
  180. if (dir.length() <= 0)
  181. m_nFlipDirection = 0xFFFF;
  182. else if (dir[0] == 'X' || dir[0] == 'x')
  183. m_nFlipDirection = 0;
  184. else if (dir[0] == 'Y' || dir[0] == 'y')
  185. m_nFlipDirection = 1;
  186. else
  187. m_nFlipDirection = 0xFFFF;
  188. }
  189. if (m_resGets.GetKeyCount("RotateAngle") <= 0)
  190. {
  191. changed = true;
  192. m_resGets.update("RotateAngle", resRotate);
  193. m_resGets.add("FlipDirection", resFlip);
  194. }
  195. if (m_resSets.GetKeyCount("RotateAngle") <= 0)
  196. {
  197. changed = true;
  198. m_resSets.add("RotateAngle", resRotate);
  199. m_resSets.add("FlipDirection", resFlip);
  200. }
  201. if (m_resUpdates.GetKeyCount("RotateAngle") <= 0)
  202. {
  203. changed = true;
  204. m_resUpdates.add("RotateAngle", resRotate);
  205. m_resUpdates.add("FlipDirection", resFlip);
  206. }
  207. m_resModuleConfig["Update"].update("RotateAngle", resRotate);
  208. m_resModuleConfig["Update"].update("FlipDirection", resFlip);
  209. if(changed)
  210. {
  211. SaveToConfigFile();
  212. }
  213. {
  214. auto hDll = LoadLibrary("ImageRotate.dll");
  215. if (hDll != INVALID_HANDLE_VALUE)
  216. {
  217. m_funcRoate = (ImageRotate) GetProcAddress(hDll, "rotateImage");
  218. m_funcFlip = (ImageFlip)GetProcAddress(hDll, "flipImage");
  219. }
  220. //mLog::FINFO("Try Get ImageFunction Roate = {$} and Flip = {$} from ImageRotate.dll", m_funcRoate, m_funcFlip);
  221. }
  222. if (((ImagePoolEx*) m_ImagePool)->SetMaxBlockSize (QueName.c_str (), BlockSize, FulBlockCount, PrevBlockSize, PrevBlockCount) == false)
  223. {
  224. //mLog::FERROR( "SetMaxBlockSize Callback Failed");
  225. }
  226. else
  227. {
  228. //mLog::FINFO( "SetMaxBlockSize Callback Succeed");
  229. }
  230. }
  231. void DEVICE_ACTION CanModelDevice::RawDataNotifyCallBackEntry(int cmdType, string keyType, string Context,string Head,char *pRawData,int DataLength)
  232. {
  233. PACKET_CMD ccoscmd = PACKET_CMD_DATA;
  234. //Logger *pDevLog = GetLogHandle();
  235. //if (ConverCmdType_IoLevel2Ccos((ATTRACTION)cmdType, ccoscmd))
  236. {
  237. BLOCK_IMAGE_TYPE imgType;
  238. if (cmdType == 0)
  239. {
  240. imgType = FULL_BLOCK_IMG;
  241. }
  242. else if (cmdType == 1 || cmdType == 2)
  243. {
  244. imgType = PREV_BLOCK_IMG;
  245. }
  246. else
  247. {
  248. //mLog::FERROR( "wrong callback for data notify. cmdtype:{$}", cmdType);
  249. return;
  250. }
  251. CanModelDPC* pDPC = (CanModelDPC*)m_pDrvDPC;
  252. char* pNewData = nullptr;
  253. int nNewWidth, nNewHeigt, nOldWidth, nOldHeigt, nBits = 0;
  254. ResDataObject resImageHead;
  255. bool bNewData = false;
  256. if (m_nRotateAngle % 360 != 0 && m_nRotateAngle % 90 == 0 && imgType == FULL_BLOCK_IMG)
  257. {
  258. if (m_funcRoate != nullptr)
  259. {
  260. resImageHead.decode(Head.c_str());
  261. nOldWidth = (int)resImageHead["ImageHead"]["ImageWidth"];
  262. nOldHeigt = (int)resImageHead["ImageHead"]["ImageHeight"];
  263. nBits = (int)resImageHead["ImageHead"]["ImageBit"];
  264. if (nBits == 16)
  265. {
  266. if (pNewData == nullptr)
  267. pNewData = new char[DataLength];
  268. memset(pNewData, 0, DataLength);
  269. bNewData = true;
  270. nNewWidth = nNewHeigt = 0;
  271. //mLog::FINFO("Rotate image [{$}] degree Start with Width={$} Heigth={$}", m_nRotateAngle, nOldWidth, nOldHeigt);
  272. m_funcRoate((WORD*)pRawData, nOldWidth, nOldHeigt, m_nRotateAngle, (WORD*)pNewData, nNewWidth, nNewHeigt);
  273. resImageHead["ImageHead"]["ImageWidth"] = nOldWidth = nNewWidth;
  274. resImageHead["ImageHead"]["ImageHeight"] = nOldHeigt = nNewHeigt;
  275. Head = resImageHead.encode();
  276. //mLog::FINFO("Rotate image [{$}] degree Over with New Width={$} Heigth={$}", m_nRotateAngle, nNewWidth, nNewHeigt);
  277. }
  278. else
  279. {
  280. //mLog::FWARN("Only 16bits Image is supported.");
  281. }
  282. }
  283. else
  284. {
  285. //mLog::FWARN("Configured Rotate Image but Algorithm is not Ready");
  286. }
  287. }
  288. if (m_nFlipDirection != 0xFFFF && imgType == FULL_BLOCK_IMG)
  289. {
  290. if (nBits == 0)
  291. {
  292. //说明没有旋转过
  293. resImageHead.decode(Head.c_str());
  294. nOldWidth = (int)resImageHead["ImageHead"]["ImageWidth"];
  295. nOldHeigt = (int)resImageHead["ImageHead"]["ImageHeight"];
  296. nBits = (int)resImageHead["ImageHead"]["ImageBit"];
  297. }
  298. if (nBits == 16)
  299. {
  300. if (pNewData == nullptr)
  301. {
  302. pNewData = new char[DataLength];
  303. memset(pNewData, 0, DataLength);
  304. }
  305. nNewWidth = nNewHeigt = 0;
  306. //mLog::FINFO("Flip image {$} Start with Width={$} Heigth={$}", m_nFlipDirection==0?"X":"Y", nOldWidth, nOldHeigt);
  307. if (bNewData)
  308. {
  309. bNewData = false;
  310. m_funcFlip((WORD*)pNewData, nOldWidth, nOldHeigt, m_nFlipDirection, (WORD*)pRawData, nNewWidth, nNewHeigt);
  311. }
  312. else
  313. {
  314. bNewData = true;
  315. m_funcFlip((WORD*)pRawData, nOldWidth, nOldHeigt, m_nFlipDirection, (WORD*)pNewData, nNewWidth, nNewHeigt);
  316. //memcpy(pRawData, pNewData, DataLength);
  317. }
  318. resImageHead["ImageHead"]["ImageWidth"] = nNewWidth;
  319. resImageHead["ImageHead"]["ImageHeight"] = nNewHeigt;
  320. //mLog::FINFO("Flip image {$} Over with New Width={$} Heigth={$}", m_nFlipDirection == 0 ? "X" : "Y", nNewWidth, nNewHeigt);
  321. }
  322. else
  323. {
  324. //mLog::FWARN("Only 16bits Image is supported.");
  325. }
  326. }
  327. //UnvalidSMBid
  328. ShareMemoryBlockID Id = ((ImagePoolEx*)m_ImagePool)->AddFrameWithHead(imgType, Head, bNewData ? pNewData : pRawData, DataLength);
  329. if (Id != UnvalidSMBid)
  330. {
  331. ResDataObject NotifyData;
  332. ResDataObject NotifyContext;
  333. //mLog::FINFO("Data Notify. key:{$},Context:{$},Head:{$} Length {$} with smid {$}", keyType.c_str(), Context.c_str(), Head.c_str(), DataLength, Id);
  334. NotifyContext.decode(Context.c_str());
  335. NotifyContext [CcosImageId] = Id;
  336. NotifyContext.add("ImageFrom", m_strCCOSDevicePath.c_str());
  337. if (pDPC != nullptr)
  338. pDPC->ImageRrocess(imgType, Head, NotifyContext, bNewData ? pNewData : pRawData, DataLength, this);
  339. PacketAnalizer::MakeNotify(NotifyData, ccoscmd, keyType.c_str(), NotifyContext);
  340. //CmdFromLogicDev(&NotifyData);
  341. SendNotify(&NotifyData);
  342. //PublishAction(&NotifyData, (m_strEBusRoot + "/Notify").c_str(), m_pMqttConntion);
  343. //PublishAction(&NotifyData, (m_strCCOSRoot + "/Notify").c_str(), m_pMqttConntion);
  344. //mLog::FINFO("Data Notify res packet done");
  345. }
  346. else
  347. {
  348. //mLog::FERROR("can't save frame 2 sharememory.keyType:{$}.Head:{$}.Context:{$}.DataLen:{$}", keyType.c_str(), Head.c_str(), Context.c_str(), DataLength);
  349. }
  350. }
  351. //else
  352. //{
  353. // //mLog::FERROR("wrong notify type:%d", cmdType);
  354. //}
  355. }
  356. void DEVICE_ACTION CanModelDevice::NotifyCallBackEntry(int cmdType, string keyType, string Context)
  357. {
  358. std::cout << "--------------NotifyCallBackEntry " << std::endl;
  359. //3. Notify
  360. PACKET_CMD ccoscmd;
  361. //Logger* pDevLog = GetLogHandle();
  362. if (ConverCmdType_IoLevel2Ccos((ATTRACTION)cmdType, ccoscmd))
  363. {
  364. /*
  365. try
  366. {
  367. if (keyType == "SubscribeTopic") {
  368. // 去MQTT Broker 订阅
  369. std::cout << "--------------SubscribeTopic " << keyType << std::endl;
  370. //mLog::FINFO(pDevLog, "SubscribeTopic.cmd:{$} key:{$},Context:{$}", cmdType, keyType.c_str(), Context.c_str());
  371. ResDataObject topics;
  372. topics.decode(Context.c_str());
  373. for (size_t i = 0; i < topics.size(); i++)
  374. {
  375. SubscribeOne((const char*)topics[i]);
  376. }
  377. }
  378. else if (keyType == "Unsubscribe") {
  379. // 取消订阅
  380. //mLog::FINFO(pDevLog, "Unsubscribe.cmd:{$} key:{$},Context:{$}", cmdType, keyType.c_str(), Context.c_str());
  381. ResDataObject topics;
  382. topics.decode(Context.c_str());
  383. for (size_t i = 0; i < topics.size(); i++)
  384. {
  385. SubscribeOne((const char*)topics[i], true);
  386. }
  387. }
  388. else {
  389. // 那就是事件通知,往主题上publish 内容
  390. //mLog::FINFO(pDevLog, "Notify to MQTT .cmd:{$} key:{$},Context:{$}", cmdType, keyType.c_str(), Context.c_str());
  391. ostringstream ostopic;
  392. ostopic << TOPIC_PREFIX << "Notify/" << keyType;
  393. //mLog::FINFO(pDevLog, "PUBLISH TO MQTT TOPIC :{$},Context:{$}", ostopic.str().c_str(), Context.c_str());
  394. if (m_pMqttClient->is_connected()) {
  395. m_pMqttClient->publish(ostopic.str(), Context);
  396. }
  397. else {
  398. //mLog::FERROR( "MQTT Server Still not Connected ", cmdType);
  399. std::cout << "MQTT Server Still not Connected, Try Publish" << cmdType << " keytype: " << keyType << "Context : " << Context << std::endl;
  400. }
  401. }
  402. }
  403. catch (const mqtt::exception& exc)
  404. {
  405. //mLog::FERROR( "MQTT Server Still not Connected %s", exc.what());
  406. }*/
  407. if (ccoscmd != PACKET_CMD_DATA)
  408. {
  409. //raw data
  410. ResDataObject NotifyData;
  411. ResDataObject NotifyContext;
  412. //mLog::FINFO( "Notify.cmd:{$} key:{$},Context:{$}", cmdType, keyType.c_str(), Context.c_str());
  413. try
  414. {
  415. NotifyContext.decode(Context.c_str());
  416. }
  417. catch (...)
  418. {
  419. NotifyContext = Context.c_str();
  420. }
  421. if (keyType == "ErrorList" && NotifyContext.size() > 0)
  422. {
  423. ResDataObject errorObj = NotifyContext[0];
  424. if (ccoscmd == PACKET_CMD_ADD)
  425. {
  426. //if (errorObj.size() > 0 && errorObj[0].GetFirstOf("Type") >= 0)
  427. //{
  428. // // Type是0表示Error
  429. // if (atoi((const char*)errorObj[0]["Type"]) == 0)
  430. // {
  431. // string strGUID = NotifyContext.GetKey(0);
  432. // if (m_pResErrorList->GetFirstOf(strGUID.c_str()) < 0)
  433. // m_pResErrorList->add(strGUID.c_str(), errorObj);
  434. // else
  435. // (*m_pResErrorList)[strGUID.c_str()].add(errorObj.GetKey(0), errorObj[0]);
  436. // PRINTA_INFO(pDevLog, "Add ErrorList %s", m_pResErrorList->encode());
  437. // }
  438. //}
  439. string strSenderId = "";
  440. int nSenderIdNum = errorObj.GetKeyCount("SenderId");
  441. if (nSenderIdNum > 0)
  442. {
  443. strSenderId = (string)errorObj["SenderId"];
  444. }
  445. string strCodeID = (string)errorObj["CodeID"];
  446. int nLevel = (int)errorObj["Level"];
  447. string strInfo = (string)errorObj["Resouceinfo"];
  448. int nType = (int)errorObj["Type"];
  449. LogicDevice::AddErrorMessage(strCodeID.c_str(), nLevel, strInfo.c_str(), nType, strSenderId.c_str());
  450. ////mLog::FINFO(pDevLog, "AddErrorMessage:{$} over", errorObj.encode());
  451. return;
  452. }
  453. else if (ccoscmd == PACKET_CMD_DEL)
  454. {
  455. //if (errorObj.size() > 0 && errorObj[0].GetFirstOf("CodeID") >= 0)
  456. //{
  457. // string CodeParam = errorObj[0]["CodeID"];
  458. // if (CodeParam.size() == 0 || CodeParam == "0" || CodeParam == "")
  459. // {
  460. // m_pResErrorList->clear();
  461. // PRINTA_INFO(pDevLog, "Clear all ErrorList");
  462. // }
  463. // else
  464. // {
  465. // for (size_t n = 0; n < m_pResErrorList->size(); n++)
  466. // {
  467. // for (size_t i = 0; i < (*m_pResErrorList)[n].size(); i++)
  468. // {
  469. // string strCodekey = (*m_pResErrorList)[n].GetKey(i);
  470. // if ((*m_pResErrorList)[n][strCodekey.c_str()].GetFirstOf("Type") >= 0)
  471. // {
  472. // string strtype = (*m_pResErrorList)[n][strCodekey.c_str()]["Type"];
  473. // if (strCodekey == CodeParam && atoi(strtype.c_str()) == 0)
  474. // {
  475. // (*m_pResErrorList)[n].eraseOneOf(CodeParam.c_str());
  476. // PRINTA_INFO(pDevLog, "Clear ErrorList %s", m_pResErrorList->encode());
  477. // break;
  478. // }
  479. // }
  480. // }
  481. // }
  482. // }
  483. //}
  484. string strCodeID = (string)errorObj["CodeID"];
  485. int nLevel = (int)errorObj["Level"];
  486. string strInfo = (string)errorObj["Resouceinfo"];
  487. int nType = (int)errorObj["Type"];
  488. LogicDevice::DelErrorMessage(strCodeID.c_str(), nLevel, strInfo.c_str(), nType);
  489. ////mLog::FINFO(pDevLog, "DelErrorMessage:{$} over", errorObj.encode());
  490. return;
  491. }
  492. }
  493. PacketAnalizer::MakeNotify(NotifyData, ccoscmd, keyType.c_str(), NotifyContext);
  494. if (ccoscmd == PACKET_CMD_UPDATE)
  495. {
  496. if (m_resUpdates.GetKeyCount(keyType.c_str()) > 0)
  497. {
  498. //允许Update的属性,有变动
  499. //mLog::FINFO("Update property {$} = {$} to Config", keyType, Context);
  500. ResDataObject resRe;
  501. DevUpdate(m_strCCOSDevicePath.c_str(),keyType.c_str(), Context.c_str(), resRe);
  502. }
  503. }
  504. CanModelDPC* pDPC = (CanModelDPC*)m_pDrvDPC;
  505. if(pDPC != nullptr)
  506. pDPC->NotifyMessageProcess(NotifyData, this);
  507. //CmdFromLogicDev(&NotifyData);
  508. SendNotify(&NotifyData);
  509. //PublishAction(&NotifyData, (m_strEBusRoot + "/Notify").c_str(), m_pMqttConntion);
  510. /*PublishAction(&NotifyData, (m_strCCOSRoot + "/Notify").c_str(), m_pMqttConntion);*/
  511. //mLog::FINFO( "Notify res packet done");
  512. }
  513. else
  514. {
  515. //mLog::FWARN("wrong callback for raw data notify");
  516. }
  517. }
  518. else
  519. {
  520. std::cout << "--------------wrong notify type " << cmdType << std::endl;
  521. //mLog::FERROR("wrong notify type:{$}", cmdType);
  522. }
  523. }
  524. void DEVICE_ACTION CanModelDevice::OnSystemLog(int LogLevel, string Code, string Context, string SenderId)
  525. {
  526. IoSystemLog(LogLevel, Code.c_str(), Context.c_str(),Context.size(),SenderId.c_str());
  527. }
  528. void DEVICE_ACTION CanModelDevice::OnLog(int LogLevel, string Context)
  529. {
  530. //mLog::FINFO( "OnLog : Level{$} Context{$} Size{$}", LogLevel, Context.c_str(), Context.size());
  531. }
  532. void DEVICE_ACTION CanModelDevice::OnPassiveDisconnected()
  533. {
  534. if (m_DisconnectEvt)
  535. {
  536. SetEvent(m_DisconnectEvt);
  537. }
  538. }
  539. bool SYSTEM_CALL CanModelDevice::GetDeviceType(GUID &DevType)
  540. {
  541. string lDevType = m_pMidObject->GetGUID();
  542. //if (m_pMidObject->GetDeviceType(lDevType))
  543. {
  544. return string_2_guid(lDevType.c_str(), DevType);
  545. }
  546. return false;
  547. }
  548. //get device resource
  549. RET_STATUS SYSTEM_CALL CanModelDevice::GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource)
  550. {
  551. if (m_resGets.size() > 0)
  552. {
  553. return ModuleDevice::GetDeviceResource(pDeviceResource);
  554. }
  555. bool ret = true;
  556. string DevRes = m_pMidObject->GetResource();
  557. //if (m_pMidObject->GetDeviceResource(DevRes))
  558. //mLog::FINFO("GET IODevice Resouce: {$}", DevRes);
  559. {
  560. GUID DrvType;
  561. string guidstr;
  562. GetDeviceType(DrvType);
  563. guid_2_string(DrvType, guidstr);
  564. ret &= pDeviceResource->add("DeviceType", guidstr.c_str());
  565. //make device type
  566. ret &= pDeviceResource->add("ClientType", DPC_UnitClient);
  567. ResDataObject LowLayerRes;
  568. //mLog::FINFO("GetDeviceResource: result: {$}", DevRes);
  569. if (LowLayerRes.decode(DevRes.c_str()))
  570. {
  571. //attr
  572. int Idx = LowLayerRes.GetFirstOf("Attribute");
  573. if (Idx >= 0)
  574. {
  575. pDeviceResource->add("Attribute", LowLayerRes[Idx]);
  576. int erroridx = (*pDeviceResource)["Attribute"].GetFirstOf("ErrorList");
  577. if (erroridx < 0)
  578. {
  579. (*pDeviceResource)["Attribute"].add("ErrorList", *m_pResErrorList);
  580. }
  581. else
  582. {
  583. (*pDeviceResource)["Attribute"]["ErrorList"] = *m_pResErrorList;
  584. }
  585. }
  586. else
  587. {
  588. ResDataObject Attribute;
  589. Attribute.add("ErrorList", *m_pResErrorList);
  590. pDeviceResource->add("Attribute", Attribute);
  591. }
  592. //action
  593. Idx = LowLayerRes.GetFirstOf("Action");
  594. if (Idx >= 0)
  595. {
  596. pDeviceResource->add("Action", LowLayerRes[Idx]);
  597. }
  598. else
  599. {
  600. pDeviceResource->add("Action", "");
  601. }
  602. return RET_SUCCEED;
  603. }
  604. }
  605. return RET_FAILED;
  606. }
  607. //normal sync routine,Request to device and response from device
  608. RET_STATUS SYSTEM_CALL CanModelDevice::Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse)
  609. {
  610. INT ret = RET_NOSUPPORT;
  611. //1. analize request
  612. PACKET_CMD cmd = PacketAnalizer::GetPacketCmd(pRequest);
  613. string keystr = PacketAnalizer::GetPacketKey(pRequest);
  614. ResDataObject Context;
  615. if (PacketAnalizer::GetPacketContext(pRequest, Context) == false)
  616. {
  617. return RET_FAILED;
  618. }
  619. ResDataObject resReponse;
  620. //Logger *pDevLog = GetLogHandle();
  621. if (cmd == PACKET_CMD_EXE)
  622. {
  623. string req, res;
  624. req = (const char *)Context.encode();
  625. //mLog::FINFO("Action[{$}].req:{$}",keystr.c_str(), req.c_str());
  626. ret = (INT)m_pMidObject->Action(keystr, req, res);
  627. if (ret >= RET_SUCCEED)
  628. {
  629. Context.clear();
  630. //mLog::FINFO("Action[{$}].res:{$}", keystr.c_str(), res.c_str());
  631. bool bObject = false;
  632. for (size_t i = 0; i < res.size(); i++)
  633. {
  634. char cValue = res[i];
  635. if (cValue != ' ' && cValue != '\t' &&
  636. cValue != '\r' && cValue != '\n')
  637. {
  638. if (cValue == '{' && res.find(':') != std::string::npos)
  639. {
  640. bObject = true;
  641. }
  642. break;
  643. }
  644. }
  645. if (bObject)
  646. {
  647. ResDataObject resp;
  648. resp.decode(res.c_str());
  649. Context.add("P0", resp);
  650. }
  651. else
  652. {
  653. Context.add("P0", res.c_str());
  654. }
  655. pResponse->update("CONTEXT", Context);
  656. //mLog::FINFO("Action res packet done");
  657. }
  658. }
  659. else if (cmd == PACKET_CMD_GET)
  660. {
  661. string res;
  662. //ret = m_pMidObject->AttributeAction(ATTRACTION_GET, keystr, res);
  663. //mLog::FINFO("get CMD_GET req:{$}", keystr.c_str());
  664. ret = DevGet(m_strCCOSDevicePath.c_str(), keystr.c_str(), resReponse);
  665. pResponse->update("CONTEXT", resReponse);
  666. }
  667. else if (cmd == PACKET_CMD_UPDATE)
  668. {
  669. //mLog::FINFO("get CMD_UPDATE req:{$}", keystr.c_str());
  670. ret = DevUpdate(m_strCCOSDevicePath.c_str(), keystr.c_str(), Context, resReponse);
  671. pResponse->update("CONTEXT", resReponse);
  672. }
  673. else if (cmd == PACKET_CMD_ADD)
  674. {
  675. //mLog::FINFO("get CMD_ADD req:{$}", keystr.c_str());
  676. ret = DevAdd(m_strCCOSDevicePath.c_str(), keystr.c_str(), Context, resReponse);
  677. pResponse->update("CONTEXT", resReponse);
  678. }
  679. else if (cmd == PACKET_CMD_DEL)
  680. {
  681. //mLog::FINFO("get CMD_DEL req:{$}", keystr.c_str());
  682. ret = DevDel(m_strCCOSDevicePath.c_str(), keystr.c_str(), Context, resReponse);
  683. pResponse->update("CONTEXT", resReponse);
  684. }
  685. else
  686. {
  687. //wtf??
  688. }
  689. PacketAnalizer::MakeRetCode((RET_STATUS)ret, pResponse);
  690. return (RET_STATUS)ret;
  691. }
  692. //notify to lower layer
  693. RET_STATUS SYSTEM_CALL CanModelDevice::CmdToLogicDev(ResDataObject PARAM_IN *pCmd)
  694. {
  695. assert(0);//not happening
  696. return RET_FAILED;
  697. }
  698. RET_STATUS CanModelDevice::OnUpdate(const char* pszProperty, const char* pszValueUpdate, ResDataObject& resRespons)
  699. {
  700. string out;
  701. string strItem = pszProperty;
  702. if (strItem == "RotateAngle" && pszValueUpdate != nullptr)
  703. {
  704. m_nRotateAngle = atoi(pszValueUpdate) / 90 * 90;
  705. resRespons = to_string(m_nRotateAngle).c_str();
  706. return RET_SUCCEED;
  707. }
  708. else if (strItem == "FlipDirection" && pszValueUpdate != nullptr )
  709. {
  710. if (strlen(pszValueUpdate) <= 0)
  711. {
  712. m_nFlipDirection = 0xFFFF;
  713. resRespons = "";
  714. return RET_SUCCEED;
  715. }
  716. m_nFlipDirection = (pszValueUpdate[0] == 'X' ? 0 : (pszValueUpdate[0] == 'Y' ? 1 : 0xFFFF));
  717. if (m_nFlipDirection != 0xFFFF)
  718. {
  719. resRespons = (m_nFlipDirection == 0 ? 'X' : 'Y');
  720. return RET_SUCCEED;
  721. }
  722. return RET_FAILED;
  723. }
  724. RET_STATUS ret = m_pMidObject->Update(pszProperty, pszValueUpdate, out);
  725. if (ret == RET_SUCCEED)
  726. {
  727. m_pMidObject->Get(pszProperty, out);
  728. try {
  729. resRespons.decode(out.c_str());
  730. }
  731. catch (...)
  732. {
  733. resRespons = out.c_str();
  734. }
  735. return ret;
  736. }
  737. return ret;
  738. }
  739. RET_STATUS CanModelDevice::OnDel(const char* pszPropery, ResDataObject& resDelValue, ResDataObject& resResponse)
  740. {
  741. string out;
  742. RET_STATUS ret = m_pMidObject->Delete(pszPropery, resDelValue.encode(), out);
  743. if (ret == RET_SUCCEED)
  744. resResponse.decode(out.c_str());
  745. return ret;
  746. }
  747. RET_STATUS CanModelDevice::OnAdd(const char* pszPropery, ResDataObject& reAddValue, ResDataObject& resResponse)
  748. {
  749. string out;
  750. RET_STATUS ret = m_pMidObject->Add(pszPropery, reAddValue.encode(), out);
  751. if (ret == RET_SUCCEED)
  752. resResponse.decode(out.c_str());
  753. return ret;
  754. }
  755. RET_STATUS CanModelDevice::SetItem(const char* pszPropery, ResDataObject& resSetValue, ResDataObject& resResponse)
  756. {
  757. string out, setv;
  758. if (resSetValue.size() > 0)
  759. setv = resSetValue.encode();
  760. else
  761. setv = (const char*)resSetValue;
  762. string strItem = pszPropery;
  763. if (strItem == "RotateAngle" && setv.length() > 0)
  764. {
  765. m_nRotateAngle = atoi(setv.c_str()) / 90 * 90;
  766. resResponse = to_string(m_nRotateAngle).c_str();
  767. return RET_SUCCEED;
  768. }
  769. else if (strItem == "FlipDirection" )
  770. {
  771. if (setv.length() <= 0)
  772. {
  773. m_nFlipDirection = 0xFFFF;
  774. resResponse = "";
  775. return RET_SUCCEED;
  776. }
  777. m_nFlipDirection = (setv[0] == 'X' ? 0 : (setv[0] == 'Y' ? 1 : 0xFFFF));
  778. if (m_nFlipDirection != 0xFFFF)
  779. {
  780. resResponse = (m_nFlipDirection == 0 ? 'X' : 'Y');
  781. return RET_SUCCEED;
  782. }
  783. return RET_FAILED;
  784. }
  785. RET_STATUS ret = m_pMidObject->Set(pszPropery, setv);
  786. if (ret == RET_SUCCEED)
  787. {
  788. m_pMidObject->Get(pszPropery, out);
  789. resResponse.decode(out.c_str());
  790. }
  791. return ret;
  792. }
  793. RET_STATUS CanModelDevice::GetItem(const char* pszPropery, ResDataObject& resResponse)
  794. {
  795. string out;
  796. RET_STATUS ret = m_pMidObject->Get(pszPropery, out);
  797. if (ret == RET_SUCCEED)
  798. {
  799. //mLog::FINFO(" Get {$} from Module result {$}", pszPropery, out);
  800. try
  801. {
  802. if (!resResponse.decode(out.c_str()))
  803. resResponse = out.c_str();
  804. }
  805. catch (...)
  806. {
  807. resResponse = out.c_str();
  808. }
  809. return ret;
  810. }
  811. string strItem = pszPropery;
  812. if (strItem == "RotateAngle")
  813. {
  814. resResponse = to_string(m_nRotateAngle).c_str();
  815. return RET_SUCCEED;
  816. }
  817. else if (strItem == "FlipDirection")
  818. {
  819. resResponse = (m_nFlipDirection == 0 ? "X" : (m_nFlipDirection == 1? "Y" :""));
  820. return RET_SUCCEED;
  821. }
  822. return RET_NOSUPPORT;
  823. }
  824. RET_STATUS CanModelDevice::OnAction(const char* pszActionName, const char* pszParams, ResDataObject& resResponse)
  825. {
  826. //先调用模型的Action
  827. string out;
  828. RET_STATUS ret = ModuleDevice::OnAction(pszActionName, pszParams, resResponse);
  829. //mLog::FINFO(" ModuleDevice::OnAction {$} Module result {$}", pszActionName, (int)ret);
  830. if (ret == RET_SUCCEED)
  831. return ret;
  832. //NewModuleDevice层的Action
  833. ret = m_pMidObject->Action(pszActionName, pszParams, out);
  834. //mLog::FINFO(" MidObject->Action {$} Module result {$} with out{$}", pszActionName, (int)ret, out);
  835. if (ret == RET_SUCCEED)
  836. resResponse.decode(out.c_str());
  837. return ret;
  838. }