Ejemplo n.º 1
0
        public void TestWoopsaValueCreateChecked()
        {
            WoopsaValue dateTime = WoopsaValue.CreateChecked("2015-03-23T14:15:01Z", WoopsaValueType.DateTime);

            Assert.AreEqual(dateTime.ToDateTime(), new DateTime(2015, 3, 23, 14, 15, 1, DateTimeKind.Utc));
            WoopsaValue integer = WoopsaValue.CreateChecked("123", WoopsaValueType.Integer);

            Assert.AreEqual(integer.ToInt64(), 123);
            WoopsaValue jsonData = WoopsaValue.CreateChecked("{ \"x\": 123 }", WoopsaValueType.JsonData);
            WoopsaValue logical  = WoopsaValue.CreateChecked("true", WoopsaValueType.Logical);

            Assert.AreEqual(logical.ToBool(), true);
            WoopsaValue woopsaNull = WoopsaValue.CreateChecked(null, WoopsaValueType.Null);

            Assert.IsTrue(woopsaNull.IsNull());
            WoopsaValue real = WoopsaValue.CreateChecked("1.25", WoopsaValueType.Real);

            Assert.AreEqual(real.ToDouble(), 1.25);
            Assert.IsFalse(real.IsNull());
            WoopsaValue resourceUrl = WoopsaValue.CreateChecked("http://www.woopsa.org/logo.png",
                                                                WoopsaValueType.ResourceUrl);
            const string sentence = "Don't worry, be happy";
            WoopsaValue  text     = WoopsaValue.CreateChecked(sentence, WoopsaValueType.Text);

            Assert.AreEqual(text.AsText, sentence);
            Assert.AreEqual(text.ToString(), sentence);
            WoopsaValue timeSpan = WoopsaValue.CreateChecked("0.001", WoopsaValueType.TimeSpan);

            Assert.AreEqual(timeSpan.ToTimeSpan(), TimeSpan.FromMilliseconds(1));
            WoopsaValue timeSpan2 = WoopsaValue.CreateChecked("3600", WoopsaValueType.TimeSpan);

            Assert.AreEqual(timeSpan2.ToTimeSpan(), TimeSpan.FromHours(1));
            WoopsaValue woopsaLink = WoopsaValue.CreateChecked("http://demo.woopsa.org/weather",
                                                               WoopsaValueType.WoopsaLink);
        }
Ejemplo n.º 2
0
        public void TestWoopsaValueExtended()
        {
            string      woopsaServer, woopsaItemPath;
            WoopsaValue v = WoopsaValue.WoopsaAbsoluteLink("http://woopsa.demo.org/", "/tunnel1/luftung");

            v.DecodeWoopsaLink(out woopsaServer, out woopsaItemPath);
            Assert.AreEqual(woopsaServer, "http://woopsa.demo.org");
            Assert.AreEqual(woopsaItemPath, "tunnel1/luftung");
            Assert.AreEqual(v.Type, WoopsaValueType.WoopsaLink);
            v = WoopsaValue.WoopsaRelativeLink("/tunnel2/luftung");
            v.DecodeWoopsaLink(out woopsaServer, out woopsaItemPath);
            Assert.AreEqual(woopsaServer, null);
            Assert.AreEqual(woopsaItemPath, "tunnel2/luftung");
            Assert.AreEqual(v.Type, WoopsaValueType.WoopsaLink);
            v = WoopsaValue.WoopsaResourceUrl("http://www.woopsa.org/logo.png");
            Assert.AreEqual(v.AsText, "http://www.woopsa.org/logo.png");
            Assert.AreEqual(v.Type, WoopsaValueType.ResourceUrl);
            v = WoopsaValue.WoopsaJsonData("{\"Name\":\"Switzerland\" , \"Year\":1291}");
            Assert.IsNotNull(v.JsonData);
            Assert.AreEqual(v.JsonData["Name"].ToString(), "Switzerland");
            Assert.AreEqual(v.JsonData["Year"].ToInt16(), 1291);
            Assert.AreEqual(v.JsonData["Year"].ToInt32(), 1291);
            Assert.AreEqual(v.JsonData["Year"].ToInt64(), 1291);
            Assert.AreEqual(v.Type, WoopsaValueType.JsonData);
        }
Ejemplo n.º 3
0
        public void TestWoopsaValuePerformance()
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();
            for (int i = 0; i < 10000; i++)
            {
                WoopsaValue value = new WoopsaValue(3.14);
            }
            watch.Stop();
            Assert.AreEqual(watch.Elapsed < TimeSpan.FromMilliseconds(500), true);
        }
Ejemplo n.º 4
0
        public void TestWoopsaValueInteger()
        {
            WoopsaValue v1 = 123;
            WoopsaValue v2 = 123;
            int         i1 = v1;

            Assert.AreEqual(v1.Type, WoopsaValueType.Integer);
            Assert.AreEqual(v1.AsText, "123");
            Assert.AreEqual(v1, v2);
            Assert.AreEqual(v1, 123);
            Assert.AreEqual(i1, 123);
        }
