Skip to content

Commit

Permalink
Merge pull request edimetia3d#3 from gc87/opcclient-dll
Browse files Browse the repository at this point in the history
feat: group operation, item operation
  • Loading branch information
gc87 authored Aug 2, 2022
2 parents 942f93f + 1b4a19c commit bae634e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 29 deletions.
108 changes: 83 additions & 25 deletions OPCClientDLL/opc_client_interface.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "pch.h" // use stdafx.h in Visual Studio 2017 and earlier
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <sys\timeb.h>
Expand All @@ -15,20 +16,27 @@

using namespace std;

using Items = map<wstring, COPCItem *>;
using GroupTuple = tuple<COPCGroup *, Items>;

class OPCClient : public OPCClientInterface
{
public:
void Init() override;
void Stop() override;
bool Connect(std::string &hostName, std::string &serverName) override;
ServerNames GetServers(std::string &hostName) override;
bool Connect(wstring &host_name, wstring &server_name) override;
ServerNames GetServers(wstring &host_name) override;
ServerItems GetItems() override;
void AddGroup(wstring &group_name) override;
void AddItem(wstring &group_name, wstring &item_name) override;
string GetItemType(wstring &item_name) override;

private:
wstring host_name_;
wstring server_name_;
COPCHost *host_;
COPCServer *server_;
map<wstring, GroupTuple> groups_;
};

OPCClientInterface *instance_create()
Expand All @@ -52,30 +60,30 @@ void OPCClient::Stop()
COPCClient::stop();
}

bool OPCClient::Connect(std::string &hostName, std::string &serverName)
bool OPCClient::Connect(wstring &host_name, wstring &server_name)
{
host_name_ = COPCHost::S2WS(hostName);
server_name_ = COPCHost::S2WS(serverName);
host_name_ = host_name;
server_name_ = server_name;

host_ = COPCClient::makeHost(host_name_);
vector<CLSID> localClassIds;
vector<wstring> localServers;
host_->getListOfDAServers(IID_CATID_OPCDAServer20, localServers, localClassIds);
vector<CLSID> local_class_ids;
vector<wstring> local_servers;
host_->getListOfDAServers(IID_CATID_OPCDAServer20, local_servers, local_class_ids);

int id = -1;
for (unsigned i = 0; i < localServers.size(); i++)
for (unsigned i = 0; i < local_servers.size(); i++)
{
if (localServers[i] == server_name_)
if (local_servers[i] == server_name_)
{
server_ = host_->connectDAServer(server_name_);
ServerStatus status{0};
const char *serverStateMsg[] = {"unknown", "running", "failed", "co-config",
"suspended", "test", "comm-fault"};
const char *server_state_msg[] = {"unknown", "running", "failed", "co-config",
"suspended", "test", "comm-fault"};
for (int i = 0; i < 10; i++)
{
server_->getStatus(status);
cout << "server state is " << serverStateMsg[status.dwServerState] << ", vendor is "
<< status.vendorInfo.c_str() << endl;
wcout << "server state is " << server_state_msg[status.dwServerState] << ", vendor is "
<< status.vendorInfo.c_str() << endl;

if (status.dwServerState == OPC_STATUS_RUNNING)
{
Expand All @@ -99,24 +107,74 @@ bool OPCClient::Connect(std::string &hostName, std::string &serverName)
return false;
}

ServerNames OPCClient::GetServers(std::string &hostName)
ServerNames OPCClient::GetServers(wstring &host_name)
{
COPCHost *host = COPCClient::makeHost(COPCHost::S2WS(hostName));
COPCHost *host = COPCClient::makeHost(host_name);
ServerNames servers;
vector<CLSID> localClassIds;
vector<wstring> localServers;
host->getListOfDAServers(IID_CATID_OPCDAServer20, localServers, localClassIds);
vector<CLSID> local_class_ids;
ServerNames local_servers;
host->getListOfDAServers(IID_CATID_OPCDAServer20, local_servers, local_class_ids);
delete host;
return servers;
}

ServerItems OPCClient::GetItems()
{
ServerItems items{};
server_->getItemNames(items);
return items;
}

for (unsigned i = 0; i < localServers.size(); i++)
void OPCClient::AddGroup(wstring &group_name)
{
if (groups_.end() != groups_.find(group_name))
{
servers.push_back(COPCHost::WS2S(localServers[i]));
return;
}

delete host;
return servers;
unsigned long refresh_rate;
COPCGroup *group = server_->makeGroup(group_name, true, 1000, refresh_rate, 0.0);
GroupTuple group_tuple = make_tuple(group, Items{});
groups_[group_name] = group_tuple;
}

ServerItems OPCClient::GetItems()
void OPCClient::AddItem(wstring &group_name, wstring &item_name)
{
return ServerItems();
if (groups_.end() == groups_.find(group_name))
{
return;
}

auto &group_tuple = groups_[group_name];
auto &items = get<1>(group_tuple);

if (items.end() != items.find(item_name))
{
return;
}

COPCItem *item = get<0>(group_tuple)->addItem(item_name, true);
items[item_name] = item;
}

string OPCClient::GetItemType(wstring &item_name)
{
for_each(groups_.begin(), groups_.end(), [item_name](auto &group_kv) {
GroupTuple &group_tuple = group_kv.second;
Items &items = get<1>(group_tuple);
if (items.end() == items.find(item_name))
{
auto item = items[item_name];

vector<CPropertyDescription> prop_desc;
item->getSupportedProperties(prop_desc);
for (unsigned i = 0; i < prop_desc.size(); ++i)
{
}

return;
}
});

return string();
}
13 changes: 9 additions & 4 deletions OPCClientDLL/opc_client_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
#endif

#include <vector>
#include <map>

using ServerNames = std::vector<std::string>;
using ServerItems = std::vector<std::string>;
using ServerNames = std::vector<std::wstring>;
using ServerItems = std::vector<std::wstring>;

class OPC_CLIENT_DLL_API OPCClientInterface
{
Expand All @@ -21,11 +22,15 @@ class OPC_CLIENT_DLL_API OPCClientInterface

virtual void Init() = 0;
virtual void Stop() = 0;
virtual ServerNames GetServers(std::string &hostName) = 0;
virtual bool Connect(std::string &hostName, std::string &serverName) = 0;
virtual ServerNames GetServers(std::wstring &host_name) = 0;
virtual bool Connect(std::wstring &host_name, std::wstring &server_name) = 0;
virtual ServerItems GetItems() = 0;
virtual void AddGroup(std::wstring &group_name) = 0;
virtual void AddItem(std::wstring &group_name, std::wstring &item_name) = 0;
virtual std::string GetItemType(std::wstring &item_name) = 0;
};

extern "C" OPC_CLIENT_DLL_API ServerNames;
extern "C" OPC_CLIENT_DLL_API ServerItems;
extern "C" OPC_CLIENT_DLL_API OPCClientInterface *instance_create();
extern "C" OPC_CLIENT_DLL_API void instance_destroy(OPCClientInterface *instance);

0 comments on commit bae634e

Please sign in to comment.