You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2019/05/05 13:20:19 UTC

[plc4x] 05/12: [PLC4X-110] Added core class implementation

This is an automated email from the ASF dual-hosted git repository.

jfeinauer pushed a commit to branch feature/code-gen
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 1304684dbe0cd663edae8b43cf67d26036536880
Author: Björn Höper <ho...@ltsoft.de>
AuthorDate: Mon Apr 15 21:06:19 2019 +0200

    [PLC4X-110] Added core class implementation
---
 plc4net/plc4net.driver/IPlcDriver.cs               |  10 ++
 plc4net/plc4net.driver/plc4net.driver.csproj       |   7 ++
 plc4net/plc4net/PlcDriverManager.cs                | 104 +++++++++++++++++++++
 plc4net/plc4net/api/IPlcConnection.cs              |  50 ++++++++++
 plc4net/plc4net/api/IPlcDriver.cs                  |  56 +++++++++++
 .../api/authentication/IPlcAuthentication.cs       |  29 ++++++
 .../PlcUsernamePasswordAuthentication.cs           |  48 ++++++++++
 .../plc4net/api/metadata/IPlcConnectionMetadata.cs |  39 ++++++++
 .../plc4net/exceptions/PlcConnectionException.cs   |  50 ++++++++++
 plc4net/plc4net/exceptions/PlcException.cs         |  50 ++++++++++
 10 files changed, 443 insertions(+)

