public void run()
        {
            string json_config = @"
            {
                'opcServerURL':'opc.tcp://localhost:4840/freeopcua/server/',
                'reconnectPeriod':10,
                'publishingInterval': 1000,
                'nodesDatabase':{
                'isInMemory':true
                },

                'loggerConfig' :{
                    'loglevel' :'info'
                },

                'nodesLoader' : {
                    'targetIdentifier' : 'browseName',
                    'whiteList':['MyVariable','MyVariable2','MyVariable3']
                }
            }";

            var    j = JObject.Parse(json_config);
            var    s = new serviceManager(j);
            Thread t = new Thread(async() =>
            {
                Thread.Sleep(5000);
                var resp = await s.writeToOPCserver(new string[] { "MyVariable", "MyVariable2", "MyVariable3" }, new object[] { 7, 8, 9 });
                Assert.True(resp[0].success);
                Assert.True(resp[1].success);
                Assert.True(resp[2].success);
                Assert.Equal("MyVariable", resp[0].name);
                Assert.Equal("MyVariable2", resp[1].name);
                Assert.Equal("MyVariable3", resp[2].name);

                Assert.Equal(StatusCodes.Good, resp[0].statusCode);
                resp = await s.writeToOPCserver(new string[] { "MyVariable", "pippo" }, new object[] { "ciao", 8 });

                Assert.Equal(StatusCodes.BadNoEntryExists, resp[1].statusCode);
                Assert.False(resp[1].success);
                Assert.Equal(StatusCodes.BadTypeMismatch, resp[0].statusCode);
                Assert.False(resp[0].success);

                s.cancellationToken.Cancel();
            });

            t.Start();
            s.run();
        }
Ejemplo n.º 2
0
        public async override Task <WriteResponse> WriteOpcNode(WriteRequest request, ServerCallContext context)
        {
            StatusCodeCollection statuses = await _services.writeToOPCserver(request.Name, request.Value);

            WriteResponse r = new WriteResponse();

            r.IsError      = (Opc.Ua.StatusCode.IsBad(statuses[0]));
            r.ErrorMessage = (r.IsError) ? "Error" : "none";

            if (r.IsError)
            {
                logger.Error("Error in writing");
            }
            else
            {
                logger.Debug("Written value: " + request.Value + "  on variable " + request.Name);
            }
            return(r);
        }