Skip to content

Commit

Permalink
reformat: add missing brackets of one line block
Browse files Browse the repository at this point in the history
  • Loading branch information
edimetia3d committed Oct 14, 2021
1 parent fded35c commit 3742093
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 21 deletions.
24 changes: 23 additions & 1 deletion LocalSyncWrapper/LocalSyncOPCCLient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ bool LocalSyncOPCCLient::Connect(std::string serverName)
{
// check if opc service runing
if (!DetectService("OpcEnum"))
{
return false;
}
is_env_ready = true;

// create local host
Expand All @@ -55,7 +57,9 @@ bool LocalSyncOPCCLient::Connect(std::string serverName)
std::vector<std::wstring> localServerList;
p_host_->getListOfDAServers(IID_CATID_OPCDAServer20, localServerList, localClassIdList);
if (!localServerList.size())
{
return false;
}

p_opc_server_ = p_host_->connectDAServer(COPCHost::S2WS(serverName));
std::vector<std::wstring> item_name_vector;
Expand All @@ -67,11 +71,13 @@ bool LocalSyncOPCCLient::Connect(std::string serverName)
// add items
int item_index = 0;
for (unsigned int i = 0; i < item_name_vector.size(); ++i)
{
if (ItemNameFilter(COPCHost::WS2S(item_name_vector[i])))
{
name_item_map_[item_name_vector[i]] = p_group_->addItem(item_name_vector[i], true);
++item_index;
} // if
}
} // if

