NewModelDevice.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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 "LogLocalHelper.h"
  10. #include "Log4CPP.h"
  11. using namespace std::placeholders;
  12. NewModelDevice::NewModelDevice()
  13. {
  14. m_ImagePool = new ImagePoolEx();
  15. m_DisconnectEvt = 0;
  16. //m_pMqttClient = NULL;
  17. m_bSendIndependently = true;
  18. m_funcRoate = nullptr;
  19. m_funcFlip = nullptr;
  20. m_nRotateAngle = 0;
  21. m_nFlipDirection = 0xFFFF;
  22. }
  23. NewModelDevice::~NewModelDevice()
  24. {
  25. CompleteUnInit();
  26. delete m_ImagePool;
  27. }
  28. bool ConverCmdType_IoLevel2Ccos(ATTRACTION iocmd, PACKET_CMD &ccoscmd)
  29. {
  30. if (iocmd == ATTRACTION_GET)
  31. {
  32. ccoscmd = PACKET_CMD_GET;
  33. }
  34. else if (iocmd == ATTRACTION_SET)
  35. {
  36. ccoscmd = PACKET_CMD_UPDATE;
  37. }
  38. else if (iocmd == ATTRACTION_ADD)
  39. {
  40. ccoscmd = PACKET_CMD_ADD;
  41. }
  42. else if (iocmd == ATTRACTION_DEL)
  43. {
  44. ccoscmd = PACKET_CMD_DEL;
  45. }
  46. else if (iocmd == ATTRACTION_UPDATE)
  47. {
  48. ccoscmd = PACKET_CMD_UPDATE;
  49. }
  50. else if (iocmd == ATTRACTION_DATA)
  51. {
  52. ccoscmd = PACKET_CMD_DATA;
  53. }
  54. else if (iocmd == ATTRACTION_MSG)
  55. {
  56. ccoscmd = PACKET_CMD_MSG;
  57. }
  58. else
  59. {
  60. //wtf??
  61. FERROR("unknown iocmd:{$},packetcmd:{$}", (int)iocmd, (int)ccoscmd);
  62. return false;
  63. }
  64. return true;
  65. }
  66. bool ConverCmdType_Ccos2IoLevel(PACKET_CMD ccoscmd, ATTRACTION &iocmd)
  67. {
  68. if (ccoscmd == PACKET_CMD_GET)
  69. {
  70. iocmd = ATTRACTION_GET;
  71. }
  72. else if (ccoscmd == PACKET_CMD_UPDATE)
  73. {
  74. iocmd = ATTRACTION_SET;
  75. }
  76. else if (ccoscmd == PACKET_CMD_ADD)
  77. {
  78. iocmd = ATTRACTION_ADD;
  79. }
  80. else if (ccoscmd == PACKET_CMD_DEL)
  81. {
  82. iocmd = ATTRACTION_DEL;
  83. }
  84. else if (ccoscmd == PACKET_CMD_PART_UPDATE)
  85. {
  86. iocmd = ATTRACTION_UPDATE;
  87. }
  88. else if (ccoscmd == PACKET_CMD_DATA)
  89. {
  90. iocmd = ATTRACTION_DATA;
  91. }
  92. else if (ccoscmd == PACKET_CMD_MSG)
  93. {
  94. iocmd = ATTRACTION_MSG;
  95. }
  96. else
  97. {
  98. //wtf??
  99. FERROR("unknown iocmd:{$},packetcmd:{$}", (int)iocmd, (int)ccoscmd);
  100. return false;
  101. }
  102. return true;
  103. }
  104. void NewModelDevice::SubscribeSelf()
  105. {
  106. LogicDevice::SubscribeSelf();
  107. //委托IODevice订阅
  108. m_pMidObject->SubscribeSelf();
  109. }
  110. void NewModelDevice::Init(unique_ptr <nsDEV::IODevice>&& pMid, std::shared_ptr<LinuxEvent> DisconnectEvt)
  111. {
  112. FINFO("NewModelDevice::Init...");
  113. std::cout << "NewModelDevice::Init..."<< std::endl;
  114. m_pMidObject = std::move(pMid);
  115. //g_pDPCDeviceObject = this;
  116. //auto a = std::bind(&NewModelDevice::NotifyCallBackEntry, this, _1, _2, _3);
  117. m_pMidObject->EventCenter->OnNotify.Push(this,&NewModelDevice::NotifyCallBackEntry);
  118. //auto b = std::bind(&NewModelDevice::RawDataNotifyCallBackEntry, this, _1, _2, _3, _4, _5, _6);
  119. m_pMidObject->EventCenter->OnDataNotify.Push(this, &NewModelDevice::RawDataNotifyCallBackEntry);
  120. //auto c = std::bind(&NewModelDevice::NotifyCallBackSetBlockSize, this, _1, _2, _3, _4, _5);
  121. m_pMidObject->EventCenter->OnMaxBlockSize.Push(this, &NewModelDevice::NotifyCallBackSetBlockSize);
  122. m_pMidObject->EventCenter->OnSystemLog.Push(this, &NewModelDevice::OnSystemLog);
  123. m_pMidObject->EventCenter->OnLog.Push(this, &NewModelDevice::OnLog);
  124. FINFO("NewModelDevice:: ...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 NewModelDevice::*callbackentry) (ATTRACTION cmdType, string &keyType, string &Context);
  131. //callbackentry func = (callbackentry)&NewModelDevice::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 NewModelDevice::NotifyCallBackSetBlockSize(string QueName, DWORD BlockSize, DWORD FulBlockCount, DWORD PrevBlockSize, DWORD PrevBlockCount)
  147. {
  148. //Logger *pDevLog = GetLogHandle ();
  149. FINFO ("SetBlockSize. Que:{$},BlockSize:{$},BlockCount:{$},PrevBlockSize:{$},PrevBlockCount:{$}", QueName.c_str(),BlockSize,FulBlockCount,PrevBlockSize,PrevBlockCount);
  150. 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. 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"].encode();
  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 = dlopen("libImageRotate.so", RTLD_LAZY);
  215. if (hDll != nullptr)
  216. {
  217. m_funcRoate = reinterpret_cast<ImageRotate>(dlsym(hDll, "rotateImage"));
  218. m_funcFlip = reinterpret_cast<ImageFlip>(dlsym(hDll, "flipImage"));
  219. }
  220. FINFO("Try Get ImageFunction Roate = {$} and Flip = {$} from libImageRotate.so", m_funcRoate, m_funcFlip);
  221. }
  222. if (((ImagePoolEx*) m_ImagePool)->SetMaxBlockSize (QueName.c_str (), BlockSize, FulBlockCount, PrevBlockSize, PrevBlockCount) == false)
  223. {
  224. FERROR( "SetMaxBlockSize Callback Failed");
  225. }
  226. else
  227. {
  228. FINFO( "SetMaxBlockSize Callback Succeed");
  229. }
  230. }
  231. void DEVICE_ACTION NewModelDevice::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. FERROR( "wrong callback for data notify. cmdtype:{$}", cmdType);
  249. return;
  250. }
  251. NewModelDPC* pDPC = (NewModelDPC*)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. FINFO("Rotate image [{$}] degree Start with Width={$} Heigth={$}", m_nRotateAngle, nOldWidth, nOldHeigt);
  272. m_funcRoate((unsigned short*)pRawData, nOldWidth, nOldHeigt, m_nRotateAngle, (unsigned short*)pNewData, nNewWidth, nNewHeigt);
  273. resImageHead["ImageHead"]["ImageWidth"] = nOldWidth = nNewWidth;
  274. resImageHead["ImageHead"]["ImageHeight"] = nOldHeigt = nNewHeigt;
  275. Head = resImageHead.encode();
  276. FINFO("Rotate image [{$}] degree Over with New Width={$} Heigth={$}", m_nRotateAngle, nNewWidth, nNewHeigt);
  277. }
  278. else
  279. {
  280. FWARN("Only 16bits Image is supported.");
  281. }
  282. }
  283. else
  284. {
  285. 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. FINFO("Flip image {$} Start with Width={$} Heigth={$}", m_nFlipDirection==0?"X":"Y", nOldWidth, nOldHeigt);
  307. if (bNewData)
  308. {
  309. bNewData = false;
  310. m_funcFlip((unsigned short*)pNewData, nOldWidth, nOldHeigt, m_nFlipDirection, (unsigned short*)pRawData, nNewWidth, nNewHeigt);
  311. }
  312. else
  313. {
  314. bNewData = true;
  315. m_funcFlip((unsigned short*)pRawData, nOldWidth, nOldHeigt, m_nFlipDirection, (unsigned short*)pNewData, nNewWidth, nNewHeigt);
  316. //memcpy(pRawData, pNewData, DataLength);
  317. }
  318. resImageHead["ImageHead"]["ImageWidth"] = nNewWidth;
  319. resImageHead["ImageHead"]["ImageHeight"] = nNewHeigt;
  320. FINFO("Flip image {$} Over with New Width={$} Heigth={$}", m_nFlipDirection == 0 ? "X" : "Y", nNewWidth, nNewHeigt);
  321. }
  322. else
  323. {
  324. 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. 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. FINFO("Data Notify res packet done");
  345. }
  346. else
  347. {
  348. 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. // FERROR("wrong notify type:%d", cmdType);
  354. //}
  355. }
  356. void DEVICE_ACTION NewModelDevice::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. 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. 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. 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. 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. 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. 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. 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. FINFO("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. FINFO("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. 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. NewModelDPC* pDPC = (NewModelDPC*)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. FINFO( "Notify res packet done");
  512. }
  513. else
  514. {
  515. FWARN("wrong callback for raw data notify");
  516. }
  517. }
  518. else
  519. {
  520. std::cout << "--------------wrong notify type " << cmdType << std::endl;
  521. FERROR("wrong notify type:{$}", cmdType);
  522. }
  523. }
  524. void DEVICE_ACTION NewModelDevice::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 NewModelDevice::OnLog(int LogLevel, string Context)
  529. {
  530. FINFO( "OnLog : Level{$} Context{$} Size{$}", LogLevel, Context.c_str(), Context.size());
  531. }
  532. void DEVICE_ACTION NewModelDevice::OnPassiveDisconnected()
  533. {
  534. if (m_DisconnectEvt)
  535. {
  536. m_DisconnectEvt->SetEvent();
  537. }
  538. }
  539. bool SYSTEM_CALL NewModelDevice::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 NewModelDevice::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. 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. 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 NewModelDevice::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. 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. 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. 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. 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. 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. 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. 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 NewModelDevice::CmdToLogicDev(ResDataObject PARAM_IN *pCmd)
  694. {
  695. assert(0);//not happening
  696. return RET_FAILED;
  697. }
  698. RET_STATUS NewModelDevice::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 NewModelDevice::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 NewModelDevice::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 NewModelDevice::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 NewModelDevice::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. 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 NewModelDevice::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. cout << "ModuleDevice::OnAction " << pszActionName
  830. << " Module result " << (int)ret << endl;
  831. FINFO(" ModuleDevice::OnAction {$} Module result {$}", pszActionName, (int)ret);
  832. if (ret == RET_SUCCEED)
  833. return ret;
  834. //NewModuleDevice层的Action
  835. ret = m_pMidObject->Action(pszActionName, pszParams, out);
  836. cout << "MidObject->Action " << pszActionName
  837. << " Module result " << (int)ret
  838. << " with out " << out << endl;
  839. FINFO(" MidObject->Action {$} Module result {$} with out{$}", pszActionName, (int)ret, out);
  840. if (ret == RET_SUCCEED)
  841. resResponse.decode(out.c_str());
  842. return ret;
  843. }