123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- #include "StdAfx.h"
- #include <assert.h>
- #include "common_api.h"
- #include "PacketAnalizer.h"
- #include "WheelUnitLogic.h"
- #define CRTDBG_MAP_ALLOC
- #include <stdlib.h>
- #include <crtdbg.h>
- #define WHEELUNITLOGICID "{1830DD9F-88B3-442A-9DDA-82D0F0B647F7}"
- //------------------unit device----------------------------
- WheelUnitLogic::WheelUnitLogic(void)
- {
- m_Status = new BaseJsonDataObject<bool>();
- m_Status->SetKey("WHELLSTATUS");
- (*m_Status) = false;
- }
- WheelUnitLogic::~WheelUnitLogic(void)
- {
- delete m_Status;
- }
- //get device type
- bool WheelUnitLogic::GetDeviceType(GUID &DevType)
- {
- return string_2_guid(WHEELUNITLOGICID, DevType);
- }
- //resource
- // DeviceType:GUID
- // attribute:{WHELLSTATUS:0}
- // action:StartRoll
- // action:StopRoll
- //get device resource
- RET_STATUS WheelUnitLogic::GetDeviceResource(ResDataObject *pDeviceResource)
- {
- bool ret = true;
- //make device type
- ret &= pDeviceResource->add("ClientType", DPC_UnitClient);
- ret &= pDeviceResource->add("DeviceType", WHEELUNITLOGICID);
- //make attributes
- ResDataObject val;
- ResDataObject Actions;
- val.add(m_Status->GetKey(), m_Status->GetVal());
- ret &= pDeviceResource->add("Attribute", val);
- if (Support_GetStatus())
- {
- //make resource
- ret &= Actions.add("GetStatus", "");
- }
- if (Support_SetStatus())
- {
- //make resource
- ret &= Actions.add("SetStatus", "");
- }
- if (Support_StartRoll())
- {
- //make resource
- ret &= Actions.add("StartRoll", "");
- }
- if (Support_StopRoll())
- {
- //make resource
- ret &= Actions.add("StopRoll", "");
- }
- if (ret)
- {
- pDeviceResource->add("Action", Actions);
- return RET_SUCCEED;
- }
- return RET_FAILED;
- }
- //ResourceCommand Request In and Response Out
- RET_STATUS WheelUnitLogic::Request(ResDataObject *pRequest, ResDataObject *pResponse)
- {
- ResDataObject res;
- RET_STATUS ret = RET_NOSUPPORT;
- //1. analize request
- PACKET_CMD cmd = PacketAnalizer::GetPacketCmd(pRequest);
- //cmd:get,set,add,del,exe,
- //ignore open/close
- //Action和Attr应该做成一个MAP,经过MAP访问这些资源,不应该是switch case
- //为了快速做一版,先保留switch case!!!
- string keystr = PacketAnalizer::GetPacketKey(pRequest);
- ACTION_SYNC_MODE syncmode = PacketAnalizer::GetPacketSyncMode(pRequest);
- switch (cmd){
- case PACKET_CMD_EXE:
- {
- if (keystr == "GetStatus")
- {
- //1. 获取参数
- //2. call api
- bool status = false;
- ret = (RET_STATUS)GetStatus(status);
- if (syncmode == ACTION_SYNC)
- {
- res.add("P0", status);
- //3. 打包
- pResponse->update("CONTEXT", res);
- }
- else
- {
- //Async
- //make notify
- //问题:Notify会发往很多地方,所以这种情况下要考虑清楚怎么发!!!
- //点对点情况,Notify要有个返回的地点,不广播
- ResDataObject NotifyData;
- PacketAnalizer::MakeNotifyP2P(NotifyData, PACKET_CMD_UPDATE, pRequest, m_Status->GetKey(), m_Status->GetVal());
- CmdFromLogicDev(&NotifyData);
- }
- }
- else if (keystr == "SetStatus")
- {
- //1. 获取参数
- ResDataObject obj;
- if (PacketAnalizer::GetParam(pRequest, 0, obj))
- {
- //2. call api
- ret = (RET_STATUS)SetStatus((bool)obj);
- //3. 打包
- }
- }
- else if (keystr == "StartRoll")
- {
- //1. 获取参数
- //2. call api
- ret = (RET_STATUS)StartRoll();
- //3. 打包
- //无参数
- }
- else if (keystr == "StopRoll")
- {
- //1. 获取参数
- //2. call api
- ret = (RET_STATUS)StopRoll();
- //3. 打包
- //无参数
- }
- else if (keystr == "ChangeStatus")
- {
- //1. 获取参数
- ResDataObject obj;
- if (PacketAnalizer::GetParam(pRequest, 0, obj))
- {
- //2. call api
- ret = (RET_STATUS)ChangeStatus((bool)obj);
- //ret = (RET_STATUS)ChangeStatus(true);
- //3. 打包
- bool status;
- GetStatus(status);
- res.add("P0", status);
- //3. 打包
- //printf("call in ChangeStatus\n");
- pResponse->update("CONTEXT", res);
- }
- }
- }
- break;
- default:
- //对当前设备来讲,其他都是浮云
- break;
- }
- //return status
- PacketAnalizer::MakeRetCode(ret, pResponse);
- return ret;
- }
- //notify to lower layer
- RET_STATUS WheelUnitLogic::CmdToLogicDev(ResDataObject *pCmd)
- {
- //这个命令只有一种情况下发生(作为客户端存在的时候)
- //1.设备的Notify消息
- //发往当前客户端的Notify
- //Data&Action notify!!!
- //Data Notify分两种
- //一种是本数据对象的更新,直接更新
- //一种是数据传递给上层,比如图像数据,探测器的剂量需求数据
- //Action Notify是发给Owner的
- //通知事件,让Owner知道,另外ActionNotify先保留下来
- //保留到哪里???
- //2.Response
- //命令Request发出后的反馈
- //1. analize request
- //2. call api
- //3. check results
- //put error log here
- assert(0);//not happening
- return RET_FAILED;
- }
- //errors,warnings
- void WheelUnitLogic::SetErrorInfo(int errCode, char *pErrInfo)
- {
- //errors
- //调用点要进行检讨
- assert(0);//NOT FINISHED YET
- }
- void WheelUnitLogic::SetWarningInfo(int warningCode, char *pWarningInfo)
- {
- //warnings
- //调用点要进行检讨
- assert(0);//NOT FINISHED YET
- }
- //Data Access
- int WheelUnitLogic::GetStatus(bool &status)
- {
- status = (*m_Status);
- return RET_SUCCEED;
- }
- int WheelUnitLogic::SetStatus(bool status)
- {
- RET_STATUS ret = RET_SUCCEED;
- if ((*m_Status) != status)
- {
- //make notify
- ResDataObject NotifyData;
- PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_UPDATE, m_Status->GetKey(), m_Status->GetVal());
- ret = CmdFromLogicDev(&NotifyData);
- }
- (*m_Status) = status;
- return ret;
- }
- //Actions
- int WheelUnitLogic::StartRoll()
- {
- return RET_NOSUPPORT;
- }
- int WheelUnitLogic::StopRoll()
- {
- return RET_NOSUPPORT;
- }
- bool WheelUnitLogic::Support_GetStatus()
- {
- return false;
- }
- bool WheelUnitLogic::Support_SetStatus()
- {
- return false;
- }
- bool WheelUnitLogic::Support_StartRoll()
- {
- return false;
- }
- bool WheelUnitLogic::Support_StopRoll()
- {
- return false;
- }
- int DEVICE_ACTION WheelUnitLogic::ChangeStatus(bool PARAM_IN st)
- {
- return RET_NOSUPPORT;
- }
- bool DEVICE_SUPPORT WheelUnitLogic::Support_ChangeStatus()
- {
- return false;
- }
|