Ejemplo n.º 1
0
 private void GetSpaceEquipment_GetSpaceChildren(MetasysObject building)
 {
     /* SNIPPET 4: START */
     IEnumerable <MetasysObject> spaceEquipment = client.GetSpaceEquipment(building.Id);
     IEnumerable <MetasysObject> spaceChildren  = client.GetSpaceChildren(building.Id);
     MetasysObject sampleSpaceEquipment         = spaceEquipment.FirstOrDefault();
     /* SNIPPET 4: END */
 }
Ejemplo n.º 2
0
        private void GetNetworkDeviceTypes_GetNetworkDevices()
        {
            /* SNIPPET 1: START */
            List <MetasysObjectType> types = client.GetNetworkDeviceTypes().ToList();

            Console.WriteLine(types[0]);

            /*
             * {
             * "Description": "NAE55-NIE59",
             * "DescriptionEnumerationKey": "objectTypeEnumSet.n50Class",
             * "Id": 185
             * }
             */
            int type1 = types[0].Id;
            List <MetasysObject> devices = client.GetNetworkDevices(type1.ToString()).ToList();
            MetasysObject        device  = devices.LastOrDefault();

            Console.WriteLine(device);

            /*
             *  {
             *    "ItemReference": "Win2016-VM2:vNAE2343996",
             *    "Id": "142558f8-c4c7-5f89-be97-d806adb72053",
             *    "Name": "vNAE2343996",
             *    "Description": "",
             *    "Type": null,
             *    "TypeUrl": "https://win2016-vm2/api/v2/enumSets/508/members/185",
             *    "Category": null,
             *    "Children": [],
             *    "ChildrenCount": 0
             *  }
             */

            List <MetasysObject> devices2 = client.GetNetworkDevices(NetworkDeviceTypeEnum.SNC).ToList();
            MetasysObject        device2  = devices2.LastOrDefault();

            Console.WriteLine(device2);

            /*
             *  {
             *    "ItemReference": "WIN-21DJ9JV9QH6:EECMI-SNC-KNX",
             *    "Id": "69b3c2a5-1090-5418-afd9-5efc7186e42f",
             *    "Name": "EECMI-SNC-KNX",
             *    "Description": "",
             *    "Type": null,
             *    "TypeUrl": "https://win-21dj9jv9qh6/api/v3/enumSets/508/members/448",
             *    "ObjectType": null,
             *    "Category": null,
             *    "Children": [],
             *    "ChildrenCount": 0
             *  }
             */


            /* SNIPPET 1: END */
        }
        public void TestMetasysObjectEqual(ApiVersion version)
        {
            string obj, obj2;

            if (version < ApiVersion.v3)
            {
                obj = string.Concat("{",
                                    "\"id\": \"11111111-2222-3333-4444-555555555555\",",
                                    "\"itemReference\": \"fully:qualified/reference\",",
                                    "\"name\": \"name\",",
                                    "\"description\": \"description\",",
                                    $"\"typeUrl\": \"https://hostname/api/{version}/enumSets/508/members/197\"}}");
                obj2 = string.Concat("{",
                                     "\"id\": \"11111111-2222-3333-4444-555555555556\",",
                                     "\"itemReference\": \"fully:qualified/reference2\",",
                                     "\"name\": \"name2\",",
                                     "\"description\": \"description2\",",
                                     $"\"typeUrl\": \"https://hostname/api/{version}/enumSets/508/members/197\"}}");
            }
            else
            {
                obj = string.Concat("{",
                                    "\"id\": \"11111111-2222-3333-4444-555555555555\",",
                                    "\"itemReference\": \"fully:qualified/reference\",",
                                    "\"name\": \"name\",",
                                    "\"description\": \"description\",",
                                    $"\"type\": \"https://hostname/api/{version}/enumSets/508/members/197\"}}");
                obj2 = string.Concat("{",
                                     "\"id\": \"11111111-2222-3333-4444-555555555556\",",
                                     "\"itemReference\": \"fully:qualified/reference2\",",
                                     "\"name\": \"name2\",",
                                     "\"description\": \"description2\",",
                                     $"\"type\": \"https://hostname/api/{version}/enumSets/508/members/197\"}}");
            }
            string               objCopy   = obj.Clone().ToString();
            MetasysObject        child     = new MetasysObject(JToken.Parse(obj2), version, null, testCulture);
            MetasysObject        childCopy = new MetasysObject(JToken.Parse(obj2), version, null, testCulture);
            List <MetasysObject> childlist = new List <MetasysObject>()
            {
                child
            };
            List <MetasysObject> childlistCopy = new List <MetasysObject>()
            {
                childCopy
            };

            MetasysObject metObj     = new MetasysObject(JToken.Parse(obj), version, childlist, testCulture);
            MetasysObject metObjCopy = new MetasysObject(JToken.Parse(objCopy), version, childlistCopy, testCulture);

            Assert.AreEqual(metObj.GetHashCode(), metObjCopy.GetHashCode());
            Assert.AreEqual(metObj, metObjCopy);
            Assert.NotNull(metObj.ToString());
        }