Ejemplo n.º 5
0
        public void TestWoopsaValueLogicalFalse()
        {
            WoopsaValue v1 = false;
            WoopsaValue v2 = false;
            bool        b1 = v1;

            Assert.AreEqual(v1.Type, WoopsaValueType.Logical);
            Assert.AreEqual(v1.AsText, WoopsaConst.WoopsaFalse);
            Assert.AreEqual(v1, v2);
            Assert.AreEqual(v1, false);
            Assert.AreEqual(b1, false);
        }
Ejemplo n.º 6
0
        public void TestWoopsaValueLogicalTrue()
        {
            WoopsaValue v1 = true;
            WoopsaValue v2 = true;
            bool        b1 = v1;

            Assert.AreEqual(v1.Type, WoopsaValueType.Logical);
            Assert.AreEqual(v1.AsText, WoopsaConst.WoopsaTrue);
            Assert.AreEqual(v1, v2);
            Assert.AreEqual(v1, true);
            Assert.AreEqual(b1, true);
        }
Ejemplo n.º 7
0
        public void TestWoopsaSerializationPerformance()
        {
            Stopwatch   watch = new Stopwatch();
            WoopsaValue v     = 3.14;

            watch.Start();
            for (int i = 0; i < 1000000; i++)
            {
                v.Serialize();
            }
            watch.Stop();
            Assert.AreEqual(watch.Elapsed < TimeSpan.FromMilliseconds(1000), true);
        }
Ejemplo n.º 8
0
        public void TestWoopsaValueDateTimeTimeSpan()
        {
            WoopsaValue v1 = new DateTime(1972, 11, 1, 10, 11, 12, 13, DateTimeKind.Utc);
            DateTime    t1 = v1;
            WoopsaValue v2 = TimeSpan.FromSeconds(1.234);
            TimeSpan    t2 = v2;

            Assert.AreEqual(v1.Type, WoopsaValueType.DateTime);
            Assert.IsTrue(v1 == t1);
            Assert.IsTrue(t1 == v1);
            Assert.AreEqual(v2.Type, WoopsaValueType.TimeSpan);
            Assert.AreEqual(v1.ToDateTime(), t1);
            Assert.IsTrue(v2 == t2);
            Assert.IsTrue(t2 == v2);
            Assert.AreEqual(v2.AsText, "1.234");
        }
        public void TestWoopsaDynamicNotificationUnexistingProperty()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                // Solution with dynamic client
                using (dynamic dynamicClient = new WoopsaDynamicClient(TestingUrl))
                {
                    int channel = dynamicClient.SubscriptionService.CreateSubscriptionChannel(QUEUE_SIZE);
                    // Subscription for an nonexistent variable (should work)
                    dynamicClient.SubscriptionService.RegisterSubscription(channel,
                                                                           WoopsaValue.WoopsaRelativeLink("/Vote"), TimeSpan.FromMilliseconds(MONITOR_INTERVAL),
                                                                           TimeSpan.FromMilliseconds(PUBLISH_INTERVAL));

                    Stopwatch      watch = new Stopwatch();
                    WoopsaValue    lastNotifications;
                    WoopsaJsonData jsonData;
                    int            lastNotificationId;
                    watch.Start();
                    do
                    {
                        lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                        Assert.AreEqual(lastNotifications.Type, WoopsaValueType.JsonData);
                        jsonData = lastNotifications.JsonData;
                        if (watch.ElapsedMilliseconds > 1000)
                        {
                            Assert.Fail("Timeout without receveiving any notification");
                        }
                    }while (jsonData.Length == 0);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 1);
                    // Get again the same notification
                    lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                    jsonData          = lastNotifications.JsonData;
                    Assert.IsTrue(jsonData.IsArray);
                    Assert.AreEqual(jsonData.Length, 1);
                    Assert.IsTrue(jsonData[0].IsDictionary);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 1);
                }
            }
        } //end TestWoopsaDynamicNotification
