public void TestWoopsaProtocolPerformance() { TestObjectServer objectServer = new TestObjectServer(); using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort)) { using (WoopsaClient client = new WoopsaClient(TestingUrl)) { WoopsaBoundClientObject root = client.CreateBoundRoot(); IWoopsaProperty property = root.Properties.ByName("Votes"); property.Value = new WoopsaValue(0); int n = property.Value.ToInt32(); Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 100; i++) { property.Value = new WoopsaValue(i); Assert.AreEqual(objectServer.Votes, i); var result = property.Value; Assert.AreEqual(result.ToInt64(), i); } TimeSpan duration = watch.Elapsed; Assert.IsTrue(duration < TimeSpan.FromMilliseconds(200), $"Duration takes ${duration.Milliseconds}ms, instead of 200ms"); Console.WriteLine($"Duration takes ${duration.Milliseconds}ms"); } } }
private string WriteValue(string path, Func <WoopsaValueType, WoopsaValue> getValue) { IWoopsaElement item = FindByPath(path); if ((item is IWoopsaProperty)) { IWoopsaProperty property = item as IWoopsaProperty; WoopsaValue argument = getValue(property.Type); if (!property.IsReadOnly) { property.Value = argument; string result = property.Value.Serialize(); OnLog(WoopsaVerb.Write, path, new WoopsaValue[] { argument }, result, true); return(result); } else { string message = String.Format( "Cannot write a read-only WoopsaProperty for path {0}", path); OnLog(WoopsaVerb.Write, path, new WoopsaValue[] { argument }, message, false); throw new WoopsaInvalidOperationException(message); } } else { WoopsaValue argument = getValue(WoopsaValueType.Text); string message = String.Format("Cannot write value of a non-WoopsaProperty for path {0}", path); OnLog(WoopsaVerb.Write, path, new WoopsaValue[] { argument }, message, false); throw new WoopsaInvalidOperationException(message); } }
public void TestWoopsaProtocolPerformance() { TestObjectServer objectServer = new TestObjectServer(); using (WoopsaServer server = new WoopsaServer(objectServer)) { using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa")) { WoopsaBoundClientObject root = client.CreateBoundRoot(); IWoopsaProperty property = root.Properties.ByName("Votes"); property.Value = new WoopsaValue(0); int n = property.Value.ToInt32(); Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 100; i++) { property.Value = new WoopsaValue(i); Assert.AreEqual(objectServer.Votes, i); var result = property.Value; Assert.AreEqual(result.ToInt64(), i); } TimeSpan duration = watch.Elapsed; Assert.IsTrue(duration < TimeSpan.FromMilliseconds(200)); } } }
public CanWatchEventArgs( BaseWoopsaSubscriptionServiceSubscription subscription, IWoopsaProperty itemProperty) { Subscription = subscription; ItemProperty = itemProperty; CanWatch = WoopsaSubscriptionServiceImplementation. CanWatchDefaultValue; }
internal string ReadValue(string path) { IWoopsaElement item = FindByPath(path); if ((item is IWoopsaProperty)) { IWoopsaProperty property = item as IWoopsaProperty; return(property.Value.Serialize()); } else { throw new WoopsaInvalidOperationException(String.Format( "Cannot read value of a non-WoopsaProperty for path {0}", path)); } }
internal string ReadValue(string path) { IWoopsaElement item = FindByPath(path); if ((item is IWoopsaProperty)) { IWoopsaProperty property = item as IWoopsaProperty; string result = property.Value.Serialize(); OnLog(WoopsaVerb.Read, path, NoArguments, result, true); return(result); } else { string message = String.Format("Cannot read value of a non-WoopsaProperty for path {0}", path); OnLog(WoopsaVerb.Read, path, NoArguments, message, false); throw new WoopsaInvalidOperationException(message); } }
internal string WriteValue(string path, string value) { IWoopsaElement item = FindByPath(path); if ((item is IWoopsaProperty)) { IWoopsaProperty property = item as IWoopsaProperty; if (property.IsReadOnly) { throw new WoopsaInvalidOperationException(String.Format( "Cannot write a read-only WoopsaProperty for path {0}", path)); } property.Value = WoopsaValue.CreateUnchecked(value, property.Type); return(property.Value.Serialize()); } else { throw new WoopsaInvalidOperationException(String.Format("Cannot write value of a non-WoopsaProperty for path {0}", path)); } }
internal string WriteValueDeserializedJson(string path, object deserializedJson) { IWoopsaElement item = FindByPath(path); if ((item is IWoopsaProperty)) { IWoopsaProperty property = item as IWoopsaProperty; if (property.IsReadOnly) { throw new WoopsaInvalidOperationException(String.Format( "Cannot write a read-only WoopsaProperty for path {0}", path)); } property.Value = WoopsaValue.DeserializedJsonToWoopsaValue(deserializedJson, property.Type); return(WoopsaValue.Null.Serialize()); } else { throw new WoopsaInvalidOperationException(String.Format("Cannot write value of a non-WoopsaProperty for path {0}", path)); } }
// TODO : optimize performances private static string SerializeMetadata(this IWoopsaProperty property) { return(String.Format(MetadataProperty, JsonEscape(property.Name), property.Type, property.IsReadOnly.ToString().ToLower())); }
private void SetProperty(IWoopsaProperty property, IWoopsaValue value) { Client.ClientProtocol.Write(this.GetPath(Root).TrimEnd(WoopsaConst.WoopsaPathSeparator) + WoopsaConst.WoopsaPathSeparator + property.Name, value.AsText); }
private WoopsaValue GetProperty(IWoopsaProperty property) { return(Client.ClientProtocol.Read(this.GetPath(Root).TrimEnd(WoopsaConst.WoopsaPathSeparator) + WoopsaConst.WoopsaPathSeparator + property.Name)); }
public void WriteAdsValue(IWoopsaProperty property, IWoopsaValue value) { WoopsaAdsProperty adsProperty = (WoopsaAdsProperty)property; AdsStream stream = new AdsStream(); switch (property.Type) { case WoopsaValueType.Integer: if (adsProperty.AdsInfo.Type == BeckhoffValueType.WORD || adsProperty.AdsInfo.Type == BeckhoffValueType.UINT) { stream = new AdsStream(BitConverter.GetBytes(value.ToUInt16())); } else if (adsProperty.AdsInfo.Type == BeckhoffValueType.DWORD || adsProperty.AdsInfo.Type == BeckhoffValueType.UDINT) { stream = new AdsStream(BitConverter.GetBytes(value.ToUInt32())); } else if (adsProperty.AdsInfo.Type == BeckhoffValueType.SINT) { stream = new AdsStream(BitConverter.GetBytes(value.ToSByte())); } else if (adsProperty.AdsInfo.Type == BeckhoffValueType.INT) { stream = new AdsStream(BitConverter.GetBytes(value.ToInt16())); } else if (adsProperty.AdsInfo.Type == BeckhoffValueType.DINT) { stream = new AdsStream(BitConverter.GetBytes(value.ToInt32())); } else if (adsProperty.AdsInfo.Type == BeckhoffValueType.LINT) { stream = new AdsStream(BitConverter.GetBytes(value.ToInt64())); } else if (adsProperty.AdsInfo.Type == BeckhoffValueType.USINT || adsProperty.AdsInfo.Type == BeckhoffValueType.BYTE) { stream = new AdsStream(1); stream.WriteByte(value.ToByte()); } else if (adsProperty.AdsInfo.Type == BeckhoffValueType.ULINT) { stream = new AdsStream(BitConverter.GetBytes(value.ToUInt64())); } else { throw new WoopsaException("Ads type not compatible with Woopsa Integer"); } break; case WoopsaValueType.Logical: stream = new AdsStream(BitConverter.GetBytes(value.ToBool())); break; case WoopsaValueType.Real: if (adsProperty.AdsInfo.Type == BeckhoffValueType.REAL) { stream = new AdsStream(BitConverter.GetBytes(value.ToFloat())); } else if (adsProperty.AdsInfo.Type == BeckhoffValueType.LREAL) { stream = new AdsStream(BitConverter.GetBytes(value.ToDouble())); } else { throw new WoopsaException("Ads type not compatible with Woopsa Real"); } break; case WoopsaValueType.Text: stream = new AdsStream(80); byte[] byteString = System.Text.Encoding.ASCII.GetBytes(value.ToString()); stream.Write(byteString, 0, byteString.Length); break; case WoopsaValueType.TimeSpan: stream = new AdsStream(BitConverter.GetBytes((uint)(value.ToTimeSpan().Ticks / TimeSpan.TicksPerMillisecond))); break; case WoopsaValueType.DateTime: TimeSpan timeSp; DateTime dateTime; if (adsProperty.AdsInfo.Type == BeckhoffValueType.TIME_OF_DAY) { dateTime = value.ToDateTime(); uint timeOfDay = (uint)dateTime.Millisecond + ((uint)dateTime.Second + ((uint)dateTime.Minute + ((uint)dateTime.Hour * 60)) * 60) * 1000; stream = new AdsStream(BitConverter.GetBytes(timeOfDay)); } else { timeSp = value.ToDateTime() - BeckhoffPlcReferenceDateTime; stream = new AdsStream(BitConverter.GetBytes((uint)timeSp.TotalSeconds)); } break; default: stream = new AdsStream(1); break; } _tcAds.Write(adsProperty.AdsInfo.IndexGroup, adsProperty.AdsInfo.IndexOffset, stream); }
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 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; } }
private WoopsaValue GetProperty(IWoopsaProperty property) { return Client.ClientProtocol.Read(this.GetPath(Root).TrimEnd(WoopsaConst.WoopsaPathSeparator) + WoopsaConst.WoopsaPathSeparator + property.Name); }
public void WriteAdsValue(IWoopsaProperty property, IWoopsaValue value) { WoopsaAdsProperty adsProperty = (WoopsaAdsProperty)property; AdsStream stream = new AdsStream(); switch (property.Type) { case WoopsaValueType.Integer: if (adsProperty.AdsInfo.Type == BeckhoffValueType.WORD || adsProperty.AdsInfo.Type == BeckhoffValueType.UINT) stream = new AdsStream(BitConverter.GetBytes(value.ToUInt16())); else if (adsProperty.AdsInfo.Type == BeckhoffValueType.DWORD || adsProperty.AdsInfo.Type == BeckhoffValueType.UDINT) stream = new AdsStream(BitConverter.GetBytes(value.ToUInt32())); else if (adsProperty.AdsInfo.Type == BeckhoffValueType.SINT) stream = new AdsStream(BitConverter.GetBytes(value.ToSByte())); else if (adsProperty.AdsInfo.Type == BeckhoffValueType.INT) stream = new AdsStream(BitConverter.GetBytes(value.ToInt16())); else if (adsProperty.AdsInfo.Type == BeckhoffValueType.DINT) stream = new AdsStream(BitConverter.GetBytes(value.ToInt32())); else if (adsProperty.AdsInfo.Type == BeckhoffValueType.LINT) stream = new AdsStream(BitConverter.GetBytes(value.ToInt64())); else if (adsProperty.AdsInfo.Type == BeckhoffValueType.USINT || adsProperty.AdsInfo.Type == BeckhoffValueType.BYTE) { stream = new AdsStream(1); stream.WriteByte(value.ToByte()); } else if (adsProperty.AdsInfo.Type == BeckhoffValueType.ULINT) stream = new AdsStream(BitConverter.GetBytes(value.ToUInt64())); else throw new WoopsaException("Ads type not compatible with Woopsa Integer"); break; case WoopsaValueType.Logical: stream = new AdsStream(BitConverter.GetBytes(value.ToBool())); break; case WoopsaValueType.Real: if (adsProperty.AdsInfo.Type == BeckhoffValueType.REAL) stream = new AdsStream(BitConverter.GetBytes(value.ToFloat())); else if (adsProperty.AdsInfo.Type == BeckhoffValueType.LREAL) stream = new AdsStream(BitConverter.GetBytes(value.ToDouble())); else throw new WoopsaException("Ads type not compatible with Woopsa Real"); break; case WoopsaValueType.Text: stream = new AdsStream(80); byte[] byteString = System.Text.Encoding.ASCII.GetBytes(value.ToString()); stream.Write(byteString, 0, byteString.Length); break; case WoopsaValueType.TimeSpan: stream = new AdsStream(BitConverter.GetBytes((uint)(value.ToTimeSpan().Ticks / TimeSpan.TicksPerMillisecond))); break; case WoopsaValueType.DateTime: TimeSpan timeSp; DateTime dateTime; if (adsProperty.AdsInfo.Type == BeckhoffValueType.TIME_OF_DAY) { dateTime = value.ToDateTime(); uint timeOfDay = (uint)dateTime.Millisecond + ((uint)dateTime.Second + ((uint)dateTime.Minute + ((uint)dateTime.Hour * 60)) * 60) * 1000; stream = new AdsStream(BitConverter.GetBytes(timeOfDay)); } else { timeSp = value.ToDateTime() - BeckhoffPlcReferenceDateTime; stream = new AdsStream(BitConverter.GetBytes((uint)timeSp.TotalSeconds)); } break; default: stream = new AdsStream(1); break; } _tcAds.Write(adsProperty.AdsInfo.IndexGroup, adsProperty.AdsInfo.IndexOffset, stream); }
private bool OnCanWatch( BaseWoopsaSubscriptionServiceSubscription subscription, IWoopsaProperty itemProperty) { return(Channel.OnCanWatch(subscription, itemProperty)); }
internal bool OnCanWatch(BaseWoopsaSubscriptionServiceSubscription subscription, IWoopsaProperty itemProperty) { if (CanWatch != null) { CanWatchEventArgs e = new CanWatchEventArgs(subscription, itemProperty); CanWatch(this, e); return(e.CanWatch); } else { return(CanWatchDefaultValue); } }
protected override bool GetWatchedPropertyValue(out IWoopsaValue value) { try { if (_watchedProperty is WoopsaElement) if (((WoopsaElement)_watchedProperty).IsDisposed) _watchedProperty = null; if (_watchedProperty == null) { var item = Root.ByPathOrNull(PropertyPath); _watchedProperty = item as IWoopsaProperty; } if (_watchedProperty != null) { value = _watchedProperty.Value; return true; } else { value = WoopsaValue.Null; return true; } } catch (Exception) { // The property might have become invalid, search it new the next time _watchedProperty = null; throw; } }
internal bool OnCanWatch(BaseWoopsaSubscriptionServiceSubscription subscription, IWoopsaProperty itemProperty) { return(ServiceImplementation.OnCanWatch(subscription, itemProperty)); }
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); } }