Ejemplo n.º 4
0
        private void GetSpaces()
        {
            /* SNIPPET 1: START */
            // Retrieves the list of Buildings using SpaceTypeEnum helper
            List <MetasysObject> buildings = client.GetSpaces(SpaceTypeEnum.Building).ToList();
            MetasysObject        building  = buildings.LastOrDefault();

            Console.WriteLine(building);

            /*
             *  {
             *    "ItemReference": "Win2016-VM2:Win2016-VM2/JCI.Building 1",
             *    "Id": "164aaba2-0fb3-5b5d-bfe9-49cf6b797c93",
             *    "Name": "North America (BACnet)",
             *    "Description": null,
             *    "Type": 2,
             *    "TypeUrl": "https://win2016-vm2/api/v2/enumSets/1766/members/1",
             *    "Category": "Building",
             *    "Children": [],
             *    "ChildrenCount": 0
             *  }
             */
            // Retrieves all the spaces in a flat hierarchy
            List <MetasysObject> spaces     = client.GetSpaces().ToList();
            MetasysObject        firstSpace = spaces.FirstOrDefault();

            Console.WriteLine(firstSpace);

            /*
             *  {
             *    "ItemReference": "Win2016-VM2:Win2016-VM2/JCI.Building 1.Floor 1.Milwaukee.507 E Michigan Street Campus",
             *    "Id": "896ba096-db3c-5038-8505-636785906cca",
             *    "Name": "507 E Michigan Street Campus",
             *    "Description": null,
             *    "Type": 2,
             *    "TypeUrl": "https://win2016-vm2/api/v2/enumSets/1766/members/3",
             *    "Category": "Room",
             *    "Children": [],
             *    "ChildrenCount": 0
             *  }
             */
            /* SNIPPET 1: END */
        }
Ejemplo n.º 5
0
        private void GetEquipment()
        {
            /* SNIPPET 3: START */
            List <MetasysObject> equipment       = client.GetEquipment().ToList();
            MetasysObject        sampleEquipment = equipment.FirstOrDefault();

            Console.WriteLine(sampleEquipment);

            /*
             *   {
             *    "ItemReference": "Win2016-VM2:Win2016-VM2/equipment.vNAE2343947.Field Bus MSTP1.AHU-07",
             *    "Id": "6c6e18b8-015f-572a-814c-1e5d66142850",
             *    "Name": "AHU-07",
             *    "Description": null,
             *    "Type": 3,
             *    "TypeUrl": null,
             *    "Category": null,
             *    "Children": [],
             *    "ChildrenCount": 0
             *  }
             */
            /* SNIPPET 3: END */
        }
Ejemplo n.º 6
0
        private void GetEquipmentPoints(MetasysObject sampleEquipment)
        {
            /* SNIPPET 5: START */
            IEnumerable <MetasysPoint> equipmentPoints = client.GetEquipmentPoints(sampleEquipment.Id);
            MetasysPoint point        = equipmentPoints.FindByShortName("Analog Input-1");
            string       presentValue = point.PresentValue?.StringValue;

            Console.WriteLine(point);

            /*
             *  {
             *    "EquipmentName": "AHU-07",
             *    "ShortName": "CLG-O",
             *    "Label": "Cooling Output",
             *    "Category": "",
             *    "IsDisplayData": true,
             *    "ObjectId": "9dd107cf-e8dc-583a-9557-813395ae1971",
             *    "AttributeUrl": "https://win2016-vm2/api/v2/enumSets/509/members/85",
             *    "ObjectUrl": "https://win2016-vm2/api/v2/objects/9dd107cf-e8dc-583a-9557-813395ae1971",
             *    "PresentValue": {
             *      "StringValue": "0",
             *      "StringValueEnumerationKey": null,
             *      "NumericValue": 0.0,
             *      "BooleanValue": false,
             *      "ArrayValue": null,
             *      "Attribute": "presentValue",
             *      "Id": "9dd107cf-e8dc-583a-9557-813395ae1971",
             *      "Reliability": "Reliable",
             *      "ReliabilityEnumerationKey": "reliabilityEnumSet.reliable",
             *      "Priority": "0 (No Priority)",
             *      "PriorityEnumerationKey": "writePriorityEnumSet.priorityNone",
             *      "IsReliable": true
             *    }
             *  }
             */
            /* SNIPPET 5: END */
        }
