public void TestWoopsaObjects()
        {
            WoopsaRoot   root    = new WoopsaRoot();
            WoopsaObject tunnel1 = new WoopsaObject(root, "Tunnel1");

            Assert.AreEqual(root.Items.Count(), 1);
            WoopsaObject tunnel2 = new WoopsaObject(root, "Tunnel2");

            Assert.AreEqual(root.Items.Count(), 2);
            WoopsaObject coMessung1 = new WoopsaObject(tunnel1, "CoMessung1");

            Assert.AreEqual(coMessung1.GetPath(), "/Tunnel1/CoMessung1");

            WoopsaProperty property1 = new WoopsaProperty(coMessung1, "Level", WoopsaValueType.Real,
                                                          (sender) => 1040.0);
            int            property2Value = 0;
            WoopsaProperty property2      = new WoopsaProperty(coMessung1, "Variation", WoopsaValueType.Real,
                                                               (sender) => property2Value, (sender, value) => property2Value = value.ToInt32());

            Assert.AreEqual(coMessung1.Properties.Count(), 2);
            Assert.AreEqual(coMessung1.Properties.First().Value.ToDouble(), 1040.0);
            coMessung1.Properties.ByName("Variation").Value = 45;
            Assert.AreEqual(coMessung1.Properties.ByName("Variation").Value.ToInt32(), 45);
            Assert.AreEqual(coMessung1.Properties.ByName("Variation").Value.ToString(), "45");
            (coMessung1.ByName("Variation") as IWoopsaProperty).Value = (WoopsaValue)36;
            Assert.AreEqual(coMessung1.Properties.ByName("Variation").Value.ToInt32(), 36);
            coMessung1.Properties["Variation"].Value = 5;
            Assert.AreEqual(property2Value, 5);
            int variation = coMessung1.Properties["Variation"].Value;

            Assert.AreEqual(variation, 5);
            WoopsaMethod method1 = new WoopsaMethod(coMessung1, "Calibrate", WoopsaValueType.Null,
                                                    new WoopsaMethodArgumentInfo[] {
                new WoopsaMethodArgumentInfo("minLevel", WoopsaValueType.Real),
                new WoopsaMethodArgumentInfo("maxLevel", WoopsaValueType.Real)
            },
                                                    Calibrate);
            IWoopsaValue result = method1.Invoke(1.1, 5.5);

            Assert.AreEqual(result, WoopsaValue.Null);
            Assert.AreEqual(_minLevel, 1.1);
            Assert.AreEqual(_maxLevel, 5.5);
        }
        public void TestWoopsaObjects()
        {
            WoopsaRoot root = new WoopsaRoot();
            WoopsaObject tunnel1 = new WoopsaObject(root, "Tunnel1");

            Assert.AreEqual(root.Items.Count(), 1);
            WoopsaObject tunnel2 = new WoopsaObject(root, "Tunnel2");
            Assert.AreEqual(root.Items.Count(), 2);
            WoopsaObject coMessung1 = new WoopsaObject(tunnel1, "CoMessung1");
            Assert.AreEqual(coMessung1.GetPath(), "/Tunnel1/CoMessung1");

            WoopsaProperty property1 = new WoopsaProperty(coMessung1, "Level", WoopsaValueType.Real,
                (sender) => 1040.0);
            int property2Value = 0;
            WoopsaProperty property2 = new WoopsaProperty(coMessung1, "Variation", WoopsaValueType.Real,
                (sender) => property2Value, (sender, value) => property2Value = value.ToInt32());

            Assert.AreEqual(coMessung1.Properties.Count(), 2);
            Assert.AreEqual(coMessung1.Properties.First().Value.ToDouble(), 1040.0);
            coMessung1.Properties.ByName("Variation").Value = 45;
            Assert.AreEqual(coMessung1.Properties.ByName("Variation").Value.ToInt32(), 45);
            Assert.AreEqual(coMessung1.Properties.ByName("Variation").Value.ToString(), "45");
            (coMessung1.ByName("Variation") as IWoopsaProperty).Value = (WoopsaValue)36;
            Assert.AreEqual(coMessung1.Properties.ByName("Variation").Value.ToInt32(), 36);
            coMessung1.Properties["Variation"].Value = 5;
            Assert.AreEqual(property2Value, 5);
            int variation = coMessung1.Properties["Variation"].Value;
            Assert.AreEqual(variation, 5);
            WoopsaMethod method1 = new WoopsaMethod(coMessung1, "Calibrate", WoopsaValueType.Null,
                new WoopsaMethodArgumentInfo[] {
                    new WoopsaMethodArgumentInfo("minLevel", WoopsaValueType.Real),
                    new WoopsaMethodArgumentInfo("maxLevel", WoopsaValueType.Real)
                },
                Calibrate);
            IWoopsaValue result = method1.Invoke(1.1, 5.5);
            Assert.AreEqual(result, WoopsaValue.Null);
            Assert.AreEqual(_minLevel, 1.1);
            Assert.AreEqual(_maxLevel, 5.5);
        }
