NewModelDevice.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. //mLog::FINFO(" ModuleDevice::OnAction {$} Module result {$}", pszActionName, (int)ret);
  829. if (ret == RET_SUCCEED)
  830. return ret;
  831. //NewModuleDevice层的Action
  832. ret = m_pMidObject->Action(pszActionName, pszParams, out);
  833. //mLog::FINFO(" MidObject->Action {$} Module result {$} with out{$}", pszActionName, (int)ret, out);
  834. if (ret == RET_SUCCEED)
  835. resResponse.decode(out.c_str());
  836. return ret;
  837. }