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 SendCommands(Guid objectId, List <Command> commands) { /* SNIPPET 2: START */ Command adjust = commands.FindById("adjust"); Command operatorOverride = commands.FindById("operatorOverride"); Command release = commands.FindById("release"); Console.WriteLine(release); /* * { * "Title": "Release", * "TitleEnumerationKey": "commandIdEnumSet.releaseCommand", * "CommandId": "Release", * "Items": [ * { * "Title": "oneOf", * "Type": "enum", * "EnumerationValues": [ * { * "Title": "Present Value", * "TitleEnumerationKey": "attributeEnumSet.presentValue" * } * ], * "Minimum": 1.0, * "Maximum": 1.0 * }, * { * "Title": "oneOf", * "Type": "enum", * "EnumerationValues": [ * { * "Title": "0 (No Priority)", * "TitleEnumerationKey": "writePriorityEnumSet.priorityNone" * }, * { * "Title": "1 (Manual Life Safety)", * "TitleEnumerationKey": "writePriorityEnumSet.priorityManualEmergency" * }, * { * "Title": "2 (Auto Life Safety)", * "TitleEnumerationKey": "writePriorityEnumSet.priorityFireApplications" * }, * { * "Title": "3 (Application)", * "TitleEnumerationKey": "writePriorityEnumSet.priority3" * }, * { * "Title": "4 (Application)", * "TitleEnumerationKey": "writePriorityEnumSet.priority4" * }, * { * "Title": "5 (Critical Equipment)", * "TitleEnumerationKey": "writePriorityEnumSet.priorityCriticalEquipment" * }, * { * "Title": "6 (Minimum On Off)", * "TitleEnumerationKey": "writePriorityEnumSet.priorityMinimumOnOff" * }, * { * "Title": "7 (Heavy Equip Delay)", * "TitleEnumerationKey": "writePriorityEnumSet.priorityHeavyEquipDelay" * }, * { * "Title": "8 (Operator Override)", * "TitleEnumerationKey": "writePriorityEnumSet.priorityOperatorOverride" * }, * { * "Title": "9 (Application)", * "TitleEnumerationKey": "writePriorityEnumSet.priority9" * }, * { * "Title": "10 (Application)", * "TitleEnumerationKey": "writePriorityEnumSet.priority10" * }, * { * "Title": "11 (Demand Limiting)", * "TitleEnumerationKey": "writePriorityEnumSet.priorityDemandLimiting" * }, * { * "Title": "12 (Application)", * "TitleEnumerationKey": "writePriorityEnumSet.priority12" * }, * { * "Title": "13 (Load Rolling)", * "TitleEnumerationKey": "writePriorityEnumSet.priorityLoadRolling" * }, * { * "Title": "14 (Application)", * "TitleEnumerationKey": "writePriorityEnumSet.priority14" * }, * { * "Title": "15 (Scheduling)", * "TitleEnumerationKey": "writePriorityEnumSet.prioritySchedulingOst" * }, * { * "Title": "16 (Default)", * "TitleEnumerationKey": "writePriorityEnumSet.priorityDefault" * } * ], * "Minimum": 1.0, * "Maximum": 1.0 * } * ] * } */ var list1 = new List <object> { 70 }; client.SendCommand(objectId, adjust.CommandId, list1); var list2 = new List <object> { 75 }; client.SendCommand(objectId, operatorOverride.CommandId, list2); var list3 = new List <object> { "attributeEnumSet.presentValue", "writePriorityEnumSet.priorityNone" }; client.SendCommand(objectId, release.CommandId, list3); /* 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 }); } }