Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <opc/ua/server/server.h>
00020
00021 #include <opc/ua/server/addons/common_addons.h>
00022 #include <opc/ua/protocol/string_utils.h>
00023
00024 #include <opc/ua/server/addons/services_registry.h>
00025 #include <opc/ua/server/addons/subscription_service.h>
00026 #include <iostream>
00027
00028 namespace OpcUa
00029 {
00030 UaServer::UaServer()
00031 {
00032 }
00033
00034 UaServer::UaServer(bool debug)
00035 : Debug(debug)
00036 {
00037 }
00038
00039 void UaServer::SetEndpoint(const std::string& endpoint)
00040 {
00041 Endpoint = endpoint;
00042 }
00043
00044 void UaServer::SetProductURI(const std::string& uri)
00045 {
00046 ProductUri = uri;
00047 }
00048
00049 void UaServer::SetServerURI(const std::string& uri)
00050 {
00051 ServerUri = uri;
00052 }
00053
00054 void UaServer::SetServerName(const std::string& name)
00055 {
00056 Name = name;
00057 }
00058
00059 void UaServer::AddAddressSpace(const std::string& path)
00060 {
00061 XmlAddressSpaces.push_back(path);
00062 }
00063
00064 void UaServer::CheckStarted() const
00065 {
00066 if ( ! Registry )
00067 {
00068 throw(std::runtime_error("Server is not started"));
00069 }
00070 }
00071
00072 uint32_t UaServer::RegisterNamespace(std::string uri)
00073 {
00074 CheckStarted();
00075 Node namespacearray(Registry->GetServer(), ObjectId::Server_NamespaceArray);
00076 std::vector<std::string> uris = namespacearray.GetValue().As<std::vector<std::string>>();
00077 uint32_t index = uris.size();
00078 uris.push_back(uri);
00079 namespacearray.SetValue(uris);
00080 return index;
00081 }
00082
00083 uint32_t UaServer::GetNamespaceIndex(std::string uri)
00084 {
00085 CheckStarted();
00086 Node namespacearray(Registry->GetServer(), ObjectId::Server_NamespaceArray);
00087 std::vector<std::string> uris = namespacearray.GetValue().As<std::vector<std::string>>();;
00088 for ( uint32_t i=0; i<uris.size(); ++i)
00089 {
00090 if (uris[i] == uri )
00091 {
00092 return i;
00093 }
00094 }
00095 throw(std::runtime_error("Error namespace uri does not exists in server"));
00096
00097 }
00098
00099 void UaServer::Start()
00100 {
00101 ApplicationDescription appDesc;
00102 appDesc.ApplicationName = LocalizedText(Name);
00103 appDesc.ApplicationUri = ServerUri;
00104 appDesc.ApplicationType = ApplicationType::Server;
00105 appDesc.ProductUri = ProductUri;
00106
00107 OpcUa::Server::Parameters params;
00108 params.Debug = Debug;
00109 params.Endpoint.Server = appDesc;
00110 params.Endpoint.EndpointUrl = Endpoint;
00111 params.Endpoint.SecurityMode = SecurityMode;
00112 params.Endpoint.SecurityPolicyUri = "http://opcfoundation.org/UA/SecurityPolicy#None";
00113 params.Endpoint.TransportProfileUri = "http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary";
00114
00115 UserTokenPolicy policy;
00116 policy.TokenType = UserTokenType::Anonymous;
00117 params.Endpoint.UserIdentityTokens.push_back(policy);
00118
00119 Addons = Common::CreateAddonsManager();
00120 Server::RegisterCommonAddons(params, *Addons);
00121 Addons->Start();
00122
00123 Registry = Addons->GetAddon<Server::ServicesRegistry>(Server::ServicesRegistryAddonId);
00124 SubscriptionService = Addons->GetAddon<Server::SubscriptionService>(Server::SubscriptionServiceAddonId);
00125
00126 Node ServerArray = GetNode(OpcUa::ObjectId::Server_ServerArray);
00127 ServerArray.SetValue(std::vector<std::string>({Endpoint}));
00128
00129 EnableEventNotification();
00130 }
00131
00132 Node UaServer::GetNode(const std::string& nodeid) const
00133 {
00134 return GetNode(ToNodeId(nodeid));
00135 }
00136
00137 Node UaServer::GetNode(const NodeId& nodeid) const
00138 {
00139 CheckStarted();
00140 return Node(Registry->GetServer(), nodeid);
00141 }
00142
00143 Node UaServer::GetNodeFromPath(const std::vector<QualifiedName>& path) const
00144 {
00145 return GetRootNode().GetChild(path);
00146 }
00147
00148 Node UaServer::GetNodeFromPath(const std::vector<std::string>& path) const
00149 {
00150 return GetRootNode().GetChild(path);
00151 }
00152
00153 void UaServer::Stop()
00154 {
00155 std::cout << "Stopping opcua server application" << std::endl;
00156 CheckStarted();
00157 Addons->Stop();
00158 }
00159
00160 Node UaServer::GetRootNode() const
00161 {
00162 return GetNode(OpcUa::ObjectId::RootFolder);
00163 }
00164
00165 Node UaServer::GetObjectsNode() const
00166 {
00167 return GetNode(ObjectId::ObjectsFolder);
00168 }
00169
00170 Node UaServer::GetServerNode() const
00171 {
00172 return GetNode(ObjectId::Server);
00173 }
00174
00175 void UaServer::EnableEventNotification()
00176 {
00177 Node server = GetServerNode();
00178
00179 uint8_t notifierval = 0;
00180 notifierval |= EventNotifier::SubscribeToEvents;
00181
00182 DataValue dval(notifierval);
00183 dval.SetSourceTimestamp(DateTime::Current());
00184
00185 server.SetAttribute(AttributeId::EventNotifier, dval);
00186 }
00187
00188 std::unique_ptr<Subscription> UaServer::CreateSubscription(unsigned int period, SubscriptionHandler& callback)
00189 {
00190 CheckStarted();
00191 CreateSubscriptionParameters params;
00192 params.RequestedPublishingInterval = period;
00193 return std::unique_ptr<Subscription>(new Subscription (Registry->GetServer(), params, callback, Debug));
00194 }
00195
00196 ServerOperations UaServer::CreateServerOperations()
00197 {
00198 return std::move(ServerOperations(Registry->GetServer()));
00199 }
00200
00201 void UaServer::TriggerEvent(Event event)
00202 {
00203 SubscriptionService->TriggerEvent(ObjectId::Server, event);
00204 }
00205
00206 }