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
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
00042 uint32_t idx = server.RegisterNamespace("http://examples.freeopcua.github.io");
00043
00044
00045 Node objects = server.GetObjectsNode();
00046
00047
00048 NodeId nid(99, idx);
00049 QualifiedName qn("NewObject", idx);
00050 Node newobject = objects.AddObject(nid, qn);
00051
00052
00053 Node myvar = newobject.AddVariable(idx, "MyVariable", Variant(8));
00054 Node myprop = newobject.AddVariable(idx, "MyProperty", Variant(8.8));
00055
00056
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
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 uint32_t counter = 0;
00077 myvar.SetValue(Variant(counter));
00078
00079
00080 server.EnableEventNotification();
00081 Event ev(ObjectId::BaseEventType);
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));
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