ICP  1
instapi_lib.cpp
Go to the documentation of this file.
1 // instapi_lib.cpp : Defines the exported functions for the DLL application.
2 //
3 
4 // instapi.cpp : Implementation of Cinstapi
5 #include <time.h>
6 #include "stdafx.h"
7 #include "instapi_lib.h"
8 #include "atlsafe.h"
9 #include <iostream>
10 #include "../selogger/selogger.h"
11 #include "../comutils/variant_utils.h"
12 
13 
14 // Cinstapi
15 
16 //#import "progid:isisicp.Idae" named_guids
17 #import "..\\isisicp\\isisicp.tlb" named_guids
18 #import "..\\tlb\\Seci.tlb" named_guids
19 #import "..\\tlb\\LabVIEW.tlb" named_guids
20 
21 void InstAPILib::createSession(BSTR instrument, BSTR user_name, BSTR password, ULONG* session_id)
22 {
23  *session_id = 1;
24  m_session[*session_id] = "";
25 }
26 
27 void InstAPILib::deleteSession(ULONG session_id)
28 {
29  m_session[session_id] = "";
30 }
31 
32 void InstAPILib::getRole(ULONG session_id, BSTR* role)
33 {
34 }
35 
36 void InstAPILib::setRole(ULONG session_id, BSTR role)
37 {
38 }
39 
40 COAUTHIDENTITY* InstAPILib::createIdentity(const std::string& user, const std::string& domain, const std::string& pass)
41 {
42  COAUTHIDENTITY* pidentity = new COAUTHIDENTITY;
43  pidentity->Domain = (USHORT*)strdup(domain.c_str());
44  pidentity->DomainLength = strlen((const char*)pidentity->Domain);
45  pidentity->Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
46  pidentity->Password = (USHORT*)strdup(pass.c_str());
47  pidentity->PasswordLength = strlen((const char*)pidentity->Password);
48  pidentity->User = (USHORT*)strdup(user.c_str());
49  pidentity->UserLength = strlen((const char*)pidentity->User);
50  return pidentity;
51 }
52 
53 HRESULT InstAPILib::setIdentity(COAUTHIDENTITY* pidentity, IUnknown* pUnk)
54 {
55  HRESULT hr;
56  if (pidentity != NULL)
57  {
58  hr = CoSetProxyBlanket(pUnk, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
59  RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, pidentity, EOAC_NONE);
60  if (FAILED(hr))
61  {
62  return hr;
63  }
64  }
65  return S_OK;
66 }
67 
68 
73 HRESULT InstAPILib::test(void)
74 {
75  // TODO: Add your implementation code here
76  CComPtr<isisicpLib::Idae> icp;
77  COSERVERINFO server_info;
78  memset(&server_info, 0, sizeof(server_info));
79  MULTI_QI multi_qi[1];
80  memset(multi_qi, 0, sizeof(multi_qi));
81  server_info.pwszName = L"ndxtest";
82  COAUTHIDENTITY* pidentity = createIdentity("spudulike", "ndxtest", "reliablebeam");
83  COAUTHINFO* pauth = new COAUTHINFO;
84  pauth->dwAuthnSvc = RPC_C_AUTHN_WINNT;
85  pauth->dwAuthnLevel = RPC_C_AUTHN_LEVEL_DEFAULT;
86  pauth->dwAuthzSvc = RPC_C_AUTHZ_NONE;
87  pauth->dwCapabilities = EOAC_NONE;
88  pauth->dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE;
89  pauth->pAuthIdentityData = pidentity;
90  pauth->pwszServerPrincName = NULL;
91  server_info.pAuthInfo = pauth;
92 
93  multi_qi[0].pIID = &isisicpLib::IID_Idae;
94  HRESULT hr = ::CoCreateInstanceEx(isisicpLib::CLSID_dae, NULL, CLSCTX_ALL,
95  &server_info, (sizeof(multi_qi) / sizeof(MULTI_QI)), multi_qi);
96  if (FAILED(hr))
97  {
98 // AtlReportError(GetObjectCLSID(), "failed1");
99  return hr;
100  }
101  if (FAILED(multi_qi[0].hr))
102  {
103 // AtlReportError(GetObjectCLSID(), "failed2");
104  return multi_qi[0].hr;
105  }
106  setIdentity(pidentity, multi_qi[0].pItf);
107  icp.Attach(reinterpret_cast<isisicpLib::Idae*>(multi_qi[0].pItf));
108  hr = icp->areYouThere();
109  if (FAILED(hr))
110  {
111 // AtlReportError(GetObjectCLSID(), "failed3");
112  return hr;
113  }
114 
115  return hr;
116 }
117 
122 HRESULT InstAPILib::testicp(void)
123 {
124  CComPtr<isisicpLib::Idae> icp;
125  HRESULT hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
126 
127  if (FAILED(hr))
128  {
129 // AtlReportError(GetObjectCLSID(), "CoCreateInstance failed");
130  return hr;
131  }
132 
133  hr = icp->areYouThere();
134 
135  if (FAILED(hr))
136  {
137 // AtlReportError(GetObjectCLSID(), "Failed to communicate with ICP");
138  return hr;
139  }
140 
141  return S_OK;
142 }
143 
148 HRESULT InstAPILib::testlv(void)
149 {
150  CComPtr<LabVIEW::_Application> lv;
151  HRESULT hr = lv.CoCreateInstance(LabVIEW::CLSID_Application, 0, CLSCTX_LOCAL_SERVER);
152 
153  if (FAILED(hr))
154  {
155 // AtlReportError(GetObjectCLSID(), "CoCreateInstance failed");
156  return hr;
157  }
158 
159  CComBSTR ccombstr((char *)lv->GetVersion());
160 
161  if (ccombstr.Length() == 0)
162  {
163  //Did not talk to LabVIEW
164 // AtlReportError(GetObjectCLSID(), "Failed to communicate with LabVIEW");
165  return E_FAIL;
166  }
167 
168  return S_OK;
169 }
170 
171 static int test()
172 {
173  CComPtr<isisicpLib::Idae> icp;
174  HRESULT hr;
175  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
176 }
177 
178 //junk<isisicp::Idae,isisicp::CLSID_Cdae,isisicp::IID_Idae>
179 template <class OBJ>
180 static int junk(const std::string& hostname, GUID C, GUID G)
181 {
182  // TODO: Add your implementation code here
183  CComPtr<OBJ> icp;
184  COSERVERINFO server_info;
185  memset(&server_info, 0, sizeof(server_info));
186  MULTI_QI multi_qi[1];
187  memset(multi_qi, 0, sizeof(multi_qi));
188  server_info.pwszName = hostname;
189  COAUTHIDENTITY* pidentity = createIdentity("spudulike", "ndxtest", "reliablebeam");
190  COAUTHINFO* pauth = new COAUTHINFO;
191  pauth->dwAuthnSvc = RPC_C_AUTHN_WINNT;
192  pauth->dwAuthnLevel = RPC_C_AUTHN_LEVEL_DEFAULT;
193  pauth->dwAuthzSvc = RPC_C_AUTHZ_NONE;
194  pauth->dwCapabilities = EOAC_NONE;
195  pauth->dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE;
196  pauth->pAuthIdentityData = pidentity;
197  pauth->pwszServerPrincName = NULL;
198  server_info.pAuthInfo = pauth;
199 
200  multi_qi[0].pIID = &G;
201  HRESULT hr = ::CoCreateInstanceEx(C, NULL, CLSCTX_ALL,
202  &server_info, (sizeof(multi_qi) / sizeof(MULTI_QI)), multi_qi);
203  if (FAILED(hr))
204  {
205 // AtlReportError(GetObjectCLSID(), "failed1");
206  return hr;
207  }
208  if (FAILED(multi_qi[0].hr))
209  {
210 // AtlReportError(GetObjectCLSID(), "failed2");
211  return multi_qi[0].hr;
212  }
213  setIdentity(pidentity, multi_qi[0].pItf);
214  icp.Attach(reinterpret_cast<OBJ*>(multi_qi[0].pItf));
215 }
216 
217 void InstAPILib::putMessage(ULONG session_id, BSTR time, BSTR severity, BSTR message)
218 {
219 }
220 
221 void InstAPILib::getMessages(ULONG session_id, LONG start_index, BSTR min_severity, VARIANT* times, VARIANT* severities, VARIANT* messages, LONG* end_index)
222 {
223 }
224 
225 void InstAPILib::getViRef(BSTR vi_name, bool reentrant, LabVIEW::VirtualInstrumentPtr &vi)
226 {
227  UINT len = SysStringLen(vi_name);
228  std::wstring ws(vi_name, SysStringLen(vi_name));
229 
230  vi_map_t::iterator it = m_vimap.find(ws);
231  if(it != m_vimap.end())
232  {
233  try
234  {
235  vi = it->second.vi_ref;
236  vi->GetExecState();
237  }
238  catch(...)
239  {
240  //Gets here if VI ref is not longer valid
241  createViRef(vi_name, reentrant, vi);
242  }
243  }
244  else
245  {
246  createViRef(vi_name, reentrant, vi);
247  }
248 }
249 
250 void InstAPILib::createViRef(BSTR vi_name, bool reentrant, LabVIEW::VirtualInstrumentPtr &vi)
251 {
252  std::wstring ws(vi_name, SysStringLen(vi_name));
253 
254  CComPtr<LabVIEW::_Application> lv;
255  HRESULT hr = lv.CoCreateInstance(LabVIEW::CLSID_Application, 0, CLSCTX_LOCAL_SERVER);
256 
257  if (FAILED(hr))
258  {
259  throw std::runtime_error("getViRef failed (could not connect to LabVIEW)");
260  }
261 
262  if (reentrant)
263  {
264  vi = lv->GetVIReference(vi_name, "", 1, 8);
265  }
266  else
267  {
268  //If a VI is reentrant then always get it as reentrant
269  vi = lv->GetVIReference(vi_name, "", 0, 0);
270  if (vi->IsReentrant)
271  {
272  vi = lv->GetVIReference(vi_name, "", 1, 8);
273  reentrant = true;
274  }
275  }
276 
277  ViRef viref;
278  viref.vi_ref = vi;
279  viref.reentrant = reentrant;
280  m_vimap[ws] = viref;
281 }
282 
283 void InstAPILib::getLabviewValue(ULONG session_id, BSTR vi_name, BSTR control_name, VARIANT* value)
284 {
285  try
286  {
287  LabVIEW::VirtualInstrumentPtr vi;
288  getViRef(vi_name, false, vi);
289 
290  *value = vi->GetControlValue(control_name).Detach();
291 
292  vi.Detach();
293  }
294  catch(...)
295  {
296  }
297 }
298 
299 void InstAPILib::sendBlockSetpoint(ULONG session_id, BSTR block_name, VARIANT setpoint_value, bool push_button, bool use_external, VARIANT* results)
300 {
301  results->vt = VT_EMPTY;
302  setBlockSetpoint(session_id, block_name, setpoint_value, push_button, use_external, results);
303 }
304 
305 void InstAPILib::logBlocksNow(ULONG session_id, BSTR source)
306 {
307  CComVariant names;
308  getBlockNames(session_id, &names);
309  CComSafeArray<BSTR> sab;
310  sab.CopyFrom(names.parray);
311 
312  UINT d = sab.GetDimensions(); //should be 1
313 
314  if (d != 1)
315  {
316  throw std::runtime_error("getBlockNames returned an array of the wrong dimensions");
317  }
318 
319  LONG numPars = sab.GetUpperBound(0); //this is inclusive, i.e. if it is 2 this means [2] is valid
320  LONG aIndex[1];
321 
322  std::vector<std::string> blocknames;
323  std::vector<std::string> blockvalues;
324  std::vector<std::string> times;
325  long runNum = 0;
326  CComBSTR messages;
327  getRunNumber(session_id, &messages, &runNum);
328 
329  //Convert source to char*
330  int len = (int) SysStringLen(source);
331  char* char_source = (char *) malloc(1+len);
332  WideCharToMultiByte( CP_ACP, 0, source, -1, char_source, len, NULL, NULL );
333  char_source[len] = '\0';
334 
335  for (UINT i = 0; i <= numPars; ++i)
336  {
337  bstr_t bname;
338  aIndex[0] = i;
339  sab.MultiDimGetAt(aIndex, bname.GetBSTR());
340 
341  //Convert the block name to std::string
342  UINT len = SysStringLen(bname);
343  std::wstring ws_name(bname, len);
344  std::string s_name;
345  s_name.assign(ws_name.begin(), ws_name.end());
346  blocknames.push_back(s_name);
347 
348  //Get the current value and convert to std::string
349  CComVariant currVal;
350  getBlockValue(session_id, bname, &currVal);
351  currVal.ChangeType(VT_BSTR);
352  bstr_t bstrVal;
353  currVal.CopyTo(&(bstrVal.GetBSTR()));
354  std::wstring ws_curr(bstrVal, SysStringLen(bstrVal));
355  std::string s_curr;
356  s_curr.assign(ws_curr.begin(), ws_curr.end());
357  blockvalues.push_back(s_curr);
358 
360  time_t rawtime;
361  struct tm * timeinfo;
362  time ( &rawtime );
363  timeinfo = localtime ( &rawtime );
364  int year = timeinfo->tm_year + 1900;
365  int month = timeinfo->tm_mon + 1;
366  char buffer[20];
367  sprintf (buffer, "%d-%02d-%02dT%02d:%02d:%02d", year, month, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
368  times.push_back(buffer);
369  }
370 
371  if (se_log_values_async(runNum, char_source, times, blocknames, blockvalues) != 0)
372  {
373  throw std::runtime_error(se_get_errmsg());
374  }
375 
376  free(char_source);
377 }
378 
379 void InstAPILib::setBlockSetpoint(ULONG session_id, BSTR block_name, VARIANT setpoint_value, bool push_button, bool use_external, VARIANT* results)
380 {
381  SEBLOCK_DETAILS details;
382  if (se_get_block_details(COLE2CT(block_name), details) != 0)
383  {
384  throw std::runtime_error(se_get_errmsg());
385  }
386 
387  _bstr_t vi(details.vi_name().c_str());
388 
389  if (details.set_control() != std::string(""))
390  {
391  _bstr_t setcontrol(details.set_control().c_str());
392 
393  if (use_external)
394  {
395  setLabviewValueExt(session_id, vi, setcontrol, setpoint_value, results);
396  }
397  else
398  {
399  setLabviewValue(session_id, vi, setcontrol, setpoint_value);
400  }
401  }
402 
403  if (push_button)
404  {
405  if (details.button_control() != std::string(""))
406  {
407  VARIANT* pushResults = new VARIANT;
408  _bstr_t gocontrol(details.button_control().c_str());
409  _variant_t button(true);
410  if (use_external)
411  {
412 
413  pushResults->vt = VT_EMPTY;
414  setLabviewValueExt(session_id, vi, gocontrol, button, pushResults);
415  }
416  else
417  {
418  setLabviewValue(session_id, vi, gocontrol, button);
419  }
420  }
421  }
422 
423  VARIANT v;
424  VariantInit(&v);
425  VariantChangeType(&v, &setpoint_value, 0, VT_BSTR);
426  se_set_block_value(COLE2CT(block_name), COLE2CT(v.bstrVal));
427  VariantClear(&v);
428 }
429 
430 void InstAPILib::setLabviewValue(ULONG session_id, BSTR vi_name, BSTR control_name, VARIANT value)
431 {
432  HRESULT hr = S_OK();
433 
434  try
435  {
436  LabVIEW::VirtualInstrumentPtr vi;
437  getViRef(vi_name, false, vi);
438  hr = vi->SetControlValue(control_name, value);
439  vi.Detach();
440  }
441  catch(...)
442  {
443  hr = -1;
444  }
445 
446  if (FAILED(hr))
447  {
448  throw std::runtime_error("SetLabviewValue failed");
449  }
450 }
451 
452 void InstAPILib::setLabviewValueExt(ULONG session_id, BSTR vi_name, BSTR control_name, VARIANT value, VARIANT* results)
453 {
454  _bstr_t path(L"C:\\LabVIEW Modules\\Common\\External Interface\\External Interface.llb\\External Interface - Set Value.vi");
455 
456  CComSafeArray<BSTR> names(6);
457  names[0].AssignBSTR(_bstr_t(L"VI Name"));
458  names[1].AssignBSTR(_bstr_t(L"Control Name"));
459  names[2].AssignBSTR(_bstr_t(L"String Control Value"));
460  names[3].AssignBSTR(_bstr_t(L"Variant Control Value"));
461  names[4].AssignBSTR(_bstr_t(L"Machine Name"));
462  names[5].AssignBSTR(_bstr_t(L"Return Message"));
463 
464  _variant_t n;
465  n.vt = VT_ARRAY | VT_BSTR;
466  n.parray = names.Detach();
467 
468  CComSafeArray<VARIANT> values(6);
469  values[0] = vi_name;
470  values[1] = control_name;
471  //values[2] =
472  values[3] = value;
473  //values[4] =
474  //values[5] =
475 
476  _variant_t v;
477  v.vt = VT_ARRAY | VT_VARIANT;
478  v.parray = values.Detach();
479 
480  //Must be called as reentrant!
481  callLabview(session_id, path, n, v, true, results);
482 }
483 
484 void InstAPILib::callLabview(ULONG session_id, BSTR vi_name, VARIANT names, VARIANT values, VARIANT_BOOL reentrant, VARIANT* results)
485 {
486  HRESULT hr = S_OK;
487 
488  try
489  {
490  LabVIEW::VirtualInstrumentPtr vi;
491 
492  if (reentrant)
493  {
494  getViRef(vi_name, true, vi);
495  }
496  else
497  {
498  getViRef(vi_name, false, vi);
499  }
500 
501  hr = vi->Call(&names, &values);
502  vi.Detach();
503 
504  CComVariant var(values);
505  var.Detach(results);
506  }
507  catch(...)
508  {
509  hr = -1;
510  }
511 
512  if (FAILED(hr))
513  {
514  throw std::runtime_error("CallLabviewValue failed");
515  }
516 }
517 
518 void InstAPILib::pushBlockButton(ULONG session_id, BSTR block_name, bool use_external, VARIANT* results)
519 {
520  results = new VARIANT;
521  results->vt = VT_EMPTY;
522 
523  SEBLOCK_DETAILS details;
524  if (se_get_block_details(COLE2CT(block_name), details) != 0)
525  {
526  throw std::runtime_error(se_get_errmsg());
527  }
528 
529  _bstr_t vi(details.vi_name().c_str());
530 
531  if (details.button_control() != std::string(""))
532  {
533  _bstr_t gocontrol(details.button_control().c_str());
534  _variant_t button(true);
535 
536  if (use_external)
537  {
538  setLabviewValueExt(session_id, vi, gocontrol, button, results);
539  }
540  else
541  {
542  setLabviewValue(session_id, vi, gocontrol, button);
543  }
544  }
545 }
546 
547 void InstAPILib::toggleViVisibility(ULONG session_id, BSTR vi_name, VARIANT_BOOL show)
548 {
549  HRESULT hr = S_OK;
550 
551  try
552  {
553  LabVIEW::VirtualInstrumentPtr vi;
554  getViRef(vi_name, false, vi);
555  vi->FPWinOpen = show;
556  vi.Detach();
557  }
558  catch(...)
559  {
560  hr = -1;
561  }
562 
563  if (FAILED(hr))
564  {
565  throw std::runtime_error("toggleViVisibility failed");
566  }
567 }
568 
569 void InstAPILib::getViState(ULONG session_id, BSTR vi_name, VARIANT* value)
570 {
571  HRESULT hr = S_OK();
572 
573  try
574  {
575  LabVIEW::VirtualInstrumentPtr vi;
576  getViRef(vi_name, false, vi);
577  CComVariant wrapper;
578  wrapper.ChangeType(VT_INT);
579  wrapper.Detach(value);
580  value->intVal = vi->ExecState;
581  vi.Detach();
582  }
583  catch(...)
584  {
585  hr = -1;
586  }
587 
588  if (FAILED(hr))
589  {
590  throw std::runtime_error("GetViState failed");
591  }
592 }
593 
594 void InstAPILib::startVi(ULONG session_id, BSTR vi_name)
595 {
596  HRESULT hr = S_OK;
597 
598  try
599  {
600  LabVIEW::VirtualInstrumentPtr vi;
601  getViRef(vi_name, false, vi);
602  // LabVIEW::ExecStateEnum.eIdle = 1
603  if (vi->ExecState == 1)
604  {
605  vi->Run(true);
606  }
607  vi.Detach();
608  }
609  catch(...)
610  {
611  hr = -1;
612  }
613 
614  if (FAILED(hr))
615  {
616  throw std::runtime_error("StartVi failed");
617  }
618 }
619 
620 void InstAPILib::stopVi(ULONG session_id, BSTR vi_name)
621 {
622  HRESULT hr = S_OK;
623 
624  try
625  {
626  LabVIEW::VirtualInstrumentPtr vi;
627  getViRef(vi_name, false, vi);
628  // LabVIEW::ExecStateEnum.eIdle = 1
629  // LabVIEW::ExecStateEnum.eRunTopLevel = 2
630  if (vi->ExecState == 1 || vi->ExecState == 2)
631  {
632  vi->Abort();
633  }
634  vi.Detach();
635  }
636  catch(...)
637  {
638  hr = -1;
639  }
640 
641  if (FAILED(hr))
642  {
643  throw std::runtime_error("StopVi failed");
644  }
645 }
646 
647 void InstAPILib::closeViFrontPanel(ULONG session_id, BSTR vi_name)
648 {
649  HRESULT hr = S_OK;
650 
651  try
652  {
653  LabVIEW::VirtualInstrumentPtr vi;
654  getViRef(vi_name, false, vi);
655  hr = vi->CloseFrontPanel();
656  vi.Detach();
657  }
658  catch(...)
659  {
660  hr = -1;
661  }
662 
663  if (FAILED(hr))
664  {
665  throw std::runtime_error("CloseViFrontPanel failed");
666  }
667 }
668 
669 void InstAPILib::getViNames(ULONG session_id, VARIANT* names)
670 {
671  CComPtr<Seci::IComInterface> seci;
672  HRESULT hr = seci.CoCreateInstance(Seci::CLSID_ComInterface, 0, CLSCTX_LOCAL_SERVER);
673 
674  if (FAILED(hr))
675  {
676  throw std::runtime_error("GetViNames failed (could not connect to SECI)");
677  }
678 
679  try
680  {
681  SAFEARRAY* temp = seci->GetVIsFullpath();
682  CComSafeArray<BSTR> saBstr(temp);
683  CComVariant varArrayWrapper (saBstr);
684  varArrayWrapper.Detach(names);
685  }
686  catch(...)
687  {
688  hr = -1;
689  }
690 
691  if (FAILED(hr))
692  {
693  throw std::runtime_error("GetViNames failed");
694  }
695 }
696 
697 void InstAPILib::getBlockValue(ULONG session_id, BSTR block_name, VARIANT* block_value)
698 {
699  SEBLOCK_DETAILS details;
700  if (se_get_block_details(COLE2CT(block_name), details) != 0)
701  {
702  throw std::runtime_error(se_get_errmsg());
703  }
704 
705  _bstr_t vi(details.vi_name().c_str());
706 
707  if (details.read_control() == std::string(""))
708  {
709  return;
710  }
711 
712  _bstr_t control(details.read_control().c_str());
713 
714  getLabviewValue(session_id, vi, control, block_value);
715 }
716 
717 void InstAPILib::getBlockSetpoint(ULONG session_id, BSTR block_name, VARIANT* setpoint_value)
718 {
719  SEBLOCK_DETAILS details;
720  if (se_get_block_details(COLE2CT(block_name), details) != 0)
721  {
722  throw std::runtime_error(se_get_errmsg());
723  }
724 
725  _bstr_t vi(details.vi_name().c_str());
726 
727  if (details.set_control() == std::string(""))
728  {
729  return;
730  }
731 
732  _bstr_t control(details.set_control().c_str());
733  getLabviewValue(session_id, vi, control, setpoint_value);
734 }
735 
736 void InstAPILib::getRunControl(ULONG session_id, BSTR block_name, VARIANT_BOOL* rc_value)
737 {
738  accessRuncontrol(session_id, block_name, NULL, rc_value, false);
739 }
740 
741 void InstAPILib::setRunControl(ULONG session_id, BSTR block_name, VARIANT_BOOL rc_value)
742 {
743  accessRuncontrol(session_id, block_name, rc_value, NULL, true);
744 }
745 
746 void InstAPILib::accessRuncontrol(ULONG session_id, BSTR block_name, VARIANT_BOOL new_value, VARIANT_BOOL* value, bool setvalue)
747 {
748  CComBSTR path(L"C:\\Labview modules\\dae\\monitor\\dae_monitor.vi");
749  CComBSTR control(L"Parameter details");
750 
751  CComVariant val;
752 
753  getLabviewValue(session_id, path, control, &val);
754 
755  CComSafeArray<BSTR> sab;
756  sab.CopyFrom(val.parray);
757  UINT d = sab.GetDimensions();
758  LONG numPars = sab.GetUpperBound(0); //this is inclusive, i.e. if it is 2 this means [2] is valid
759  LONG aIndex[2];
760  bstr_t bname;
761 
762  for (UINT i = 0; i <= numPars; ++i)
763  {
764  aIndex[0] = i;
765  aIndex[1] = 0;
766  sab.MultiDimGetAt(aIndex, bname.GetBSTR());
767 
768  if (wcscmp(bname, block_name) == 0)
769  {
770  if (setvalue)
771  {
772  //The runcontrol setting is found at the 7 column of the 2d array in LabVIEW.
773  aIndex[1] = 7;
774 
775  if (new_value)
776  {
777  CComBSTR bnew(L"1");
778  sab.MultiDimSetAt(aIndex, bnew);
779  }
780  else
781  {
782  CComBSTR bnew(L"0");
783  sab.MultiDimSetAt(aIndex, bnew);
784  }
785  CComVariant vnew(sab);
786  setLabviewValue(session_id, path, control, vnew);
787  break;
788  }
789  else
790  {
791  bstr_t bvalue;
792  aIndex[1] = 7;
793  sab.MultiDimGetAt(aIndex, bvalue.GetBSTR());
794 
795  if (wcscmp(bvalue, L"1") == 0)
796  {
797  *value = true;
798  }
799  else
800  {
801  *value = false;
802  }
803 
804  break;
805  }
806  }
807  }
808 }
809 
810 void InstAPILib::accessLimits(ULONG session_id, BSTR block_name, VARIANT new_value, BSTR* value, bool setvalue, bool low)
811 {
812  CComBSTR path(L"C:\\Labview modules\\dae\\monitor\\dae_monitor.vi");
813  CComBSTR control(L"Parameter details");
814 
815  CComVariant val;
816 
817  getLabviewValue(session_id, path, control, &val);
818 
819  CComSafeArray<BSTR> sab;
820  sab.CopyFrom(val.parray);
821  UINT d = sab.GetDimensions();
822  LONG numPars = sab.GetUpperBound(0); //this is inclusive, i.e. if it is 2 this means [2] is valid
823  LONG aIndex[2];
824  bstr_t bname;
825 
826  for (UINT i = 0; i <= numPars; ++i)
827  {
828  aIndex[0] = i;
829  aIndex[1] = 0;
830  sab.MultiDimGetAt(aIndex, bname.GetBSTR());
831 
832  if (wcscmp(bname, block_name) == 0)
833  {
834  if (setvalue)
835  {
836  CComVariant vWrapper(new_value);
837  vWrapper.ChangeType(VT_BSTR);
838 
839  if (low)
840  {
841  //The low limit is found at the 8 column of the 2d array in LabVIEW.
842  aIndex[1] = 8;
843  bstr_t bnew(vWrapper.bstrVal);
844  sab.MultiDimSetAt(aIndex, bnew);
845  }
846  else
847  {
848  //The high limit is found at the 9 column of the 2d array in LabVIEW.
849  aIndex[1] = 9;
850  bstr_t bnew(vWrapper.bstrVal);
851  sab.MultiDimSetAt(aIndex, bnew);
852  }
853  CComVariant vnew(sab);
854  setLabviewValue(session_id, path, control, vnew);
855  return;
856  }
857  else
858  {
859  if (low)
860  {
861  aIndex[1] = 8;
862  sab.MultiDimGetAt(aIndex, *value);
863  }
864  else
865  {
866  aIndex[1] = 9;
867  sab.MultiDimGetAt(aIndex, *value);
868  }
869 
870  return;
871  }
872  }
873  }
874 }
875 
876 void InstAPILib::getRunControlLimit(ULONG session_id, BSTR block_name, VARIANT* limit_value, bool low)
877 {
878  VARIANT null = VARIANT();
879  null.vt = VT_ERROR;
880  null.scode = DISP_E_PARAMNOTFOUND;
881 
882  CComBSTR result;
883  accessLimits(session_id, block_name, null, &result, false, low);
884 
885  CComVariant wrapper = CComVariant(result);
886 
887  //Try to convert the result to a double
888  HRESULT hr = wrapper.ChangeType(VT_R8);
889 
890  if (hr != 0)
891  {
892  //Conversion failed so change it back to a string
893  wrapper.ChangeType(VT_BSTR);
894  }
895 
896  wrapper.Detach(limit_value);
897 }
898 
899 void InstAPILib::setRunControlLimit(ULONG session_id, BSTR block_name, VARIANT new_value, bool low)
900 {
901  accessLimits(session_id, block_name, new_value, NULL, true, low);
902 }
903 
904 void InstAPILib::getRunState(ULONG session_id, BSTR* messages, LONG* status)
905 {
906  CComPtr<isisicpLib::Idae> icp;
907  HRESULT hr;
908  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
909 
910  if (FAILED(hr))
911  {
912  throw std::runtime_error("Could not connect to icp");
913  }
914 
915  *status = icp->getRunState(messages);
916 }
917 
918 void InstAPILib::beginRun(ULONG session_id, LONG period, VARIANT_BOOL wait, VARIANT_BOOL quiet, VARIANT_BOOL paused, BSTR* messages)
919 {
920  //Do we need to proprogate "quiet" this far?
921  CComPtr<isisicpLib::Idae> icp;
922  HRESULT hr;
923  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
924 
925  if (FAILED(hr))
926  {
927  throw std::runtime_error("Could not connect to icp");
928  }
929 
930  long options = 0;
931  if (paused) options += 1;
932  if (wait) options += 2;
933 
934  icp->beginRunEx(options, period, messages);
935 }
936 
937 void InstAPILib::abortRun(ULONG session_id, BSTR* messages)
938 {
939  CComPtr<isisicpLib::Idae> icp;
940  HRESULT hr;
941  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
942 
943  if (FAILED(hr))
944  {
945  throw std::runtime_error("Could not connect to icp");
946  }
947 
948  icp->abortRun(messages);
949 }
950 
951 void InstAPILib::endRun(ULONG session_id, BSTR* messages)
952 {
953  CComPtr<isisicpLib::Idae> icp;
954  HRESULT hr;
955  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
956 
957  if (FAILED(hr))
958  {
959  throw std::runtime_error("Could not connect to icp");
960  }
961 
962  icp->endRun(messages);
963 }
964 
965 void InstAPILib::pauseRun(ULONG session_id, BSTR* messages)
966 {
967  CComPtr<isisicpLib::Idae> icp;
968  HRESULT hr;
969  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
970 
971  if (FAILED(hr))
972  {
973  throw std::runtime_error("Could not connect to icp");
974  }
975 
976  icp->pauseRun(messages);
977 }
978 
979 void InstAPILib::resumeRun(ULONG session_id, BSTR* messages)
980 {
981  CComPtr<isisicpLib::Idae> icp;
982  HRESULT hr;
983  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
984 
985  if (FAILED(hr))
986  {
987  throw std::runtime_error("Could not connect to icp");
988  }
989 
990  icp->resumeRun(messages);
991 }
992 
993 void InstAPILib::recoverRun(ULONG session_id, BSTR* messages)
994 {
995  CComPtr<isisicpLib::Idae> icp;
996  HRESULT hr;
997  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
998 
999  if (FAILED(hr))
1000  {
1001  throw std::runtime_error("Could not connect to icp");
1002  }
1003 
1004  icp->setICPValueLong(L"RUN_STATUS", 3, messages);
1005 }
1006 
1007 void InstAPILib::saveRun(ULONG session_id, BSTR* messages)
1008 {
1009  CComPtr<isisicpLib::Idae> icp;
1010  HRESULT hr;
1011  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1012 
1013  if (FAILED(hr))
1014  {
1015  throw std::runtime_error("Could not connect to icp");
1016  }
1017 
1018  icp->saveRun(messages);
1019 }
1020 
1021 void InstAPILib::updateCRPT(ULONG session_id, BSTR* messages)
1022 {
1023  CComPtr<isisicpLib::Idae> icp;
1024  HRESULT hr;
1025  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1026 
1027  if (FAILED(hr))
1028  {
1029  throw std::runtime_error("Could not connect to icp");
1030  }
1031 
1032  icp->updateCRPT(messages);
1033 }
1034 
1035 void InstAPILib::storeCRPT(ULONG session_id, BSTR* messages)
1036 {
1037  CComPtr<isisicpLib::Idae> icp;
1038  HRESULT hr;
1039  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1040 
1041  if (FAILED(hr))
1042  {
1043  throw std::runtime_error("Could not connect to icp");
1044  }
1045 
1046  icp->storeCRPT(messages);
1047 }
1048 
1049 void InstAPILib::getGoodFrames(ULONG session_id, VARIANT_BOOL curr_period, BSTR* messages, LONG* frames)
1050 {
1051  CComPtr<isisicpLib::Idae> icp;
1052  HRESULT hr;
1053  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1054 
1055  if (FAILED(hr))
1056  {
1057  throw std::runtime_error("Could not connect to icp");
1058  }
1059 
1060  if (curr_period)
1061  {
1062  *frames = icp->getGoodFramesPeriod(messages);
1063  }
1064  else
1065  {
1066  *frames = icp->getGoodFramesTotal(messages);
1067  }
1068 }
1069 
1070 void InstAPILib::getUamps(ULONG session_id, VARIANT_BOOL curr_period, BSTR* messages, DOUBLE* uamps)
1071 {
1072  CComPtr<isisicpLib::Idae> icp;
1073  HRESULT hr;
1074  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1075 
1076  if (FAILED(hr))
1077  {
1078  throw std::runtime_error("Could not connect to icp");
1079  }
1080 
1081  if (curr_period)
1082  {
1083  *uamps = icp->getGoodUAmpHPeriod(messages);
1084  }
1085  else
1086  {
1087  *uamps = icp->getGoodUAmpH(messages);
1088  }
1089 }
1090 
1091 void InstAPILib::getMEvents(ULONG session_id, BSTR* messages, DOUBLE* mev)
1092 {
1093  CComPtr<isisicpLib::Idae> icp;
1094  HRESULT hr;
1095  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1096 
1097  if (FAILED(hr))
1098  {
1099  throw std::runtime_error("Could not connect to icp");
1100  }
1101 
1102  *mev = icp->getMEvents(messages);
1103 }
1104 
1105 void InstAPILib::getCurrentPeriod(ULONG session_id, BSTR* messages, LONG* period, LONG* daq_period)
1106 {
1107  CComPtr<isisicpLib::Idae> icp;
1108  HRESULT hr;
1109  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1110 
1111  if (FAILED(hr))
1112  {
1113  throw std::runtime_error("Could not connect to icp");
1114  }
1115 
1116  hr = icp->getCurrentPeriodNumber(period, daq_period, messages);
1117 }
1118 
1119 void InstAPILib::getRunNumber(ULONG session_id, BSTR* messages, LONG* run_no)
1120 {
1121  CComPtr<isisicpLib::Idae> icp;
1122  HRESULT hr;
1123  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1124 
1125  if (FAILED(hr))
1126  {
1127  throw std::runtime_error("Could not connect to icp");
1128  }
1129 
1130  *run_no = icp->getRunNumber(messages);
1131 }
1132 
1133 void InstAPILib::getTotalCounts(ULONG session_id, BSTR* messages, LONG* counts)
1134 {
1135  CComPtr<isisicpLib::Idae> icp;
1136  HRESULT hr;
1137  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1138 
1139  if (FAILED(hr))
1140  {
1141  throw std::runtime_error("Could not connect to icp");
1142  }
1143 
1144  *counts = icp->getTotalCounts(messages);
1145 }
1146 
1147 void InstAPILib::snapshotCRPT(ULONG session_id, BSTR filename, LONG do_update, LONG do_pause, BSTR* messages)
1148 {
1149  CComPtr<isisicpLib::Idae> icp;
1150  HRESULT hr;
1151  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1152 
1153  if (FAILED(hr))
1154  {
1155  throw std::runtime_error("Could not connect to icp");
1156  }
1157 
1158  hr = icp->snapshotCRPT(filename, do_update, do_pause, messages);
1159 }
1160 
1161 void InstAPILib::sumAllHistogramMemory(ULONG session_id, BSTR* messages, LONG* counts)
1162 {
1163  CComPtr<isisicpLib::Idae> icp;
1164  HRESULT hr;
1165  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1166 
1167  if (FAILED(hr))
1168  {
1169  throw std::runtime_error("Could not connect to icp");
1170  }
1171 
1172  hr = icp->sumAllHistogramMemory(counts, messages);
1173 }
1174 
1175 void InstAPILib::sumAllSpectra(ULONG session_id, BSTR* messages, LONG* counts, LONG* bin_counts)
1176 {
1177  CComPtr<isisicpLib::Idae> icp;
1178  HRESULT hr;
1179  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1180 
1181  if (FAILED(hr))
1182  {
1183  throw std::runtime_error("Could not connect to icp");
1184  }
1185 
1186  try
1187  {
1188  //This method appears to throw though it does return values
1189  hr = icp->sumAllSpectra(counts, bin_counts, messages);
1190  }
1191  catch(...)
1192  {
1193  }
1194 }
1195 
1196 void InstAPILib::setRbNumber(ULONG session_id, VARIANT rb_no)
1197 {
1198  _bstr_t path(L"C:\\Labview modules\\Common\\User Details\\User Details.vi");
1199  _bstr_t control(L"RB Number");
1200 
1201  setLabviewValue(session_id, path, control, rb_no);
1202 }
1203 
1204 void InstAPILib::getRbNumber(ULONG session_id, VARIANT* rb_no)
1205 {
1206  _bstr_t path(L"C:\\Labview modules\\Common\\User Details\\User Details.vi");
1207  _bstr_t control(L"RB Number");
1208 
1209  getLabviewValue(session_id, path, control, rb_no);
1210 }
1211 
1212 void InstAPILib::setPeriod(ULONG session_id, LONG period_number, BSTR* messages)
1213 {
1214  CComPtr<isisicpLib::Idae> icp;
1215  HRESULT hr;
1216  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1217 
1218  if (FAILED(hr))
1219  {
1220  throw std::runtime_error("Could not connect to icp");
1221  }
1222 
1223  try
1224  {
1225  //This method throws if the period number is invalid
1226  hr = icp->changePeriod(period_number, messages);
1227  }
1228  catch(...)
1229  {
1230  CComBSTR tmpbstr(L"Could not change period - is the number entered valid?");
1231  tmpbstr.CopyTo(messages);
1232  }
1233 }
1234 
1235 void InstAPILib::setTitle(ULONG session_id, VARIANT title)
1236 {
1237  _bstr_t path(L"C:\\Labview modules\\dae\\DAE Control.vi");
1238  _bstr_t title_control(L"New Run Title");
1239  _bstr_t button_control(L"Set Title");
1240 
1241  //Set the title
1242  setLabviewValue(session_id, path, title_control, title);
1243 
1244  _variant_t ans(true);
1245  setLabviewValue(session_id, path, button_control, ans);
1246 }
1247 
1248 void InstAPILib::getSpectrum(ULONG session_id, LONG spectrum_no, LONG period, VARIANT_BOOL as_dist, VARIANT* time_channels, VARIANT* signal, LONG* sum, BSTR* messages)
1249 {
1250  CComPtr<isisicpLib::Idae> icp;
1251  HRESULT hr;
1252  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1253 
1254  if (FAILED(hr))
1255  {
1256  throw std::runtime_error("Could not connect to icp");
1257  }
1258 
1259  if (as_dist != 0)
1260  {
1261  //Get a dist
1262  hr = icp->getSpectrum(spectrum_no, period, time_channels, signal, 0, 1, sum, messages);
1263  }
1264  else
1265  {
1266  hr = icp->getSpectrum(spectrum_no, period, time_channels, signal, 1, 0, sum, messages);
1267  }
1268 }
1269 
1270 void InstAPILib::changeNumberOfSoftwarePeriods(ULONG session_id, LONG num_periods, BSTR* messages)
1271 {
1272  CComPtr<isisicpLib::Idae> icp;
1273  HRESULT hr;
1274  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1275 
1276  if (FAILED(hr))
1277  {
1278  throw std::runtime_error("Could not connect to icp");
1279  }
1280 
1281  try
1282  {
1283  hr = icp->changeNumberOfSoftwarePeriods(num_periods, messages);
1284  }
1285  catch(...)
1286  {
1287  CComBSTR tmpbstr(L"Could not change number of software periods - is the DAE set to use software periods?");
1288  tmpbstr.CopyTo(messages);
1289  }
1290 }
1291 
1292 void InstAPILib::getNumberOfPeriods(ULONG session_id, BSTR* messages, LONG* num_periods)
1293 {
1294  CComPtr<isisicpLib::Idae> icp;
1295  HRESULT hr;
1296  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1297 
1298  if (FAILED(hr))
1299  {
1300  throw std::runtime_error("Could not connect to icp");
1301  }
1302 
1303  *num_periods = icp->getNumberOfPeriods(messages);
1304 }
1305 
1306 void InstAPILib::setBeamlineParameter(ULONG session_id, VARIANT name, VARIANT value, VARIANT units)
1307 {
1308  _bstr_t path(L"C:\\Labview modules\\Common\\Experiment Parameters\\Beamline Parameters.vi");
1309  _bstr_t name_ctrl(L"Custom Parameter Name");
1310  _bstr_t value_ctrl(L"Custom Parameter Value");
1311  _bstr_t units_ctrl(L"Custom Parameter Units "); //Note extra space
1312  _bstr_t set_ctrl(L"Add Custom parameter");
1313 
1314  setLabviewValue(session_id, path, name_ctrl, name);
1315  setLabviewValue(session_id, path, value_ctrl, value);
1316  setLabviewValue(session_id, path, units_ctrl, units);
1317 
1318  _variant_t ans(true);
1319  setLabviewValue(session_id, path, set_ctrl, ans);
1320 }
1321 
1322 void InstAPILib::deleteBeamlineParameter(ULONG session_id, VARIANT name)
1323 {
1324  _bstr_t path(L"C:\\Labview modules\\Common\\Experiment Parameters\\Beamline Parameters.vi");
1325  _bstr_t name_ctrl(L"Custom Parameter Name");
1326  _bstr_t set_ctrl(L"Remove Custom Parameter");
1327 
1328  setLabviewValue(session_id, path, name_ctrl, name);
1329 
1330  _variant_t ans(true);
1331  setLabviewValue(session_id, path, set_ctrl, ans);
1332 }
1333 
1335 {
1336  _bstr_t path(L"C:\\Labview modules\\Common\\Experiment Parameters\\Beamline Parameters.vi");
1337  _bstr_t del_ctrl(L"Clear Parameters");
1338  _variant_t ans(true);
1339  setLabviewValue(session_id, path, del_ctrl, ans);
1340 }
1341 
1342 void InstAPILib::getBeamlineParameterNames(ULONG session_id, VARIANT* names)
1343 {
1344  _bstr_t path(L"C:\\Labview modules\\Common\\Experiment Parameters\\Beamline Parameters.vi");
1345  _bstr_t name_ctrl(L"Additional Parameters");
1346 
1347  CComVariant val;
1348 
1349  getLabviewValue(session_id, path, name_ctrl, &val);
1350 
1351  int dims[2];
1352  int n = 2;
1353  arrayVariantDimensions(&val, dims, n);
1354 
1355  // Get a pointer to the elements of the array.
1356  bool changed = false;
1357  BSTR HUGEP *pbstr;
1358  HRESULT hr = SafeArrayAccessData(val.parray, (void HUGEP**)&pbstr);
1359 
1360  if (FAILED(hr))
1361  {
1362  return;
1363  }
1364 
1365  CComSafeArray<BSTR> saBstr(dims[0], 0);
1366 
1367  //dims[0] gives the number of entries (rows)
1368  for (int x = 0; x < dims[0]; ++x)
1369  {
1370  //Add the name
1371  LONG index[1];
1372  index[0] = x;
1373  HRESULT hr = saBstr.MultiDimSetAt(index, pbstr[x]);
1374  }
1375 
1376  CComVariant varArrayWrapper(saBstr);
1377  varArrayWrapper.Detach(names);
1378 
1379  SafeArrayUnaccessData(val.parray);
1380 }
1381 
1382 void InstAPILib::getBeamlineParameter(ULONG session_id, BSTR name, BSTR* value, BSTR* units, BSTR* type)
1383 {
1384  _bstr_t path(L"C:\\Labview modules\\Common\\Experiment Parameters\\Beamline Parameters.vi");
1385  _bstr_t name_ctrl(L"Additional Parameters");
1386 
1387  CComVariant val;
1388  getLabviewValue(session_id, path, name_ctrl, &val);
1389 
1390  CComSafeArray<BSTR> sab;
1391  sab.CopyFrom(val.parray);
1392  UINT d = sab.GetDimensions();
1393  LONG numPars = sab.GetUpperBound(0); //this is inclusive, i.e. if it is 2 this means [2] is valid
1394 
1395  LONG aIndex[2];
1396  bstr_t bname;
1397 
1398  for (LONG i = 0; i <= numPars; ++i)
1399  {
1400  aIndex[0] = i;
1401  aIndex[1] = 0;
1402  sab.MultiDimGetAt(aIndex, bname.GetBSTR());
1403 
1404  if (wcscmp(bname, name) == 0)
1405  {
1406  aIndex[1] = 3;
1407  sab.MultiDimGetAt(aIndex, *value);
1408  aIndex[1] = 2;
1409  sab.MultiDimGetAt(aIndex, *units);
1410  aIndex[1] = 1;
1411  sab.MultiDimGetAt(aIndex, *type);
1412  break;
1413  }
1414  }
1415 }
1416 
1417 void InstAPILib::setSampleParameter(ULONG session_id, VARIANT name, VARIANT value, VARIANT units)
1418 {
1419  _bstr_t path(L"C:\\Labview modules\\Common\\Experiment Parameters\\Sample Parameters.vi");
1420  _bstr_t name_ctrl(L"Custom Parameter Name");
1421  _bstr_t value_ctrl(L"Custom Parameter Value");
1422  _bstr_t units_ctrl(L"Custom Parameter Units "); //Note extra space
1423  _bstr_t set_ctrl(L"Add Custom parameter");
1424 
1425  setLabviewValue(session_id, path, name_ctrl, name);
1426  setLabviewValue(session_id, path, value_ctrl, value);
1427  setLabviewValue(session_id, path, units_ctrl, units);
1428 
1429  _variant_t ans(true);
1430  setLabviewValue(session_id, path, set_ctrl, ans);
1431 }
1432 
1433 void InstAPILib::deleteSampleParameter(ULONG session_id, VARIANT name)
1434 {
1435  _bstr_t path(L"C:\\Labview modules\\Common\\Experiment Parameters\\Sample Parameters.vi");
1436  _bstr_t name_ctrl(L"Custom Parameter Name");
1437  _bstr_t set_ctrl(L"Remove Custom Parameter");
1438 
1439  setLabviewValue(session_id, path, name_ctrl, name);
1440 
1441  _variant_t ans(true);
1442  setLabviewValue(session_id, path, set_ctrl, ans);
1443 }
1444 
1446 {
1447  _bstr_t path(L"C:\\Labview modules\\Common\\Experiment Parameters\\Sample Parameters.vi");
1448  _bstr_t del_ctrl(L"Clear Parameters");
1449  _variant_t ans(true);
1450  setLabviewValue(session_id, path, del_ctrl, ans);
1451 }
1452 
1453 void InstAPILib::getSampleParameterNames(ULONG session_id, VARIANT* names)
1454 {
1455  _bstr_t path(L"C:\\Labview modules\\Common\\Experiment Parameters\\Sample Parameters.vi");
1456  _bstr_t name_ctrl(L"Additional Parameters");
1457 
1458  CComVariant val;
1459 
1460  getLabviewValue(session_id, path, name_ctrl, &val);
1461 
1462  int dims[2];
1463  int n = 2;
1464  arrayVariantDimensions(&val, dims, n);
1465 
1466  // Get a pointer to the elements of the array.
1467  bool changed = false;
1468  BSTR HUGEP *pbstr;
1469  HRESULT hr = SafeArrayAccessData(val.parray, (void HUGEP**)&pbstr);
1470 
1471  if (FAILED(hr))
1472  {
1473  return;
1474  }
1475 
1476  CComSafeArray<BSTR> saBstr(dims[0], 0);
1477 
1478  //dims[0] gives the number of entries (rows)
1479  for (int x = 0; x < dims[0]; ++x)
1480  {
1481  //Add the name
1482  LONG index[1];
1483  index[0] = x;
1484  HRESULT hr = saBstr.MultiDimSetAt(index, pbstr[x]);
1485  }
1486 
1487  CComVariant varArrayWrapper (saBstr);
1488  varArrayWrapper.Detach(names);
1489 
1490  SafeArrayUnaccessData(val.parray);
1491 }
1492 
1493 void InstAPILib::getSampleParameter(ULONG session_id, BSTR name, BSTR* value, BSTR* units, BSTR* type)
1494 {
1495  _bstr_t path(L"C:\\Labview modules\\Common\\Experiment Parameters\\Sample Parameters.vi");
1496  _bstr_t name_ctrl(L"Additional Parameters");
1497 
1498  CComVariant val;
1499  getLabviewValue(session_id, path, name_ctrl, &val);
1500 
1501  CComSafeArray<BSTR> sab;
1502  sab.CopyFrom(val.parray);
1503  UINT d = sab.GetDimensions();
1504  LONG numPars = sab.GetUpperBound(0); //this is inclusive, i.e. if it is 2 this means [2] is valid
1505 
1506  LONG aIndex[2];
1507  bstr_t bname;
1508 
1509  for (LONG i = 0; i <= numPars; ++i)
1510  {
1511  aIndex[0] = i;
1512  aIndex[1] = 0;
1513  sab.MultiDimGetAt(aIndex, bname.GetBSTR());
1514 
1515  if (wcscmp(bname, name) == 0)
1516  {
1517  aIndex[1] = 3;
1518  sab.MultiDimGetAt(aIndex, *value);
1519  aIndex[1] = 2;
1520  sab.MultiDimGetAt(aIndex, *units);
1521  aIndex[1] = 1;
1522  sab.MultiDimGetAt(aIndex, *type);
1523  break;
1524  }
1525  }
1526 }
1527 
1528 void InstAPILib::vmeWriteValue(ULONG session_id, ULONG card_id, ULONG card_address, ULONG word_size, ULONG value, ULONG mode, BSTR* messages)
1529 {
1530  CComPtr<isisicpLib::Idae> icp;
1531  HRESULT hr;
1532  hr = icp.CoCreateInstance(isisicpLib::CLSID_dae, 0, CLSCTX_LOCAL_SERVER);
1533 
1534  if (FAILED(hr))
1535  {
1536  throw std::runtime_error("Could not connect to icp");
1537  }
1538 
1539  hr = icp->VMEWriteValue(card_id, card_address, word_size, value, mode, messages);
1540 
1541  if (FAILED(hr))
1542  {
1543  throw std::runtime_error("Problem writing value to VME");
1544  }
1545 }
1546 
1547 void InstAPILib::getSeciConfig(ULONG session_id, BSTR* name)
1548 {
1549  std::string config = "";
1550  se_get_seci_config(config);
1551  CComBSTR tmpbstr(config.c_str());
1552  tmpbstr.CopyTo(name);
1553 }
1554 
1555 void InstAPILib::clearDatabaseLogs(ULONG session_id, int run_num)
1556 {
1557  //If the run-number is 0 then the current run-number is used
1558  se_clear_values(run_num, run_num);
1559 }
1560 
1561 void InstAPILib::clearLogFiles(ULONG session_id, int run_num, BSTR path)
1562 {
1563  _bstr_t myb(path);
1564  std::string dir(myb);
1565 
1566  Poco::File logDir(dir);
1567 
1568  std::string s = boost::lexical_cast<std::string>(run_num);
1569 
1570  std::string nameformat("^[a-z]+\\w*[a-z]+0*" + s + "(_(\\w|_)+.txt|.log)");
1571 
1572  if (logDir.exists() && logDir.isDirectory())
1573  {
1574  Poco::RegularExpression re(nameformat, Poco::RegularExpression::RE_CASELESS);
1575 
1576  std::vector<std::string> files;
1577  logDir.list(files);
1578  std::vector<std::string>::iterator it = files.begin();
1579  for (; it != files.end(); ++it)
1580  {
1581  if (re.match(*it))
1582  {
1583  //Delete it
1584  std::string fullname(dir + "\\" + *it);
1585  Poco::File temp(fullname);
1586  temp.remove();
1587  }
1588  }
1589  }
1590 }
1591 
1592 void InstAPILib::getMeasurementLabel(ULONG session_id, BSTR id, BSTR* label)
1593 {
1594  _bstr_t temp(id);
1595  const size_t newsize = (temp.length()+1)*2;
1596  char *nstring = new char[newsize];
1597  strcpy_s(nstring, newsize, (char *)temp);
1598  std::string ans;
1599  se_get_measurement_label(temp, ans);
1600  *label = CComBSTR(ans.c_str()).Detach();
1601 }
1602 
1603 void InstAPILib::setMeasurementLabel(ULONG session_id, BSTR id, BSTR label)
1604 {
1605  _bstr_t b_id(id);
1606  size_t newsize = (b_id.length()+1)*2;
1607  char *id_string = new char[newsize];
1608  strcpy_s(id_string, newsize, (char *)b_id);
1609 
1610  _bstr_t b_label(label);
1611  newsize = (b_label.length()+1)*2;
1612  char *label_string = new char[newsize];
1613  strcpy_s(label_string, newsize, (char *)b_label);
1614 
1615  se_set_measurement_label(id_string, label_string);
1616 }
1617 
1618 void InstAPILib::getMeasurementId(ULONG session_id, BSTR label, VARIANT_BOOL newId, BSTR* id)
1619 {
1620  _bstr_t temp(label);
1621  const size_t newsize = (temp.length()+1)*2;
1622  char *nstring = new char[newsize];
1623  strcpy_s(nstring, newsize, (char *)temp);
1624  std::string ans;
1625 
1626  if (newId)
1627  {
1628  se_new_measurement(nstring, ans);
1629  }
1630  else
1631  {
1632  se_get_measurement_id(nstring, ans);
1633  }
1634 
1635  *id = CComBSTR(ans.c_str()).Detach();
1636 }
1637 
1638 void InstAPILib::getBlockNames(ULONG session_id, VARIANT* block_names)
1639 {
1640  //For now we will get the values from Seci, but this will be changed at some point
1641  CComPtr<Seci::IComInterface> seci;
1642  HRESULT hr = seci.CoCreateInstance(Seci::CLSID_ComInterface, 0, CLSCTX_LOCAL_SERVER);
1643 
1644  if (FAILED(hr))
1645  {
1646  throw std::runtime_error("Could not connect to Seci");
1647  }
1648 
1649  block_names->vt = VT_ARRAY | VT_BSTR;
1650  block_names->parray = seci->GetBlockNames(true);
1651 }
1652 
1653 void InstAPILib::clearSeciGraphs(ULONG session_id)
1654 {
1655  CComPtr<Seci::IComInterface> seci;
1656  HRESULT hr = seci.CoCreateInstance(Seci::CLSID_ComInterface, 0, CLSCTX_LOCAL_SERVER);
1657 
1658  if (FAILED(hr))
1659  {
1660  throw std::runtime_error("Could not connect to Seci");
1661  }
1662 
1663  seci->ClearGraphs();
1664 }
1665 
1666 void InstAPILib::changeSeciConfiguration(ULONG session_id, BSTR name, VARIANT_BOOL* result)
1667 {
1668  CComPtr<Seci::IComInterface> seci;
1669  HRESULT hr = seci.CoCreateInstance(Seci::CLSID_ComInterface, 0, CLSCTX_LOCAL_SERVER);
1670 
1671  if (FAILED(hr))
1672  {
1673  throw std::runtime_error("Could not connect to Seci");
1674  }
1675 
1676  *result = seci->ChangeConfiguration(name);
1677 }
1678 
1679 void InstAPILib::isSeciBusy(ULONG session_id, VARIANT_BOOL* result)
1680 {
1681  CComPtr<Seci::IComInterface> seci;
1682  HRESULT hr = seci.CoCreateInstance(Seci::CLSID_ComInterface, 0, CLSCTX_LOCAL_SERVER);
1683 
1684  if (FAILED(hr))
1685  {
1686  throw std::runtime_error("Could not connect to Seci");
1687  }
1688 
1689  *result = seci->IsSeciBusy();
1690 }
1691 
void beginRun(ULONG session_id, LONG period, VARIANT_BOOL wait, VARIANT_BOOL quiet, VARIANT_BOOL paused, BSTR *messages)
void sumAllSpectra(ULONG session_id, BSTR *messages, LONG *counts, LONG *bin_counts)
void changeSeciConfiguration(ULONG session_id, BSTR name, VARIANT_BOOL *result)
void clearDatabaseLogs(ULONG session_id, int run_num)
void createSession(BSTR instrument, BSTR user_name, BSTR password, ULONG *session_id)
Definition: instapi_lib.cpp:21
void isSeciBusy(ULONG session_id, VARIANT_BOOL *result)
void deleteSession(ULONG session_id)
Definition: instapi_lib.cpp:27
void getRunState(ULONG session_id, BSTR *messages, LONG *status)
void pushBlockButton(ULONG session_id, BSTR block_name, bool use_external, VARIANT *results)
void getMEvents(ULONG session_id, BSTR *messages, DOUBLE *mev)
HRESULT testicp()
void createViRef(BSTR vi_name, bool reentrant, LabVIEW::VirtualInstrumentPtr &vi)
void getSeciConfig(ULONG session_id, BSTR *name)
SELOGGER_API int __stdcall se_new_measurement(const char *label, std::string &res)
Definition: selogger.cpp:872
SELOGGER_API int __stdcall se_get_measurement_id(const char *label, std::string &measurement_id)
Definition: selogger.cpp:947
vi_map_t m_vimap
Definition: instapi_lib.h:26
static int test()
void snapshotCRPT(ULONG session_id, BSTR filename, LONG do_update, LONG do_pause, BSTR *messages)
void abortRun(ULONG session_id, BSTR *messages)
void startVi(ULONG session_id, BSTR vi_name)
void toggleViVisibility(ULONG session_id, BSTR vi_name, VARIANT_BOOL show)
void setRole(ULONG session_id, BSTR role)
Definition: instapi_lib.cpp:36
void saveRun(ULONG session_id, BSTR *messages)
void recoverRun(ULONG session_id, BSTR *messages)
void setLabviewValueExt(ULONG session_id, BSTR vi_name, BSTR control_name, VARIANT value, VARIANT *results)
void getRunControl(ULONG session_id, BSTR block_name, VARIANT_BOOL *rc_value)
void getGoodFrames(ULONG session_id, VARIANT_BOOL curr_period, BSTR *messages, LONG *frames)
void setMeasurementLabel(ULONG session_id, BSTR id, BSTR label)
void getBlockNames(ULONG session_id, VARIANT *block_names)
void logBlocksNow(ULONG session_id, BSTR source)
void getSpectrum(ULONG session_id, LONG spectrum_no, LONG period, VARIANT_BOOL as_dist, VARIANT *time_channels, VARIANT *signal, LONG *sum, BSTR *messages)
static COAUTHIDENTITY * createIdentity(const std::string &user, const std::string &domain, const std::string &pass)
Definition: instapi_lib.cpp:40
void pauseRun(ULONG session_id, BSTR *messages)
void vmeWriteValue(ULONG session_id, ULONG card_id, ULONG card_address, ULONG word_size, ULONG value, ULONG mode, BSTR *messages)
void setPeriod(ULONG session_id, LONG period_number, BSTR *messages)
SELOGGER_API const char *__stdcall se_get_errmsg()
Definition: selogger.cpp:696
bool reentrant
Definition: instapi_lib.h:16
void getBeamlineParameterNames(ULONG session_id, VARIANT *names)
void getRunNumber(ULONG session_id, BSTR *messages, LONG *run_no)
std::string & set_control()
Definition: selogger.h:36
void setBlockSetpoint(ULONG session_id, BSTR block_name, VARIANT setpoint_value, bool push_button, bool use_external, VARIANT *results)
void deleteAllBeamlineParameters(ULONG session_id)
void setRunControl(ULONG session_id, BSTR block_name, VARIANT_BOOL rc_value)
void getBlockSetpoint(ULONG session_id, BSTR block_name, VARIANT *setpoint_value)
void callLabview(ULONG session_id, BSTR vi_name, VARIANT names, VARIANT values, VARIANT_BOOL reentrant, VARIANT *results)
void clearSeciGraphs(ULONG session_id)
void getRunControlLimit(ULONG session_id, BSTR block_name, VARIANT *limit_value, bool low)
void getMeasurementLabel(ULONG session_id, BSTR id, BSTR *label)
void getMessages(ULONG session_id, LONG start_index, BSTR min_severity, VARIANT *times, VARIANT *severities, VARIANT *messages, LONG *end_index)
void getBlockValue(ULONG session_id, BSTR block_name, VARIANT *block_value)
void updateCRPT(ULONG session_id, BSTR *messages)
void setRunControlLimit(ULONG session_id, BSTR block_name, VARIANT new_value, bool low)
void closeViFrontPanel(ULONG session_id, BSTR vi_name)
HRESULT test()
Definition: instapi_lib.cpp:73
void changeNumberOfSoftwarePeriods(ULONG session_id, LONG num_periods, BSTR *messages)
void getSampleParameterNames(ULONG session_id, VARIANT *names)
SELOGGER_API int __stdcall se_get_block_details(const char *block_name, SEBLOCK_DETAILS &block)
Definition: selogger.cpp:1481
SELOGGER_API int __stdcall se_get_measurement_label(const char *measurement_id, std::string &res)
Definition: selogger.cpp:905
session_map_t m_session
Definition: instapi_lib.h:23
void deleteAllSampleParameters(ULONG session_id)
void getNumberOfPeriods(ULONG session_id, BSTR *messages, LONG *num_periods)
void setSampleParameter(ULONG session_id, VARIANT name, VARIANT value, VARIANT units)
void accessRuncontrol(ULONG session_id, BSTR block_name, VARIANT_BOOL new_value, VARIANT_BOOL *value, bool setvalue)
void getCurrentPeriod(ULONG session_id, BSTR *messages, LONG *period, LONG *daq_period)
void setBeamlineParameter(ULONG session_id, VARIANT name, VARIANT value, VARIANT units)
SELOGGER_API int __stdcall se_get_seci_config(std::string &seci_config)
Definition: selogger.cpp:739
void stopVi(ULONG session_id, BSTR vi_name)
static HRESULT setIdentity(COAUTHIDENTITY *pidentity, IUnknown *pUnk)
Definition: instapi_lib.cpp:53
HRESULT testlv()
std::string & vi_name()
Definition: selogger.h:34
void endRun(ULONG session_id, BSTR *messages)
void sendBlockSetpoint(ULONG session_id, BSTR block_name, VARIANT setpoint_value, bool push_button, bool use_external, VARIANT *results)
void setRbNumber(ULONG session_id, VARIANT rb_no)
void getUamps(ULONG session_id, VARIANT_BOOL curr_period, BSTR *messages, DOUBLE *uamps)
void putMessage(ULONG session_id, BSTR time, BSTR severity, BSTR message)
void setTitle(ULONG session_id, VARIANT title)
SELOGGER_API int __stdcall se_clear_values(int run_start, int run_finish)
Definition: selogger.cpp:1040
void getViState(ULONG session_id, BSTR vi_name, VARIANT *value)
SELOGGER_API int __stdcall se_set_block_value(const char *block_name, const char *block_value)
Definition: selogger.cpp:475
std::string & read_control()
Definition: selogger.h:35
static int junk(const std::string &hostname, GUID C, GUID G)
SELOGGER_API int __stdcall se_set_measurement_label(const char *measurement_id, const char *label)
Definition: selogger.cpp:931
std::string & button_control()
Definition: selogger.h:37
void accessLimits(ULONG session_id, BSTR block_name, VARIANT new_value, BSTR *value, bool setvalue, bool low)
void resumeRun(ULONG session_id, BSTR *messages)
void getRbNumber(ULONG session_id, VARIANT *rb_no)
void getViRef(BSTR vi_name, bool reentrant, LabVIEW::VirtualInstrumentPtr &vi)
void getSampleParameter(ULONG session_id, BSTR name, BSTR *value, BSTR *units, BSTR *type)
void storeCRPT(ULONG session_id, BSTR *messages)
void getMeasurementId(ULONG session_id, BSTR label, VARIANT_BOOL newId, BSTR *id)
void getLabviewValue(ULONG session_id, BSTR vi_name, BSTR control_name, VARIANT *value)
void clearLogFiles(ULONG session_id, int run_num, BSTR path)
LabVIEW::VirtualInstrumentPtr vi_ref
Definition: instapi_lib.h:15
void getBeamlineParameter(ULONG session_id, BSTR name, BSTR *value, BSTR *units, BSTR *type)
void getTotalCounts(ULONG session_id, BSTR *messages, LONG *counts)
void sumAllHistogramMemory(ULONG session_id, BSTR *messages, LONG *counts)
void setLabviewValue(ULONG session_id, BSTR vi_name, BSTR control_name, VARIANT value)
void deleteBeamlineParameter(ULONG session_id, VARIANT name)
int arrayVariantDimensions(VARIANT *v, int dims_array[], int &ndims)
void getRole(ULONG session_id, BSTR *role)
Definition: instapi_lib.cpp:32
void deleteSampleParameter(ULONG session_id, VARIANT name)
void getViNames(ULONG session_id, VARIANT *names)
SELOGGER_API int __stdcall se_log_values_async(long run_number, const char *source, const std::vector< std::string > &iso_times, const std::vector< std::string > &block_names, const std::vector< std::string > &block_values)
Definition: selogger.cpp:572