FrmVmsCamOldF.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. //#include "ITSDbF.h"
  4. //#include "ITSUtilF.h"
  5. //#include "AppGlobalF.h"
  6. #pragma hdrstop
  7. #include "XnsCommon.h"
  8. #include "XnsDeviceInterface.h"
  9. #include "FrmVmsCamOldF.h"
  10. #include "AppGlobalF.h"
  11. //---------------------------------------------------------------------------
  12. #pragma package(smart_init)
  13. #pragma link "XNSSDKWINDOWLib_OCX"
  14. #pragma link "XNSSDKDEVICELib_OCX"
  15. #pragma link "XNSSDKDEVICELib_OCX"
  16. #pragma resource "*.dfm"
  17. //TFrmVmsCam *FrmVmsCam = NULL;
  18. //---------------------------------------------------------------------------
  19. __fastcall TFrmVmsCamOld::TFrmVmsCamOld(TComponent* Owner)
  20. : TForm(Owner)
  21. {
  22. //::CoInitialize(NULL);
  23. FDispBlob = false;
  24. XnsSdkDevice1->Left = -500;
  25. XnsSdkWindow1->Parent = PnlClient;
  26. XnsSdkWindow1->Align = alClient;
  27. m_hDevice = 0; // Device handle
  28. m_hMediaSource = 0; // Media stream ID
  29. m_nControlId = 0; // Control ID
  30. m_bIsMediaPlay = false;
  31. }
  32. //---------------------------------------------------------------------------
  33. /*
  34. * form을 보여줄때 호출되는 event 메서드이다.
  35. * arguments
  36. * Sender : event handler 객체
  37. * return
  38. * void
  39. */
  40. void __fastcall TFrmVmsCamOld::FormShow(TObject *Sender)
  41. {
  42. RefreshVms();
  43. TmrShow->Enabled = true;
  44. }
  45. //---------------------------------------------------------------------------
  46. void __fastcall TFrmVmsCamOld::TmrShowTimer(TObject *Sender)
  47. {
  48. TmrShow->Enabled = false;
  49. try
  50. {
  51. if (FObj)
  52. {
  53. Caption = FObj->VMS_CTLR_ID;// + ": " + FObj->VMS_NM;
  54. WebCamPlay();
  55. }
  56. }
  57. catch(...){}
  58. }
  59. //---------------------------------------------------------------------------
  60. void __fastcall TFrmVmsCamOld::FormClose(TObject *Sender, TCloseAction &Action)
  61. {
  62. CommClose();
  63. Action = caFree;
  64. }
  65. //---------------------------------------------------------------------------
  66. void __fastcall TFrmVmsCamOld::FormDestroy(TObject *Sender)
  67. {
  68. try {
  69. if (FObj)
  70. {
  71. FObj->FData2 = NULL;
  72. }
  73. WebCamStop();
  74. //ReleaseWebCamera();
  75. } catch(...) {}
  76. }
  77. //---------------------------------------------------------------------------
  78. /*
  79. * 닫기버튼 이나 x버튼 클릭시 공통으로 처리하는 로직이들어간다..
  80. * Form과 DataModule class를 delete시킨다.
  81. * arguments
  82. *
  83. * return
  84. * void
  85. */
  86. void __fastcall TFrmVmsCamOld::CommClose()
  87. {
  88. //FrmVmsCam = NULL;
  89. //::CoUninitialize();
  90. }
  91. //---------------------------------------------------------------------------
  92. void __fastcall TFrmVmsCamOld::WebCamPlay()
  93. {
  94. TItsVms *pVmsObj = FObj;
  95. if (!pVmsObj) return;
  96. if (!SetupWebCamera()) return;
  97. try
  98. {
  99. // 신규 영상 연결
  100. ConnectWebCamera();
  101. }
  102. catch(...)
  103. {
  104. }
  105. }
  106. //---------------------------------------------------------------------------
  107. void __fastcall TFrmVmsCamOld::WebCamStop()
  108. {
  109. try {
  110. DisconnectWebCamera();
  111. } catch(...) {}
  112. }
  113. //---------------------------------------------------------------------------
  114. bool __fastcall TFrmVmsCamOld::SetupWebCamera()
  115. {
  116. String sErrMsg;
  117. long nRet = XnsSdkDevice1->Initialize();
  118. if (nRet != ERR_SUCCESS && nRet != ERR_ALREADY_INITIALIZE)
  119. {
  120. sErrMsg.printf(L"Samsung XnsSdkDevice Initialize() fail: return=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
  121. Application->MessageBox(sErrMsg.c_str(), Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
  122. return false;
  123. }
  124. //nRet = XnsSdkWindow1->Initialize((long)PnlClient->Handle, NULL);
  125. nRet = XnsSdkWindow1->Initialize(NULL, NULL);
  126. if (nRet != ERR_SUCCESS && nRet != ERR_ALREADY_INITIALIZE)
  127. {
  128. sErrMsg.printf(L"Samsung XnsSdkWindow Initialize() fail: return=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
  129. Application->MessageBox(sErrMsg.c_str(), Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
  130. return false;
  131. }
  132. m_lDeviceId = XnsSdkDevice1->CreateDeviceEx();
  133. m_hDevice = XnsSdkDevice1->GetDeviceHandle(m_lDeviceId);
  134. if (m_hDevice == 0)
  135. {
  136. sErrMsg = "CreateDevice() fail";
  137. Application->MessageBox(sErrMsg.c_str(), Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
  138. return false;
  139. }
  140. return true;
  141. }
  142. //---------------------------------------------------------------------------
  143. void __fastcall TFrmVmsCamOld::ReleaseWebCamera()
  144. {
  145. if (m_hDevice != 0)
  146. {
  147. if (m_hMediaSource)
  148. {
  149. XnsSdkWindow1->Stop();
  150. XnsSdkDevice1->CloseMedia(m_hDevice, m_hMediaSource);
  151. }
  152. if (XnsSdkDevice1->GetDeviceStatus(m_hDevice) == XDEVICE_STATUS_CONNECTED)
  153. {
  154. XnsSdkDevice1->Disconnect(m_hDevice);
  155. }
  156. //XnsSdkDevice1->ReleaseDevice(m_hDevice);
  157. }
  158. }
  159. //---------------------------------------------------------------------------
  160. bool __fastcall TFrmVmsCamOld::ConnectWebCamera()
  161. {
  162. bool bConnect = true;
  163. if (!FObj) return false;
  164. WideString sDefModel = "Samsung";
  165. WideString sModelName = "SNP-3371TH";
  166. WideString sIpAddress = FObj->WEB_CMRA_IP;
  167. int nPort = FObj->WEB_CMRA_PORT.ToIntDef(4520);
  168. WideString sID = FObj->WEB_CMRA_ID;
  169. WideString sPass = FObj->WEB_CMRA_PSWD;
  170. sDefModel = WideString(g_AppCfg.vmscam.sComModel);
  171. sModelName = WideString(g_AppCfg.vmscam.sDevModel);
  172. XnsSdkDevice1->SetConnectionInfo(
  173. m_hDevice, // [in] Device handle
  174. sDefModel.Detach(), // [in] Fixed as 'Samsung'
  175. sModelName.Detach(), // [in] Name of model to connect to. The maximum length allowed is 126-byte.
  176. XADDRESS_IP, sIpAddress.Detach(), // [in] Address type, actual address according to addresstype.
  177. nPort, 0, // [in] Port number, port number for web access
  178. sID.Detach(), sPass.Detach() // [in] Login ID, password
  179. );
  180. long nRet = XnsSdkDevice1->ConnectNonBlock(
  181. m_hDevice, // [in] Device handle
  182. false, // [in] Flag to decide where to forcibly log in or not.
  183. false // [in] If this value is 1, try to connect again.
  184. );
  185. if (nRet != ERR_SUCCESS)
  186. {
  187. String sErrMsg;
  188. sErrMsg.printf(L"ConnectNonBlock() fail: errno=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
  189. Application->MessageBox(sErrMsg.c_str(), Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
  190. bConnect = false;
  191. }
  192. return bConnect;
  193. }
  194. //---------------------------------------------------------------------------
  195. bool __fastcall TFrmVmsCamOld::OpenWebCamera()
  196. {
  197. XnsSdkDevice1->OpenMedia(m_hDevice, 3, MEDIA_TYPE_LIVE, 0, 0, &m_hMediaSource);
  198. XnsSdkWindow1->Start(m_hMediaSource);
  199. m_nControlId = 3;
  200. m_bIsMediaPlay = true;
  201. return true;
  202. }
  203. //---------------------------------------------------------------------------
  204. void __fastcall TFrmVmsCamOld::DisconnectWebCamera()
  205. {
  206. if (m_hDevice == 0) return;
  207. CloseWebCamera();
  208. // Disconnects from the device.
  209. if (XnsSdkDevice1->GetDeviceStatus(m_hDevice) == XDEVICE_STATUS_CONNECTED)
  210. {
  211. XnsSdkDevice1->Disconnect(m_hDevice);
  212. }
  213. XnsSdkDevice1->ReleaseDevice(m_hDevice);
  214. m_bIsMediaPlay = false;
  215. m_hMediaSource = 0;
  216. }
  217. //---------------------------------------------------------------------------
  218. void __fastcall TFrmVmsCamOld::CloseWebCamera()
  219. {
  220. if (!m_hMediaSource)
  221. {
  222. return;
  223. }
  224. if (m_bIsMediaPlay)
  225. {
  226. XnsSdkWindow1->Stop();
  227. m_bIsMediaPlay = false;
  228. }
  229. XnsSdkDevice1->CloseMedia(m_hDevice, m_hMediaSource);
  230. m_hMediaSource = 0;
  231. m_nControlId = 0;
  232. }
  233. //---------------------------------------------------------------------------
  234. void __fastcall TFrmVmsCamOld::XnsSdkDevice1ConnectFailed(TObject *Sender, long nDeviceID, long nControlID)
  235. {
  236. //Caption = Caption + " - Connect Failed.";
  237. }
  238. //---------------------------------------------------------------------------
  239. void __fastcall TFrmVmsCamOld::XnsSdkDevice1DeviceStatusChanged(TObject *Sender, long nDeviceID, long nErrorCode, long nDeviceStatus,
  240. long nHddCondition)
  241. {
  242. if (nErrorCode == ERR_SUCCESS && nDeviceStatus == 1)
  243. {
  244. if (FObj)
  245. {
  246. Caption = FObj->VMS_CTLR_ID + ": " + FObj->VMS_NM;
  247. String sId = FObj->VMS_CTLR_ID;
  248. String sName = FObj->VMS_NM;
  249. String sCaption;
  250. sCaption = sName;
  251. XnsSdkWindow1->SetOSDOnOff(OSD_ALL, false);
  252. XnsSdkWindow1->SetOSDOnOff(OSD_TIME1|OSD_IPS, true);
  253. XnsSdkWindow1->SetCustomOSDOnOff(false);
  254. WideString sOsdText = sCaption;
  255. XnsSdkWindow1->SetCustomOSD(sOsdText.Detach(), 1, 1, 255,255,255);
  256. XnsSdkWindow1->SetCustomOSDOnOff(true);
  257. XnsSdkWindow1->SetOSDOnOff(OSD_TIME1|OSD_IPS, true);
  258. }
  259. OpenWebCamera();
  260. }
  261. }
  262. //---------------------------------------------------------------------------
  263. void __fastcall TFrmVmsCamOld::RefreshVms()
  264. {
  265. FDispBlob = false;
  266. }
  267. //---------------------------------------------------------------------------