/// <summary> /// Load: Write/store message into Geo SCADA /// </summary> /// <param name="message">message from the queue</param> static void Load <T>(T Tag) { #region sampleTag // Tag64 format with all fields. /* { * "device": "88AC1A", * "time": "2018-09-28T13:56:31", * "avgSnr": "20.77", * "data": "4040000100000000000001", * "duplicate": "false", * "snr": "15.04", * "station": "326D", * "lat": "46.0", * "lng": "0.0", * "rssi": "-124.00", * "seqNumber": "722", * "RecordType": 64, * "FrameCnt1": 2, * "CommandDone": 0, * "HWError": 0, * "LowBatError": 0, * "ConfigOK": 0, * "S1ClosedCnt": 1, * "S2ClosedCnt": 0, * "S3ClosedCnt": 0, * "S4ClosedCnt": 0, * "S4PreviousState": 0, * "S4State": 0, * "S3PreviousState": 0, * "S3State": 0, * "S2PreviousState": 0, * "S2State": 0, * "S1PreviousState": 0, * "S1State": 1, * "FrameCnt2": 2, * "FrameCnt3": 2, * "FrameCnt4": 2, * "SwitchError": 0, * "S1OpenCnt": 1, * "S2OpenCnt": 0 * } */ #endregion // To iterate over the tags Type tagType = Tag.GetType(); PropertyInfo[] props = tagType.GetProperties(); // We need the device, time and station properties, the rest could be points string device = ""; string time = ""; string station = ""; foreach (var prop in props) { if (prop.GetIndexParameters().Length == 0) { switch (prop.Name) { case "device": device = prop.GetValue(Tag).ToString(); break; case "time": time = prop.GetValue(Tag).ToString(); break; case "station": station = prop.GetValue(Tag).ToString(); break; default: break; } } } // Convert timestamp to a DateTime - default to now. DateTime MessageTime = DateTime.UtcNow; try { MessageTime = DateTime.Parse(time); } catch { Console.WriteLine($"Cannot parse time: {time}"); } // XIOT devices are identified uniquely by a "device" property. // We will use the "device" property and a fixed prefix "TagBase" to construct a group name. // e.g. "My XIOT Devices.88AC1A" // Then add the tag property as the point name (analog, digital or string Internal point) // e.g. "My XIOT Devices.88AC1A.S1State" // The "time" field (UTC) will be used to set the point's PresetTimestamp // If a point does not exist we log and move on. // Define the group reference ClearScada.Client.Simple.DBObject myGroup; string tagname = TagBase + "." + device; // Find a group object which could contain the points myGroup = connection.GetObject(tagname); if (myGroup == null) { // Set breakpoints here to find device groups not in the database Console.WriteLine($"*** Cannot find group {TagBase + "." + device}"); return; } if (!((string)myGroup["TypeName"] == "CGroup") && !((string)myGroup["TypeName"] == "CTemplateInstance")) { Console.WriteLine($"Object {(string)myGroup["TypeName"]} is not a group."); return; } // Get a list of all the child objects - could get all descendants here if points are actually in further groups. ClearScada.Client.Simple.DBObjectCollection childObjects; childObjects = myGroup.GetChildren("CDBPoint", ""); if (childObjects.Count == 0) { Console.WriteLine($"Group {myGroup.FullName} contains no points"); return; } // For each property, see if there is a matching point. foreach (var prop in props) { if (prop.GetIndexParameters().Length == 0) { // See if there is a point of this name foreach (var PointObject in childObjects) { if (prop.Name == PointObject.Name) { // Get Property value from the JSON structure string Value = ""; if (prop.GetValue(Tag).ToString() != null) { Value = prop.GetValue(Tag).ToString(); } Console.WriteLine($"Try to set point {PointObject.FullName} to value {Value} at time {time}"); try { TrySetPointValue(PointObject, Value, MessageTime); } catch (Exception e) { Console.WriteLine("Exception writing point data: " + e.Message); } } } } } }
// 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); }
static void Main(string[] args) { if (args.Length != 5) { Console.WriteLine("List the referenced items and types of all objects matching an SQL query\n" + "Arguments:\n" + "1 - Server IP/host\n" + "2 - Port\n" + "3 - User\n" + "4 - Password\n" + "5 - SQL query returning a list of FullNames\n" + " Example: ListReferences localhost 5481 freddy password \"select fullname from clogiccore\" \n" + " Output: object name, type, referenced object name, type "); return; } var node = new ClearScada.Client.ServerNode(ClearScada.Client.ConnectionType.Standard, args[0], int.Parse(args[1])); var connection = new ClearScada.Client.Simple.Connection("Utility"); connection.Connect(node); var AdvConnection = node.Connect("UtilityA", false); using (var spassword = new System.Security.SecureString()) { foreach (var c in args[3]) { spassword.AppendChar(c); } connection.LogOn(args[2], spassword); AdvConnection.LogOn(args[2], spassword); } string sql = args[4]; ClearScada.Client.Advanced.IQuery serverQuery = AdvConnection.PrepareQuery(sql, new ClearScada.Client.Advanced.QueryParseParameters()); ClearScada.Client.Advanced.QueryResult queryResult = serverQuery.ExecuteSync(new ClearScada.Client.Advanced.QueryExecuteParameters()); if (queryResult.Status == ClearScada.Client.Advanced.QueryStatus.Succeeded || queryResult.Status == ClearScada.Client.Advanced.QueryStatus.NoDataFound) { if (queryResult.Rows.Count > 0) { IEnumerator <ClearScada.Client.Advanced.QueryRow> e = queryResult.Rows.GetEnumerator(); while (e.MoveNext()) { string fullname = (string)e.Current.Data[0]; var dbobject = connection.GetObject(fullname); var reflist = dbobject.GetReferencesFrom(); if (reflist.Count == 0) { Console.WriteLine(fullname + "," + dbobject.ClassDefinition.Name + ","); } else { foreach (var refobject in reflist) { Console.Write(fullname + "," + dbobject.ClassDefinition.Name + ","); Console.WriteLine(refobject.FullName + ", " + refobject.ClassDefinition.Name); } } } } } serverQuery.Dispose(); }
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); }
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; }
static void Main() { string user = "******"; string pass = "******"; 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"); connection.Connect(node); var spassword = new System.Security.SecureString(); foreach (var c in pass) { spassword.AppendChar(c); } connection.LogOn(user, spassword); // Insert point name here ClearScada.Client.Simple.DBObject PointObj = connection.GetObject("New Analog Point"); DateTime now = DateTime.UtcNow; // Add a value Object[] p1 = new Object[4]; p1[0] = 1; p1[1] = 192; p1[2] = now; p1[3] = 1; PointObj.Aggregates["Historic"].InvokeMethod("LoadDataValue", p1); // Various calls to read values back Object[] p2 = new Object[5]; p2[0] = now.AddSeconds(-1); p2[1] = now.AddSeconds(1); p2[2] = 0; p2[3] = true; p2[4] = "All"; object r = PointObj.Aggregates["Historic"].InvokeMethod("RawValue", p2); Console.WriteLine(r); Object[] p3 = new Object[6]; p3[0] = now.AddSeconds(-1); p3[1] = now.AddSeconds(1); p3[2] = 0; p3[3] = 1; p3[4] = true; p3[5] = "All"; object [] k = (object [])PointObj.Aggregates["Historic"].InvokeMethod("RawValues", p3); Console.WriteLine(k[0]); Object[] p4 = new Object[6]; p4[0] = now.AddSeconds(-1); p4[1] = "2S"; p4[2] = 0; p4[3] = 1; p4[4] = true; p4[5] = "All"; object[] q = (object[])PointObj.Aggregates["Historic"].InvokeMethod("RawValuesRange", p4); Console.WriteLine(q[0]); Console.ReadKey(); }