Ejemplo n.º 10
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);
                        }
                    }
                }
            }
        }
        public void TestWoopsaDynamicNotification()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                // Solution with dynamic client
                using (dynamic dynamicClient = new WoopsaDynamicClient(TestingUrl))
                {
                    int channel = dynamicClient.SubscriptionService.CreateSubscriptionChannel(QUEUE_SIZE);
                    // Subscription for a valid variable
                    dynamicClient.SubscriptionService.RegisterSubscription(channel,
                                                                           WoopsaValue.WoopsaRelativeLink("/Votes"), TimeSpan.FromMilliseconds(MONITOR_INTERVAL),
                                                                           TimeSpan.FromMilliseconds(PUBLISH_INTERVAL));

                    Stopwatch      watch = new Stopwatch();
                    WoopsaValue    lastNotifications;
                    WoopsaJsonData jsonData;
                    int            lastNotificationId;
                    watch.Start();
                    do
                    {
                        lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                        Assert.AreEqual(lastNotifications.Type, WoopsaValueType.JsonData);
                        jsonData = lastNotifications.JsonData;
                        if (watch.ElapsedMilliseconds > 1000)
                        {
                            Assert.Fail("Timeout without receveiving any notification");
                        }
                    }while (jsonData.Length == 0);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 1);
                    // Get again the same notification
                    lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                    jsonData          = lastNotifications.JsonData;
                    Assert.IsTrue(jsonData.IsArray);
                    Assert.AreEqual(jsonData.Length, 1);
                    Assert.IsTrue(jsonData[0].IsDictionary);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 1);
                    // Generate a new notification
                    objectServer.Votes++;
                    Thread.Sleep(PUBLISH_INTERVAL * 10);
                    // Check we have now 2
                    lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                    jsonData          = lastNotifications.JsonData;
                    Assert.IsTrue(jsonData.IsArray);
                    Assert.AreEqual(jsonData.Length, 2);
                    Assert.IsTrue(jsonData[0].IsDictionary);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 2);
                    // Check we can remove 1 and still have 1
                    lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 1);
                    jsonData          = lastNotifications.JsonData;
                    Assert.AreEqual(jsonData.Length, 1);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 2);
                    // Enable the code below to test the wait of the timeout when they are 0 notifications pending

                    /*
                     * // Check we can remove 1 and have 0. This takes 5 seconds (we wait the timeout)
                     * lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, lastNotificationId);
                     * jsonData = lastNotifications.JsonData;
                     * Assert.AreEqual(jsonData.Length, 0);
                     */
                }
            }
        } //end TestWoopsaDynamicNotification
Ejemplo n.º 13
0
        public WoopsaValue ReadAdsValue(IWoopsaProperty woopsaProperty)
        {
            WoopsaAdsProperty property = (WoopsaAdsProperty)woopsaProperty;
            AdsStream         stream   = new AdsStream(80); // for STRING(80)
            long data = 0;

            stream.Flush();
            try
            {
                _tcAds.Read(property.AdsInfo.IndexGroup, property.AdsInfo.IndexOffset, stream);
            }
            catch (Exception)
            {
                isAdsConnected = false;
                return(null);
            }
            switch (property.Type)
            {
            case WoopsaValueType.Integer:
                if (property.AdsInfo.Type == BeckhoffValueType.WORD ||
                    property.AdsInfo.Type == BeckhoffValueType.UINT)
                {
                    return(BitConverter.ToUInt16(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.DWORD ||
                         property.AdsInfo.Type == BeckhoffValueType.UDINT)
                {
                    return(BitConverter.ToUInt32(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.SINT)
                {
                    return(Convert.ToSByte((sbyte)stream.GetBuffer()[0]));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.INT)
                {
                    return(BitConverter.ToInt16(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.DINT)
                {
                    return(BitConverter.ToInt32(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.LINT || property.AdsInfo.Type == BeckhoffValueType.ULINT)
                {
                    return(BitConverter.ToInt64(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.USINT || property.AdsInfo.Type == BeckhoffValueType.BYTE)
                {
                    return((byte)stream.GetBuffer()[0]);
                }
                else
                {
                    throw new WoopsaException("Ads type not compatible with Woopsa Integer");
                }

            case WoopsaValueType.Logical:
                return(Convert.ToBoolean(stream.GetBuffer()[0]));

            case WoopsaValueType.Real:
                if (property.AdsInfo.Type == BeckhoffValueType.REAL)
                {
                    return(BitConverter.ToSingle(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.LREAL)
                {
                    return(BitConverter.ToDouble(stream.GetBuffer(), 0));
                }
                else
                {
                    throw new WoopsaException("Ads type not compatible with Woopsa Real");
                }

            case WoopsaValueType.Text:
                string s     = System.Text.Encoding.ASCII.GetString(stream.GetBuffer());
                int    index = s.IndexOf('\0');
                return(s.Remove(index, s.Length - index));

            case WoopsaValueType.TimeSpan:
                data = bufferToLong(stream.GetBuffer(), 4);
                TimeSpan timeSpan = new TimeSpan(TimeSpan.TicksPerMillisecond * data);
                return(timeSpan);

            case WoopsaValueType.DateTime:
                TimeSpan timeSp;
                DateTime dateTime;
                data     = bufferToLong(stream.GetBuffer(), 4);
                dateTime = BeckhoffPlcReferenceDateTime;
                if (property.AdsInfo.Type == BeckhoffValueType.TIME_OF_DAY)
                {
                    timeSp = TimeSpan.FromMilliseconds(data);
                }
                else
                {
                    timeSp = TimeSpan.FromSeconds(data);
                }
                dateTime = dateTime + timeSp;
                WoopsaValue value = new WoopsaValue(dateTime);
                return(value);

            default:
                return(null);
            }
        }
Ejemplo n.º 14
0
 public override WoopsaValue ToWoopsaValue(object value, WoopsaValueType woopsaValueType,
                                           DateTime?timeStamp) => WoopsaValue.ToWoopsaValue(value, woopsaValueType, timeStamp);