diff --git a/plc4net/plc4net.driver/IPlcDriver.cs b/plc4net/plc4net.driver/IPlcDriver.cs
new file mode 100644
index 0000000..b6b1f99
--- /dev/null
+++ b/plc4net/plc4net.driver/IPlcDriver.cs
@@ -0,0 +1,10 @@
+namespace plc4net.driver
+{
+    /// <summary>
+    /// Interface for PLC drivers to be implemented
+    /// </summary>
+    public interface IPlcDriver
+    {
+        IPlcC
+    }
+}
\ No newline at end of file
diff --git a/plc4net/plc4net.driver/plc4net.driver.csproj b/plc4net/plc4net.driver/plc4net.driver.csproj
new file mode 100644
index 0000000..9f5c4f4
--- /dev/null
+++ b/plc4net/plc4net.driver/plc4net.driver.csproj
@@ -0,0 +1,7 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>netstandard2.0</TargetFramework>
+  </PropertyGroup>
+
+</Project>
diff --git a/plc4net/plc4net/PlcDriverManager.cs b/plc4net/plc4net/PlcDriverManager.cs
new file mode 100644
index 0000000..7597e3a
--- /dev/null
+++ b/plc4net/plc4net/PlcDriverManager.cs
@@ -0,0 +1,104 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using org.apache.plc4net.api;
+using org.apache.plc4net.api.authentication;
+using org.apache.plc4net.exceptions;
+
+namespace org.apache.plc4net
+{
+    /// <summary>
+    /// Manages connections to PLCs
+    /// </summary>
+    public class PlcDriverManager
+    {
+        /// <summary>
+        /// Singleton instance of the manager
+        /// </summary>
+        private static PlcDriverManager _instance;
+
+        /// <summary>
+        /// Get the singleton instance
+        /// </summary>
+        public static PlcDriverManager Instance => _instance ?? (_instance = new PlcDriverManager());
+
+        /// <summary>
+        /// Dictionary for the drivers
+        /// </summary>
+        private readonly Dictionary<string, IPlcDriver> _drivers;
+
+        /// <summary>
+        /// Private constructor for the singleton driver manager.
+        /// </summary>
+        private PlcDriverManager()
+        {
+            _drivers = new Dictionary<string, IPlcDriver>();
+
+            /*
+             * TODO: Implement some mechanism to provide drivers -> MEF?
+             */
+        }
+
+        /// <summary>
+        /// Get the connection to the a PLC identified by the URL
+        /// </summary>
+        /// <param name="url">URL including the schema to connect to the PLC</param>
+        /// <param name="authentication">Authentication to use</param>
+        /// <returns>Created PLC connection</returns>
+        public async Task<IPlcConnection> GetConnection(string url, IPlcAuthentication authentication)
+        {
+            var plcDriver = GetDriver(url);
+            var connection = await plcDriver.ConnectAsync(url, authentication);
+            
+            //TODO: Does the driver method already connect or is a separate connect needed?
+            //TODO: Should we do it like this?
+            if (!connection.IsConnected)
+            {
+                await connection.ConnectAsync();
+            }
+
+            return connection;
+        }
+
+        public IPlcDriver GetDriver(string url)
+        {
+            try
+            {
+                Uri plcUri = new Uri(url);
+                var proto = plcUri.Scheme;
+
+                _drivers.TryGetValue(proto, out var plcDriver);
+
+                if (plcDriver == null)
+                {
+                    throw new PlcConnectionException($"Unknown driver for protocol '{proto}'");
+                }
+
+                return plcDriver;
+            }
+            catch (UriFormatException invalidUriException)
+            {
+                throw new PlcConnectionException($"Provided connection string '{url}' is invalid", invalidUriException);
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/plc4net/plc4net/api/IPlcConnection.cs b/plc4net/plc4net/api/IPlcConnection.cs
new file mode 100644
index 0000000..63be8dc
--- /dev/null
+++ b/plc4net/plc4net/api/IPlcConnection.cs
@@ -0,0 +1,50 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+using System;
+using System.Threading.Tasks;
+
+namespace org.apache.plc4net.api
+{
+    /// <inheritdoc />
+    /// <summary>
+    /// Interface for generalized PLC connections providing
+    /// functionality for basic operations like connect / disconnect etc.
+    /// </summary>
+    public interface IPlcConnection: IDisposable
+    {
+        /// <summary>
+        /// Connect to the PLC asynchronously
+        /// </summary>
+        /// <returns>Awaitable task</returns>
+        /// <exception cref="org.apache.plc4net.exceptions.PlcConnectionException">Thrown if the connection to the PLC fails</exception>
+        Task ConnectAsync();
+
+        /// <summary>
+        /// Indicates the connection state
+        /// </summary>
+        bool IsConnected { get; }
+
+        /// <summary>
+        /// Close the PLC connection asynchronously
+        /// </summary>
+        /// <returns>Awaitable task</returns>
+        Task CloseAsync();
+    }
+}
\ No newline at end of file
diff --git a/plc4net/plc4net/api/IPlcDriver.cs b/plc4net/plc4net/api/IPlcDriver.cs
new file mode 100644
index 0000000..6a969d9
--- /dev/null
+++ b/plc4net/plc4net/api/IPlcDriver.cs
@@ -0,0 +1,56 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+using System.Threading.Tasks;
+using org.apache.plc4net.api.authentication;
+
+namespace org.apache.plc4net.api
+{
+    /// <summary>
+    /// Interface for PLC drivers to be implemented
+    /// </summary>
+    public interface IPlcDriver
+    {
+        /// <summary>
+        /// Get the code of the implemented protocol
+        /// </summary>
+        string ProtocolCode { get; }
+
+        /// <summary>
+        /// Full Name of the implemented protocol
+        /// </summary>
+        string ProtocolName { get; }
+
+        /// <summary>
+        /// Connects to the PLC identified by the connection string
+        /// </summary>
+        /// <param name="connectionString">Connection string identifying the PLC to connect to</param>
+        /// <returns>Awaitable task returning the <see cref="IPlcConnection"/> to which the connection was established</returns>
+        /// <exception cref="org.apache.plc4net.exceptions.PlcConnectionException">Thrown on connection failure</exception>
+        Task<IPlcConnection> ConnectAsync(string connectionString);
+
+        /// <summary>
+        /// Connects to the PLC identified by the connection string and using the 
+        /// </summary>
+        /// <param name="connectionString"></param>
+        /// <param name="authentication"></param>
+        /// <returns></returns>
+        Task<IPlcConnection> ConnectAsync(string connectionString, IPlcAuthentication authentication);
+    }
+}
\ No newline at end of file
diff --git a/plc4net/plc4net/api/authentication/IPlcAuthentication.cs b/plc4net/plc4net/api/authentication/IPlcAuthentication.cs
new file mode 100644
index 0000000..4a94403
--- /dev/null
+++ b/plc4net/plc4net/api/authentication/IPlcAuthentication.cs
@@ -0,0 +1,29 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+namespace org.apache.plc4net.api.authentication
+{
+    /// <summary>
+    /// Base interface for possible authentication methods
+    /// used by the PLCs
+    /// </summary>
+    public interface IPlcAuthentication
+    {
+    }
+}
\ No newline at end of file
diff --git a/plc4net/plc4net/api/authentication/PlcUsernamePasswordAuthentication.cs b/plc4net/plc4net/api/authentication/PlcUsernamePasswordAuthentication.cs
new file mode 100644
index 0000000..2c7f52c
--- /dev/null
+++ b/plc4net/plc4net/api/authentication/PlcUsernamePasswordAuthentication.cs
@@ -0,0 +1,48 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+namespace org.apache.plc4net.api.authentication
+{
+    /// <summary>
+    /// Authentication using Username and Password
+    /// </summary>
+    public class PlcUsernamePasswordAuthentication : IPlcAuthentication
+    {
+        /// <summary>
+        /// Create a new instance using username and password
+        /// </summary>
+        /// <param name="username">Username to set</param>
+        /// <param name="password">Password to set</param>
+        public PlcUsernamePasswordAuthentication(string username, string password)
+        {
+            Username = username;
+            Password = password;
+        }
+
+        /// <summary>
+        /// Username
+        /// </summary>
+        public string Username { get; protected set; }
+
+        /// <summary>
+        /// Password
+        /// </summary>
+        public string Password { get; protected set; }
+    }
+}
\ No newline at end of file
diff --git a/plc4net/plc4net/api/metadata/IPlcConnectionMetadata.cs b/plc4net/plc4net/api/metadata/IPlcConnectionMetadata.cs
new file mode 100644
index 0000000..7504c91
--- /dev/null
+++ b/plc4net/plc4net/api/metadata/IPlcConnectionMetadata.cs
@@ -0,0 +1,39 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+namespace org.apache.plc4net.api.metadata
+{
+    public interface IPlcConnectionMetadata
+    {
+        /// <summary>
+        /// Indicates that the connection is able to read from the PLC
+        /// </summary>
+        bool CanRead { get; }
+
+        /// <summary>
+        /// Indicates that the connection is able to write to the PLC
+        /// </summary>
+        bool CanWrite { get; }
+
+        /// <summary>
+        /// Indicates that the connection is able to subscribe to PLC data
+        /// </summary>
+        bool CanSubscribe { get; }
+    }
+}
\ No newline at end of file
diff --git a/plc4net/plc4net/exceptions/PlcConnectionException.cs b/plc4net/plc4net/exceptions/PlcConnectionException.cs
new file mode 100644
index 0000000..bc6af44
--- /dev/null
+++ b/plc4net/plc4net/exceptions/PlcConnectionException.cs
@@ -0,0 +1,50 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+using System;
+using System.Runtime.Serialization;
+
+namespace org.apache.plc4net.exceptions
+{
+    /// <summary>
+    /// Exception raised when the connection to a
+    /// PLC fails
+    /// </summary>
+    [Serializable]
+    public class PlcConnectionException : PlcException
+    {
+        public PlcConnectionException()
+        {
+        }
+
+        public PlcConnectionException(string message) : base(message)
+        {
+        }
+
+        public PlcConnectionException(string message, Exception inner) : base(message, inner)
+        {
+        }
+
+        protected PlcConnectionException(
+            SerializationInfo info,
+            StreamingContext context) : base(info, context)
+        {
+        }
+    }
+}
\ No newline at end of file
diff --git a/plc4net/plc4net/exceptions/PlcException.cs b/plc4net/plc4net/exceptions/PlcException.cs
new file mode 100644
index 0000000..6345854
--- /dev/null
+++ b/plc4net/plc4net/exceptions/PlcException.cs
@@ -0,0 +1,50 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+using System;
+using System.Runtime.Serialization;
+
+namespace org.apache.plc4net.exceptions
+{
+    /// <summary>
+    /// General exception that gets thrown when errors while communicating
+    /// with PLC
+    /// </summary>
+    [Serializable]
+    public class PlcException : Exception
+    {        
+        public PlcException()
+        {
+        }
+
+        public PlcException(string message) : base(message)
+        {
+        }
+
+        public PlcException(string message, Exception inner) : base(message, inner)
+        {
+        }
+
+        protected PlcException(
+            SerializationInfo info,
+            StreamingContext context) : base(info, context)
+        {
+        }
+    }              
+}
\ No newline at end of file