Ejemplo n.º 7
0
 private void GetSingleAlarm_GetAlarmsForAnObject_GetAlarmsForNetworkDevice(Alarm alarm, Guid objectId, AlarmFilter alarmFilter, MetasysObject device)
 {
     /* SNIPPET 2: START */
     Alarm singleAlarm = client.Alarms.FindById(alarm.Id);
     PagedResult <Alarm> objectAlarms = client.Alarms.GetForObject(objectId, alarmFilter);
     PagedResult <Alarm> deviceAlarms = client.Alarms.GetForNetworkDevice(device.Id, alarmFilter);
     /* SNIPPET 2: END */
 }
Ejemplo n.º 8
0
        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 */
        }
Ejemplo n.º 9
0
        public void Run()
        {
            try
            {
                /**** Common vars shared between different snippets: modify the following input according to your testing server *****/
                Guid objectId = new Guid("8e3b3738-2f5f-494d-bde1-fac15da28c86");
                Guid id1      = objectId;
                Guid id2      = new Guid("01e025e8-0fb3-59da-a9b8-2f238c6f011c");

                // Common objects are mocked to do not waste precious roundtrip time on each snippet
                List <Command> commands = new List <Command> {
                    new Command {
                        CommandId = "Adjust"
                    }, new Command {
                        CommandId = "OperatorOverride"
                    }, new Command(), new Command(), new Command {
                        CommandId = "Release"
                    }
                };

                MetasysObject building = new MetasysObject {
                    Id = new Guid("2f321415-35d9-5c48-b643-60c5132cc001")
                };

                MetasysObject sampleEquipment = new MetasysObject {
                    Id = new Guid("0e8c034b-1586-5ed9-8455-dd33e99c2c7e")
                };

                MetasysObject device = new MetasysObject {
                    Id = new Guid("21c605fb-4755-5d65-8e9f-4fc8283b0366")
                };

                AlarmFilter alarmFilter = new AlarmFilter
                {
                    StartTime           = new DateTime(2020, 06, 01),
                    EndTime             = new DateTime(2020, 07, 10),
                    ExcludeAcknowledged = true
                };

                Alarm alarm = new Alarm {
                    Id = new Guid("f1a17676-f421-4649-80a5-b13935ae7404")
                };

                AuditFilter auditFilter = new AuditFilter
                {
                    StartTime          = new DateTime(2020, 06, 01),
                    EndTime            = new DateTime(2020, 06, 24),
                    OriginApplications = OriginApplicationsEnum.SystemSecurity | OriginApplicationsEnum.AuditTrails,
                    ActionTypes        = ActionTypeEnum.Subsystem | ActionTypeEnum.Command
                };

                Audit audit = new Audit {
                    Id = new Guid("f798591b-e6d6-441f-8f7b-596fc2e6c003")
                };

                Audit auditForAnnotation = new Audit {
                    Id = new Guid("f179a59c-ce36-47e1-afa6-f873b80259ed")
                };

                /********************************************************************************************************************/
                var option = "";
                while (option != "99")
                {
                    PrintMenu();
                    Console.Write("\r\nSelect an option: ");
                    option = Console.ReadLine();
                    Console.WriteLine();
                    switch (option)
                    {
                    case "1":
                        TryLogin_Refresh();
                        break;

                    case "2":
                        GetObjectIdentifier();
                        break;

                    case "3":
                        ReadProperty(objectId);
                        break;

                    case "4":
                        ReadPropertyMultiple(id1, id2);
                        break;

                    case "5":
                        WriteProperty();
                        break;

                    case "6":
                        WritePropertyMultiple(id1, id2);
                        break;

                    case "7":
                        GetCommands(objectId);
                        break;

                    case "8":
                        SendCommands(objectId, commands);
                        break;

                    case "9":
                        GetNetworkDeviceTypes_GetNetworkDevices();
                        break;

                    case "10":
                        GetObjects();
                        break;

                    case "11":
                        Localize();
                        break;

                    case "12":
                        GetSpaces();
                        break;

                    case "13":
                        GetSpaceTypes();
                        break;

                    case "14":
                        GetEquipment();
                        break;

                    case "15":
                        GetSpaceEquipment_GetSpaceChildren(building);
                        break;

                    case "16":
                        GetEquipmentPoints(sampleEquipment);
                        break;

                    case "17":
                        GetAlarms();
                        break;

                    case "18":
                        GetSingleAlarm_GetAlarmsForAnObject_GetAlarmsForNetworkDevice(alarm, objectId, alarmFilter, device);
                        break;

                    case "19":
                        GetAlarmsAnnotation(alarm);
                        break;

                    case "20":
                        GetTrendedAttributes_GetSamples();
                        break;

                    case "21":
                        GetAudits();
                        break;

                    case "22":
                        GetSingleAudit_GetAuditsForAnObject(audit, objectId, auditFilter);
                        break;

                    case "23":
                        GetAuditsAnnotation(audit);
                        break;

                    case "24":
                        DiscardSingleAudit(audit);
                        break;

                    case "25":
                        ChangeApiVersion();
                        break;

                    case "26":
                        ChangeHostname();
                        break;

                    case "27":
                        AddAuditAnnotation(auditForAnnotation);
                        break;

                    case "28":
                        AddAuditAnnotationMultiple();
                        break;

                    case "29":
                        DiscardAuditMultiple();
                        break;

                    case "30":
                        GetServerTime();
                        break;

                    case "99":
                        return;     // Exit from JSON output demo
                    }
                    Console.WriteLine();
                    Console.WriteLine("Press Enter to return to the JSON output menu...");
                    Console.ReadLine();
                    Console.WriteLine();
                }
            }
            catch (Exception exception)
            {
                log.Logger.Error(string.Format("{0}", exception.Message));
                Console.WriteLine("\n \nAn Error occurred. Press Enter to return to Main Menu");
                Console.ReadLine();
            }
        }
