public OpcUaClient GetClient(DatasourceRequest request, OpcUaJsonData jsonData)
        {
            OpcUaClient client = null;

            try
            {
                client = clientConnections[request.Datasource.Url];
            }
            catch (System.Collections.Generic.KeyNotFoundException)
            {
                Task <OpcUaClient> connectTask;
                if (jsonData.tlsAuth)
                {
                    connectTask = ConnectAsync(request.Datasource.Url, request.Datasource.DecryptedSecureJsonData["tlsClientCert"], request.Datasource.DecryptedSecureJsonData["tlsClientKey"]);
                }
                else
                {
                    connectTask = ConnectAsync(request.Datasource.Url);
                }

                connectTask.Wait();
                client = connectTask.Result;
                clientConnections[request.Datasource.Url] = client;
            }
            catch (System.NullReferenceException)
            {
                clientConnections = new Dictionary <string, OpcUaClient>();
            }

            return(client);
        }
예제 #2
0
        public OpcUaJsonData(ByteString base64encoded)
        {
            byte[]        byDecoded = System.Convert.FromBase64String(base64encoded.ToString());
            OpcUaJsonData jsonData  = JsonSerializer.Deserialize <OpcUaJsonData>(byDecoded);

            tlsAuth           = jsonData.tlsAuth;
            tlsAuthWithCACert = jsonData.tlsAuthWithCACert;
            tlsSkipVerify     = jsonData.tlsSkipVerify;
        }
        public async override Task <DatasourceResponse> Query(DatasourceRequest request, ServerCallContext context)
        {
            DatasourceResponse response = new DatasourceResponse {
            };

            try
            {
                log.Information("got a request: {0}", request);
                List <OpcUAQuery> queries  = ParseJSONQueries(request);
                OpcUaJsonData     jsonData = JsonSerializer.Deserialize <OpcUaJsonData>(request.Datasource.JsonData);
                OpcUaClient       client   = GetClient(request, jsonData);
                log.Information("got a client {0}", client);

                // Process the queries
                foreach (OpcUAQuery query in queries)
                {
                    QueryResult queryResult = new QueryResult();
                    queryResult.RefId = query.refId;
                    switch (query.call)
                    {
                    case "Browse":
                    {
                        log.Information("client {0}", client);
                        var results = client.BrowseNodeReference(query.callParams["nodeId"]);
                        BrowseResultsEntry[] browseResults = new BrowseResultsEntry[results.Length];
                        for (int i = 0; i < results.Length; i++)
                        {
                            browseResults[i] = new BrowseResultsEntry(results[i].DisplayName.ToString(), results[i].BrowseName.ToString(), results[i].NodeId.ToString());
                        }
                        var jsonResults = JsonSerializer.Serialize <BrowseResultsEntry[]>(browseResults);
                        queryResult.MetaJson = jsonResults;
                    }
                    break;

                    case "FlatBrowse":
                    {
                        if (this.browseResults == null)
                        {
                            var browseResults = this.FlatBrowse(client);
                            var jsonResults   = JsonSerializer.Serialize <BrowseResultsEntry[]>(browseResults);
                            this.browseResults = jsonResults;
                        }
                        queryResult.MetaJson = this.browseResults;
                    }
                    break;

                    case "ReadNode":
                    {
                        log.Information("ReadNode {0}", query.callParams["nodeId"]);
                        var readResults = client.ReadNode(query.callParams["nodeId"]);
                        log.Information("Results: {0}", readResults);
                        var jsonResults = JsonSerializer.Serialize <DataValue>(readResults);
                        queryResult.MetaJson = jsonResults;
                    }
                    break;

/*                        case "Subscription":
 *                          {
 *                              client.AddSubscription(query.refId, query.callParams["nodeId"], SubCallback);
 *                              queryResult.MetaJson = "";
 *                          }
 *                          break;*/
                    case "ReadDataRaw":
                    {
                        log.Information("Query: {0}", request.TimeRange);
                        DateTime fromTime = DateTimeOffset.FromUnixTimeMilliseconds(request.TimeRange.FromEpochMs).UtcDateTime;
                        DateTime toTime   = DateTimeOffset.FromUnixTimeMilliseconds(request.TimeRange.ToEpochMs).UtcDateTime;
                        log.Information("Parsed Time: {0} {1}", fromTime, toTime);

                        var readResults = client.ReadHistoryRawDataValues(
                            query.callParams["nodeId"],
                            fromTime,
                            toTime,
                            (uint)query.maxDataPoints,
                            false);

                        var jsonResults = JsonSerializer.Serialize <IEnumerable <DataValue> >(readResults);
                        queryResult.MetaJson = jsonResults;
                    }
                    break;

                    case "ReadDataProcessed":
                    {
                        DateTime fromTime    = DateTimeOffset.FromUnixTimeMilliseconds(request.TimeRange.FromEpochMs).UtcDateTime;
                        DateTime toTime      = DateTimeOffset.FromUnixTimeMilliseconds(request.TimeRange.ToEpochMs).UtcDateTime;
                        var      readResults = client.ReadHistoryProcessed(
                            query.callParams["nodeId"],
                            fromTime,
                            toTime,
                            query.callParams["aggregate"],
                            query.intervalMs,
                            (uint)query.maxDataPoints,
                            true);
                        var jsonResults = JsonSerializer.Serialize <IEnumerable <DataValue> >(readResults);
                        log.Information("Results: {0}", readResults);
                        queryResult.MetaJson = jsonResults;
                    }
                    break;
                    }
                    response.Results.Add(queryResult);
                }
            }
            catch (Exception ex)
            {
                // Close out the client connection.
                clientConnections[request.Datasource.Url].Disconnect();
                clientConnections.Remove(request.Datasource.Url);
                log.Information("Error: {0}", ex);
                QueryResult queryResult = new QueryResult();
                queryResult.Error = ex.ToString();
                response.Results.Add(queryResult);
            }

            return(response);
        }