public static WoopsaValue DeserializeWoopsaValue(string jsonText) { var result = JsonSerializer.Deserialize <WoopsaReadResult>(jsonText); if (result != null) { var valueType = (WoopsaValueType)Enum.Parse(typeof(WoopsaValueType), result.Type); WoopsaValue resultWoopsaValue; DateTime? timeStamp; if (result.TimeStamp != null) { timeStamp = DateTime.Parse(result.TimeStamp, CultureInfo.InvariantCulture); } else { timeStamp = null; } if (valueType == WoopsaValueType.JsonData) { resultWoopsaValue = new WoopsaValue(WoopsaJsonData.CreateFromDeserializedData(result.Value), timeStamp); } else { resultWoopsaValue = WoopsaValue.CreateChecked(WoopsaFormat.ToStringWoopsa(result.Value), valueType, timeStamp); } return(resultWoopsaValue); } else { return(WoopsaValue.Null); } }
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 override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) { result = null; foreach (var method in InnerObject.Methods) { if (method.Name.Equals(binder.Name)) { var argumentInfos = method.ArgumentInfos.ToArray(); var arguments = new IWoopsaValue[argumentInfos.Length]; for (int i = 0; i < argumentInfos.Length; i++) { WoopsaValueType woopsaValueType; WoopsaConverter woopsaConverter; if (CustomTypeConverters != null) { CustomTypeConverters.InferWoopsaType(args[i].GetType(), out woopsaValueType, out woopsaConverter); arguments[i] = woopsaConverter.ToWoopsaValue(args[i], woopsaValueType, null); } else { arguments[i] = WoopsaValue.ToWoopsaValue(args[i], argumentInfos[i].Type); } } result = method.Invoke(arguments); return(true); } } return(false); }
public static WoopsaValue ToWoopsaValue(object value, WoopsaValueType type, DateTime?timeStamp = null) { try { switch (type) { case WoopsaValueType.Logical: return(new WoopsaValue((bool)value, timeStamp)); case WoopsaValueType.Integer: return(new WoopsaValue(Convert.ToInt64(value), timeStamp)); case WoopsaValueType.Real: return(new WoopsaValue(Convert.ToDouble(value), timeStamp)); case WoopsaValueType.DateTime: return(new WoopsaValue((DateTime)value, timeStamp)); case WoopsaValueType.TimeSpan: if (value is TimeSpan) { return(new WoopsaValue((TimeSpan)value, timeStamp)); } else { return(new WoopsaValue(TimeSpan.FromSeconds(Convert.ToDouble(value)), timeStamp)); } case WoopsaValueType.Text: if (value == null) { return(new WoopsaValue(string.Empty, timeStamp)); } else if (value.GetType().IsEnum) { return(new WoopsaValue(value.ToString(), timeStamp)); } else if (string.IsNullOrEmpty((string)value)) { return(new WoopsaValue(string.Empty, timeStamp)); } else { return(new WoopsaValue(WoopsaFormat.ToStringWoopsa(value), timeStamp)); } default: return(WoopsaValue.CreateUnchecked(WoopsaFormat.ToStringWoopsa(value), type, timeStamp)); } } catch (InvalidCastException) { throw new WoopsaException(String.Format("Cannot typecast object of type {0} to Woopsa Type {1}", value.GetType(), type.ToString())); } }
public override bool TrySetMember(SetMemberBinder binder, object value) { foreach (var property in InnerObject.Properties) { if (binder.Name.Equals(property.Name)) { property.Value = WoopsaValue.ToWoopsaValue(value, property.Type); return(true); } } return(false); }
public WoopsaClientRequest Write(string propertyPath, WoopsaValue value) { WoopsaClientRequest newRequest = new WoopsaClientRequest() { Request = new ClientRequest() { Id = GetNextRequestId(), Verb = WoopsaFormat.VerbWrite, Path = propertyPath, Value = value } }; Add(newRequest); return(newRequest); }
private string InvokeMethod(string path, int argumentsCount, Func <string, WoopsaValueType, WoopsaValue> getArgumentByName) { IWoopsaElement item = FindByPath(path); if (item is IWoopsaMethod) { IWoopsaMethod method = item as IWoopsaMethod; if (argumentsCount == method.ArgumentInfos.Count()) { List <WoopsaValue> woopsaArguments = new List <WoopsaValue>(); foreach (var argInfo in method.ArgumentInfos) { WoopsaValue argumentValue = getArgumentByName(argInfo.Name, argInfo.Type); if (argumentValue == null) { string message = String.Format("Missing argument {0} for method {1}", argInfo.Name, item.Name); OnLog(WoopsaVerb.Invoke, path, NoArguments, message, false); throw new WoopsaInvalidOperationException(message); } else { woopsaArguments.Add(argumentValue); } } WoopsaValue[] argumentsArray = woopsaArguments.ToArray(); IWoopsaValue methodResult = method.Invoke(argumentsArray); string result = methodResult != null?methodResult.Serialize() : WoopsaConst.WoopsaNull; OnLog(WoopsaVerb.Invoke, path, argumentsArray, result, true); return(result); } else { string message = String.Format("Wrong argument count for method {0}", item.Name); OnLog(WoopsaVerb.Invoke, path, NoArguments, message, false); throw new WoopsaInvalidOperationException(message); } } else { string message = String.Format("Cannot invoke a {0}", item.GetType()); OnLog(WoopsaVerb.Invoke, path, NoArguments, message, false); throw new WoopsaInvalidOperationException(message); } }
internal string InvokeMethod(string path, NameValueCollection arguments) { int argumentsCount = arguments != null ? arguments.Count : 0; return(InvokeMethod(path, argumentsCount, (argumentName, woopsaValueType) => { string argumentValue = arguments[argumentName]; if (argumentValue != null) { return WoopsaValue.CreateChecked(argumentValue, woopsaValueType); } else { return null; } })); }
private WoopsaValue HandleCall(IWoopsaValue requestsArgument) { using (new WoopsaServerModelAccessFreeSection(_server)) { ServerRequest[] requestsList = JsonSerializer.Deserialize <ServerRequest[]>(requestsArgument.AsText, WoopsaUtils.ObjectToInferredTypesConverterOptions); List <MultipleRequestResponse> responses = new List <MultipleRequestResponse>(); foreach (var request in requestsList) { string result = null; try { using (new WoopsaServerModelAccessLockedSection(_server)) { if (request.Verb.Equals(WoopsaFormat.VerbRead)) { result = _server.ReadValue(request.Path); } else if (request.Verb.Equals(WoopsaFormat.VerbMeta)) { result = _server.GetMetadata(request.Path); } else if (request.Verb.Equals(WoopsaFormat.VerbWrite)) { result = _server.WriteValueDeserializedJson(request.Path, request.Value); } else if (request.Verb.Equals(WoopsaFormat.VerbInvoke)) { result = _server.InvokeMethodDeserializedJson(request.Path, request.Arguments); } } } catch (Exception e) { result = WoopsaFormat.Serialize(e); } MultipleRequestResponse response = new MultipleRequestResponse(); response.Id = request.Id; response.Result = result; responses.Add(response); } return(WoopsaValue.CreateUnchecked(responses.Serialize(), WoopsaValueType.JsonData)); } }
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) { result = null; foreach (var method in InnerObject.Methods) { if (method.Name.Equals(binder.Name)) { var argumentInfos = method.ArgumentInfos.ToArray(); var arguments = new IWoopsaValue[argumentInfos.Length]; for (int i = 0; i < argumentInfos.Length; i++) { arguments[i] = WoopsaValue.ToWoopsaValue(args[i], argumentInfos[i].Type); } result = method.Invoke(arguments); return(true); } } return(false); }
internal string InvokeMethodDeserializedJson(string path, Dictionary <string, object> arguments) { int argumentsCount = arguments != null ? arguments.Count : 0; return(InvokeMethod(path, argumentsCount, (argumentName, woopsaValueType) => { object argumentValue = arguments[argumentName]; if (argumentValue != null) { return WoopsaValue.DeserializedJsonToWoopsaValue( argumentValue, woopsaValueType); } else { return null; } })); }
protected void EnqueueNewMonitoredValue(IWoopsaValue newValue) { if (!newValue.IsSameValue(_oldValue)) { _oldValue = newValue; if (newValue.TimeStamp == null) { newValue = WoopsaValue.CreateUnchecked(newValue.AsText, newValue.Type, DateTime.Now); } WoopsaServerNotification newNotification = new WoopsaServerNotification(newValue, SubscriptionId); lock (_lock) { if (MonitorInterval == WoopsaSubscriptionServiceConst.MonitorIntervalLastPublishedValueOnly) { _notifications.Clear(); } _notifications.Add(newNotification); } } }
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)); } }
private WoopsaValue HandleCall(IWoopsaValue requestsArgument) { var serializer = new JavaScriptSerializer(); ServerRequest[] requestsList = serializer.Deserialize <ServerRequest[]>(requestsArgument.AsText); List <MultipleRequestResponse> responses = new List <MultipleRequestResponse>(); foreach (var request in requestsList) { string result = null; try { if (request.Verb.Equals(WoopsaFormat.VerbRead)) { result = _server.ReadValue(request.Path); } else if (request.Verb.Equals(WoopsaFormat.VerbMeta)) { result = _server.GetMetadata(request.Path); } else if (request.Verb.Equals(WoopsaFormat.VerbWrite)) { result = _server.WriteValueDeserializedJson(request.Path, request.Value); } else if (request.Verb.Equals(WoopsaFormat.VerbInvoke)) { result = _server.InvokeMethodDeserializedJson(request.Path, request.Arguments); } } catch (Exception e) { result = WoopsaFormat.Serialize(e); } MultipleRequestResponse response = new MultipleRequestResponse(); response.Id = request.Id; response.Result = result; responses.Add(response); } return(WoopsaValue.CreateUnchecked(responses.Serialize(), WoopsaValueType.JsonData)); }
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)); } }
protected virtual void PopulateProperties(object targetObject, Type exposedType, IEnumerable <PropertyDescription> properties) { HashSet <string> addedElements = new HashSet <string>(); foreach (var property in properties) { if (IsMemberWoopsaVisible(targetObject, property.PropertyInfo)) { if (!addedElements.Contains(property.Name)) { AddWoopsaProperty(property); addedElements.Add(property.Name); } } } if (typeof(IEnumerable <object>).IsAssignableFrom(exposedType) && Visibility.HasFlag(WoopsaVisibility.IEnumerableObject)) { new WoopsaProperty(this, nameof(OrderedItemIds), WoopsaValueType.JsonData, (p) => WoopsaValue.WoopsaJsonData(OrderedItemIds)); } }
internal string InvokeMethod(string path, NameValueCollection arguments) { IWoopsaElement item = FindByPath(path); if (item is IWoopsaMethod) { int argumentsCount = arguments != null ? arguments.Count : 0; IWoopsaMethod method = item as IWoopsaMethod; if (argumentsCount == method.ArgumentInfos.Count()) { List <WoopsaValue> woopsaArguments = new List <WoopsaValue>(); foreach (var argInfo in method.ArgumentInfos) { string argumentValue = arguments[argInfo.Name]; if (argumentValue == null) { throw new WoopsaInvalidOperationException(String.Format( "Missing argument {0} for method {1}", argInfo.Name, item.Name)); } woopsaArguments.Add(WoopsaValue.CreateChecked(argumentValue, argInfo.Type)); } IWoopsaValue result = method.Invoke(woopsaArguments.ToArray()); return(result != null?result.Serialize() : WoopsaConst.WoopsaNull); } else { throw new WoopsaInvalidOperationException(String.Format( "Wrong argument count for method {0}", item.Name)); } } else { throw new WoopsaInvalidOperationException(String.Format( "Cannot invoke a {0}", item.GetType())); } }
public void ExecuteMultiRequest(WoopsaClientMultiRequest multiRequest) { if (multiRequest.Count > 0) { multiRequest.Reset(); if (!_disableRemoteMultiRequest) { try { WoopsaValue results = _remoteMethodMultiRequest.Invoke( WoopsaValue.WoopsaJsonData(multiRequest.Requests.Serialize())); multiRequest.DispatchResults(results.JsonData); } catch (WoopsaNotFoundException) { _disableRemoteMultiRequest = true; } } if (_disableRemoteMultiRequest) { ExecuteMultiRequestLocally(multiRequest); } } }
internal string WriteValueDeserializedJson(string path, object deserializedJson) { return(WriteValue(path, (woopsaValueType) => WoopsaValue.DeserializedJsonToWoopsaValue(deserializedJson, woopsaValueType))); }
public WoopsaClientRequest Write(string propertyPath, WoopsaValue value) { WoopsaClientRequest newRequest = new WoopsaClientRequest() { Request = new ClientRequest() { Id = GetNextRequestId(), Verb = WoopsaFormat.VerbWrite, Path = propertyPath, Value = value } }; Add(newRequest); return newRequest; }
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; } }
public override WoopsaValue ToWoopsaValue(object value, WoopsaValueType woopsaValueType, DateTime?timeStamp) { return(WoopsaValue.ToWoopsaValue(value, woopsaValueType, timeStamp)); }
public WoopsaClientNotification(WoopsaValue value, int subscriptionId) { Value = value; SubscriptionId = subscriptionId; }
public void TestWoopsaValuePerfo() { 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); }
internal string WriteValue(string path, string value) { return(WriteValue(path, (woopsaValueType) => WoopsaValue.CreateUnchecked(value, woopsaValueType))); }
public static WoopsaValue DeserializeWoopsaValue(string jsonText) { var serializer = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }; var result = serializer.Deserialize<WoopsaReadResult>(jsonText); if (result != null) { var valueType = (WoopsaValueType)Enum.Parse(typeof(WoopsaValueType), result.Type); WoopsaValue resultWoopsaValue; DateTime? timeStamp; if (result.TimeStamp != null) timeStamp = DateTime.Parse(result.TimeStamp, CultureInfo.InvariantCulture); else timeStamp = null; if (valueType == WoopsaValueType.JsonData) resultWoopsaValue = new WoopsaValue(WoopsaJsonData.CreateFromDeserializedData(result.Value), timeStamp); else resultWoopsaValue = WoopsaValue.CreateChecked(WoopsaFormat.ToStringWoopsa(result.Value), valueType, timeStamp); return resultWoopsaValue; } else return WoopsaValue.Null; }