1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015 |
- #include <assert.h>
- #include <iostream>
- #include <functional>
- #include "NewModelDPC.h"
- #include "common_api.h"
- #include "PacketAnalizer.h"
- #include "NewModelDevice.h"
- #include "ImagePool.h"
- //#include "logger.h"
- using namespace std::placeholders;
- NewModelDevice::NewModelDevice()
- {
- m_ImagePool = new ImagePoolEx();
- m_DisconnectEvt = 0;
- //m_pMqttClient = NULL;
- m_bSendIndependently = true;
- m_funcRoate = nullptr;
- m_funcFlip = nullptr;
- m_nRotateAngle = 0;
- m_nFlipDirection = 0xFFFF;
- }
- NewModelDevice::~NewModelDevice()
- {
- CompleteUnInit();
- delete m_ImagePool;
- }
- bool ConverCmdType_IoLevel2Ccos(ATTRACTION iocmd, PACKET_CMD &ccoscmd)
- {
- if (iocmd == ATTRACTION_GET)
- {
- ccoscmd = PACKET_CMD_GET;
- }
- else if (iocmd == ATTRACTION_SET)
- {
- ccoscmd = PACKET_CMD_UPDATE;
- }
- else if (iocmd == ATTRACTION_ADD)
- {
- ccoscmd = PACKET_CMD_ADD;
- }
- else if (iocmd == ATTRACTION_DEL)
- {
- ccoscmd = PACKET_CMD_DEL;
- }
- else if (iocmd == ATTRACTION_UPDATE)
- {
- ccoscmd = PACKET_CMD_UPDATE;
- }
- else if (iocmd == ATTRACTION_DATA)
- {
- ccoscmd = PACKET_CMD_DATA;
- }
- else if (iocmd == ATTRACTION_MSG)
- {
- ccoscmd = PACKET_CMD_MSG;
- }
- else
- {
- //wtf??
- //mLog::FERROR("unknown iocmd:{$},packetcmd:{$}", (int)iocmd, (int)ccoscmd);
- return false;
- }
- return true;
- }
- bool ConverCmdType_Ccos2IoLevel(PACKET_CMD ccoscmd, ATTRACTION &iocmd)
- {
- if (ccoscmd == PACKET_CMD_GET)
- {
- iocmd = ATTRACTION_GET;
- }
- else if (ccoscmd == PACKET_CMD_UPDATE)
- {
- iocmd = ATTRACTION_SET;
- }
- else if (ccoscmd == PACKET_CMD_ADD)
- {
- iocmd = ATTRACTION_ADD;
- }
- else if (ccoscmd == PACKET_CMD_DEL)
- {
- iocmd = ATTRACTION_DEL;
- }
- else if (ccoscmd == PACKET_CMD_PART_UPDATE)
- {
- iocmd = ATTRACTION_UPDATE;
- }
- else if (ccoscmd == PACKET_CMD_DATA)
- {
- iocmd = ATTRACTION_DATA;
- }
- else if (ccoscmd == PACKET_CMD_MSG)
- {
- iocmd = ATTRACTION_MSG;
- }
- else
- {
- //wtf??
- //mLog::FERROR("unknown iocmd:{$},packetcmd:{$}", (int)iocmd, (int)ccoscmd);
- return false;
- }
- return true;
- }
- void NewModelDevice::SubscribeSelf()
- {
- LogicDevice::SubscribeSelf();
- //委托IODevice订阅
- m_pMidObject->SubscribeSelf();
- }
- void NewModelDevice::Init(unique_ptr <nsDEV::IODevice>&& pMid, std::shared_ptr<LinuxEvent> DisconnectEvt)
- {
- //mLog::FINFO("NewModelDevice::Init...");
- std::cout << "NewModelDevice::Init..."<< std::endl;
-
- m_pMidObject = std::move(pMid);
- //g_pDPCDeviceObject = this;
- //auto a = std::bind(&NewModelDevice::NotifyCallBackEntry, this, _1, _2, _3);
- m_pMidObject->EventCenter->OnNotify.Push(this,&NewModelDevice::NotifyCallBackEntry);
- //auto b = std::bind(&NewModelDevice::RawDataNotifyCallBackEntry, this, _1, _2, _3, _4, _5, _6);
- m_pMidObject->EventCenter->OnDataNotify.Push(this, &NewModelDevice::RawDataNotifyCallBackEntry);
- //auto c = std::bind(&NewModelDevice::NotifyCallBackSetBlockSize, this, _1, _2, _3, _4, _5);
- m_pMidObject->EventCenter->OnMaxBlockSize.Push(this, &NewModelDevice::NotifyCallBackSetBlockSize);
- m_pMidObject->EventCenter->OnSystemLog.Push(this, &NewModelDevice::OnSystemLog);
- m_pMidObject->EventCenter->OnLog.Push(this, &NewModelDevice::OnLog);
- //mLog::FINFO("NewModelDevice:: ...m_pMidObject->Prepare");
- m_pMidObject->Prepare();
- CompleteInit();
- m_pMidObject->SetClientRootID((m_strClientID + "_IODevice").c_str(), "");
- m_pMidObject->CompleteInit();
- //regist notify call back
- //typedef RET_STATUS(DEVICE_ACTION NewModelDevice::*callbackentry) (ATTRACTION cmdType, string &keyType, string &Context);
- //callbackentry func = (callbackentry)&NewModelDevice::CallBackEntry;
- //typedef union _funcContext {
- // callbackentry func_entry;
- // NotifyCALLBACK func_callback;
- //}FUNCCONTEXT;
- //FUNCCONTEXT fc;
- //memset(&fc, 0, sizeof(fc));
- //fc.func_entry = func;
- //NotifyCALLBACK callback = fc.func_callback;
- //std::function<void(const Foo&, int)> fun = std::mem_fn(&Foo::bar);
- m_DisconnectEvt = DisconnectEvt;
- }
- #define CcosImageFul ("ImageFull")
- #define CcosImagePrev ("ImagePrev")
- #define CcosImageId ("Sharememid")
- void DEVICE_ACTION NewModelDevice::NotifyCallBackSetBlockSize(string QueName, DWORD BlockSize, DWORD FulBlockCount, DWORD PrevBlockSize, DWORD PrevBlockCount)
- {
- //Logger *pDevLog = GetLogHandle ();
- //mLog::FINFO ("SetBlockSize. Que:{$},BlockSize:{$},BlockCount:{$},PrevBlockSize:{$},PrevBlockCount:{$}", QueName.c_str(),BlockSize,FulBlockCount,PrevBlockSize,PrevBlockCount);
- //mLog::FINFO("Need Check and Set Rotate and Flip configItem");
- //if (strItem == "RotateAngle")
- //{
- // resResponse = to_string(m_nRotateAngle).c_str();
- // return RET_SUCCEED;
- //}
- //else if (strItem == "FlipDirection")
- ResDataObject resRotate, resFlip;
- resRotate.add("Level", "Public");
- resFlip.add("Level", "Public");
- resRotate.add("Value", "0");
- resFlip.add("Value", "");
- ResDataObject resVs;
- resVs.add("", "0"); resVs.add("", "90"); resVs.add("", "180"); resVs.add("", "270");
- resRotate.add("List", resVs);
- resVs.clear();
- resVs.add("", ""); resVs.add("", "X"); resVs.add("", "Y");
- resFlip.add("List", resVs);
- bool changed = false;
- if (m_resProperties.GetKeyCount("RotateAngle") <= 0)
- {
- changed = true;
- m_resProperties.add("RotateAngle", resRotate);
- m_resProperties.add("FlipDirection", resFlip);
- //mLog::FINFO("No Rotate and Flip configItem Add default");
- }
- else
- {
- m_nRotateAngle = m_resProperties["RotateAngle"]["Value"];
- string dir = m_resProperties["FlipDirection"]["Value"].encode();
- if (dir.length() <= 0)
- m_nFlipDirection = 0xFFFF;
- else if (dir[0] == 'X' || dir[0] == 'x')
- m_nFlipDirection = 0;
- else if (dir[0] == 'Y' || dir[0] == 'y')
- m_nFlipDirection = 1;
- else
- m_nFlipDirection = 0xFFFF;
- }
- if (m_resGets.GetKeyCount("RotateAngle") <= 0)
- {
- changed = true;
- m_resGets.update("RotateAngle", resRotate);
- m_resGets.add("FlipDirection", resFlip);
- }
- if (m_resSets.GetKeyCount("RotateAngle") <= 0)
- {
- changed = true;
- m_resSets.add("RotateAngle", resRotate);
- m_resSets.add("FlipDirection", resFlip);
- }
- if (m_resUpdates.GetKeyCount("RotateAngle") <= 0)
- {
- changed = true;
- m_resUpdates.add("RotateAngle", resRotate);
- m_resUpdates.add("FlipDirection", resFlip);
- }
- m_resModuleConfig["Update"].update("RotateAngle", resRotate);
- m_resModuleConfig["Update"].update("FlipDirection", resFlip);
- if(changed)
- {
- SaveToConfigFile();
- }
- {
- auto hDll = dlopen("libImageRotate.so", RTLD_LAZY);
- if (hDll != nullptr)
- {
- m_funcRoate = reinterpret_cast<ImageRotate>(dlsym(hDll, "rotateImage"));
- m_funcFlip = reinterpret_cast<ImageFlip>(dlsym(hDll, "flipImage"));
- }
- //mLog::FINFO("Try Get ImageFunction Roate = {$} and Flip = {$} from libImageRotate.so", m_funcRoate, m_funcFlip);
- }
- if (((ImagePoolEx*) m_ImagePool)->SetMaxBlockSize (QueName.c_str (), BlockSize, FulBlockCount, PrevBlockSize, PrevBlockCount) == false)
- {
- //mLog::FERROR( "SetMaxBlockSize Callback Failed");
- }
- else
- {
- //mLog::FINFO( "SetMaxBlockSize Callback Succeed");
- }
- }
- void DEVICE_ACTION NewModelDevice::RawDataNotifyCallBackEntry(int cmdType, string keyType, string Context,string Head,char *pRawData,int DataLength)
- {
- PACKET_CMD ccoscmd = PACKET_CMD_DATA;
- //Logger *pDevLog = GetLogHandle();
- //if (ConverCmdType_IoLevel2Ccos((ATTRACTION)cmdType, ccoscmd))
- {
- BLOCK_IMAGE_TYPE imgType;
- if (cmdType == 0)
- {
- imgType = FULL_BLOCK_IMG;
- }
- else if (cmdType == 1 || cmdType == 2)
- {
- imgType = PREV_BLOCK_IMG;
- }
- else
- {
- //mLog::FERROR( "wrong callback for data notify. cmdtype:{$}", cmdType);
- return;
- }
- NewModelDPC* pDPC = (NewModelDPC*)m_pDrvDPC;
-
- char* pNewData = nullptr;
- int nNewWidth, nNewHeigt, nOldWidth, nOldHeigt, nBits = 0;
- ResDataObject resImageHead;
- bool bNewData = false;
- if (m_nRotateAngle % 360 != 0 && m_nRotateAngle % 90 == 0 && imgType == FULL_BLOCK_IMG)
- {
- if (m_funcRoate != nullptr)
- {
- resImageHead.decode(Head.c_str());
- nOldWidth = (int)resImageHead["ImageHead"]["ImageWidth"];
- nOldHeigt = (int)resImageHead["ImageHead"]["ImageHeight"];
- nBits = (int)resImageHead["ImageHead"]["ImageBit"];
- if (nBits == 16)
- {
- if (pNewData == nullptr)
- pNewData = new char[DataLength];
- memset(pNewData, 0, DataLength);
- bNewData = true;
- nNewWidth = nNewHeigt = 0;
- //mLog::FINFO("Rotate image [{$}] degree Start with Width={$} Heigth={$}", m_nRotateAngle, nOldWidth, nOldHeigt);
- m_funcRoate((unsigned short*)pRawData, nOldWidth, nOldHeigt, m_nRotateAngle, (unsigned short*)pNewData, nNewWidth, nNewHeigt);
- resImageHead["ImageHead"]["ImageWidth"] = nOldWidth = nNewWidth;
- resImageHead["ImageHead"]["ImageHeight"] = nOldHeigt = nNewHeigt;
- Head = resImageHead.encode();
- //mLog::FINFO("Rotate image [{$}] degree Over with New Width={$} Heigth={$}", m_nRotateAngle, nNewWidth, nNewHeigt);
- }
- else
- {
- //mLog::FWARN("Only 16bits Image is supported.");
- }
- }
- else
- {
- //mLog::FWARN("Configured Rotate Image but Algorithm is not Ready");
- }
- }
- if (m_nFlipDirection != 0xFFFF && imgType == FULL_BLOCK_IMG)
- {
- if (nBits == 0)
- {
- //说明没有旋转过
- resImageHead.decode(Head.c_str());
- nOldWidth = (int)resImageHead["ImageHead"]["ImageWidth"];
- nOldHeigt = (int)resImageHead["ImageHead"]["ImageHeight"];
- nBits = (int)resImageHead["ImageHead"]["ImageBit"];
- }
- if (nBits == 16)
- {
- if (pNewData == nullptr)
- {
- pNewData = new char[DataLength];
- memset(pNewData, 0, DataLength);
- }
- nNewWidth = nNewHeigt = 0;
- //mLog::FINFO("Flip image {$} Start with Width={$} Heigth={$}", m_nFlipDirection==0?"X":"Y", nOldWidth, nOldHeigt);
- if (bNewData)
- {
- bNewData = false;
- m_funcFlip((unsigned short*)pNewData, nOldWidth, nOldHeigt, m_nFlipDirection, (unsigned short*)pRawData, nNewWidth, nNewHeigt);
- }
- else
- {
- bNewData = true;
- m_funcFlip((unsigned short*)pRawData, nOldWidth, nOldHeigt, m_nFlipDirection, (unsigned short*)pNewData, nNewWidth, nNewHeigt);
- //memcpy(pRawData, pNewData, DataLength);
- }
- resImageHead["ImageHead"]["ImageWidth"] = nNewWidth;
- resImageHead["ImageHead"]["ImageHeight"] = nNewHeigt;
- //mLog::FINFO("Flip image {$} Over with New Width={$} Heigth={$}", m_nFlipDirection == 0 ? "X" : "Y", nNewWidth, nNewHeigt);
- }
- else
- {
- //mLog::FWARN("Only 16bits Image is supported.");
- }
- }
- //UnvalidSMBid
- ShareMemoryBlockID Id = ((ImagePoolEx*)m_ImagePool)->AddFrameWithHead(imgType, Head, bNewData ? pNewData : pRawData, DataLength);
- if (Id != UnvalidSMBid)
- {
- ResDataObject NotifyData;
- ResDataObject NotifyContext;
- //mLog::FINFO("Data Notify. key:{$},Context:{$},Head:{$} Length {$} with smid {$}", keyType.c_str(), Context.c_str(), Head.c_str(), DataLength, Id);
- NotifyContext.decode(Context.c_str());
- NotifyContext [CcosImageId] = Id;
- NotifyContext.add("ImageFrom", m_strCCOSDevicePath.c_str());
- if (pDPC != nullptr)
- pDPC->ImageRrocess(imgType, Head, NotifyContext, bNewData ? pNewData : pRawData, DataLength, this);
- PacketAnalizer::MakeNotify(NotifyData, ccoscmd, keyType.c_str(), NotifyContext);
- //CmdFromLogicDev(&NotifyData);
- SendNotify(&NotifyData);
- //PublishAction(&NotifyData, (m_strEBusRoot + "/Notify").c_str(), m_pMqttConntion);
- //PublishAction(&NotifyData, (m_strCCOSRoot + "/Notify").c_str(), m_pMqttConntion);
-
- //mLog::FINFO("Data Notify res packet done");
- }
- else
- {
- //mLog::FERROR("can't save frame 2 sharememory.keyType:{$}.Head:{$}.Context:{$}.DataLen:{$}", keyType.c_str(), Head.c_str(), Context.c_str(), DataLength);
- }
- }
- //else
- //{
- // //mLog::FERROR("wrong notify type:%d", cmdType);
- //}
- }
- void DEVICE_ACTION NewModelDevice::NotifyCallBackEntry(int cmdType, string keyType, string Context)
- {
- std::cout << "--------------NotifyCallBackEntry " << std::endl;
- //3. Notify
- PACKET_CMD ccoscmd;
- //Logger* pDevLog = GetLogHandle();
- if (ConverCmdType_IoLevel2Ccos((ATTRACTION)cmdType, ccoscmd))
- {
- /*
- try
- {
- if (keyType == "SubscribeTopic") {
- // 去MQTT Broker 订阅
- std::cout << "--------------SubscribeTopic " << keyType << std::endl;
- //mLog::FINFO(pDevLog, "SubscribeTopic.cmd:{$} key:{$},Context:{$}", cmdType, keyType.c_str(), Context.c_str());
- ResDataObject topics;
- topics.decode(Context.c_str());
-
- for (size_t i = 0; i < topics.size(); i++)
- {
- SubscribeOne((const char*)topics[i]);
- }
-
-
- }
- else if (keyType == "Unsubscribe") {
- // 取消订阅
- //mLog::FINFO(pDevLog, "Unsubscribe.cmd:{$} key:{$},Context:{$}", cmdType, keyType.c_str(), Context.c_str());
- ResDataObject topics;
- topics.decode(Context.c_str());
- for (size_t i = 0; i < topics.size(); i++)
- {
- SubscribeOne((const char*)topics[i], true);
- }
- }
- else {
- // 那就是事件通知,往主题上publish 内容
- //mLog::FINFO(pDevLog, "Notify to MQTT .cmd:{$} key:{$},Context:{$}", cmdType, keyType.c_str(), Context.c_str());
- ostringstream ostopic;
- ostopic << TOPIC_PREFIX << "Notify/" << keyType;
- //mLog::FINFO(pDevLog, "PUBLISH TO MQTT TOPIC :{$},Context:{$}", ostopic.str().c_str(), Context.c_str());
- if (m_pMqttClient->is_connected()) {
- m_pMqttClient->publish(ostopic.str(), Context);
- }
- else {
- //mLog::FERROR( "MQTT Server Still not Connected ", cmdType);
- std::cout << "MQTT Server Still not Connected, Try Publish" << cmdType << " keytype: " << keyType << "Context : " << Context << std::endl;
- }
-
- }
- }
- catch (const mqtt::exception& exc)
- {
- //mLog::FERROR( "MQTT Server Still not Connected %s", exc.what());
- }*/
- if (ccoscmd != PACKET_CMD_DATA)
- {
- //raw data
- ResDataObject NotifyData;
- ResDataObject NotifyContext;
- //mLog::FINFO( "Notify.cmd:{$} key:{$},Context:{$}", cmdType, keyType.c_str(), Context.c_str());
- try
- {
- NotifyContext.decode(Context.c_str());
- }
- catch (...)
- {
- NotifyContext = Context.c_str();
- }
- if (keyType == "ErrorList" && NotifyContext.size() > 0)
- {
- ResDataObject errorObj = NotifyContext[0];
- if (ccoscmd == PACKET_CMD_ADD)
- {
- //if (errorObj.size() > 0 && errorObj[0].GetFirstOf("Type") >= 0)
- //{
- // // Type是0表示Error
- // if (atoi((const char*)errorObj[0]["Type"]) == 0)
- // {
- // string strGUID = NotifyContext.GetKey(0);
- // if (m_pResErrorList->GetFirstOf(strGUID.c_str()) < 0)
- // m_pResErrorList->add(strGUID.c_str(), errorObj);
- // else
- // (*m_pResErrorList)[strGUID.c_str()].add(errorObj.GetKey(0), errorObj[0]);
- // PRINTA_INFO(pDevLog, "Add ErrorList %s", m_pResErrorList->encode());
- // }
- //}
- string strSenderId = "";
- int nSenderIdNum = errorObj.GetKeyCount("SenderId");
- if (nSenderIdNum > 0)
- {
- strSenderId = (string)errorObj["SenderId"];
- }
- string strCodeID = (string)errorObj["CodeID"];
- int nLevel = (int)errorObj["Level"];
- string strInfo = (string)errorObj["Resouceinfo"];
- int nType = (int)errorObj["Type"];
- LogicDevice::AddErrorMessage(strCodeID.c_str(), nLevel, strInfo.c_str(), nType, strSenderId.c_str());
- ////mLog::FINFO(pDevLog, "AddErrorMessage:{$} over", errorObj.encode());
- return;
- }
- else if (ccoscmd == PACKET_CMD_DEL)
- {
- //if (errorObj.size() > 0 && errorObj[0].GetFirstOf("CodeID") >= 0)
- //{
- // string CodeParam = errorObj[0]["CodeID"];
- // if (CodeParam.size() == 0 || CodeParam == "0" || CodeParam == "")
- // {
- // m_pResErrorList->clear();
- // PRINTA_INFO(pDevLog, "Clear all ErrorList");
- // }
- // else
- // {
- // for (size_t n = 0; n < m_pResErrorList->size(); n++)
- // {
- // for (size_t i = 0; i < (*m_pResErrorList)[n].size(); i++)
- // {
- // string strCodekey = (*m_pResErrorList)[n].GetKey(i);
- // if ((*m_pResErrorList)[n][strCodekey.c_str()].GetFirstOf("Type") >= 0)
- // {
- // string strtype = (*m_pResErrorList)[n][strCodekey.c_str()]["Type"];
- // if (strCodekey == CodeParam && atoi(strtype.c_str()) == 0)
- // {
- // (*m_pResErrorList)[n].eraseOneOf(CodeParam.c_str());
- // PRINTA_INFO(pDevLog, "Clear ErrorList %s", m_pResErrorList->encode());
- // break;
- // }
- // }
- // }
- // }
- // }
- //}
- string strCodeID = (string)errorObj["CodeID"];
- int nLevel = (int)errorObj["Level"];
- string strInfo = (string)errorObj["Resouceinfo"];
- int nType = (int)errorObj["Type"];
- LogicDevice::DelErrorMessage(strCodeID.c_str(), nLevel, strInfo.c_str(), nType);
- ////mLog::FINFO(pDevLog, "DelErrorMessage:{$} over", errorObj.encode());
- return;
- }
- }
- PacketAnalizer::MakeNotify(NotifyData, ccoscmd, keyType.c_str(), NotifyContext);
- if (ccoscmd == PACKET_CMD_UPDATE)
- {
- if (m_resUpdates.GetKeyCount(keyType.c_str()) > 0)
- {
- //允许Update的属性,有变动
- //mLog::FINFO("Update property {$} = {$} to Config", keyType, Context);
- ResDataObject resRe;
- DevUpdate(m_strCCOSDevicePath.c_str(),keyType.c_str(), Context.c_str(), resRe);
- }
- }
- NewModelDPC* pDPC = (NewModelDPC*)m_pDrvDPC;
- if(pDPC != nullptr)
- pDPC->NotifyMessageProcess(NotifyData, this);
- //CmdFromLogicDev(&NotifyData);
- SendNotify(&NotifyData);
- //PublishAction(&NotifyData, (m_strEBusRoot + "/Notify").c_str(), m_pMqttConntion);
- /*PublishAction(&NotifyData, (m_strCCOSRoot + "/Notify").c_str(), m_pMqttConntion);*/
- //mLog::FINFO( "Notify res packet done");
- }
- else
- {
- //mLog::FWARN("wrong callback for raw data notify");
- }
- }
- else
- {
- std::cout << "--------------wrong notify type " << cmdType << std::endl;
- //mLog::FERROR("wrong notify type:{$}", cmdType);
- }
- }
- void DEVICE_ACTION NewModelDevice::OnSystemLog(int LogLevel, string Code, string Context, string SenderId)
- {
- IoSystemLog(LogLevel, Code.c_str(), Context.c_str(),Context.size(),SenderId.c_str());
- }
- void DEVICE_ACTION NewModelDevice::OnLog(int LogLevel, string Context)
- {
- //mLog::FINFO( "OnLog : Level{$} Context{$} Size{$}", LogLevel, Context.c_str(), Context.size());
- }
- void DEVICE_ACTION NewModelDevice::OnPassiveDisconnected()
- {
- if (m_DisconnectEvt)
- {
- m_DisconnectEvt->SetEvent();
- }
- }
- bool SYSTEM_CALL NewModelDevice::GetDeviceType(GUID &DevType)
- {
- string lDevType = m_pMidObject->GetGUID();
- //if (m_pMidObject->GetDeviceType(lDevType))
- {
- return string_2_guid(lDevType.c_str(), DevType);
- }
- return false;
- }
- //get device resource
- RET_STATUS SYSTEM_CALL NewModelDevice::GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource)
- {
- if (m_resGets.size() > 0)
- {
- return ModuleDevice::GetDeviceResource(pDeviceResource);
- }
- bool ret = true;
- string DevRes = m_pMidObject->GetResource();
- //if (m_pMidObject->GetDeviceResource(DevRes))
- //mLog::FINFO("GET IODevice Resouce: {$}", DevRes);
- {
- GUID DrvType;
- string guidstr;
- GetDeviceType(DrvType);
- guid_2_string(DrvType, guidstr);
- ret &= pDeviceResource->add("DeviceType", guidstr.c_str());
- //make device type
- ret &= pDeviceResource->add("ClientType", DPC_UnitClient);
- ResDataObject LowLayerRes;
- //mLog::FINFO("GetDeviceResource: result: {$}", DevRes);
- if (LowLayerRes.decode(DevRes.c_str()))
- {
- //attr
- int Idx = LowLayerRes.GetFirstOf("Attribute");
- if (Idx >= 0)
- {
- pDeviceResource->add("Attribute", LowLayerRes[Idx]);
- int erroridx = (*pDeviceResource)["Attribute"].GetFirstOf("ErrorList");
- if (erroridx < 0)
- {
- (*pDeviceResource)["Attribute"].add("ErrorList", *m_pResErrorList);
- }
- else
- {
- (*pDeviceResource)["Attribute"]["ErrorList"] = *m_pResErrorList;
- }
- }
- else
- {
- ResDataObject Attribute;
- Attribute.add("ErrorList", *m_pResErrorList);
- pDeviceResource->add("Attribute", Attribute);
- }
- //action
- Idx = LowLayerRes.GetFirstOf("Action");
- if (Idx >= 0)
- {
- pDeviceResource->add("Action", LowLayerRes[Idx]);
- }
- else
- {
- pDeviceResource->add("Action", "");
- }
- return RET_SUCCEED;
- }
-
- }
- return RET_FAILED;
- }
- //normal sync routine,Request to device and response from device
- RET_STATUS SYSTEM_CALL NewModelDevice::Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse)
- {
- INT ret = RET_NOSUPPORT;
- //1. analize request
- PACKET_CMD cmd = PacketAnalizer::GetPacketCmd(pRequest);
- string keystr = PacketAnalizer::GetPacketKey(pRequest);
- ResDataObject Context;
- if (PacketAnalizer::GetPacketContext(pRequest, Context) == false)
- {
- return RET_FAILED;
- }
- ResDataObject resReponse;
- //Logger *pDevLog = GetLogHandle();
- if (cmd == PACKET_CMD_EXE)
- {
- string req, res;
- req = (const char *)Context.encode();
- //mLog::FINFO("Action[{$}].req:{$}",keystr.c_str(), req.c_str());
- ret = (INT)m_pMidObject->Action(keystr, req, res);
- if (ret >= RET_SUCCEED)
- {
- Context.clear();
- //mLog::FINFO("Action[{$}].res:{$}", keystr.c_str(), res.c_str());
- bool bObject = false;
- for (size_t i = 0; i < res.size(); i++)
- {
- char cValue = res[i];
- if (cValue != ' ' && cValue != '\t' &&
- cValue != '\r' && cValue != '\n')
- {
- if (cValue == '{' && res.find(':') != std::string::npos)
- {
- bObject = true;
- }
- break;
- }
- }
- if (bObject)
- {
- ResDataObject resp;
- resp.decode(res.c_str());
- Context.add("P0", resp);
- }
- else
- {
- Context.add("P0", res.c_str());
- }
- pResponse->update("CONTEXT", Context);
- //mLog::FINFO("Action res packet done");
- }
- }
- else if (cmd == PACKET_CMD_GET)
- {
- string res;
- //ret = m_pMidObject->AttributeAction(ATTRACTION_GET, keystr, res);
- //mLog::FINFO("get CMD_GET req:{$}", keystr.c_str());
- ret = DevGet(m_strCCOSDevicePath.c_str(), keystr.c_str(), resReponse);
- pResponse->update("CONTEXT", resReponse);
- }
- else if (cmd == PACKET_CMD_UPDATE)
- {
- //mLog::FINFO("get CMD_UPDATE req:{$}", keystr.c_str());
- ret = DevUpdate(m_strCCOSDevicePath.c_str(), keystr.c_str(), Context, resReponse);
- pResponse->update("CONTEXT", resReponse);
- }
- else if (cmd == PACKET_CMD_ADD)
- {
- //mLog::FINFO("get CMD_ADD req:{$}", keystr.c_str());
- ret = DevAdd(m_strCCOSDevicePath.c_str(), keystr.c_str(), Context, resReponse);
- pResponse->update("CONTEXT", resReponse);
- }
- else if (cmd == PACKET_CMD_DEL)
- {
- //mLog::FINFO("get CMD_DEL req:{$}", keystr.c_str());
- ret = DevDel(m_strCCOSDevicePath.c_str(), keystr.c_str(), Context, resReponse);
- pResponse->update("CONTEXT", resReponse);
- }
- else
- {
- //wtf??
- }
- PacketAnalizer::MakeRetCode((RET_STATUS)ret, pResponse);
- return (RET_STATUS)ret;
- }
- //notify to lower layer
- RET_STATUS SYSTEM_CALL NewModelDevice::CmdToLogicDev(ResDataObject PARAM_IN *pCmd)
- {
- assert(0);//not happening
- return RET_FAILED;
- }
- RET_STATUS NewModelDevice::OnUpdate(const char* pszProperty, const char* pszValueUpdate, ResDataObject& resRespons)
- {
- string out;
- string strItem = pszProperty;
- if (strItem == "RotateAngle" && pszValueUpdate != nullptr)
- {
- m_nRotateAngle = atoi(pszValueUpdate) / 90 * 90;
- resRespons = to_string(m_nRotateAngle).c_str();
- return RET_SUCCEED;
- }
- else if (strItem == "FlipDirection" && pszValueUpdate != nullptr )
- {
- if (strlen(pszValueUpdate) <= 0)
- {
- m_nFlipDirection = 0xFFFF;
- resRespons = "";
- return RET_SUCCEED;
- }
- m_nFlipDirection = (pszValueUpdate[0] == 'X' ? 0 : (pszValueUpdate[0] == 'Y' ? 1 : 0xFFFF));
- if (m_nFlipDirection != 0xFFFF)
- {
- resRespons = (m_nFlipDirection == 0 ? 'X' : 'Y');
- return RET_SUCCEED;
- }
- return RET_FAILED;
- }
- RET_STATUS ret = m_pMidObject->Update(pszProperty, pszValueUpdate, out);
- if (ret == RET_SUCCEED)
- {
- m_pMidObject->Get(pszProperty, out);
- try {
- resRespons.decode(out.c_str());
- }
- catch (...)
- {
- resRespons = out.c_str();
- }
- return ret;
- }
- return ret;
- }
- RET_STATUS NewModelDevice::OnDel(const char* pszPropery, ResDataObject& resDelValue, ResDataObject& resResponse)
- {
- string out;
- RET_STATUS ret = m_pMidObject->Delete(pszPropery, resDelValue.encode(), out);
- if (ret == RET_SUCCEED)
- resResponse.decode(out.c_str());
- return ret;
- }
- RET_STATUS NewModelDevice::OnAdd(const char* pszPropery, ResDataObject& reAddValue, ResDataObject& resResponse)
- {
- string out;
- RET_STATUS ret = m_pMidObject->Add(pszPropery, reAddValue.encode(), out);
- if (ret == RET_SUCCEED)
- resResponse.decode(out.c_str());
- return ret;
- }
- RET_STATUS NewModelDevice::SetItem(const char* pszPropery, ResDataObject& resSetValue, ResDataObject& resResponse)
- {
- string out, setv;
- if (resSetValue.size() > 0)
- setv = resSetValue.encode();
- else
- setv = (const char*)resSetValue;
- string strItem = pszPropery;
- if (strItem == "RotateAngle" && setv.length() > 0)
- {
- m_nRotateAngle = atoi(setv.c_str()) / 90 * 90;
- resResponse = to_string(m_nRotateAngle).c_str();
- return RET_SUCCEED;
- }
- else if (strItem == "FlipDirection" )
- {
- if (setv.length() <= 0)
- {
- m_nFlipDirection = 0xFFFF;
- resResponse = "";
- return RET_SUCCEED;
- }
- m_nFlipDirection = (setv[0] == 'X' ? 0 : (setv[0] == 'Y' ? 1 : 0xFFFF));
- if (m_nFlipDirection != 0xFFFF)
- {
- resResponse = (m_nFlipDirection == 0 ? 'X' : 'Y');
- return RET_SUCCEED;
- }
- return RET_FAILED;
- }
- RET_STATUS ret = m_pMidObject->Set(pszPropery, setv);
- if (ret == RET_SUCCEED)
- {
- m_pMidObject->Get(pszPropery, out);
- resResponse.decode(out.c_str());
- }
- return ret;
- }
- RET_STATUS NewModelDevice::GetItem(const char* pszPropery, ResDataObject& resResponse)
- {
- string out;
- RET_STATUS ret = m_pMidObject->Get(pszPropery, out);
- if (ret == RET_SUCCEED)
- {
- //mLog::FINFO(" Get {$} from Module result {$}", pszPropery, out);
- try
- {
- if (!resResponse.decode(out.c_str()))
- resResponse = out.c_str();
- }
- catch (...)
- {
- resResponse = out.c_str();
- }
- return ret;
- }
- string strItem = pszPropery;
- if (strItem == "RotateAngle")
- {
- resResponse = to_string(m_nRotateAngle).c_str();
- return RET_SUCCEED;
- }
- else if (strItem == "FlipDirection")
- {
- resResponse = (m_nFlipDirection == 0 ? "X" : (m_nFlipDirection == 1? "Y" :""));
- return RET_SUCCEED;
- }
- return RET_NOSUPPORT;
- }
- RET_STATUS NewModelDevice::OnAction(const char* pszActionName, const char* pszParams, ResDataObject& resResponse)
- {
- //先调用模型的Action
- string out;
- RET_STATUS ret = ModuleDevice::OnAction(pszActionName, pszParams, resResponse);
- cout << "ModuleDevice::OnAction " << pszActionName
- << " Module result " << (int)ret << endl;
- //mLog::FINFO(" ModuleDevice::OnAction {$} Module result {$}", pszActionName, (int)ret);
- if (ret == RET_SUCCEED)
- return ret;
- //NewModuleDevice层的Action
- ret = m_pMidObject->Action(pszActionName, pszParams, out);
- cout << "MidObject->Action " << pszActionName
- << " Module result " << (int)ret
- << " with out " << out << endl;
- //mLog::FINFO(" MidObject->Action {$} Module result {$} with out{$}", pszActionName, (int)ret, out);
- if (ret == RET_SUCCEED)
- resResponse.decode(out.c_str());
- return ret;
- }
|