static void TrySetPointValue(ClearScada.Client.Simple.DBObject PointObject, string Value, DateTime MessageTime) { // Check the type of point and item. switch ((string)PointObject["TypeName"]) { case "CPointStringInternal": PointObject["PresetTimestamp"] = MessageTime; PointObject.InvokeMethod("CurrentValue", Value); break; case "CPointDigitalManual": int IntValue; if (int.TryParse(Value, out IntValue)) { if (IntValue < 8) { PointObject["PresetTimestamp"] = MessageTime; PointObject.InvokeMethod("CurrentState", IntValue); } } else { Console.WriteLine($"Value {Value} not valid for: {PointObject.FullName}"); } break; case "CPointAlgManual": double AlgValue; if (double.TryParse(Value, out AlgValue)) { PointObject["PresetTimestamp"] = MessageTime; PointObject.InvokeMethod("CurrentValue", AlgValue); } else { Console.WriteLine($"Value {Value} not valid for: {PointObject.FullName}"); } break; default: Console.WriteLine($"Point type not valid for: {PointObject.FullName}"); break; } }
static int Main(string[] args) { if (args.Length != 4) { Console.WriteLine("Usage: SetInternalPoint \"username\" \"password\" \"Point-Name\" \"Value\" "); return(1); } string user = args[0]; string pass = args[1]; string pointname = args[2]; string valuetext = args[3]; double valuedouble; if (!double.TryParse(valuetext, out valuedouble)) { Console.WriteLine("Value is not numeric"); return(1); } ClearScada.Client.Simple.Connection connection; var node = new ClearScada.Client.ServerNode(ClearScada.Client.ConnectionType.Standard, "127.0.0.1", 5481); connection = new ClearScada.Client.Simple.Connection("Utility"); try { connection.Connect(node); } catch (CommunicationsException) { Console.WriteLine("Unable to communicate with Geo SCADA server."); return(1); } if (!connection.IsConnected) { Console.WriteLine("Not connected to Geo SCADA server."); return(1); } using (var spassword = new System.Security.SecureString()) { foreach (var c in pass) { spassword.AppendChar(c); } try { connection.LogOn(user, spassword); } catch (AccessDeniedException) { Console.WriteLine("Access denied, incorrect user Id or password"); return(1); } catch (PasswordExpiredException) { Console.WriteLine("Credentials expired."); return(1); } } // Get point object ClearScada.Client.Simple.DBObject pointobject = null; try { pointobject = connection.GetObject(pointname); } catch (Exception e) { Console.WriteLine("Cannot get point object. " + e.Message); return(1); } // Set value try { object [] callparam = new object [1]; callparam[0] = valuedouble; pointobject.InvokeMethod("CurrentValue", valuedouble); } catch (Exception e) { Console.WriteLine("Error setting value. " + e.Message); return(1); } // Demo to read historic /* * var pointObject2 = connection.GetObject("Example Projects.Oil and Gas.Transportation.Inflow Computer.GasFlow"); * DateTime hisStart = new DateTime(2021, 1, 19, 0, 0, 0); * DateTime hisEnd = new DateTime(2021, 1, 20, 0, 0, 0); * object[] hisArgs = { hisStart, hisEnd, 0, 1000, true, "All" }; * var hisResult = pointObject2.InvokeMethod("Historic.RawValues", hisArgs); * Console.WriteLine(hisResult); * Console.ReadKey(); */ connection.Disconnect(); return(0); }
// Alarm acknowledgement private bool TryAlarmAck(string UserId, string PIN, long Cookie, string Phone) { // Log on to the .Net Client API with these details // This requires a Reference from the project to this dll ClearScada.Client.Simple.Connection connection; var node = new ClearScada.Client.ServerNode(ClearScada.Client.ConnectionType.Standard, "127.0.0.1", 5481); ((DrvNotifyChannel)this.Channel).LogAndEvent("Acknowledge - connection created."); connection = new ClearScada.Client.Simple.Connection("Notify"); try { ((DrvNotifyChannel)this.Channel).LogAndEvent("Acknowledge - connecting."); connection.Connect(node); } catch (CommunicationsException) { ((DrvNotifyChannel)this.Channel).LogAndEvent("Ack request - Unable to log in to ClearSCADA server using UserId and PIN."); return(false); } if (!connection.IsConnected) { ((DrvNotifyChannel)this.Channel).LogAndEvent("Acknowledge - failed connection."); return(false); } using (var spassword = new System.Security.SecureString()) { foreach (var c in PIN) { spassword.AppendChar(c); } try { ((DrvNotifyChannel)this.Channel).LogAndEvent("Acknowledge - logging in."); connection.LogOn(UserId, spassword); } catch (AccessDeniedException) { ((DrvNotifyChannel)this.Channel).LogAndEvent("Ack request - Access denied, incorrect user Id or PIN."); return(false); } catch (PasswordExpiredException) { ((DrvNotifyChannel)this.Channel).LogAndEvent("Ack request - credentials expired."); return(false); } } // Get root object ClearScada.Client.Simple.DBObject root = null; try { ((DrvNotifyChannel)this.Channel).LogAndEvent("Acknowledge - get database object."); root = connection.GetObject("$Root"); } catch (Exception e) { ((DrvNotifyChannel)this.Channel).LogAndEvent("Ack request - Cannot get root object. " + e.Message); return(false); } object[] parameters = new object[2]; parameters[0] = Cookie; parameters[1] = "By Phone"; // Try acknowledging alarm try { ((DrvNotifyChannel)this.Channel).LogAndEvent("Acknowledge - accepting by cookie."); root.InvokeMethod("AcceptAlarmByCookie", parameters); } catch (Exception e) { ((DrvNotifyChannel)this.Channel).LogAndEvent("Ack request - Cannot acknowledge. " + e.Message); return(false); } return(true); }
async static Task Main(string[] args) { if (args.Length != 5) { Console.WriteLine("Usage: SetAndWatchInternalPoint \"username\" \"password\" \"Point-Name\" \"Value\" \"Watch-Point-Name\" "); return; } string user = args[0]; string pass = args[1]; string pointname = args[2]; string valuetext = args[3]; string watchpointname = args[4]; double valuedouble; if (!double.TryParse(valuetext, out valuedouble)) { Console.WriteLine("Value is not numeric"); return; } ClearScada.Client.Simple.Connection connection; var node = new ClearScada.Client.ServerNode(ClearScada.Client.ConnectionType.Standard, "127.0.0.1", 5481); connection = new ClearScada.Client.Simple.Connection("Utility"); IServer AdvConnection; try { connection.Connect(node); AdvConnection = node.Connect("Utility", false); } catch (CommunicationsException) { Console.WriteLine("Unable to communicate with Geo SCADA server."); return; } if (!connection.IsConnected) { Console.WriteLine("Not connected to Geo SCADA server."); return; } using (var spassword = new System.Security.SecureString()) { foreach (var c in pass) { spassword.AppendChar(c); } try { connection.LogOn(user, spassword); AdvConnection.LogOn(user, spassword); } catch (AccessDeniedException) { Console.WriteLine("Access denied, incorrect user Id or password"); return; } catch (PasswordExpiredException) { Console.WriteLine("Credentials expired."); return; } } // Set event callback AdvConnection.TagsUpdated += TagUpdateEvent; // Disconnect Callback AdvConnection.StateChanged += DBStateChangeEvent; AdvConnection.AdviseStateChange(); // Register for change for this point // ReadSeconds is the interval for the Server to watch for change. Be careful not to set this small int ReadSeconds = 10; try { int RegisterId = 1; // Use a unique number for each point waited for (not needed to be same as Object Id) // Registration is by point name, specify the field which has to change (could use CurrentTime to catch every change). AdvConnection.AddTags(new TagDetails(RegisterId, watchpointname + ".CurrentValue", new TimeSpan(0, 0, ReadSeconds))); } catch (ClearScada.Client.CommunicationsException) { Console.WriteLine("*** Comms exception (AddTags)"); return; } // Get point object to be modified ClearScada.Client.Simple.DBObject pointobject = null; try { pointobject = connection.GetObject(pointname); } catch (Exception e) { Console.WriteLine("Cannot get point object. " + e.Message); return; } // Set value try { pointobject.InvokeMethod("CurrentValue", valuedouble); } catch (Exception e) { Console.WriteLine("Error setting value. " + e.Message); return; } // Wait and watch for changes Console.WriteLine("Waiting for changes"); while (WaitUntilStopped) { await Task.Delay(1000); } // Demo to read historic data /* * var pointObject2 = connection.GetObject("Example Projects.Oil and Gas.Transportation.Inflow Computer.GasFlow"); * DateTime hisStart = new DateTime(2021, 1, 19, 0, 0, 0); * DateTime hisEnd = new DateTime(2021, 1, 20, 0, 0, 0); * object[] hisArgs = { hisStart, hisEnd, 0, 1000, true, "All" }; * var hisResult = pointObject2.InvokeMethod("Historic.RawValues", hisArgs); * Console.WriteLine(hisResult); * Console.ReadKey(); */ connection.Disconnect(); return; }