Ejemplo n.º 10
0
        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();
        }
        public void TestMetasysObjectDoesNotEqual(ApiVersion version)
        {
            string obj, obj2, obj3;

            if (version < ApiVersion.v3)
            {
                obj = string.Concat("{",
                                    "\"id\": \"11111111-2222-3333-4444-555555555555\",",
                                    "\"itemReference\": \"fully:qualified/reference\",",
                                    "\"name\": \"name\",",
                                    "\"description\": \"description\",",
                                    $"\"typeUrl\": \"https://hostname/api/{version}/enumSets/508/members/197\"}}");
                obj2 = string.Concat("{",
                                     "\"id\": \"11111111-2222-3333-4444-555555555556\",",
                                     "\"itemReference\": \"fully:qualified/reference2\",",
                                     "\"name\": \"name2\",",
                                     "\"description\": \"description2\",",
                                     $"\"typeUrl\": \"https://hostname/api/{version}/enumSets/508/members/197\"}}");
                obj3 = string.Concat("{",
                                     "\"id\": \"11111111-2222-3333-4444-555555555557\",",
                                     "\"itemReference\": \"fully:qualified/reference3\",",
                                     "\"name\": \"name3\",",
                                     "\"description\": \"description3\",",
                                     $"\"typeUrl\": \"https://hostname/api/{version}/enumSets/508/members/197\"}}");
            }
            else
            {
                obj = string.Concat("{",
                                    "\"id\": \"11111111-2222-3333-4444-555555555555\",",
                                    "\"itemReference\": \"fully:qualified/reference\",",
                                    "\"name\": \"name\",",
                                    "\"description\": \"description\",",
                                    $"\"type\": \"https://hostname/api/{version}/enumSets/508/members/197\"}}");
                obj2 = string.Concat("{",
                                     "\"id\": \"11111111-2222-3333-4444-555555555556\",",
                                     "\"itemReference\": \"fully:qualified/reference2\",",
                                     "\"name\": \"name2\",",
                                     "\"description\": \"description2\",",
                                     $"\"type\": \"https://hostname/api/{version}/enumSets/508/members/197\"}}");
                obj3 = string.Concat("{",
                                     "\"id\": \"11111111-2222-3333-4444-555555555557\",",
                                     "\"itemReference\": \"fully:qualified/reference3\",",
                                     "\"name\": \"name3\",",
                                     "\"description\": \"description3\",",
                                     $"\"type\": \"https://hostname/api/{version}/enumSets/508/members/197\"}}");
            }
            MetasysObject        child     = new MetasysObject(JToken.Parse(obj3), version, null, testCulture);
            MetasysObject        child2    = new MetasysObject(JToken.Parse(obj3), version, null, testCulture);
            List <MetasysObject> childlist = new List <MetasysObject>()
            {
                child
            };
            List <MetasysObject> childlist2 = new List <MetasysObject>()
            {
                child2
            };

            MetasysObject metObj  = new MetasysObject(JToken.Parse(obj), version, childlist, testCulture);
            MetasysObject metObj2 = new MetasysObject(JToken.Parse(obj2), version, childlist2, testCulture);
            MetasysObject metObj3 = new MetasysObject(JToken.Parse(obj), version, null, testCulture);
            MetasysObject metObj4 = new MetasysObject(JToken.Parse(obj2), version, null, testCulture);

            Assert.AreNotEqual(metObj.GetHashCode(), metObj2.GetHashCode());
            Assert.AreNotEqual(metObj, metObj2);
            Assert.AreNotEqual(metObj3.GetHashCode(), metObj4.GetHashCode());
            Assert.AreNotEqual(metObj3, metObj4);
        }
Ejemplo n.º 12
0
        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
                });
            }
        }