NewModelDevice.cpp 26 KB

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