public void Run() { try { #region GetObjectIdentifier Console.WriteLine("\nIndicate the object you want to run this example code on."); Console.Write("Enter the fully qualified reference of the object (Example: \"site:device/itemReference\"): "); string object1 = Console.ReadLine(); Console.WriteLine("\nIndicate the second object you want to run this example code on."); Console.Write("Enter the fully qualified reference of the object: "); string object2 = Console.ReadLine(); Console.WriteLine("\n\nGetObjectIdentifier..."); // These variables are needed to run the other sections Guid id1 = client.GetObjectIdentifier(object1); Console.WriteLine($"{object1} id: {id1}"); Guid id2 = client.GetObjectIdentifier(object2); Console.WriteLine($"{object2} id: {id2}"); List <Guid> ids = new List <Guid>() { id1, id2 }; #endregion #region ReadProperty Console.WriteLine("\n\nReadProperty..."); Console.Write("\n!!!Please note ReadProperty will return null if the attribute does not exist, and will cause an exception in this example!!!"); Console.Write("\nEnter an attribute of the objects (Examples: name, description, presentValue): "); string attribute1 = Console.ReadLine(); Console.Write("Enter a second attribute of the objects: "); string attribute2 = Console.ReadLine(); List <string> attributes = new List <string>() { attribute1, attribute2 }; Console.WriteLine($"Get {attribute1} property..."); Variant result1Attr1 = client.ReadProperty(id1, attribute1); Variant result2Attr1 = client.ReadProperty(id2, attribute1); Console.WriteLine($"{result1Attr1.Id} {result1Attr1.Attribute} values (string, int, bool, reliability): " + $"\n{result1Attr1.StringValue}, {result1Attr1.NumericValue}, {result1Attr1.BooleanValue}, {result1Attr1.Reliability}"); Console.WriteLine($"{result2Attr1.Id} {result2Attr1.Attribute} values (string, int, bool, reliability): " + $"\n{result2Attr1.StringValue}, {result2Attr1.NumericValue}, {result2Attr1.BooleanValue}, {result2Attr1.Reliability}"); #endregion #region ReadPropertyMultiple Console.WriteLine("\n\nReadPropertyMultiple..."); Console.WriteLine($"\nGet {attribute1}, {attribute2} properties for each object..."); IEnumerable <VariantMultiple> results = client.ReadPropertyMultiple(ids, attributes); foreach (var varMultiple in results) { // Grab the list of Variants for each id var variants = varMultiple.Values; foreach (var result in variants) { Console.WriteLine($"{result.Id} {result.Attribute} values (string, int, bool, reliability): " + $"\n{result.StringValue}, {result.NumericValue}, {result.BooleanValue}, {result.Reliability}"); } } #endregion #region WriteProperty Methods Console.Write("\nEnter an attribute of the objects to change (don't worry, this will be reset to it's original value): "); string attribute3 = Console.ReadLine(); Console.Write("\nEnter one new value for this attribute (this will be applied to both objects): "); object change = Console.ReadLine(); // Get original values, this code will throw an exception if the Variant result1Attr3 = client.ReadProperty(id1, attribute3); Variant result2Attr3 = client.ReadProperty(id2, attribute3); Console.WriteLine($"{result1Attr3.Id} {result1Attr3.Attribute} original value: {result1Attr3.StringValue}"); Console.WriteLine($"{result2Attr3.Id} {result2Attr3.Attribute} original value: {result2Attr3.StringValue}"); Console.WriteLine("\nWriteProperty..."); // Change value client.WriteProperty(id1, attribute3, change); System.Threading.Thread.Sleep(1000); // View changes Variant result1Attr3Updated = client.ReadProperty(id1, attribute3); Console.WriteLine($"{result1Attr3Updated.Id} {result1Attr3Updated.Attribute} updated value: {result1Attr3Updated.StringValue}"); // Reset value client.WriteProperty(id1, attribute3, result1Attr3.StringValue); System.Threading.Thread.Sleep(1000); Console.WriteLine("\nWrite Property Multiple..."); // Change value List <(string, object)> attributes2 = new List <(string, object)>() { (attribute3, change) }; client.WritePropertyMultiple(ids, attributes2); System.Threading.Thread.Sleep(1000); // View changes Variant result1Attr3UpdatedM = client.ReadProperty(id1, attribute3); Variant result2Attr3UpdatedM = client.ReadProperty(id2, attribute3); Console.WriteLine($"{result1Attr3UpdatedM.Id} {result1Attr3UpdatedM.Attribute} updated value: {result1Attr3UpdatedM.StringValue}"); Console.WriteLine($"{result2Attr3UpdatedM.Id} {result2Attr3UpdatedM.Attribute} updated value: {result2Attr3UpdatedM.StringValue}"); System.Threading.Thread.Sleep(1000); // Reset Values client.WriteProperty(id1, attribute3, result1Attr3.StringValue); client.WriteProperty(id2, attribute3, result2Attr3.StringValue); #endregion #region Commands Console.WriteLine("\n\nGetCommands..."); Console.WriteLine($"{id1} Commands:"); IEnumerable <Command> commands = client.GetCommands(id1); if (commands != null && commands.Count() > 0) { Console.WriteLine($"{commands.Count()}\n"); foreach (Command command in commands) { Console.WriteLine($"{command.ToString()}\n"); } } else { Console.WriteLine("No commands found."); } Console.WriteLine("\nSendCommand..."); Console.Write("\nPlease enter a commandId to execute: "); string cmd = Console.ReadLine(); Console.WriteLine("\nEnter a number for a number type, enter the key for an enum type."); Console.WriteLine("Please enter the values for the command in order followed by an empty line: "); List <object> list = new List <object>() { }; string option = ""; do { option = Console.ReadLine(); if (option != null && option.Length > 0) { list.Add(option); } } while (option != null && option.Length > 0); Console.WriteLine("\nSending Command, please wait..."); client.SendCommand(id1, cmd, list); System.Threading.Thread.Sleep(5000); Console.WriteLine("Sent successfully."); #endregion #region GetNetworkDevices Console.WriteLine("\n\nGetNetworkDevices..."); IEnumerable <MetasysObjectType> types = client.GetNetworkDeviceTypes(); foreach (var type in types) { Console.WriteLine($"\nAvailable Type {type.Id}: {type.Description}, {type.DescriptionEnumerationKey}"); string typeId = type.Id.ToString(); IEnumerable <MetasysObject> devices = client.GetNetworkDevices(typeId); Console.WriteLine($"Devices found: {devices.Count()}"); Console.WriteLine($"First Device: {devices.ElementAt(0).Name}"); } #endregion #region GetObjects Console.WriteLine("\n\nGet Objects.."); Console.WriteLine("\nPlease enter the depth of objects to retrieve for the first object."); Console.Write("(1 = only this object, 2 = immediate children only): "); int level = Convert.ToInt32(Console.ReadLine()); IEnumerable <MetasysObject> objects = client.GetObjects(id2, level); if (objects.Count() > 0) { MetasysObject obj = objects.ElementAt(0); Console.WriteLine($"Parent object: {obj.Id}"); for (int i = 1; i < level; i++) { Console.WriteLine($"Child at level {i}: {obj.Id} - {obj.Name}"); if (objects.ElementAt(0).ChildrenCount > 0) { obj = objects.ElementAt(0).Children.ElementAt(0); } else { Console.WriteLine("This object has no children."); } } } else { Console.WriteLine("This object has no children."); } } #endregion catch (Exception exception) { log.Logger.Error(string.Format("An error occured while getting general information - {0}", exception.Message)); Console.WriteLine("\n \nAn Error occurred. Press Enter to return to Main Menu"); } Console.ReadLine(); }
private void GetObjects() { /* SNIPPET 2: START */ Guid parentId = client.GetObjectIdentifier("WIN-21DJ9JV9QH6:EECMI-NCE25-2/FCB"); // Get direct children (1 level) List <MetasysObject> directChildren = client.GetObjects(parentId).ToList(); MetasysObject lastChild = directChildren.LastOrDefault(); Console.WriteLine(lastChild); /* * { * "ItemReference": "Win2016-VM2:vNAE2343996/Field Bus MSTP1.VAV-08.ZN-T", * "Id": "d5d96cd3-db4a-52e0-affd-8bc3393c30ec", * "Name": "ZN-T", * "Description": null, * "Type": null, * "TypeUrl": "https://win2016-vm2/api/v2/enumSets/508/members/601", * "Category": null, * "Children": [], * "ChildrenCount": 0 * } */ // Get direct children (1 level) with internal objects directChildren = client.GetObjects(parentId, includeInternalObjects: true).ToList(); // Get descendant for 2 levels (it could take long time, depending on the number of objects) List <MetasysObject> level2Descendants = client.GetObjects(parentId, 2).ToList(); MetasysObject level1Parent = level2Descendants.FindByName("Time"); Console.WriteLine(level1Parent); /* * { * "ItemReference": "Win2016-VM2:vNAE2343996/WeatherForecast.Time", * "Id": "22bb952e-7557-5de9-b7e5-dce39e21addd", * "Name": "Time", * "Description": null, * "Type": null, * "TypeUrl": "https://win2016-vm2/api/v2/enumSets/508/members/176", * "Category": null, * "Children": [ * { * "ItemReference": "Win2016-VM2:vNAE2343996/WeatherForecast.Time.Day", * "Id": "5886a93f-9260-553c-995e-6a65374de85d", * "Name": "Day", * "Description": null, * "Type": null, * "TypeUrl": "https://win2016-vm2/api/v2/enumSets/508/members/165", * "Category": null, * "Children": [], * "ChildrenCount": 0 * }, * { * "ItemReference": "Win2016-VM2:vNAE2343996/WeatherForecast.Time.Hour", * "Id": "6a50d3af-d0a2-537c-a2f7-9c1b5f271cc5", * "Name": "Hour", * "Description": null, * "Type": null, * "TypeUrl": "https://win2016-vm2/api/v2/enumSets/508/members/165", * "Category": null, * "Children": [], * "ChildrenCount": 0 * }, * { * "ItemReference": "Win2016-VM2:vNAE2343996/WeatherForecast.Time.Minute", * "Id": "19a53f38-2fd7-5ac3-a12c-f3b9704ac194", * "Name": "Minute", * "Description": null, * "Type": null, * "TypeUrl": "https://win2016-vm2/api/v2/enumSets/508/members/165", * "Category": null, * "Children": [], * "ChildrenCount": 0 * }, * { * "ItemReference": "Win2016-VM2:vNAE2343996/WeatherForecast.Time.Year", * "Id": "74dfc214-22c1-57a7-ace5-606636d0049c", * "Name": "Year", * "Description": null, * "Type": null, * "TypeUrl": "https://win2016-vm2/api/v2/enumSets/508/members/165", * "Category": null, * "Children": [], * "ChildrenCount": 0 * } * ], * "ChildrenCount": 4 * } */ /* SNIPPET 2: END */ }
static void Main(string[] args) { // Add support for JSON Config File IConfiguration config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("AppSettings.json", optional: false, reloadOnChange: true) .Build(); // Read hostname from credential manager first var hostnameTarget = config.GetSection("CredentialManager:Targets:MetasysServer").Value; // Hostname is stored in the Credential Manager using password field var secureHostname = CredentialUtil.GetCredential(hostnameTarget); MetasysClient metasysClient = new MetasysClient(CredentialUtil.convertToUnSecureString(secureHostname.Password)); // Retrieve Metasys Credentials to login var credTarget = config.GetSection("CredentialManager:Targets:MetasysCredentials").Value; // Use TryLogin overload that accepts Credential Manager Target metasysClient.TryLogin(credTarget); // Forecast container target is securely stored in Credential manager var containerTarget = config.GetSection("CredentialManager:Targets:ForecastContainer").Value; var secureContainer = CredentialUtil.GetCredential(containerTarget); // Get parent object of Weather Forecast to retrieve related children (securely stored in Credential Manager) Guid parentObjectId = metasysClient.GetObjectIdentifier(CredentialUtil.convertToUnSecureString(secureContainer.Password)); IEnumerable <MetasysObject> weatherForecast = metasysClient.GetObjects(parentObjectId, 1); // Retrieve latitude and longitude to get weather forecast MetasysObject latitudePoint = weatherForecast.FindByName("Latitude"); MetasysObject longitudePoint = weatherForecast.FindByName("Longitude"); double latitude = metasysClient.ReadProperty(latitudePoint.Id, "presentValue").NumericValue; double longitude = metasysClient.ReadProperty(longitudePoint.Id, "presentValue").NumericValue; // Forecast API key is securely stored in Credential manager var apiKeyTarget = config.GetSection("CredentialManager:Targets:OpenWeather").Value; var secureApiKey = CredentialUtil.GetCredential(apiKeyTarget); // Get Next Five Days forecast every 3 hours OpenWeatherMapClient weatherMapclient = new OpenWeatherMapClient(CredentialUtil.convertToUnSecureString(secureApiKey.Password)); ForecastResult forecastResult = weatherMapclient.GetForecast(latitude, longitude).GetAwaiter().GetResult(); // Get the closest forecast to be written Forecast forecast = forecastResult.list.First(); // Read Metasys points to write the response back MetasysObject Day = weatherForecast.FindByName("Day"); MetasysObject Month = weatherForecast.FindByName("Month"); MetasysObject Year = weatherForecast.FindByName("Year"); MetasysObject Hour = weatherForecast.FindByName("Hour"); MetasysObject Minute = weatherForecast.FindByName("Minute"); MetasysObject Temperature = weatherForecast.FindByName("Temperature"); MetasysObject Humidity = weatherForecast.FindByName("Humidity"); MetasysObject Rain = weatherForecast.FindByName("Rain"); MetasysObject Snow = weatherForecast.FindByName("Snow"); // Use commands to write the results string adjustCommand = "Adjust"; var date = OpenWeatherMapClient.UnixTimeStampToDateTime(forecast.dt); metasysClient.SendCommand(Day.Id, adjustCommand, new List <object> { date.Day }); metasysClient.SendCommand(Month.Id, adjustCommand, new List <object> { date.Month }); metasysClient.SendCommand(Year.Id, adjustCommand, new List <object> { date.Year }); metasysClient.SendCommand(Hour.Id, adjustCommand, new List <object> { date.Hour }); metasysClient.SendCommand(Minute.Id, adjustCommand, new List <object> { date.Minute }); metasysClient.SendCommand(Temperature.Id, adjustCommand, new List <object> { forecast.main.temp }); metasysClient.SendCommand(Humidity.Id, adjustCommand, new List <object> { forecast.main.humidity }); if (forecast.rain != null) { metasysClient.SendCommand(Rain.Id, adjustCommand, new List <object> { forecast.rain.ThreeHours }); } else { // Reset values in case there is no rain metasysClient.SendCommand(Rain.Id, adjustCommand, new List <object> { 0 }); } if (forecast.snow != null) { metasysClient.SendCommand(Snow.Id, adjustCommand, new List <object> { forecast.snow.ThreeHours }); } else { // Reset values in case there is no snow metasysClient.SendCommand(Rain.Id, adjustCommand, new List <object> { 0 }); } }