├── .gitignore ├── CLI ├── CLI.csproj ├── Commands │ ├── CommandBase.cs │ ├── ListCommand.cs │ ├── SetConnectionCommand.cs │ ├── UpdateBlockNumberCommand.cs │ └── UpdateConnection.cs ├── Exceptions │ └── InvalidCommandException.cs ├── FodyWeavers.xml ├── FodyWeavers.xsd ├── Inerfaces │ ├── IConnectionService.cs │ ├── IDatabaseService.cs │ └── IVariableService.cs ├── Logic │ ├── ConnectionService.cs │ ├── DatabaseService.cs │ └── VariableService.cs ├── Model │ ├── Connection.cs │ ├── Model.cs │ └── Variable.cs └── Program.cs ├── LICENSE ├── README.md └── WinCCConnectionTool.sln /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | -------------------------------------------------------------------------------- /CLI/CLI.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 7.3 5 | Exe 6 | net462 7 | WinCCCT 8 | 1.0.0 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | all 18 | runtime; build; native; contentfiles; analyzers; buildtransitive 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /CLI/Commands/CommandBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using System.Windows.Input; 9 | using CLI.Exceptions; 10 | using CLI.Inerfaces; 11 | using McMaster.Extensions.CommandLineUtils; 12 | 13 | namespace CLI.Commands 14 | { 15 | public class CommandBase 16 | { 17 | protected readonly IDatabaseService DatabaseService; 18 | 19 | [Option("--path", Description = "Path of MDF file")] 20 | public string Path { get; set; } 21 | 22 | [Option("--dbName", Description = "Name of MDF file")] 23 | public string DbName { get; set; } 24 | 25 | 26 | protected readonly IConnectionService ConnectionService; 27 | 28 | public CommandBase(IConnectionService connectionService, IDatabaseService databaseService) 29 | { 30 | this.DatabaseService = databaseService; 31 | this.ConnectionService = connectionService; 32 | } 33 | 34 | protected async Task LoadDatabase() 35 | { 36 | if (string.IsNullOrEmpty(Path)) 37 | { 38 | if (string.IsNullOrEmpty(DbName)) 39 | { 40 | SearchMdfFile(); 41 | } 42 | else 43 | { 44 | SearchMdfFile(DbName); 45 | } 46 | } 47 | 48 | await LoadDatabase(Path); 49 | } 50 | 51 | private async Task LoadDatabase(string path) 52 | { 53 | await DatabaseService.LoadDatabase(@".\WINCC", path); 54 | await ConnectionService.LoadConnections(); 55 | } 56 | 57 | protected void CloseDatabase() 58 | { 59 | DatabaseService.CloseDatabase(); 60 | } 61 | 62 | private void SearchMdfFile(string pattern = null) 63 | { 64 | var listOfFiles = Directory.EnumerateFiles(Environment.CurrentDirectory, pattern ?? "*.MDF", 65 | SearchOption.AllDirectories); 66 | if (listOfFiles.Count() == 1) 67 | { 68 | Path = listOfFiles.First(); 69 | } 70 | else if (listOfFiles.Count() > 1) 71 | { 72 | foreach (var file in listOfFiles) 73 | { 74 | Console.WriteLine($"Found DB file at: '{file}'"); 75 | } 76 | 77 | throw new InvalidCommandException( 78 | "Found more than 1 MDF file. Please specify which one to use via the --path or --dbName option"); 79 | } 80 | else 81 | { 82 | throw new InvalidCommandException( 83 | "Could not find MDF file. Please specify which one to use via the --path or --dbName option"); 84 | } 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /CLI/Commands/ListCommand.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using CLI.Inerfaces; 9 | using CLI.Logic; 10 | using ConsoleTables; 11 | using McMaster.Extensions.CommandLineUtils; 12 | 13 | namespace CLI.Commands 14 | { 15 | [Command("list", Description = "List all WinCC connections")] 16 | class ListCommand : CommandBase 17 | { 18 | public async Task OnExecute() 19 | { 20 | await LoadDatabase(); 21 | 22 | ConsoleTable 23 | .From(ConnectionService.Connections) 24 | .Write(Format.Alternative); 25 | 26 | CloseDatabase(); 27 | 28 | } 29 | 30 | public ListCommand(IConnectionService connectionService, IDatabaseService databaseService) : base(connectionService, databaseService) 31 | { 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /CLI/Commands/SetConnectionCommand.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using CLI.Inerfaces; 8 | using McMaster.Extensions.CommandLineUtils; 9 | 10 | namespace CLI.Commands 11 | { 12 | [Command("setCon", Description = "Set WinCC connection string")] 13 | class SetConnectionCommand : CommandBase 14 | { 15 | [Argument(0)] 16 | [Required] 17 | public string ConnectionName { get; set; } 18 | 19 | [Argument(1)] 20 | [Required] 21 | public string Parameter { get; set; } 22 | 23 | public async Task OnExecute() 24 | { 25 | await LoadDatabase(); 26 | await SetConnectionParameter(); 27 | CloseDatabase(); 28 | } 29 | 30 | private async Task SetConnectionParameter() 31 | { 32 | var connection = ConnectionService.Connections.FirstOrDefault(c => c.ConnectionName.Equals(ConnectionName, StringComparison.InvariantCultureIgnoreCase)); 33 | if (connection == null) 34 | { 35 | Console.WriteLine($"No connection with name '{ConnectionName}' available."); 36 | return; 37 | } 38 | 39 | connection.Parameter = Parameter; 40 | await ConnectionService.UpdateConectionParameter(connection); 41 | return; 42 | } 43 | 44 | public SetConnectionCommand(IConnectionService connectionService, IDatabaseService databaseService) : base(connectionService, databaseService) 45 | { 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /CLI/Commands/UpdateBlockNumberCommand.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using CLI.Inerfaces; 8 | using CLI.Logic; 9 | using CLI.Model; 10 | using McMaster.Extensions.CommandLineUtils; 11 | using Microsoft.EntityFrameworkCore; 12 | 13 | namespace CLI.Commands 14 | { 15 | [Command("setOpcNS", Description = "Update OPC Namespace number of all variables in a connection")] 16 | class UpdateBlockNumberCommand : CommandBase 17 | { 18 | [Argument(0)] 19 | [Required] 20 | public string ConnectionName { get; set; } 21 | 22 | [Argument(1)] 23 | [Required] 24 | public int NewBlockNumber { get; set; } 25 | 26 | public async Task OnExecute() 27 | { 28 | await LoadDatabase(); 29 | await variableService.LoadVariables(); 30 | await UpdateBlockname(); 31 | CloseDatabase(); 32 | } 33 | 34 | public async Task UpdateBlockname() 35 | { 36 | var connection = ConnectionService.Connections.FirstOrDefault(c => c.ConnectionName.Equals(ConnectionName, StringComparison.InvariantCultureIgnoreCase)); 37 | if (connection == null) 38 | { 39 | Console.WriteLine($"No connection with name '{ConnectionName}' available."); 40 | return; 41 | } 42 | 43 | var connectionId = connection.ConnectionId; 44 | var varsToUpdate = variableService.Variables.Where(variable => variable.ConnectionId == connectionId); 45 | 46 | using (var dbcxtransaction = DatabaseService.BeginTransaction()) 47 | { 48 | try 49 | { 50 | //databaseService.CurrentContext.AddRange(varsToUpdate); 51 | foreach (var variable in varsToUpdate) 52 | { 53 | variable.Blockname = NewBlockNumber; 54 | DatabaseService.CurrentContext.Entry(variable).State = EntityState.Modified; 55 | //await variableService.UpdateVariable(variable); 56 | } 57 | 58 | dbcxtransaction.Commit(); 59 | await DatabaseService.CurrentContext.SaveChangesAsync(); 60 | Console.WriteLine($"Block number successfully updated in {varsToUpdate.Count()} rows"); 61 | } 62 | catch(Exception ex) 63 | { 64 | Console.WriteLine(ex.InnerException.Message); 65 | Console.WriteLine($"Rollback"); 66 | dbcxtransaction.Rollback(); 67 | } 68 | } 69 | } 70 | 71 | public UpdateBlockNumberCommand(IConnectionService connectionService, IVariableService variableService, IDatabaseService databaseService) : base(connectionService, databaseService) 72 | { 73 | this.variableService = variableService; 74 | } 75 | 76 | private readonly IVariableService variableService; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /CLI/Commands/UpdateConnection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Text.RegularExpressions; 7 | using System.Threading.Tasks; 8 | using CLI.Inerfaces; 9 | using McMaster.Extensions.CommandLineUtils; 10 | 11 | namespace CLI.Commands 12 | { 13 | [Command("setOpc", Description = "Update a WinCC connection string of type OPC UA")] 14 | class UpdateConnection : CommandBase 15 | { 16 | [Argument(0)] 17 | [Required] 18 | public string ConnectionName { get; set; } 19 | 20 | [Argument(1)] 21 | [Required] 22 | public string Host { get; set; } 23 | 24 | [Argument(2)] 25 | [Required] 26 | public string Port { get; set; } 27 | 28 | public async Task OnExecute() 29 | { 30 | await LoadDatabase(); 31 | await UpdateConnectionString(); 32 | CloseDatabase(); 33 | } 34 | 35 | private async Task UpdateConnectionString() 36 | { 37 | var connection = ConnectionService.Connections.FirstOrDefault(c => c.ConnectionName.Equals(ConnectionName, StringComparison.InvariantCultureIgnoreCase)); 38 | if (connection == null) 39 | { 40 | Console.WriteLine($"No connection with name '{ConnectionName}' available."); 41 | return; 42 | } 43 | 44 | connection.Parameter = ReplaceHostAndPort(connection.Parameter,Host,Port); 45 | await ConnectionService.UpdateConectionParameter(connection); 46 | return; 47 | } 48 | 49 | 50 | private string ReplaceHostAndPort(string connectionParam, string host, string port) 51 | { 52 | return Regex.Replace(connectionParam, @"^opc.tcp://([a-zA-Z0-9\-._~%]+:[0-9]+)?", delegate (Match match) 53 | { 54 | return $"opc.tcp://{host}:{port}"; 55 | }); 56 | } 57 | 58 | 59 | public UpdateConnection(IConnectionService connectionService, IDatabaseService databaseService) : base(connectionService, databaseService) 60 | { 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /CLI/Exceptions/InvalidCommandException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CLI.Exceptions 4 | { 5 | public class InvalidCommandException : Exception 6 | { 7 | //Exception which Stops CLI execution and display help text 8 | public InvalidCommandException(string message) :base(message) 9 | { 10 | 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /CLI/FodyWeavers.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /CLI/FodyWeavers.xsd: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | A list of assembly names to exclude from the default action of "embed all Copy Local references", delimited with line breaks 13 | 14 | 15 | 16 | 17 | A list of assembly names to include from the default action of "embed all Copy Local references", delimited with line breaks. 18 | 19 | 20 | 21 | 22 | A list of unmanaged 32 bit assembly names to include, delimited with line breaks. 23 | 24 | 25 | 26 | 27 | A list of unmanaged 64 bit assembly names to include, delimited with line breaks. 28 | 29 | 30 | 31 | 32 | The order of preloaded assemblies, delimited with line breaks. 33 | 34 | 35 | 36 | 37 | 38 | This will copy embedded files to disk before loading them into memory. This is helpful for some scenarios that expected an assembly to be loaded from a physical file. 39 | 40 | 41 | 42 | 43 | Controls if .pdbs for reference assemblies are also embedded. 44 | 45 | 46 | 47 | 48 | Embedded assemblies are compressed by default, and uncompressed when they are loaded. You can turn compression off with this option. 49 | 50 | 51 | 52 | 53 | As part of Costura, embedded assemblies are no longer included as part of the build. This cleanup can be turned off. 54 | 55 | 56 | 57 | 58 | Costura by default will load as part of the module initialization. This flag disables that behavior. Make sure you call CosturaUtility.Initialize() somewhere in your code. 59 | 60 | 61 | 62 | 63 | Costura will by default use assemblies with a name like 'resources.dll' as a satellite resource and prepend the output path. This flag disables that behavior. 64 | 65 | 66 | 67 | 68 | A list of assembly names to exclude from the default action of "embed all Copy Local references", delimited with | 69 | 70 | 71 | 72 | 73 | A list of assembly names to include from the default action of "embed all Copy Local references", delimited with |. 74 | 75 | 76 | 77 | 78 | A list of unmanaged 32 bit assembly names to include, delimited with |. 79 | 80 | 81 | 82 | 83 | A list of unmanaged 64 bit assembly names to include, delimited with |. 84 | 85 | 86 | 87 | 88 | The order of preloaded assemblies, delimited with |. 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed. 97 | 98 | 99 | 100 | 101 | A comma-separated list of error codes that can be safely ignored in assembly verification. 102 | 103 | 104 | 105 | 106 | 'false' to turn off automatic generation of the XML Schema file. 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /CLI/Inerfaces/IConnectionService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | using CLI.Model; 4 | using Microsoft.EntityFrameworkCore.Storage; 5 | 6 | namespace CLI.Inerfaces 7 | { 8 | public interface IConnectionService 9 | { 10 | Task LoadConnections(); 11 | IEnumerable Connections { get; } 12 | Task UpdateConectionParameter(Connection connection); 13 | } 14 | } -------------------------------------------------------------------------------- /CLI/Inerfaces/IDatabaseService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | using CLI.Model; 5 | using Microsoft.EntityFrameworkCore.Storage; 6 | 7 | namespace CLI.Inerfaces 8 | { 9 | public interface IDatabaseService 10 | { 11 | Task LoadDatabase(string dbInstance, string dbPath); 12 | void CloseDatabase(); 13 | IDbContextTransaction BeginTransaction(); 14 | Context CurrentContext { get; } 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /CLI/Inerfaces/IVariableService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using CLI.Model; 7 | 8 | namespace CLI.Inerfaces 9 | { 10 | public interface IVariableService 11 | { 12 | Task LoadVariables(); 13 | IEnumerable Variables { get; } 14 | Task UpdateVariable(Variable variable); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CLI/Logic/ConnectionService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading.Tasks; 4 | using CLI.Inerfaces; 5 | using CLI.Model; 6 | using Microsoft.EntityFrameworkCore; 7 | using Microsoft.EntityFrameworkCore.Storage; 8 | 9 | namespace CLI.Logic 10 | { 11 | public class ConnectionService : IConnectionService 12 | { 13 | private readonly IDatabaseService databaseService; 14 | 15 | public ConnectionService(IDatabaseService databaseService) 16 | { 17 | this.databaseService = databaseService; 18 | } 19 | public async Task LoadConnections() 20 | { 21 | Connections = await databaseService.CurrentContext.Connection.ToArrayAsync(); 22 | } 23 | 24 | public async Task UpdateConectionParameter(Connection connection) 25 | { 26 | databaseService.CurrentContext.Update(connection); 27 | await databaseService.CurrentContext.SaveChangesAsync(); 28 | } 29 | 30 | public IEnumerable Connections { get; private set; } 31 | } 32 | } -------------------------------------------------------------------------------- /CLI/Logic/DatabaseService.cs: -------------------------------------------------------------------------------- 1 | using System.Reactive; 2 | using System.Threading.Tasks; 3 | using CLI.Inerfaces; 4 | using CLI.Model; 5 | using Microsoft.EntityFrameworkCore; 6 | using Microsoft.EntityFrameworkCore.Storage; 7 | 8 | namespace CLI.Logic 9 | { 10 | public class DatabaseService : IDatabaseService 11 | { 12 | public Task LoadDatabase(string dbInstance, string dbPath) 13 | { 14 | var optionsBuilder = new DbContextOptionsBuilder(); 15 | 16 | optionsBuilder.UseSqlServer($@"Data Source={dbInstance};Integrated Security=SSPI;AttachDbFilename={dbPath};TrustServerCertificate=true"); 17 | 18 | 19 | CurrentContext = new Context(optionsBuilder.Options); 20 | 21 | return Task.FromResult(Unit.Default); 22 | } 23 | 24 | public void CloseDatabase() 25 | { 26 | CurrentContext.Dispose(); 27 | } 28 | 29 | 30 | 31 | public IDbContextTransaction BeginTransaction() 32 | { 33 | return CurrentContext.Database.BeginTransaction(); 34 | } 35 | 36 | public Context CurrentContext { get; private set; } 37 | } 38 | } -------------------------------------------------------------------------------- /CLI/Logic/VariableService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using CLI.Inerfaces; 7 | using CLI.Model; 8 | using Microsoft.EntityFrameworkCore; 9 | 10 | namespace CLI.Logic 11 | { 12 | public class VariableService : IVariableService 13 | { 14 | private readonly IDatabaseService databaseService; 15 | 16 | public VariableService(IDatabaseService databaseService) 17 | { 18 | this.databaseService = databaseService; 19 | } 20 | public async Task LoadVariables() 21 | { 22 | Variables = await databaseService.CurrentContext.Variables.ToArrayAsync(); 23 | } 24 | 25 | public async Task UpdateVariable(Variable variable) 26 | { 27 | databaseService.CurrentContext.Update(variable); 28 | databaseService.CurrentContext.Entry(variable).State = EntityState.Modified; 29 | await databaseService.CurrentContext.SaveChangesAsync(false); 30 | } 31 | 32 | 33 | public IEnumerable Variables { get; private set; } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /CLI/Model/Connection.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations.Schema; 2 | 3 | namespace CLI.Model 4 | { 5 | [Table("MCPTCONNECTION")] 6 | public class Connection 7 | { 8 | [Column("CONNECTIONID")] 9 | public int ConnectionId { get; set; } 10 | 11 | [Column("CONNECTIONNAME")] 12 | public string ConnectionName { get; set; } 13 | 14 | [Column("PARAMETER")] 15 | public string Parameter { get; set; } 16 | 17 | public override string ToString() 18 | { 19 | return $"{nameof(ConnectionName)}: {ConnectionName}, {nameof(Parameter)}: {Parameter}"; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /CLI/Model/Model.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using Microsoft.EntityFrameworkCore; 4 | 5 | namespace CLI.Model 6 | { 7 | public class Context : DbContext 8 | { 9 | public DbSet Connection { get; set; } 10 | public DbSet Variables { get; set; } 11 | 12 | public Context() { } 13 | public Context(DbContextOptions options) 14 | : base(options) { } 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /CLI/Model/Variable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations.Schema; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace CLI.Model 9 | { 10 | [Table("MCPTVARIABLEDESC")] 11 | public class Variable 12 | { 13 | [Column("VARIABLEID")] 14 | [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 15 | public int VariableId { get; set; } 16 | 17 | [Column("CONNECTIONID")] 18 | public int ConnectionId { get; set; } 19 | 20 | [Column("VARIABLENAME")] 21 | public string VariableName { get; set; } 22 | 23 | [Column("PLCBLOCKNAME")] 24 | public int Blockname { get; set; } 25 | 26 | public override string ToString() 27 | { 28 | return $"{nameof(ConnectionId)}: {ConnectionId},{nameof(ConnectionId)}: {VariableName}, {nameof(Blockname)}: {Blockname}"; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /CLI/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | using CLI.Commands; 5 | using CLI.Inerfaces; 6 | using CLI.Logic; 7 | using CLI.Model; 8 | using ConsoleTables; 9 | using McMaster.Extensions.CommandLineUtils; 10 | using Microsoft.EntityFrameworkCore; 11 | using Microsoft.Extensions.DependencyInjection; 12 | 13 | namespace CLI 14 | { 15 | [Subcommand(typeof(ListCommand), typeof(SetConnectionCommand), typeof(UpdateConnection), typeof(UpdateBlockNumberCommand))] 16 | public class Program 17 | { 18 | private static CommandLineApplication app; 19 | 20 | public static int Main(string[] args) 21 | { 22 | try 23 | { 24 | var databaseService = new DatabaseService(); 25 | var services = new ServiceCollection() 26 | .AddSingleton(databaseService) 27 | .AddSingleton(new ConnectionService(databaseService)) 28 | .AddSingleton(new VariableService(databaseService)) 29 | .BuildServiceProvider(); 30 | 31 | using (app = new CommandLineApplication()) 32 | { 33 | app.Conventions 34 | .UseDefaultConventions() 35 | .UseConstructorInjection(services); 36 | return app.Execute(args); 37 | } 38 | }catch(Exception ex) 39 | { 40 | Console.WriteLine(ex.Message); 41 | if(ex.InnerException != null) 42 | { 43 | Console.WriteLine(ex.InnerException.Message); 44 | } 45 | app.ShowHelp(); 46 | return -1; 47 | } 48 | } 49 | 50 | 51 | public void OnExecute() 52 | { 53 | app.ShowHelp(); 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Stefan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WinCCConnectionTool 2 | 3 | [![Build status](https://ci.appveyor.com/api/projects/status/u0gav7sv0jt2v6wg?svg=true)](https://ci.appveyor.com/project/fbarresi/winccconnectiontool) 4 | 5 | This software change the connection parameters of a WinCC Project. 6 | In this way you can keep a single project and change the connected target machine directly from the runtime without recompiling the project. 7 | 8 | [Download it here](https://github.com/evopro-ag/WinCCConnectionTool/releases/latest) 9 | 10 | ## Usage: 11 | `WinCCCT [command] [options]` 12 | 13 | ### Options: 14 | - `--path` : Path (absolute or relative) of MDF file 15 | - `--dbName` : Name or pattern of MDF file (i.E.: `HMI_577I.MDF` or `HMI_*.MDF`) 16 | 17 | if no `--path` or `--dbName` is set the tool will search for a WinCC database in all subfolders. 18 | Use one of theese options if you have many elegible .MDF file in your subfolders 19 | 20 | `-?|-h|--help`: Show help information 21 | 22 | ### Commands: 23 | 24 | - `list` : List all WinCC connections 25 | - `setCon` : Set WinCC connection string 26 | - `setOpc` : Update a WinCC connection string of type OPC UA 27 | - `setOpcNS` : Update OPC Namespace number of all variables in a connection 28 | 29 | Run `WinCCCT [command] --help` for more information about a command. 30 | 31 | ### Examples 32 | - List all WinCC connections: `WinCCCT list --dbName HMI_577I.MDF` 33 | - Change parameter of a generic connection: `WinCCCT setCon connection1 "IP,10.10.100.119,,0,2,02" --dbName HMI_577I.MDF` 34 | - Change OPC UA connection with host name and port number: `WinCCCT setOpc connection1 192.168.1.10 4048 --dbName HMI_577I.MDF` 35 | - Change OPC UA namespace number for all variables in a connection: `WinCCCT setOpcNS connection1 81 --dbName HMI_577I.MDF` 36 | 37 | -------------------------------------------------------------------------------- /WinCCConnectionTool.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.28803.352 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CLI", "CLI\CLI.csproj", "{5874E5DF-E69E-481D-B69F-F47C157A081C}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {5874E5DF-E69E-481D-B69F-F47C157A081C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {5874E5DF-E69E-481D-B69F-F47C157A081C}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {5874E5DF-E69E-481D-B69F-F47C157A081C}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {5874E5DF-E69E-481D-B69F-F47C157A081C}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {35400167-2573-4EEB-AFDE-F0A9723A2604} 24 | EndGlobalSection 25 | EndGlobal 26 | --------------------------------------------------------------------------------