if (!IsConnected())
{
Expand All @@ -87,11 +93,15 @@ bool LocalSyncOPCCLient::Connect(std::string serverName)
bool LocalSyncOPCCLient::IsConnected()
{
if (!is_env_ready)
{
return false;
}

// check is opc server runing
if (IsOPCRuning() && IsOPCConnectedPLC())
{
return true;
}

return false;

Expand All @@ -100,7 +110,9 @@ bool LocalSyncOPCCLient::IsConnected()
bool LocalSyncOPCCLient::DisConnect()
{
if (!is_env_ready)
{
return true;
}

CleanOPCMember();
return true;
Expand All @@ -110,12 +122,16 @@ bool LocalSyncOPCCLient::DisConnect()
bool LocalSyncOPCCLient::IsOPCRuning()
{
if (!p_opc_server_)
{
return false;
}

ServerStatus status = {0};
p_opc_server_->getStatus(status);
if (status.dwServerState != OPC_STATUS_RUNNING)
{
return false;
}

return true;

Expand Down Expand Up @@ -241,7 +257,9 @@ bool LocalSyncOPCCLient::ReadUint16Array(std::string item_name, uint16_t *item_v
uint16_t *buf;
SafeArrayAccessData(array_variant.parray, (void **)&buf);
for (int i = 0; i < array_size; ++i)
{
item_value_array[i] = buf[i];
}

SafeArrayUnaccessData(array_variant.parray);
VariantClear(&array_variant);
Expand All @@ -263,7 +281,9 @@ bool LocalSyncOPCCLient::WriteUint16Array(std::string item_name, uint16_t *item_
uint16_t *buf;
SafeArrayAccessData(array_variant.parray, (void **)&buf);
for (int i = 0; i < array_size; ++i)
{
buf[i] = item_value_array[i];
}

SafeArrayUnaccessData(array_variant.parray);
// write variant
Expand All @@ -278,7 +298,9 @@ bool LocalSyncOPCCLient::CleanOPCMember()
if (IsOPCRuning()) // delete heap if connected
{
for (auto iter = name_item_map_.begin(); iter != name_item_map_.end(); ++iter)
{
delete iter->second;
}

name_item_map_.clear();

Expand Down
29 changes: 23 additions & 6 deletions OPCClientDemo/OPCClientDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ class CMyCallback : public IAsyncDataCallback
const COPCItem *item = data->item(); // retrieve original item pointer from item data..

if (item)
{
printf("-----> '%ws', handle: %u, changed async read quality %d value %d\n",
item->getName().c_str(), handle, data->wQuality, data->vDataValue.iVal);
}
} // if
} // while

Expand Down Expand Up @@ -118,15 +120,16 @@ void main(void)
{
COPCClient::init();

printf(
"input hostname (warning: NOT an IP address, use such as: 'Jhon-PC' or 'localhost' / <ENTER> ):\n"); // See
// readme
// to find
// reason
printf("input hostname (warning: NOT an IP address, use such as: 'Jhon-PC' or 'localhost' / <ENTER> ):\n"); // See
// readme
// to find
// reason
char c_string[100] = {0};
gets_s(c_string);
if (!strlen(c_string))
{
strcpy_s(c_string, sizeof(c_string), "localhost");
}
COPCHost *host = COPCClient::makeHost(COPCHost::S2WS(c_string));

// list servers
Expand All @@ -140,14 +143,18 @@ void main(void)
{
printf("%d: '%ws'\n", i, localServerList[i].c_str());
if (localServerList[i] == L"Matrikon.OPC.Simulation.1")
{
server_id = i;
}
} // for

// connect to OPC
printf("\nselect server ID or <ENTER> for Matrikon:\n");
gets_s(c_string);
if (strlen(c_string))
{
server_id = atol(c_string);
}
std::wstring serverName = localServerList[server_id];
printf("server name: '%ws'\n", serverName.c_str());
COPCServer *opcServer = host->connectDAServer(serverName);
Expand All @@ -168,7 +175,9 @@ void main(void)
opcServer->getItemNames(opcItemNames);
printf("\n\ngot %d names:\n", static_cast<int>(opcItemNames.size()));
for (unsigned i = 0; i < opcItemNames.size(); ++i)
{
printf("%3d: '%ws'\n", i + 1, opcItemNames[i].c_str());
}

// make demo group
unsigned long refreshRate;
Expand All @@ -186,21 +195,28 @@ void main(void)
itemNames.push_back(opcItemNames[21]); // 15 -> Bucket Brigade.UInt2
itemNames.push_back(opcItemNames[22]); // 16 -> Bucket Brigade.UInt4
if (demoGroup->addItems(itemNames, itemsCreated, errors, true) != 0)
{
printf("add items to group FAILED\n");
}

// get properties
std::vector<CPropertyDescription> propDesc;
readWritableItem->getSupportedProperties(propDesc);
printf("supported properties for '%ws'\n", readWritableItem->getName().c_str());
for (unsigned i = 0; i < propDesc.size(); ++i)
{
printf("%3d: ID = %u, description = '%ws', type = %d\n", i, propDesc[i].id, propDesc[i].desc.c_str(),
propDesc[i].type);
}

CAutoPtrArray<SPropertyValue> propVals;
readWritableItem->getProperties(propDesc, propVals);
for (unsigned i = 0; i < propDesc.size(); ++i)
{
if (!propVals[i])
{
printf("FAILED to get property %u\n", propDesc[i].id);
}
else
{
printf("Property %u=", propDesc[i].id);
Expand Down Expand Up @@ -229,7 +245,8 @@ void main(void)
printf("\n");
break;
} // switch
} // else
}
} // else

// sync read of item
OPCItemData data;
Expand Down
12 changes: 12 additions & 0 deletions OPCClientToolKit/OPCClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ bool COPCClient::init(OPCOLEInitMode mode)
{
HRESULT result = -1;
if (mode == APARTMENTTHREADED)
{
result = CoInitialize(nullptr);
}
if (mode == MULTITHREADED)
{
result = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
}

if (FAILED(result))
{
throw OPCException(L"COPCClient::init: CoInitialize failed");
}

CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr,
EOAC_NONE, nullptr);
Expand All @@ -55,7 +61,9 @@ bool COPCClient::init(OPCOLEInitMode mode)
{
result = CoGetMalloc(MEMCTX_TASK, &iMalloc);
if (FAILED(result))
{
throw OPCException(L"COPCClient::init: CoGetMalloc FAILED");
}
} // if

++ReleaseCount;
Expand All @@ -66,7 +74,9 @@ bool COPCClient::init(OPCOLEInitMode mode)
void COPCClient::stop()
{
if (--ReleaseCount <= 0)
{
iMalloc.Release();
}

CoUninitialize();

Expand All @@ -90,7 +100,9 @@ void COPCClient::comFreeVariant(VARIANT *memory, unsigned size)
COPCHost *COPCClient::makeHost(const std::wstring &hostName)
{
if (!hostName.size() || (hostName == L"localhost") || (hostName == L"127.0.0.1"))
{
return new CLocalHost();
}

return new CRemoteHost(hostName);

Expand Down
Loading

0 comments on commit 3742093

Please sign in to comment.