static void Main(string[] args) { Log("{json:scada} IEC60870-5-104 Driver - Copyright 2020 RLO"); Log("Driver version " + DriverVersion); Log("Using lib60870.NET version " + LibraryCommon.GetLibraryVersionString()); if (args.Length > 0) // first argument in number of the driver instance { int num; bool res = int.TryParse(args[0], out num); if (res) { ProtocolDriverInstanceNumber = num; } } if (args.Length > 1) // second argument is logLevel { int num; bool res = int.TryParse(args[1], out num); if (res) { LogLevel = num; } } string fname = JsonConfigFilePath; if (!File.Exists(fname)) { fname = JsonConfigFilePathAlt; } if (!File.Exists(fname)) { Log("Missing config file " + JsonConfigFilePath); Environment.Exit(-1); } Log("Reading config file " + fname); string json = File.ReadAllText(fname); JSConfig = JsonSerializer.Deserialize <JSONSCADAConfig>(json); if ( JSConfig.mongoConnectionString == "" || JSConfig.mongoConnectionString == null ) { Log("Missing MongoDB connection string in JSON config file " + fname); Environment.Exit(-1); } // Log("MongoDB connection string: " + JSConfig.mongoConnectionString); if ( JSConfig.mongoDatabaseName == "" || JSConfig.mongoDatabaseName == null ) { Log("Missing MongoDB database name in JSON config file " + fname); Environment.Exit(-1); } Log("MongoDB database name: " + JSConfig.mongoDatabaseName); if (JSConfig.nodeName == "" || JSConfig.nodeName == null) { Log("Missing nodeName parameter in JSON config file " + fname); Environment.Exit(-1); } Log("Node name: " + JSConfig.nodeName); var Client = ConnectMongoClient(JSConfig); var DB = Client.GetDatabase(JSConfig.mongoDatabaseName); // read and process instances configuration var collinsts = DB .GetCollection <protocolDriverInstancesClass >(ProtocolDriverInstancesCollectionName); var instances = collinsts .Find(inst => inst.protocolDriver == ProtocolDriverName && inst.protocolDriverInstanceNumber == ProtocolDriverInstanceNumber && inst.enabled == true) .ToList(); var foundInstance = false; foreach (protocolDriverInstancesClass inst in instances) { if ( ProtocolDriverName == inst.protocolDriver && ProtocolDriverInstanceNumber == inst.protocolDriverInstanceNumber ) { foundInstance = true; if (!inst.enabled) { Log("Driver instance [" + ProtocolDriverInstanceNumber.ToString() + "] disabled!"); Environment.Exit(-1); } Log("Instance: " + inst.protocolDriverInstanceNumber.ToString()); var nodefound = false; foreach (var name in inst.nodeNames) { if (JSConfig.nodeName == name) { nodefound = true; } } if (!nodefound) { Log("Node '" + JSConfig.nodeName + "' not found in instances configuration!"); Environment.Exit(-1); } DriverInstance = inst; break; } break; // process just first result } if (!foundInstance) { Log("Driver instance [" + ProtocolDriverInstanceNumber + "] not found in configuration!"); Environment.Exit(-1); } // read and process connections configuration for this driver instance var collconns = DB .GetCollection <IEC10X_connection>(ProtocolConnectionsCollectionName); var conns = collconns .Find(conn => conn.protocolDriver == ProtocolDriverName && conn.protocolDriverInstanceNumber == ProtocolDriverInstanceNumber && conn.enabled == true) .ToList(); foreach (IEC10X_connection isrv in conns) { if (isrv.ipAddresses.Length < 1) { Log("Missing ipAddresses list!"); Environment.Exit(-1); } IEC10Xconns.Add(isrv); Log(isrv.name.ToString() + " - New Connection"); } if (IEC10Xconns.Count == 0) { Log("No connections found!"); Environment.Exit(-1); } // start thread to process redundancy control Thread thrMongoRedundacy = new Thread(() => ProcessRedundancyMongo(JSConfig)); thrMongoRedundacy.Start(); // start thread to update acquired data to database Thread thrMongo = new Thread(() => ProcessMongo(JSConfig)); thrMongo.Start(); // start thread to watch for commands in the database using a change stream Thread thrMongoCmd = new Thread(() => ProcessMongoCmd(JSConfig)); thrMongoCmd.Start(); Log("Setting up IEC Connections & ASDU handlers..."); int cntIecSrv = 0; foreach (IEC10X_connection srv in IEC10Xconns) { var apcipars = new APCIParameters(); apcipars.K = srv.k; apcipars.W = srv.w; apcipars.T0 = srv.t0; apcipars.T1 = srv.t1; apcipars.T2 = srv.t2; apcipars.T3 = srv.t3; var alpars = new ApplicationLayerParameters(); alpars.SizeOfCOT = srv.sizeOfCOT; alpars.SizeOfCA = srv.sizeOfCA; alpars.SizeOfIOA = srv.sizeOfIOA; alpars.OA = srv.localLinkAddress; var tcpPort = 2404; string[] ipAddrPort = srv.ipAddresses[0].Split(':'); if (ipAddrPort.Length > 1) { if (int.TryParse(ipAddrPort[1], out _)) { tcpPort = System.Convert.ToInt32(ipAddrPort[1]); } } var con = new Connection(ipAddrPort[0], tcpPort, apcipars, alpars); con.Parameters.OA = srv.localLinkAddress; srv.connection = con; srv.CntGI = srv.giInterval - 3; srv.CntTestCommand = srv.testCommandInterval - 1; srv.CntTimeSync = 0; srv.CntTestCommandSeq = 0; if (LogLevel >= LogLevelDebug) { con.DebugOutput = true; } con.SetASDUReceivedHandler(AsduReceivedHandler, cntIecSrv); con.SetConnectionHandler(ConnectionHandler, cntIecSrv); // create timer to increment counters each second srv.TimerCnt = new System.Timers.Timer(); srv.TimerCnt.Interval = 1000; srv.TimerCnt.Elapsed += (sender, e) => MyElapsedMethod(sender, e, srv);
static void Main(string[] args) { Log("{json:scada} IEC60870-5-104 Server Driver - Copyright 2020 RLO"); Log("Driver version " + DriverVersion); Log("Using lib60870.NET version " + LibraryCommon.GetLibraryVersionString()); if ( args.Length > 0 // first argument in number of the driver instance ) { int num; bool res = int.TryParse(args[0], out num); if (res) { ProtocolDriverInstanceNumber = num; } } if ( args.Length > 1 // second argument is logLevel ) { int num; bool res = int.TryParse(args[1], out num); if (res) { LogLevel = num; } } string fname = JsonConfigFilePath; if (args.Length > 2) // third argument is config file name { if (File.Exists(args[2])) { fname = args[2]; } } if (!File.Exists(fname)) { fname = JsonConfigFilePathAlt; } if (!File.Exists(fname)) { Log("Missing config file " + JsonConfigFilePath); Environment.Exit(-1); } Log("Reading config file " + fname); string json = File.ReadAllText(fname); JSConfig = JsonSerializer.Deserialize <JSONSCADAConfig>(json); if ( JSConfig.mongoConnectionString == "" || JSConfig.mongoConnectionString == null ) { Log("Missing MongoDB connection string in JSON config file " + fname); Environment.Exit(-1); } // Log("MongoDB connection string: " + JSConfig.mongoConnectionString); if ( JSConfig.mongoDatabaseName == "" || JSConfig.mongoDatabaseName == null ) { Log("Missing MongoDB database name in JSON config file " + fname); Environment.Exit(-1); } Log("MongoDB database name: " + JSConfig.mongoDatabaseName); if (JSConfig.nodeName == "" || JSConfig.nodeName == null) { Log("Missing nodeName parameter in JSON config file " + fname); Environment.Exit(-1); } Log("Node name: " + JSConfig.nodeName); // connect to MongoDB Database server Client = ConnectMongoClient(JSConfig); var DB = Client.GetDatabase(JSConfig.mongoDatabaseName); // read and process instances configuration var collinsts = DB .GetCollection <protocolDriverInstancesClass >(ProtocolDriverInstancesCollectionName); var instances = collinsts .Find(inst => inst.protocolDriver == ProtocolDriverName && inst.protocolDriverInstanceNumber == ProtocolDriverInstanceNumber && inst.enabled == true) .ToList(); var foundInstance = false; foreach (protocolDriverInstancesClass inst in instances) { if ( ProtocolDriverName == inst.protocolDriver && ProtocolDriverInstanceNumber == inst.protocolDriverInstanceNumber ) { foundInstance = true; if (!inst.enabled) { Log("Driver instance [" + ProtocolDriverInstanceNumber.ToString() + "] disabled!"); Environment.Exit(-1); } Log("Instance: " + inst.protocolDriverInstanceNumber.ToString()); var nodefound = false; foreach (var name in inst.nodeNames) { if (JSConfig.nodeName == name) { nodefound = true; } } if (!nodefound) { Log("Node '" + JSConfig.nodeName + "' not found in instances configuration!"); Environment.Exit(-1); } DriverInstance = inst; break; } break; // process just first result } if (!foundInstance) { Log("Driver instance [" + ProtocolDriverInstanceNumber + "] not found in configuration!"); Environment.Exit(-1); } // read and process connections configuration for this driver instance var collconns = DB .GetCollection <IEC10X_connection>(ProtocolConnectionsCollectionName); var conns = collconns .Find(conn => conn.protocolDriver == ProtocolDriverName && conn.protocolDriverInstanceNumber == ProtocolDriverInstanceNumber && conn.enabled == true) .ToList(); foreach (IEC10X_connection isrv in conns) { IEC10Xconns.Add(isrv); Log(isrv.name.ToString()); } if (IEC10Xconns.Count == 0) { Log("No connections found!"); Environment.Exit(-1); } // start thread to dequeue iec data and send to connections Thread thrDeqIecInfo = new Thread(() => DequeueIecInfo()); thrDeqIecInfo.Start(); // start thread to watch for commands in the database using a change stream Thread thrMongoCS = new Thread(() => ProcessMongoCS(JSConfig)); thrMongoCS.Start(); Log("Setting up IEC Connections & ASDU handlers..."); int cntIecSrv = 0; foreach (IEC10X_connection srv in IEC10Xconns) { var apcipars = new APCIParameters(); apcipars.K = srv.k; apcipars.W = srv.w; apcipars.T0 = srv.t0; apcipars.T1 = srv.t1; apcipars.T2 = srv.t2; apcipars.T3 = srv.t3; var alpars = new ApplicationLayerParameters(); alpars.SizeOfCOT = srv.sizeOfCOT; alpars.SizeOfCA = srv.sizeOfCA; alpars.SizeOfIOA = srv.sizeOfIOA; alpars.OA = srv.localLinkAddress; var server = new Server(apcipars, alpars); srv.server = server; if (srv.serverModeMultiActive) { server.ServerMode = ServerMode.CONNECTION_IS_REDUNDANCY_GROUP; } else { server.ServerMode = ServerMode.SINGLE_REDUNDANCY_GROUP; } var localBindIpAddress = "0.0.0.0"; var tcpPort = 2404; string[] ipAddrPort = srv.ipAddressLocalBind.Split(':'); if (ipAddrPort.Length > 0) { if (ipAddrPort[0] != "") { localBindIpAddress = ipAddrPort[0]; } } if (ipAddrPort.Length > 1) { if (int.TryParse(ipAddrPort[1], out _)) { tcpPort = System.Convert.ToInt32(ipAddrPort[1]); } } server.SetLocalAddress(localBindIpAddress); server.SetLocalPort(tcpPort); //RedundancyGroup redGroup = new RedundancyGroup("catch all"); //server.AddRedundancyGroup(redGroup); if (LogLevel >= LogLevelDebug) { server.DebugOutput = true; } server.MaxQueueSize = srv.maxQueueSize; server.MaxOpenConnections = srv.maxClientConnections; Log(srv.name + " - Max Queue Size: " + server.MaxQueueSize); Log(srv.name + " - Max Client Connections: " + server.MaxOpenConnections); server.SetConnectionRequestHandler( ConnectionRequestHandler, cntIecSrv ); server.SetConnectionEventHandler( ConnectionEventHandler, cntIecSrv ); server.SetInterrogationHandler( InterrogationHandler, cntIecSrv ); server.SetASDUHandler(AsduReceivedHandler, cntIecSrv); server.Start(); Log(srv.name + " - New server listening on " + localBindIpAddress + ":" + tcpPort); cntIecSrv++; } Thread.Sleep(1000); bool running = true; Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; running = false; }; Log("Press [CTRL]+[C] to terminate..."); do { try { if (Client == null) { // retry connection Client = new MongoClient(JSConfig.mongoConnectionString); DB = Client.GetDatabase(JSConfig.mongoDatabaseName); } IsMongoLive = DB .RunCommandAsync((Command <BsonDocument>) "{ping:1}") .Wait(10000); if (!IsMongoLive) { throw new Exception("Error on MongoDB connection "); } //foreach (IEC104_connection srv in IEC10Xconns) //{ //} } catch (Exception e) { // Disconnects to retry after some time Client = null; Log("Exception Mongo"); Log(e); Log(e .ToString() .Substring(0, e.ToString().IndexOf(Environment.NewLine))); System.Threading.Thread.Sleep(3000); } Thread.Sleep(1000); }while (running); Log("Exiting application!"); Environment.Exit(0); /* Synchronize clock of the controlled station */ //con.SendClockSyncCommand(1 /* CA */, new CP56Time2a(DateTime.Now)); }
static void Main(string[] args) { Log("{json:scada} IEC60870-5-104 Driver - Copyright 2020 RLO"); Log("Driver version " + DriverVersion); Log("Using lib60870.NET version " + LibraryCommon.GetLibraryVersionString()); if (args.Length > 0) // first argument in number of the driver instance { int num; bool res = int.TryParse(args[0], out num); if (res) { ProtocolDriverInstanceNumber = num; } } if (args.Length > 1) // second argument is logLevel { int num; bool res = int.TryParse(args[1], out num); if (res) { LogLevel = num; } } string fname = JsonConfigFilePath; if (args.Length > 2) // third argument is config file name { if (File.Exists(args[2])) { fname = args[2]; } } if (!File.Exists(fname)) { fname = JsonConfigFilePathAlt; } if (!File.Exists(fname)) { Log("Missing config file " + JsonConfigFilePath); Environment.Exit(-1); } Log("Reading config file " + fname); string json = File.ReadAllText(fname); JSConfig = JsonSerializer.Deserialize <JSONSCADAConfig>(json); if ( JSConfig.mongoConnectionString == "" || JSConfig.mongoConnectionString == null ) { Log("Missing MongoDB connection string in JSON config file " + fname); Environment.Exit(-1); } // Log("MongoDB connection string: " + JSConfig.mongoConnectionString); if ( JSConfig.mongoDatabaseName == "" || JSConfig.mongoDatabaseName == null ) { Log("Missing MongoDB database name in JSON config file " + fname); Environment.Exit(-1); } Log("MongoDB database name: " + JSConfig.mongoDatabaseName); if (JSConfig.nodeName == "" || JSConfig.nodeName == null) { Log("Missing nodeName parameter in JSON config file " + fname); Environment.Exit(-1); } Log("Node name: " + JSConfig.nodeName); var Client = ConnectMongoClient(JSConfig); var DB = Client.GetDatabase(JSConfig.mongoDatabaseName); // read and process instances configuration var collinsts = DB .GetCollection <protocolDriverInstancesClass >(ProtocolDriverInstancesCollectionName); var instances = collinsts .Find(inst => inst.protocolDriver == ProtocolDriverName && inst.protocolDriverInstanceNumber == ProtocolDriverInstanceNumber && inst.enabled == true) .ToList(); var foundInstance = false; foreach (protocolDriverInstancesClass inst in instances) { if ( ProtocolDriverName == inst.protocolDriver && ProtocolDriverInstanceNumber == inst.protocolDriverInstanceNumber ) { foundInstance = true; if (!inst.enabled) { Log("Driver instance [" + ProtocolDriverInstanceNumber.ToString() + "] disabled!"); Environment.Exit(-1); } Log("Instance: " + inst.protocolDriverInstanceNumber.ToString()); var nodefound = false; foreach (var name in inst.nodeNames) { if (JSConfig.nodeName == name) { nodefound = true; } } if (!nodefound) { Log("Node '" + JSConfig.nodeName + "' not found in instances configuration!"); Environment.Exit(-1); } DriverInstance = inst; break; } break; // process just first result } if (!foundInstance) { Log("Driver instance [" + ProtocolDriverInstanceNumber + "] not found in configuration!"); Environment.Exit(-1); } // read and process connections configuration for this driver instance var collconns = DB .GetCollection <IEC10X_connection>(ProtocolConnectionsCollectionName); var conns = collconns .Find(conn => conn.protocolDriver == ProtocolDriverName && conn.protocolDriverInstanceNumber == ProtocolDriverInstanceNumber && conn.enabled == true) .ToList(); foreach (IEC10X_connection isrv in conns) { if (isrv.ipAddresses.Length < 1) { Log("Missing ipAddresses list!"); Environment.Exit(-1); } IEC10Xconns.Add(isrv); Log(isrv.name.ToString() + " - New Connection"); } if (IEC10Xconns.Count == 0) { Log("No connections found!"); Environment.Exit(-1); } // start thread to process redundancy control Thread thrMongoRedundacy = new Thread(() => ProcessRedundancyMongo(JSConfig)); thrMongoRedundacy.Start(); // start thread to update acquired data to database Thread thrMongo = new Thread(() => ProcessMongo(JSConfig)); thrMongo.Start(); // start thread to watch for commands in the database using a change stream Thread thrMongoCmd = new Thread(() => ProcessMongoCmd(JSConfig)); thrMongoCmd.Start(); Log("Setting up IEC Connections & ASDU handlers..."); int cntIecSrv = 0; foreach (IEC10X_connection srv in IEC10Xconns) { var apcipars = new APCIParameters(); apcipars.K = srv.k; apcipars.W = srv.w; apcipars.T0 = srv.t0; apcipars.T1 = srv.t1; apcipars.T2 = srv.t2; apcipars.T3 = srv.t3; var alpars = new ApplicationLayerParameters(); alpars.SizeOfCOT = srv.sizeOfCOT; alpars.SizeOfCA = srv.sizeOfCA; alpars.SizeOfIOA = srv.sizeOfIOA; alpars.OA = srv.localLinkAddress; TlsSecurityInformation secInfo = null; if (srv.localCertFilePath != "") { try { // Own certificate has to be a pfx file that contains the private key X509Certificate2 ownCertificate = new X509Certificate2(srv.localCertFilePath); // Create a new security information object to configure TLS secInfo = new TlsSecurityInformation(null, ownCertificate); // Add allowed server certificates - not required when AllowOnlySpecificCertificates == false secInfo.AddAllowedCertificate(new X509Certificate2(srv.peerCertFilePath)); // Add a CA certificate to check the certificate provided by the server - not required when ChainValidation == false secInfo.AddCA(new X509Certificate2(srv.rootCertFilePath)); // Check if the certificate is signed by a provided CA secInfo.ChainValidation = srv.chainValidation; // Check that the shown server certificate is in the list of allowed certificates secInfo.AllowOnlySpecificCertificates = srv.allowOnlySpecificCertificates; } catch (Exception e) { Log(srv.name + " - Error configuring TLS certficates."); Log(srv.name + " - " + e.Message); Environment.Exit(1); } } var tcpPort = 2404; string[] ipAddrPort = srv.ipAddresses[0].Split(':'); if (ipAddrPort.Length > 1) { if (int.TryParse(ipAddrPort[1], out _)) { tcpPort = System.Convert.ToInt32(ipAddrPort[1]); } } var con = new Connection(ipAddrPort[0], tcpPort, apcipars, alpars); con.Parameters.OA = srv.localLinkAddress; srv.conn1 = con; srv.conn2 = con; srv.connection = con; srv.CntGI = srv.giInterval - 3; srv.CntTestCommand = srv.testCommandInterval - 1; srv.CntTimeSync = 0; srv.CntTestCommandSeq = 0; if (LogLevel >= LogLevelDebug) { con.DebugOutput = true; } con.SetASDUReceivedHandler(AsduReceivedHandler, cntIecSrv); con.SetConnectionHandler(ConnectionHandler, cntIecSrv); if (srv.ipAddresses.Length > 1) // is there a secondary server ? { string[] ipAddrPort2 = srv.ipAddresses[1].Split(':'); if (ipAddrPort2.Length > 1) { if (int.TryParse(ipAddrPort2[1], out _)) { tcpPort = System.Convert.ToInt32(ipAddrPort2[1]); } } var c2 = new Connection(ipAddrPort2[0], tcpPort, apcipars, alpars); con.Parameters.OA = srv.localLinkAddress; srv.conn2 = c2; srv.connection = c2; // force initial swap to primary server if (LogLevel >= LogLevelDebug) { c2.DebugOutput = true; } c2.SetASDUReceivedHandler(AsduReceivedHandler, cntIecSrv); c2.SetConnectionHandler(ConnectionHandler, cntIecSrv); } if (srv.localCertFilePath != "" && secInfo != null) { srv.conn1.SetTlsSecurity(secInfo); srv.conn2.SetTlsSecurity(secInfo); } // create timer to increment counters each second srv.TimerCnt = new System.Timers.Timer(); srv.TimerCnt.Interval = 1000; srv.TimerCnt.Elapsed += (sender, e) => MyElapsedMethod(sender, e, srv);
static void Main(string[] args) { Log("{json:scada} IEC60870-5-101 Server Driver - Copyright 2020 RLO"); Log("Driver version " + DriverVersion); Log("Using lib60870.NET version " + LibraryCommon.GetLibraryVersionString()); if ( args.Length > 0 // first argument in number of the driver instance ) { int num; bool res = int.TryParse(args[0], out num); if (res) { ProtocolDriverInstanceNumber = num; } } if ( args.Length > 1 // second argument is logLevel ) { int num; bool res = int.TryParse(args[1], out num); if (res) { LogLevel = num; } } string fname = JsonConfigFilePath; if (args.Length > 2) // third argument is config file name { if (File.Exists(args[2])) { fname = args[2]; } } if (!File.Exists(fname)) { fname = JsonConfigFilePathAlt; } if (!File.Exists(fname)) { Log("Missing config file " + JsonConfigFilePath); Environment.Exit(-1); } Log("Reading config file " + fname); string json = File.ReadAllText(fname); JSConfig = JsonSerializer.Deserialize <JSONSCADAConfig>(json); if ( JSConfig.mongoConnectionString == "" || JSConfig.mongoConnectionString == null ) { Log("Missing MongoDB connection string in JSON config file " + fname); Environment.Exit(-1); } // Log("MongoDB connection string: " + JSConfig.mongoConnectionString); if ( JSConfig.mongoDatabaseName == "" || JSConfig.mongoDatabaseName == null ) { Log("Missing MongoDB database name in JSON config file " + fname); Environment.Exit(-1); } Log("MongoDB database name: " + JSConfig.mongoDatabaseName); if (JSConfig.nodeName == "" || JSConfig.nodeName == null) { Log("Missing nodeName parameter in JSON config file " + fname); Environment.Exit(-1); } Log("Node name: " + JSConfig.nodeName); // connect to MongoDB Database server Client = ConnectMongoClient(JSConfig); var DB = Client.GetDatabase(JSConfig.mongoDatabaseName); // read and process instances configuration var collinsts = DB .GetCollection <protocolDriverInstancesClass >(ProtocolDriverInstancesCollectionName); var instances = collinsts .Find(inst => inst.protocolDriver == ProtocolDriverName && inst.protocolDriverInstanceNumber == ProtocolDriverInstanceNumber && inst.enabled == true) .ToList(); var foundInstance = false; foreach (protocolDriverInstancesClass inst in instances) { if ( ProtocolDriverName == inst.protocolDriver && ProtocolDriverInstanceNumber == inst.protocolDriverInstanceNumber ) { foundInstance = true; if (!inst.enabled) { Log("Driver instance [" + ProtocolDriverInstanceNumber.ToString() + "] disabled!"); Environment.Exit(-1); } Log("Instance: " + inst.protocolDriverInstanceNumber.ToString()); var nodefound = false; foreach (var name in inst.nodeNames) { if (JSConfig.nodeName == name) { nodefound = true; } } if (!nodefound) { Log("Node '" + JSConfig.nodeName + "' not found in instances configuration!"); Environment.Exit(-1); } DriverInstance = inst; break; } break; // process just first result } if (!foundInstance) { Log("Driver instance [" + ProtocolDriverInstanceNumber + "] not found in configuration!"); Environment.Exit(-1); } // read and process connections configuration for this driver instance var collconns = DB .GetCollection <IEC10X_connection>(ProtocolConnectionsCollectionName); var conns = collconns .Find(conn => conn.protocolDriver == ProtocolDriverName && conn.protocolDriverInstanceNumber == ProtocolDriverInstanceNumber && conn.enabled == true) .ToList(); foreach (IEC10X_connection isrv in conns) { IEC10Xconns.Add(isrv); Log(isrv.name.ToString()); } if (IEC10Xconns.Count == 0) { Log("No connections found!"); Environment.Exit(-1); } // start thread to dequeue iec data and send to connections Thread thrDeqIecInfo = new Thread(() => DequeueIecInfo()); thrDeqIecInfo.Start(); // start thread to watch for commands in the database using a change stream Thread thrMongoCS = new Thread(() => ProcessMongoCS(JSConfig)); thrMongoCS.Start(); Log("Setting up IEC Connections & ASDU handlers..."); int cntIecSrv = 0; foreach (IEC10X_connection srv in IEC10Xconns) { TcpClientVirtualSerialPort virtualPort = null; SerialPort port = null; if (srv.portName.Contains(":")) { var hostport = srv.portName.Split(":"); virtualPort = new TcpClientVirtualSerialPort(hostport[0], System.Convert.ToInt32(hostport[1])); if (LogLevel >= LogLevelDebug) { virtualPort.DebugOutput = true; } virtualPort.Start(); } else { port = new SerialPort(); port.PortName = srv.portName; port.BaudRate = srv.baudRate; switch (srv.parity.ToLower()) { default: // Even is the starndard parity for 101 case "even": port.Parity = Parity.Even; break; case "none": port.Parity = Parity.None; break; case "odd": port.Parity = Parity.Odd; break; case "mark": port.Parity = Parity.Mark; break; case "space": port.Parity = Parity.Space; break; } switch (srv.stopBits.ToLower()) { default: case "one": port.StopBits = StopBits.One; break; case "one5": case "onepointfive": port.StopBits = StopBits.OnePointFive; break; case "two": port.StopBits = StopBits.Two; break; } switch (srv.handshake.ToLower()) { default: case "none": port.Handshake = Handshake.None; break; case "xon": case "xonxoff": port.Handshake = Handshake.XOnXOff; break; case "rts": case "requesttosend": port.Handshake = Handshake.RequestToSend; break; case "rtsxon": case "requesttosendxonxoff": port.Handshake = Handshake.RequestToSendXOnXOff; break; } port.Open(); port.DiscardInBuffer(); } LinkLayerParameters llParameters = new LinkLayerParameters(); llParameters.AddressLength = srv.sizeOfLinkAddress; llParameters.TimeoutForACK = srv.timeoutForACK; llParameters.TimeoutRepeat = srv.timeoutRepeat; llParameters.UseSingleCharACK = srv.useSingleCharACK; CS101Slave slave; if (port != null) { slave = new CS101Slave(port, llParameters); } else { slave = new CS101Slave(virtualPort, llParameters); } slave.Parameters.SizeOfCOT = srv.sizeOfCOT; slave.Parameters.SizeOfCA = srv.sizeOfCA; slave.Parameters.SizeOfIOA = srv.sizeOfIOA; slave.Parameters.OA = srv.localLinkAddress; if (LogLevel >= LogLevelDebug) { slave.DebugOutput = true; } slave.LinkLayerAddress = srv.localLinkAddress; slave.LinkLayerMode = lib60870.linklayer.LinkLayerMode.UNBALANCED; slave.SetInterrogationHandler(InterrogationHandler, cntIecSrv); slave.SetUserDataQueueSizes(srv.maxQueueSize, srv.maxQueueSize); srv.server = slave; slave.SetASDUHandler(AsduReceivedHandler, cntIecSrv); // slave.Start(); Log(srv.name + " - New server listening on " + srv.portName); cntIecSrv++; } Thread.Sleep(1000); bool running = true; Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; running = false; }; Log("Press [CTRL]+[C] to terminate..."); int cnt = 1; do { try { foreach (IEC10X_connection srv in IEC10Xconns) { srv.server.Run(); } if (Client == null) { // retry connection IsMongoLive = false; Client = new MongoClient(JSConfig.mongoConnectionString); DB = Client.GetDatabase(JSConfig.mongoDatabaseName); IsMongoLive = true; } if ((cnt % 20) == 0) // each 1 second test mongo connection { IsMongoLive = DB .RunCommandAsync((Command <BsonDocument>) "{ping:1}") .Wait(1000); if (!IsMongoLive) { throw new Exception("Error on MongoDB connection "); } } } catch (Exception e) { // Disconnects to retry after some time IsMongoLive = false; Client = null; Log("Exception"); Log(e); Log(e .ToString() .Substring(0, e.ToString().IndexOf(Environment.NewLine))); System.Threading.Thread.Sleep(100); } Thread.Sleep(50); cnt++; }while (running); Log("Exiting application!"); Environment.Exit(0); }
public static void Main(string[] args) { Log("{json:scada} IEC60870-5-101 Driver - Copyright 2020 RLO"); Log("Driver version " + DriverVersion); Log("Using lib60870.NET version " + LibraryCommon.GetLibraryVersionString()); if (args.Length > 0) // first argument in number of the driver instance { int num; bool res = int.TryParse(args[0], out num); if (res) { ProtocolDriverInstanceNumber = num; } } if (args.Length > 1) // second argument is logLevel { int num; bool res = int.TryParse(args[1], out num); if (res) { LogLevel = num; } } string fname = JsonConfigFilePath; if (args.Length > 2) // third argument is config file name { if (File.Exists(args[2])) { fname = args[2]; } } if (!File.Exists(fname)) { fname = JsonConfigFilePathAlt; } if (!File.Exists(fname)) { Log("Missing config file " + JsonConfigFilePath); Environment.Exit(-1); } Log("Reading config file " + fname); string json = File.ReadAllText(fname); JSConfig = JsonSerializer.Deserialize <JSONSCADAConfig>(json); if ( JSConfig.mongoConnectionString == "" || JSConfig.mongoConnectionString == null ) { Log("Missing MongoDB connection string in JSON config file " + fname); Environment.Exit(-1); } // Log("MongoDB connection string: " + JSConfig.mongoConnectionString); if ( JSConfig.mongoDatabaseName == "" || JSConfig.mongoDatabaseName == null ) { Log("Missing MongoDB database name in JSON config file " + fname); Environment.Exit(-1); } Log("MongoDB database name: " + JSConfig.mongoDatabaseName); if (JSConfig.nodeName == "" || JSConfig.nodeName == null) { Log("Missing nodeName parameter in JSON config file " + fname); Environment.Exit(-1); } Log("Node name: " + JSConfig.nodeName); // connect to MongoDB Database server var Client = ConnectMongoClient(JSConfig); var DB = Client.GetDatabase(JSConfig.mongoDatabaseName); // read and process instances configuration var collinsts = DB .GetCollection <protocolDriverInstancesClass >(ProtocolDriverInstancesCollectionName); var instances = collinsts .Find(inst => inst.protocolDriver == ProtocolDriverName && inst.protocolDriverInstanceNumber == ProtocolDriverInstanceNumber && inst.enabled == true) .ToList(); var foundInstance = false; foreach (protocolDriverInstancesClass inst in instances) { if ( ProtocolDriverName == inst.protocolDriver && ProtocolDriverInstanceNumber == inst.protocolDriverInstanceNumber ) { foundInstance = true; if (!inst.enabled) { Log("Driver instance [" + ProtocolDriverInstanceNumber.ToString() + "] disabled!"); Environment.Exit(-1); } Log("Instance: " + inst.protocolDriverInstanceNumber.ToString()); var nodefound = false; foreach (var name in inst.nodeNames) { if (JSConfig.nodeName == name) { nodefound = true; } } if (!nodefound) { Log("Node '" + JSConfig.nodeName + "' not found in instances configuration!"); Environment.Exit(-1); } DriverInstance = inst; break; } break; // process just first result } if (!foundInstance) { Log("Driver instance [" + ProtocolDriverInstanceNumber + "] not found in configuration!"); Environment.Exit(-1); } // read and process connections configuration for this driver instance var collconns = DB .GetCollection <IEC10X_connection>(ProtocolConnectionsCollectionName); var conns = collconns .Find(conn => conn.protocolDriver == ProtocolDriverName && conn.protocolDriverInstanceNumber == ProtocolDriverInstanceNumber && conn.enabled == true) .ToList(); foreach (IEC10X_connection isrv in conns) { IEC10Xconns.Add(isrv); Log(isrv.name.ToString() + " - New Connection"); } if (IEC10Xconns.Count == 0) { Log("No connections found!"); Environment.Exit(-1); } // start thread to process redundancy control Thread thrMongoRedundacy = new Thread(() => ProcessRedundancyMongo(JSConfig)); thrMongoRedundacy.Start(); // start thread to update acquired data to database Thread thrMongo = new Thread(() => ProcessMongo(JSConfig)); thrMongo.Start(); // start thread to watch for commands in the database using a change stream Thread thrMongoCmd = new Thread(() => ProcessMongoCmd(JSConfig)); thrMongoCmd.Start(); Log("Setting up IEC Connections & ASDU handlers..."); int cntIecSrv = 0; foreach (IEC10X_connection srv in IEC10Xconns) { TcpClientVirtualSerialPort virtualPort = null; SerialPort port = null; if (srv.portName.Contains(":")) { var hostport = srv.portName.Split(":"); virtualPort = new TcpClientVirtualSerialPort(hostport[0], System.Convert.ToInt32(hostport[1])); if (LogLevel >= LogLevelDebug) { virtualPort.DebugOutput = true; } virtualPort.Start(); } else { port = new SerialPort(); port.PortName = srv.portName; port.BaudRate = srv.baudRate; switch (srv.parity.ToLower()) { default: // Even is the starndard parity for 101 case "even": port.Parity = Parity.Even; break; case "none": port.Parity = Parity.None; break; case "odd": port.Parity = Parity.Odd; break; case "mark": port.Parity = Parity.Mark; break; case "space": port.Parity = Parity.Space; break; } switch (srv.stopBits.ToLower()) { default: case "one": port.StopBits = StopBits.One; break; case "one5": case "onepointfive": port.StopBits = StopBits.OnePointFive; break; case "two": port.StopBits = StopBits.Two; break; } switch (srv.handshake.ToLower()) { default: case "none": port.Handshake = Handshake.None; break; case "xon": case "xonxoff": port.Handshake = Handshake.XOnXOff; break; case "rts": case "requesttosend": port.Handshake = Handshake.RequestToSend; break; case "rtsxon": case "requesttosendxonxoff": port.Handshake = Handshake.RequestToSendXOnXOff; break; } port.Open(); port.DiscardInBuffer(); } LinkLayerParameters llParameters = new LinkLayerParameters(); llParameters.AddressLength = srv.sizeOfLinkAddress; llParameters.TimeoutForACK = srv.timeoutForACK; llParameters.TimeoutRepeat = srv.timeoutRepeat; llParameters.UseSingleCharACK = srv.useSingleCharACK; ApplicationLayerParameters alpars = new ApplicationLayerParameters(); alpars.SizeOfCOT = srv.sizeOfCOT; alpars.SizeOfCA = srv.sizeOfCA; alpars.SizeOfIOA = srv.sizeOfIOA; alpars.OA = srv.localLinkAddress; CS101Master master; if (port != null) { Log("Serial Port: " + srv.portName); master = new CS101Master(port, LinkLayerMode.UNBALANCED, llParameters, alpars); } else { Log("Virtual Serial Port: " + srv.portName); master = new CS101Master(virtualPort, LinkLayerMode.UNBALANCED, llParameters, alpars); } // master.OwnAddress = srv.localLinkAddress; master.SetTimeouts(srv.timeoutMessage, srv.timeoutCharacter); master.AddSlave(srv.remoteLinkAddress); master.SlaveAddress = srv.remoteLinkAddress; srv.master = master; srv.CntGI = srv.giInterval - 5; srv.CntTestCommand = srv.testCommandInterval - 2; srv.CntTimeSync = srv.timeSyncInterval; if (LogLevel >= LogLevelDebug) { master.DebugOutput = true; } master.SetASDUReceivedHandler(AsduReceivedHandlerPre, cntIecSrv); master.SetLinkLayerStateChangedHandler(linkLayerStateChanged, cntIecSrv); master.SetReceivedRawMessageHandler(rcvdRawMessageHandler, cntIecSrv); master.Start(); // create timer to increment counters each second srv.TimerCnt = new System.Timers.Timer(); srv.TimerCnt.Interval = 1000; srv.TimerCnt.Elapsed += (sender, e) => MyElapsedMethod(sender, e, srv);