example_server.cpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 #include <iostream>
00009 #include <algorithm>
00010 #include <time.h>
00011 
00012 #include <thread>         
00013 #include <chrono>   
00014 
00015 #include <opc/ua/node.h>
00016 #include <opc/ua/subscription.h>
00017 #include <opc/ua/server/server.h>
00018 
00019 
00020 
00021 
00022 using namespace OpcUa;
00023 
00024 class SubClient : public SubscriptionHandler
00025 {
00026   void DataChange(uint32_t handle, const Node& node, const Variant& val, AttributeId attr) override
00027   {
00028     std::cout << "Received DataChange event for Node " << node << std::endl;
00029   }
00030 };
00031 
00032 void RunServer()
00033 {
00034   //First setup our server
00035   const bool debug = true;
00036   OpcUa::UaServer server(debug);
00037   server.SetEndpoint("opc.tcp://localhost:4841/freeopcua/server");
00038   server.SetServerURI("urn://exampleserver.freeopcua.github.io");
00039   server.Start();
00040   
00041   //then register our server namespace and get its index in server
00042   uint32_t idx = server.RegisterNamespace("http://examples.freeopcua.github.io");
00043   
00044   //Create our address space using different methods
00045   Node objects = server.GetObjectsNode();
00046   
00047   //Add a custom object with specific nodeid
00048   NodeId nid(99, idx);
00049   QualifiedName qn("NewObject", idx);
00050   Node newobject = objects.AddObject(nid, qn);
00051 
00052   //Add a variable and a property with auto-generated nodeid to our custom object
00053   Node myvar = newobject.AddVariable(idx, "MyVariable", Variant(8));
00054   Node myprop = newobject.AddVariable(idx, "MyProperty", Variant(8.8));
00055 
00056   //browse root node on server side
00057   Node root = server.GetRootNode();
00058   std::cout << "Root node is: " << root << std::endl;
00059   std::cout << "Childs are: " << std::endl;
00060   for (Node node : root.GetChildren())
00061   {
00062     std::cout << "    " << node << std::endl;
00063   }
00064 
00065 
00066   //Uncomment following to subscribe to datachange events inside server
00067   /*
00068   SubClient clt;
00069   std::unique_ptr<Subscription> sub = server.CreateSubscription(100, clt);
00070   sub->SubscribeDataChange(myvar);
00071   */
00072 
00073 
00074 
00075   //Now write values to address space and send events so clients can have some fun
00076   uint32_t counter = 0;
00077   myvar.SetValue(Variant(counter)); //will change value and trigger datachange event
00078 
00079   //Create event
00080   server.EnableEventNotification();
00081   Event ev(ObjectId::BaseEventType); //you should create your own type
00082   ev.Severity = 2;
00083   ev.SourceNode = ObjectId::Server;
00084   ev.SourceName = "Event from FreeOpcUA";
00085   ev.Time = DateTime::Current();
00086 
00087 
00088   std::cout << "Ctrl-C to exit" << std::endl;
00089   for (;;)
00090   {
00091     myvar.SetValue(Variant(++counter)); //will change value and trigger datachange event
00092     std::stringstream ss;
00093     ss << "This is event number: " << counter;
00094     ev.Message = LocalizedText(ss.str());
00095     server.TriggerEvent(ev);
00096     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
00097   }
00098 
00099   server.Stop();
00100 }
00101 
00102 int main(int argc, char** argv)
00103 {
00104   try
00105   {
00106     RunServer();
00107   }
00108   catch (const std::exception& exc)
00109   {
00110     std::cout << exc.what() << std::endl;
00111   }
00112   return 0;
00113 }
00114 


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:40