Blame view

src/ModbusConnections.cpp 3.28 KB
46785270   Peter M. Groen   Setting up workin...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  #include "ModbusConnections.h"
  
  ModbusConnections::ModbusConnections()
  {
  
  }
  
  ModbusConnections::~ModbusConnections()
  {
      if( m_mapSerial.size() > 0 )
      {
          // Iterate, remove and destroy ( Searching, Seek & Destroy )
      }
  
      if( m_mapTcp.size() > 0 )
      {
          // Iterate, remove and destroy ( Searching, Seek & Destroy )
      }
  }
  
  bool ModbusConnections::CreateConnection( const ConnectionConfig &config )
  {
      std::shared_ptr<IModbusAdapter> ptrNewAdapter = std::make_shared<ModbusAdapter>();
      if( ptrNewAdapter == nullptr )
      {
          // Log a message and bail out
          return false;
      }
  
      // It looks like the pointer is valid. Time to connect.
      ptrNewAdapter->ModbusConnect( config );
      if( !ptrNewAdapter->isConnected() )
      {
          // Unsuccessful. Delete the object and return false
          ptrNewAdapter.reset();
          return false;
      }
  
      std::shared_ptr<IModbusAdapter> ptr = connectionExist( config.getPortEnum(), createEndPoint( config ) );
      if( ptr != nullptr )
      {
          if( !DeleteConnection( config.getPortEnum(), createEndPoint( config ) ) )
          {
              // Something went wrong here.. Our administration is "wonky" so report false and bail.
              // New connection is not created.
              ptrNewAdapter.reset();
              return false;
          }
      }
  
      if( config.getType() == ConnectionType::CT_TCP )
      {
          m_mapTcp.insert( { createEndPoint( config ), ptrNewAdapter } );
      }
      else if( config.getType() == ConnectionType::CT_SERIAL )
      {
          m_mapSerial.insert( { config.getPortEnum(), ptrNewAdapter } );
      }
      else
      {
          // No idea what the type is but not something we recognize.
          ptrNewAdapter.reset();
          return false;
      }
      return true;
  }
  
  bool ModbusConnections::DeleteConnection( const ConnectionPort portName, const std::string &endpoint )
  {
  
  }
  
  int ModbusConnections::ConnectionCount()
  {
      return ( m_mapSerial.size() + m_mapTcp.size() );
  }
  
  std::shared_ptr<IModbusAdapter> ModbusConnections::getConnection( const ConnectionPort portName, const std::string &endpoint )
  {
      return this->connectionExist( portName, endpoint );
  }
  
  std::shared_ptr<IModbusAdapter> ModbusConnections::connectionExist( const ConnectionPort portName, const std::string &endpoint )
  {
      if( portName == ConnectionPort::CP_TCP )
      {
          auto search = m_mapTcp.find( endpoint );
          if( search != m_mapTcp.end() )
          {
              return search->second;
          }
          else
          {
              return nullptr;
          }
      }
      else
      {
          auto search = m_mapSerial.find( portName );
          if( search != m_mapSerial.end() )
          {
              return search->second;
          }
          else
          {
              return nullptr;
          }
      }
  }
  
  std::string ModbusConnections::createEndPoint( const std::string &ipAddress, int portNumber )
  {
      if( portNumber > 0 && !ipAddress.empty() )
      {
          return std::string( "tcp://" + ipAddress + ":" + std::to_string( portNumber ) );
      }
  
      return std::string();
  }
  
  std::string ModbusConnections::createEndPoint( const ConnectionConfig &config )
  {
      if( config.getType() != ConnectionType::CT_TCP )
      {
          // Early opt-out
          return std::string();
      }
  
      return createEndPoint( config.getIpAddress(), config.getTcpPort() );
  }