public object Any(RecursiveNode request) => request;
Example #2
0
        static int Main(string[] args)
        {
            _application.ApplicationName = "Nodeset2Json";
            _application.ApplicationType = ApplicationType.Server;

            try
            {
                //check if argument has been passed
                if (args.Length < 2)
                {
                    Console.WriteLine("usage: nodeset2json.exe [dir]nodeset_file_xml [dir]output_file");
                    return(-1);
                }
                if (args[0].EndsWith(".xml"))
                {
                    _server.NodesetFilePath = args[0]; //Input file

                    //Load configuration from xml file "NodesetServer.Config.xml"
                    string configFilePath = Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "NodesetServer.Config.xml");
                    _application.LoadApplicationConfiguration(configFilePath, false).Wait();

                    // check the application certificate.
                    // here the opc foundation certificates are used
                    bool certOk = _application.CheckApplicationInstanceCertificate(false, 0).Result;
                    if (!certOk)
                    {
                        throw new Exception("Application instance certificate invalid!");
                    }

                    // Start the server
                    _application.Start(_server).GetAwaiter().OnCompleted(OnServerStarted);

                    var nsm = _server.NodeManager;
                    var ns  = nsm.NodeSet;                      //BINGO: I got myself a nodeset!

                    RecursiveNode tree = new RecursiveNode(ns); //get the nodeset as a non binary tree

                    //the money shot
                    using (StreamWriter file = File.CreateText(args[1])) //Output file
                    {
                        var settings = new JsonSerializerSettings
                        {
                            ContractResolver  = NodesetContractResolver.Instance,
                            NullValueHandling = NullValueHandling.Ignore
                        };

                        var json = JsonConvert.SerializeObject(tree, Formatting.Indented, settings);
                        file.Write(json);
                    }
                    return(0);
                }
                else
                {
                    throw new Exception("Invalid nodeset xml file!");
                }
            }
            catch (Exception e)
            {
                //TODO Implement logging sink instead of console.writeline
                Console.WriteLine($"{e.Message} \n {e.InnerException?.Message} \n {e.StackTrace}");
                return(-1);
            }
        }