Exemple #3
0
        public void TestWoopsaObjectAdapterExposedType()
        {
            // TODO : Cleanup what is redundant with TestWoopsaObjectAdapter
            ClassA a = new ClassA();
            WoopsaObjectAdapter adapterA1 = new WoopsaObjectAdapter(null, "a", a, typeof(ClassA));

            Assert.IsNotNull(adapterA1.Properties.ByNameOrNull(nameof(a.APropertyBool)));
            Assert.AreEqual(adapterA1.Properties.ByNameOrNull(nameof(a.APropertyBool)).Value.Type, WoopsaValueType.Logical);
            Assert.IsFalse(adapterA1.Properties.ByNameOrNull(nameof(a.APropertyBool)).Value.ToBool());
            Assert.IsNull(adapterA1.Methods.ByNameOrNull(nameof(ClassA.ToString)));
            adapterA1.Properties.ByNameOrNull(nameof(a.APropertyBool)).Value = new WoopsaValue(true);
            Assert.IsTrue(a.APropertyBool);
            Assert.IsTrue(adapterA1.Properties.ByNameOrNull(nameof(a.APropertyBool)).Value.ToBool());
            Assert.IsNull(adapterA1.Properties.ByNameOrNull(nameof(a.APropertyBool)).Value.TimeStamp);
            Assert.IsNull(adapterA1.Properties.ByNameOrNull(nameof(a.APropertyDateTime)));
            Assert.IsNotNull(adapterA1.Properties.ByNameOrNull(nameof(a.APropertyDateTime2)));
            Assert.IsNotNull(adapterA1.Items.ByNameOrNull("Inner1"));
            IWoopsaObject inner1 = adapterA1.Items.ByName("Inner1") as IWoopsaObject;

            Assert.IsNotNull(inner1);
            Assert.IsNotNull(inner1.Properties.ByNameOrNull(nameof(ClassAInner1.APropertyInt)));
            Assert.IsNull(inner1.Properties.ByNameOrNull(nameof(ClassAInner1.APropertyIntHidden)));
            inner1.Properties.ByNameOrNull(nameof(ClassAInner1.APropertyInt)).Value = new WoopsaValue(5);
            Assert.AreEqual(a.Inner1.APropertyInt, 5);
            a.Inner1.APropertyInt = 12;
            Assert.AreEqual(inner1.Properties.ByNameOrNull(nameof(ClassAInner1.APropertyInt)).Value, 12);
            Assert.IsNull(inner1.Methods.ByNameOrNull(nameof(ClassAInner1.ToString)));

            // dynamic object change with polymorphism
            a.Inner1 = new ClassAInner1()
            {
                APropertyInt = 123, APropertyIntHidden = 0
            };
            Assert.AreEqual(inner1.Properties.ByName(nameof(ClassAInner1.APropertyInt)).Value, 123);
            Assert.IsNull(inner1.Properties.ByNameOrNull(nameof(SubClassAInner1.ExtraProperty)));
            a.Inner1 = new SubClassAInner1()
            {
                APropertyInt       = 123,
                APropertyIntHidden = 0,
                ExtraProperty      = 555
            };
            // Should not find this property, as we are using declared type instaed of actual type
            Assert.IsNull(inner1.Properties.ByNameOrNull(nameof(SubClassAInner1.ExtraProperty)));

            WoopsaObjectAdapter adapterA1All = new WoopsaObjectAdapter(null, "a", a, null, null,
                                                                       WoopsaObjectAdapterOptions.None, WoopsaVisibility.All);

            Assert.IsNotNull(adapterA1All.Methods.ByNameOrNull(nameof(ClassA.ToString)));
            IWoopsaObject inner1All = adapterA1All.Items.ByName("Inner1") as IWoopsaObject;

            Assert.IsNotNull(inner1All.Methods.ByNameOrNull(nameof(ClassAInner1.ToString)));



            WoopsaObjectAdapter adapterA2 = new WoopsaObjectAdapter(null, "a", a, null, null,
                                                                    WoopsaObjectAdapterOptions.SendTimestamps);

            Assert.IsNotNull(adapterA2.Properties.ByNameOrNull(nameof(a.APropertyBool)).Value.TimeStamp);

            WoopsaObjectAdapter adapterA3 = new WoopsaObjectAdapter(null, "a", a, null, null,
                                                                    WoopsaObjectAdapterOptions.None);

            Assert.IsNotNull(adapterA1.Properties.ByNameOrNull(nameof(a.APropertyBool)));

            ClassB b = new ClassB();
            WoopsaObjectAdapter adapterB = new WoopsaObjectAdapter(null, "b", b, null, null,
                                                                   WoopsaObjectAdapterOptions.None, WoopsaVisibility.DefaultIsVisible | WoopsaVisibility.MethodSpecialName |
                                                                   WoopsaVisibility.Inherited);

            Assert.IsNotNull(adapterB.Methods.ByNameOrNull("get_" + nameof(ClassB.APropertyBool)));
            Assert.IsNotNull(adapterB.Properties.ByNameOrNull(nameof(b.APropertyBool)));

            ClassC c = new ClassC();
            WoopsaObjectAdapter adapterC = new WoopsaObjectAdapter(null, "c", c);

            Assert.IsNull(adapterC.Properties.ByNameOrNull(nameof(c.APropertyBool)));
            Assert.IsNotNull(adapterC.Properties.ByNameOrNull(nameof(c.APropertyTimeSpan)));
            Assert.IsNull(adapterC.Properties.ByNameOrNull("APropertyDouble2"));
            Assert.IsNotNull(adapterC.Properties.ByNameOrNull(nameof(c.APropertyText)));
            IWoopsaProperty propertyText = adapterC.Properties.ByNameOrNull(nameof(c.APropertyText));

            Assert.AreEqual(propertyText.Type, WoopsaValueType.Integer);
            c.APropertyText = "123";
            Assert.AreEqual(propertyText.Value.ToInt64(), 123);
            // Json data
            Assert.IsNotNull(adapterC.Properties.ByNameOrNull(nameof(c.APropertyJson)));
            IWoopsaProperty propertyJson = adapterC.Properties.ByNameOrNull(nameof(c.APropertyJson));

            Assert.AreEqual(propertyJson.Type, WoopsaValueType.JsonData);
            // JSon structure
            c.APropertyJson = "{ \"x\" : 8, \"y\": 9 }";
            Assert.IsTrue(propertyJson.Value is WoopsaValue);
            WoopsaValue jsonValue = (WoopsaValue)propertyJson.Value;

            Assert.IsNotNull(jsonValue.JsonData);
            Assert.AreEqual(jsonValue.JsonData["x"].ToInt64(), 8);
            Assert.AreEqual(jsonValue.JsonData["y"].ToInt64(), 9);
            // JSon array
            c.APropertyJson = "{ \"a\" : [11, 12, 13] }";
            Assert.IsTrue(propertyJson.Value is WoopsaValue);
            jsonValue = (WoopsaValue)propertyJson.Value;
            Assert.IsNotNull(jsonValue.JsonData);
            Assert.AreEqual(jsonValue.JsonData["a"][0].ToInt64(), 11);
            Assert.AreEqual(jsonValue.JsonData["a"][1].ToInt64(), 12);
            Assert.AreEqual(jsonValue.JsonData["a"][2].ToInt64(), 13);

            ClassD[]            array = new ClassD[] { new ClassD(4), new ClassD(3), new ClassD(2) };
            WoopsaObjectAdapter adapterArrayObject = new WoopsaObjectAdapter(null, "array", array, null, null,
                                                                             WoopsaObjectAdapterOptions.None,
                                                                             WoopsaVisibility.IEnumerableObject | WoopsaVisibility.DefaultIsVisible);

            Assert.IsNotNull(adapterArrayObject.Items.ByNameOrNull(WoopsaObjectAdapter.EnumerableItemDefaultName(1)));
            Assert.IsNotNull(adapterArrayObject.Items.ByNameOrNull(WoopsaObjectAdapter.EnumerableItemDefaultName(1)) as IWoopsaObject);
            IWoopsaObject item1 = (IWoopsaObject)adapterArrayObject.Items.ByNameOrNull(WoopsaObjectAdapter.EnumerableItemDefaultName(1));

            Assert.IsNotNull(item1.Properties.ByNameOrNull(nameof(ClassD.APropertyInt)));
            Assert.AreEqual(item1.Properties.ByNameOrNull(nameof(ClassD.APropertyInt)).Value.ToInt64(), 3);
            item1.Properties.ByNameOrNull(nameof(ClassD.APropertyInt)).Value = new WoopsaValue(5, DateTime.Now);
            Assert.AreEqual(array[1].APropertyInt, 5);
            Assert.AreEqual(item1.Properties.ByNameOrNull(nameof(ClassD.APropertyInt)).Value.ToInt64(), 5);

            int[] dataArray = new int[] { 3, 4, 5 };
            WoopsaObjectAdapter adapterArrayValue = new WoopsaObjectAdapter(null, "array", dataArray, null, null,
                                                                            WoopsaObjectAdapterOptions.None,
                                                                            WoopsaVisibility.IEnumerableObject | WoopsaVisibility.DefaultIsVisible);
            WoopsaMethod methodGet = adapterArrayValue.Methods.ByNameOrNull("Get");

            Assert.IsNotNull(methodGet);
            int dataItem1 = methodGet.Invoke(1);

            Assert.AreEqual(dataItem1, dataArray[1]);
            WoopsaMethod methodSet = adapterArrayValue.Methods.ByNameOrNull("Set");

            Assert.IsNotNull(methodSet);
            methodSet.Invoke(1, 7);
            dataItem1 = methodGet.Invoke(1);
            Assert.AreEqual(dataArray[1], 7);
            Assert.AreEqual(dataItem1, 7);
        }
        public void TestWoopsaWaitNotification()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl))
                {
                    WoopsaBoundClientObject root = client.CreateBoundRoot();
                    // Just to show how to see all items
                    foreach (var item in root.Items)
                    {
                        Console.WriteLine("Item = " + item.Name);
                        if (item.Name == "SubscriptionService")
                        {
                            Console.WriteLine("Trouvé");
                        }
                    }

                    // create a subscription object
                    WoopsaObject subscription = root.Items.ByNameOrNull("SubscriptionService") as WoopsaObject;
                    if (subscription != null)
                    {
                        int          result = 0;
                        WoopsaMethod methodCreateScubscriptionChannel = subscription.Methods.ByNameOrNull("CreateSubscriptionChannel");
                        if (methodCreateScubscriptionChannel != null)
                        {
                            // call the method "CreateSubscriptionChannel" on the server
                            result = methodCreateScubscriptionChannel.Invoke(1000);   // define the queue size
                        }
                        int channel = result;

                        WoopsaMethod methodRegisterScubscription = subscription.Methods.ByNameOrNull("RegisterSubscription");
                        if (methodRegisterScubscription != null)
                        {
                            // call the method "registerScubscription" on the server
                            result = methodRegisterScubscription.Invoke(channel, WoopsaValue.WoopsaRelativeLink("/Votes"), 0.01, 0.01);
                        }
                        int subscriptionNbr = result;

                        WoopsaJsonData jData;
                        WoopsaMethod   methodWaitNotification = subscription.Methods.ByNameOrNull("WaitNotification");
                        if (methodWaitNotification != null)
                        {
                            Stopwatch watch = new Stopwatch();
                            watch.Start();
                            // call the method "WaitNotification" on the server
                            Thread.Sleep(100);
                            jData = methodWaitNotification.Invoke(channel, 0).JsonData;
                            Assert.IsTrue(jData.Length > 0);
                            int lastNotification;
                            lastNotification = jData[0]["Id"];
                            Assert.AreEqual(lastNotification, 1);
                            // Get notifications again
                            Thread.Sleep(100);
                            jData = methodWaitNotification.Invoke(channel, 0).JsonData;
                            Assert.IsTrue(jData.Length > 0);
                            lastNotification = jData[0]["Id"];
                            Assert.AreEqual(lastNotification, 1);
                        }
                    }
                }
            }
        }