Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <opc/ua/services/services.h>
00012
00013 #include <stdexcept>
00014
00015 namespace
00016 {
00017 using namespace OpcUa;
00018
00019 class TestEndpoints : public EndpointServices
00020 {
00021 public:
00022 TestEndpoints(const std::string& url)
00023 : Applications(GetApplications(url))
00024 , Endpoints(GetEndpoints(url))
00025 {
00026 }
00027
00028 virtual std::vector<ApplicationDescription> FindServers(const FindServersParameters& params) const
00029 {
00030 return Applications;
00031 }
00032
00033 virtual std::vector<EndpointDescription> GetEndpoints(const EndpointsFilter& filter) const
00034 {
00035 return Endpoints;
00036 }
00037
00038 virtual void RegisterServer(const ServerParameters& parameters)
00039 {
00040 throw std::logic_error("not implemented.");
00041 }
00042
00043 private:
00044 std::vector<ApplicationDescription> GetApplications(const std::string& url) const
00045 {
00046 ApplicationDescription app;
00047 app.URI = "URI";
00048 app.Type = ApplicationType::Client;
00049 app.ProductURI = "ProductURI";
00050 app.Name.Text = "Name";
00051 app.GatewayServerURI = "GatewayServerURI";
00052 app.DiscoveryProfileURI = "DiscoveryProfileURI";
00053 app.DiscoveryURLs.push_back(url);
00054 return std::vector<ApplicationDescription>(1, app);
00055 }
00056
00057 std::vector<EndpointDescription> GetEndpoints(const std::string& url) const
00058 {
00059 EndpointDescription endpoint;
00060 endpoint.EndpointUrl = url;
00061 endpoint.SecurityLevel = 1;
00062 endpoint.SecurityMode = OpcUa::MessageSecurityMode::None;
00063 endpoint.SecurityPolicyUri = "SecurityPolicyUri";
00064 endpoint.ServerCertificate.push_back(1);
00065 endpoint.Server = GetApplications(url).front();
00066 endpoint.TransportProfileUri = "TransportProfileURI";
00067 endpoint.UserIdentityTokens = GetUserTokens();
00068 return std::vector<EndpointDescription>(1, endpoint);
00069 }
00070
00071 std::vector<OpcUa::UserTokenPolicy> GetUserTokens() const
00072 {
00073 OpcUa::UserTokenPolicy policy;
00074 policy.IssuedTokenType = "IssuedTokenType";
00075 policy.IssuerEndpointUrl = "IssuerEndpointUrl";
00076 policy.PolicyId = "PolicyId";
00077 policy.SecurityPolicyUri = "SecurityPolicyUri";
00078 policy.TokenType = UserTokenType::UserName;
00079 return std::vector<OpcUa::UserTokenPolicy>(1, policy);
00080 }
00081
00082 private:
00083 std::vector<ApplicationDescription> Applications;
00084 std::vector<EndpointDescription> Endpoints;
00085 };
00086
00087 void Assert(bool isTrue, const char* msg)
00088 {
00089 if (!isTrue)
00090 {
00091 throw std::logic_error(msg);
00092 }
00093 }
00094
00095 class TestAttributes : public AttributeServices
00096 {
00097 public:
00098 virtual std::vector<DataValue> Read(const OpcUa::ReadParameters& params) const
00099 {
00100 Assert(params.MaxAge == 1, "Invalid MaxAgeValue");
00101 Assert(params.TimestampsType == TimestampsToReturn::BOTH, "Invalid value of TimestampsToReturn.");
00102 Assert(params.AttributesToRead.size() == 1, "Invalid size of AttributesToRead.");
00103
00104 OpcUa::ReadValueId id = params.AttributesToRead[0];
00105 Assert(id.Attribute == AttributeId::Value, "Invalid value of Attribute Id.");
00106 Assert(id.DataEncoding.NamespaceIndex == 3, "Invalid namespace index in DataEncoding.");
00107 Assert(id.DataEncoding.Name == "binary", "Invalid name in DataEncoding.");
00108 Assert(id.IndexRange == "1:2", "Invalid value of IndexRange.");
00109 Assert(id.Node.IsInteger(), "Node id is not integer.");
00110 Assert(id.Node.GetIntegerIdentifier() == 2, "Node id is not equal to 2.");
00111 Assert(id.Node.GetNamespaceIndex() == 1, "NodeId's namespace index is not equal to 1.");
00112
00113 DataValue data;
00114 data.Encoding =
00115 DATA_VALUE |
00116 DATA_VALUE_STATUS_CODE |
00117 DATA_VALUE_SOURCE_TIMESTAMP |
00118 DATA_VALUE_Server_TIMESTAMP |
00119 DATA_VALUE_SOURCE_PICOSECONDS |
00120 DATA_VALUE_Server_PICOSECONDS;
00121 data.ServerPicoseconds = 1;
00122 data.ServerTimestamp.Value = 2;
00123 data.SourcePicoseconds = 3;
00124 data.SourceTimestamp.Value = 4;
00125 data.Status = StatusCode::BadNotReadable;
00126 data.Value = std::string("value");
00127
00128 return std::vector<DataValue>(1, data);
00129 }
00130
00131 virtual std::vector<StatusCode> Write(const std::vector<OpcUa::WriteValue>& data)
00132 {
00133 Assert(data.size() == 1, "Invalid number od data for write.");
00134 const OpcUa::WriteValue& value = data[0];
00135 Assert(value.Attribute == OpcUa::AttributeId::Value, "Invalid id of attribute.");
00136 Assert(value.Node.Encoding == NodeIdEncoding::EV_STRING, "Invalid encoding of node.");
00137 Assert(value.Node.StringData.NamespaceIndex == 1, "Invalid namespace of node.");
00138 Assert(value.Node.StringData.Identifier == "node", "Invalid identifier of node.");
00139 Assert(value.NumericRange == "1:2", "Invalid numeric range.");
00140 Assert(value.Data.ServerPicoseconds == 1, "Invalid ServerPicoseconds.");
00141 Assert(value.Data.ServerTimestamp.Value == 2, "Invalid ServerTimeStamp.");
00142 Assert(value.Data.SourcePicoseconds == 3, "Invalid SourcePicoseconds.");
00143 Assert(value.Data.SourceTimestamp.Value == 4, "Invalid SourceTimeStamp.");
00144 Assert(value.Data.Status == StatusCode::BadNotReadable, "Invalid data status.");
00145 Assert(value.Data.Value.Type() == VariantType::STRING, "Invalid data type.");
00146 Assert(value.Data.Value.As<std::string>().size() == 1, "Invalid number of strings in variant.");
00147 Assert(value.Data.Value == std::vector<std::string>(1, "value"), "Invalid data value.");
00148
00149 const uint8_t encoding =
00150 DATA_VALUE |
00151 DATA_VALUE_STATUS_CODE |
00152 DATA_VALUE_SOURCE_TIMESTAMP |
00153 DATA_VALUE_Server_TIMESTAMP |
00154 DATA_VALUE_SOURCE_PICOSECONDS |
00155 DATA_VALUE_Server_PICOSECONDS;
00156
00157 Assert(value.Data.Encoding == encoding, "Invalid encoding mask.");
00158
00159 return std::vector<StatusCode>(1, StatusCode::BadNotReadable);
00160 }
00161 };
00162
00163 class TestViewServices : public ViewServices
00164 {
00165 public:
00166 virtual std::vector<ReferenceDescription> Browse(const OpcUa::NodesQuery& query) const
00167 {
00168 ReferenceDescription ref;
00169 ref.BrowseName.Name = "Name";
00170 ref.BrowseName.NamespaceIndex = 1;
00171 ref.DisplayName.Text = "Text";
00172 ref.IsForward = true;
00173 ref.ReferenceTypeId.Encoding = OpcUa::NodeIdEncoding::EV_STRING;
00174 ref.ReferenceTypeId.StringData.NamespaceIndex = 2;
00175 ref.ReferenceTypeId.StringData.Identifier = "Identifier";
00176 ref.TargetNodeClass = OpcUa::NodeClass::Variable;
00177 ref.TargetNodeId.Encoding = OpcUa::NodeIdEncoding::EV_FOUR_BYTE;
00178 ref.TargetNodeId.FourByteData.NamespaceIndex = 3;
00179 ref.TargetNodeId.FourByteData.Identifier = 4;
00180 ref.TargetNodeTypeDefinition.Encoding = OpcUa::NodeIdEncoding::EV_NUMERIC;
00181 ref.TargetNodeTypeDefinition.NumericData.NamespaceIndex = 5;
00182 ref.TargetNodeTypeDefinition.NumericData.Identifier = 6;
00183 return std::vector<ReferenceDescription>(1, ref);
00184 }
00185
00186 virtual std::vector<ReferenceDescription> BrowseNext() const
00187 {
00188 return std::vector<ReferenceDescription>();
00189 }
00190
00191 virtual std::vector<BrowsePathResult> TranslateBrowsePathsToNodeIds(const TranslateBrowsePathsParameters& params) const
00192 {
00193 throw std::logic_error("not implemented.");
00194 }
00195 };
00196
00197 class TestComputer : public Services
00198 {
00199 public:
00200 TestComputer(const std::string& url)
00201 : EndpointsImpl(new TestEndpoints(url))
00202 , ViewsImpl(new TestViewServices())
00203 , AttributesImpl(new TestAttributes)
00204 {
00205 }
00206
00207 virtual void CreateSession(const RemoteSessionParameters& parameters)
00208 {
00209 throw std::logic_error("not implemented.");
00210 }
00211
00212 virtual void ActivateSession()
00213 {
00214 throw std::logic_error("not implemented.");
00215 }
00216
00217 virtual void CloseSession()
00218 {
00219 throw std::logic_error("not implemented.");
00220 }
00221
00222 virtual std::shared_ptr<EndpointServices> Endpoints()
00223 {
00224 return EndpointsImpl;
00225 }
00226
00227 virtual std::shared_ptr<ViewServices> Views()
00228 {
00229 return ViewsImpl;
00230 }
00231
00232 virtual std::shared_ptr<AttributeServices> Attributes()
00233 {
00234 return AttributesImpl;
00235 }
00236
00237 virtual std::shared_ptr<SubscriptionServices> Subscriptions()
00238 {
00239 throw std::logic_error("not implemented.");
00240 }
00241
00242 virtual std::shared_ptr<NodeManagementServices> NodeManagement()
00243 {
00244 throw std::logic_error("not implemented.");
00245 }
00246
00247 private:
00248 EndpointServices::SharedPtr EndpointsImpl;
00249 ViewServices::SharedPtr ViewsImpl;
00250 AttributeServices::SharedPtr AttributesImpl;
00251 };
00252
00253 }
00254
00255 namespace OpcUa
00256 {
00257
00258 std::unique_ptr<Services> Connect(const std::string& url)
00259 {
00260 return std::unique_ptr<Services>(new TestComputer(url));
00261 }
00262
00263 }
00264