Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update OPC DA #65

Merged
merged 5 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/com/opc/Cmd_AddConnection.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2012, 2023 AIT, Hit robot group
* 2024 Samator Indo Gas
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand All @@ -9,13 +10,14 @@
* Contributors:
* Filip Andren - initial API and implementation and/or initial documentation
* Tibalt Zhao - Merge additem into opc connect
* Ketut Kumajaya - Code refactoring from char* to std::string
*******************************************************************************/
#include "Cmd_AddConnection.h"
#include "opcconnectionimpl.h"
#include "opcconnection.h"
#include "opcprocessvar.h"

CCmd_AddConnection::CCmd_AddConnection(COpcConnectionImpl *paConnectionimpl, const char* paGroupName,
CCmd_AddConnection::CCmd_AddConnection(COpcConnectionImpl *paConnectionimpl, const std::string& paGroupName,
unsigned long paReqUpdateRate, float paDeadBand, std::vector<COpcProcessVar*> paNewItems) :
mConnection(paConnectionimpl), mGroupName(paGroupName), mReqUpdateRate(paReqUpdateRate), mDeadBand(paDeadBand) {
for(auto opcVar : paNewItems){
Expand Down
6 changes: 4 additions & 2 deletions src/com/opc/Cmd_AddConnection.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2012, 2023 AIT, Hit robot group
* 2024 Samator Indo Gas
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand All @@ -9,6 +10,7 @@
* Contributors:
* Filip Andren - initial API and implementation and/or initial documentation
* Tibalt Zhao - Merge additem into opc connect
* Ketut Kumajaya - Code refactoring from char* to std::string
*******************************************************************************/
#ifndef CMDADDCONNECTION_H_
#define CMDADDCONNECTION_H_
Expand All @@ -24,7 +26,7 @@ class COpcConnection;

class CCmd_AddConnection : public ICmd{
public:
explicit CCmd_AddConnection(COpcConnectionImpl *paConnection, const char* paGroupName, unsigned long paReqUpdateRate, float paDeadBand,
explicit CCmd_AddConnection(COpcConnectionImpl *paConnection, const std::string& paGroupName, unsigned long paReqUpdateRate, float paDeadBand,
std::vector<COpcProcessVar*> paNewItems) ;
~CCmd_AddConnection() = default;

Expand All @@ -33,7 +35,7 @@ class CCmd_AddConnection : public ICmd{

private:
COpcConnectionImpl* mConnection;
const char *mGroupName ;
const std::string mGroupName;
unsigned long mReqUpdateRate;
float mDeadBand;
std::vector<std::string> mReadItems;
Expand Down
8 changes: 5 additions & 3 deletions src/com/opc/Cmd_SetProcessVarValue.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2012, 2022 AIT, HIT robot group
* 2024 Samator Indo Gas
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand All @@ -9,19 +10,20 @@
* Contributors:
* Filip Andren - initial API and implementation and/or initial documentation
* Tibalt Zhao - get rid of opcprocessvar from commfb
* Ketut Kumajaya - Code refactoring from char* to std::string
*******************************************************************************/
#include "Cmd_SetProcessVarValue.h"

#include "opcprocessvar.h"
#include "opcconnectionimpl.h"

CCmd_SetProcessVarValue::CCmd_SetProcessVarValue(COpcConnectionImpl *paConnection, std::string &paGroupName,
std::string &paItemName, Variant paVar) :
CCmd_SetProcessVarValue::CCmd_SetProcessVarValue(COpcConnectionImpl *paConnection, const std::string &paGroupName,
const std::string &paItemName, Variant paVar) :
mConnection(paConnection), mVar(paVar), mGroupName(paGroupName), mItemName(paItemName) {
}

void CCmd_SetProcessVarValue::runCommand() {
mConnection->sendItemData(mGroupName.c_str(), mItemName.c_str(), mVar);
mConnection->sendItemData(mGroupName, mItemName, mVar);
}

const char* CCmd_SetProcessVarValue::getCommandName() const {
Expand Down
6 changes: 3 additions & 3 deletions src/com/opc/Cmd_SetProcessVarValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class COpcConnectionImpl;

class CCmd_SetProcessVarValue : public ICmd{
public:
explicit CCmd_SetProcessVarValue(COpcConnectionImpl *paConnection, std::string& paGroupName, std::string& paItemName, Variant paVar);
explicit CCmd_SetProcessVarValue(COpcConnectionImpl *paConnection, const std::string& paGroupName, const std::string& paItemName, Variant paVar);
~CCmd_SetProcessVarValue() = default;

void runCommand() override;
Expand All @@ -33,8 +33,8 @@ class CCmd_SetProcessVarValue : public ICmd{
private:
COpcConnectionImpl* mConnection;
COpcProcessVar* mOpcVar;
std::string mGroupName;
std::string mItemName;
const std::string mGroupName;
const std::string mItemName;
Variant mVar;
};

Expand Down
69 changes: 37 additions & 32 deletions src/com/opc/opccomlayer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2012, 2022 AIT, ACIN, fortiss GmbH, Hit robot group
* 2024 Samator Indo Gas
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand All @@ -10,6 +11,8 @@
* Filip Andren, Patrick Smejkal, Alois Zoitl, Martin Melik-Merkumians - initial API and implementation and/or initial documentation
* ys guo - Fix opc module compilation errors and deadlock bug
* Tibalt Zhao -Merge additem into Connect and take advantage of detailed event info from opcconnection
* Ketut Kumajaya - Code refactoring from char* to std::string
* - Proper memory usage and deallocation
*******************************************************************************/
#include "opccomlayer.h"
#include "../../arch/devlog.h"
Expand All @@ -23,14 +26,13 @@
using namespace forte::com_infra;

COpcComLayer::COpcComLayer(CComLayer* paUpperLayer, CBaseCommFB* paComFB) :
CComLayer(paUpperLayer, paComFB), mHost(0), mServerName(0), mUpdateRate(0), mDeadBand(0),
mLayerParamsOK(false), mOpcConnection(0), mInterruptResp(e_Nothing){
mOpcGroupName = mFb->getInstanceName();
DEVLOG_DEBUG("new OpcComLayer: [0x%lX][%s]\n", this, mOpcGroupName);
CComLayer(paUpperLayer, paComFB), mHost(), mServerName(), mUpdateRate(0), mDeadBand(0),
mLayerParamsOK(false), mOpcConnection(0), mInterruptResp(e_Nothing), mOpcGroupName(mFb->getInstanceName()){
DEVLOG_DEBUG("new OpcComLayer: [0x%lX][%s]\n", this, mOpcGroupName.c_str());
}

COpcComLayer::~COpcComLayer(){
DEVLOG_DEBUG("delete OpcComLayer: [0x%lX][%s]\n", this, mOpcGroupName);
DEVLOG_DEBUG("delete OpcComLayer: [0x%lX][%s]\n", this, mOpcGroupName.c_str());
for(auto &it :mFBInputVars){
delete it;
}
Expand Down Expand Up @@ -303,35 +305,37 @@ void COpcComLayer::processClientParams(char* paLayerParams){
*chrStorage = '\0';
++chrStorage;
chrHost = (char*) malloc(strlen(paLayerParams) + 1);
strcpy(chrHost, paLayerParams);
if(strcmp(chrHost, "127.0.0.1") == 0 || strcmp(chrHost, "localhost") == 0) {
if (nullptr != chrHost) {
strcpy(chrHost, paLayerParams);
if(strcmp(chrHost, "127.0.0.1") == 0 || strcmp(chrHost, "localhost") == 0) {
mHost = "";
} else {
mHost = chrHost;
} else {
mHost = chrHost;
}
free(chrHost);
chrHost = nullptr;
}

// Get server name
temp = chrStorage;
chrStorage = strchr(chrStorage, ':');
if(chrStorage == 0){
if (chrHost){
free(chrHost);
}
return;
}
*chrStorage = '\0';
++chrStorage;
chrServer = (char*) malloc(strlen(temp) + 1);
strcpy(chrServer, temp);
mServerName = chrServer;
if (nullptr != chrServer) {
strcpy(chrServer, temp);
mServerName = chrServer;
free(chrServer);
chrServer = nullptr;
}

// Get update rate
mUpdateRate = atol(chrStorage);
chrStorage = strchr(chrStorage, ':');
if(chrStorage == 0){
if (chrHost){
free(chrHost);
}
return;
}
*chrStorage = '\0';
Expand All @@ -341,9 +345,6 @@ void COpcComLayer::processClientParams(char* paLayerParams){
mDeadBand = (float) atof(chrStorage);
chrStorage = strchr(chrStorage, ':');
if(chrStorage == 0){
if (chrHost){
free(chrHost);
}
return;
}

Expand All @@ -354,9 +355,6 @@ void COpcComLayer::processClientParams(char* paLayerParams){
char * inputItems = chrStorage;
chrStorage = strchr(chrStorage, ':');
if(chrStorage == 0){
if (chrHost){
free(chrHost);
}
return;
}
*chrStorage = '\0';
Expand All @@ -366,21 +364,28 @@ void COpcComLayer::processClientParams(char* paLayerParams){
pch = strtok(inputItems, ",");
while(pch != nullptr){
char *itemName = (char*) malloc(strlen(pch) + 1);
strcpy(itemName, pch);
mFBInputVars.push_back(new COpcProcessVar(mOpcGroupName, itemName, COpcProcessVar::e_FBInput));
nrItems++;
pch = strtok(nullptr, ",");
if (nullptr != itemName) {
strcpy(itemName, pch);
mFBInputVars.push_back(new COpcProcessVar(mOpcGroupName, itemName, COpcProcessVar::e_FBInput));
nrItems++;
pch = strtok(nullptr, ",");
free(itemName);
itemName = nullptr;
}
}

// Get FB output items
pch = strtok(chrStorage, ",");
while(pch != nullptr){
char *itemName = (char*) malloc(strlen(pch) + 1);
strcpy(itemName, pch);
mFBOutputVars.push_back(new COpcProcessVar(mOpcGroupName, itemName, COpcProcessVar::e_FBOutput));
nrItems++;

pch = strtok(nullptr, ",");
if (nullptr != itemName) {
strcpy(itemName, pch);
mFBOutputVars.push_back(new COpcProcessVar(mOpcGroupName, itemName, COpcProcessVar::e_FBOutput));
nrItems++;
pch = strtok(nullptr, ",");
free(itemName);
itemName = nullptr;
}
}

if(nrItems > 0) {
Expand Down
8 changes: 5 additions & 3 deletions src/com/opc/opccomlayer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2012, 2022 AIT, ACIN, fortiss GmbH, Hit robot group
* 2024 Samator Indo Gas
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand All @@ -9,6 +10,7 @@
* Contributors:
* Filip Andren, Alois Zoitl - initial API and implementation and/or initial documentation
* Tibalt Zhao - replace forte list with stl vector
* Ketut Kumajaya - Code refactoring from char* to std::string
*******************************************************************************/
#ifndef OPCCOMLAYER_H_
#define OPCCOMLAYER_H_
Expand Down Expand Up @@ -48,11 +50,11 @@ namespace forte {
template<typename T>
void getInputValue(void * paData, Variant * paNewValue);

const char* mHost;
const char* mServerName;
std::string mHost;
std::string mServerName;
long mUpdateRate;
float mDeadBand;
const char* mOpcGroupName;
const std::string mOpcGroupName;

bool mLayerParamsOK;

Expand Down
Loading