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
00020 #include "server_object.h"
00021
00022 #include <boost/chrono.hpp>
00023 #include <opc/ua/node.h>
00024 #include <opc/ua/server/addons/services_registry.h>
00025 #include <functional>
00026
00027 #ifdef WIN32
00028 #undef GetObject
00029 #endif
00030
00031 namespace
00032 {
00033 OpcUa::RelativePathElement GetHierarchicalElement(const std::string& browseName)
00034 {
00035 OpcUa::RelativePathElement element;
00036 element.ReferenceTypeId = OpcUa::ObjectId::HierarchicalReferences;
00037 element.IncludeSubtypes = true;
00038 element.TargetName.Name = browseName;
00039 return element;
00040 }
00041
00042
00043 OpcUa::RelativePath GetCurrentTimeRelativepath()
00044 {
00045 OpcUa::RelativePath path;
00046 path.Elements.push_back(GetHierarchicalElement(OpcUa::Names::ServerStatus));
00047 path.Elements.push_back(GetHierarchicalElement(OpcUa::Names::CurrentTime));
00048 return path;
00049 }
00050 }
00051
00052 namespace OpcUa
00053 {
00054 namespace Server
00055 {
00056
00057 ServerObject::ServerObject(Services::SharedPtr services, boost::asio::io_service& io, bool debug)
00058 : Server(services)
00059 , Io(io)
00060 , Debug(debug)
00061 , Instance(std::move(CreateServerObject(services)))
00062 , ServerTime(Instance.GetVariable(GetCurrentTimeRelativepath()))
00063 , Timer(io)
00064 {
00065 Timer.Start(boost::posix_time::seconds(1), [this](){
00066 UpdateTime();
00067 });
00068
00069 std::vector<std::string> uris;
00070 uris.push_back("http://opcfoundation.org/UA/");
00071 uris.push_back("http://freeopcua.github.io");
00072 Node node = Node(Server, ObjectId::Server_NamespaceArray);
00073 node.SetValue(uris);
00074 node = Node(Server, ObjectId::Server_ServerStatus_BuildInfo_ManufacturerName);
00075 node.SetValue(std::string("FreeOpcUa"));
00076 node = Node(Server, ObjectId::Server_ServerCapabilities_LocaleIdArray);
00077 node.SetValue(std::vector<std::string>({ "en" }));
00078 node = Node(Server, ObjectId::Server_ServerStatus_BuildInfo_BuildNumber);
00079 node.SetValue(std::string("0.8"));
00080 node = Node(Server, ObjectId::Server_ServerStatus_BuildInfo_ProductName);
00081 node.SetValue(std::string("FreeOpcUa"));
00082 node = Node(Server, ObjectId::Server_ServerStatus_BuildInfo_ProductUri);
00083 node.SetValue(std::string("https://freeopcua.github.io"));
00084 node = Node(Server, ObjectId::Server_ServerStatus_State);
00085 node.SetValue((int32_t) 0);
00086 node = Node(Server, ObjectId::Server_ServerStatus_CurrentTime);
00087 node.SetValue(DateTime::Current());
00088
00089 }
00090
00091 ServerObject::~ServerObject()
00092 {
00093 if (Debug) std::clog << "server_object| canceling timer..." << std::endl;
00094 Timer.Cancel();
00095 if (Debug) std::clog << "server_object| timer stopped." << std::endl;
00096 }
00097
00098 Model::Object ServerObject::CreateServerObject(const Services::SharedPtr& services) const
00099 {
00100 Model::Server server(services);
00101 Model::Object serverObject = server.GetObject(ObjectId::Server);
00102 return serverObject;
00103 }
00104
00105 void ServerObject::UpdateTime()
00106 {
00107 try
00108 {
00109 DateTime t = OpcUa::DateTime::Current();
00110 DataValue timeData(t);
00111 timeData.SetSourceTimestamp(t);
00112 timeData.SetServerTimestamp(t);
00113
00114 if (Debug) std::clog << "server_object| Updating server time: " << t << std::endl;
00115 ServerTime.SetValue(timeData);
00116 }
00117 catch (std::exception& ex)
00118 {
00119 std::cerr << "Failed to update time at server object: " << ex.what() << std::endl;
00120 }
00121
00122 }
00123
00124 }
00125 }