private static async Task <MethodResponse> lightsMethodCallBack(MethodRequest methodRequest, object userContext)
        {
            var lightsResponse = new LightsResponse();

            try
            {
                System.Console.WriteLine($"Lights method: {methodRequest.DataAsJson}");
                dynamic request = JsonConvert.DeserializeObject(methodRequest.DataAsJson);

                uint relay1 = (uint)request.relay1;
                uint relay2 = (uint)request.relay2;

                OpcStatus result1 = opcClient.WriteNode("ns=2;s=Wise4012E:Relay01", relay1);  // typemismatch was a bitch
                OpcStatus result2 = opcClient.WriteNode("ns=2;s=Wise4012E:Relay02", relay2);

                lightsResponse.state1 = result1.Description;
                lightsResponse.state2 = result2.Description;

                Console.WriteLine($"Response 1: '{result1.Description}'; Response 1: '{result2.Description}'");
            }
            catch (Exception ex)
            {
                lightsResponse.errorMessage = ex.Message;
            }

            var json     = JsonConvert.SerializeObject(lightsResponse);
            var response = new MethodResponse(Encoding.UTF8.GetBytes(json), 200);

            await Task.Delay(TimeSpan.FromSeconds(0));

            return(response);
        }
Esempio n. 2
0
        public OpcClient(Uri url)
        {
            string m_scheme = url.Scheme.ToLower();

            if ("opcda" == m_scheme)
            {
                _client = new Da.DaClient(url);
            }
            else if ("opc.tcp" == m_scheme)
            {
                _client = new Ua.UaClient(url);
            }
            try
            {
                _client.Connect();
                Connect = OpcStatus.Connected;
            }
            catch (Exception ex)
            {
                Connect = OpcStatus.NotConnected;
            }
        }