protected override void Execute(NativeActivityContext context) { string WorkflowInstanceId = context.WorkflowInstanceId.ToString(); bool waitforcompleted = WaitForCompleted.Get(context); string bookmarkname = null; IDictionary <string, object> _payload = new System.Dynamic.ExpandoObject(); if (Arguments == null || Arguments.Count == 0) { var vars = context.DataContext.GetProperties(); foreach (dynamic v in vars) { var value = v.GetValue(context.DataContext); if (value != null) { //_payload.Add(v.DisplayName, value); try { var test = new { value = value }; if (value.GetType() == typeof(System.Data.DataView)) { continue; } if (value.GetType() == typeof(System.Data.DataRowView)) { continue; } if (value.GetType() == typeof(System.Data.DataTable)) { if (value != null) { _payload[v.DisplayName] = ((System.Data.DataTable)value).ToJArray(); } } else { var asjson = JObject.FromObject(test); _payload[v.DisplayName] = value; } } catch (Exception) { } } else { _payload[v.DisplayName] = null; } } } else { Dictionary <string, object> arguments = (from argument in Arguments where argument.Value.Direction != ArgumentDirection.Out select argument).ToDictionary((KeyValuePair <string, Argument> argument) => argument.Key, (KeyValuePair <string, Argument> argument) => argument.Value.Get(context)); foreach (var a in arguments) { var value = a.Value; if (value != null) { if (value.GetType() == typeof(System.Data.DataView)) { continue; } if (value.GetType() == typeof(System.Data.DataRowView)) { continue; } if (value.GetType() == typeof(System.Data.DataTable)) { if (value != null) { _payload[a.Key] = ((System.Data.DataTable)value).ToJArray(); } } else { _payload[a.Key] = a.Value; } } else { _payload[a.Key] = null; } } } try { bookmarkname = Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", ""); if (waitforcompleted) { context.CreateBookmark(bookmarkname, new BookmarkCallback(OnBookmarkCallback)); } } catch (Exception ex) { Log.Error(ex.ToString()); throw; } try { if (!string.IsNullOrEmpty(bookmarkname)) { var result = global.webSocketClient.QueueMessage(workflow, _payload, RobotInstance.instance.robotqueue, bookmarkname); result.Wait(5000); } } catch (Exception ex) { var i = WorkflowInstance.Instances.Where(x => x.InstanceId == WorkflowInstanceId).FirstOrDefault(); if (i != null) { i.Abort(ex.Message); } //context.RemoveBookmark(bookmarkname); Log.Error(ex.ToString()); } }
void OnBookmarkCallback(NativeActivityContext context, Bookmark bookmark, object obj) { bool waitforcompleted = WaitForCompleted.Get(context); if (!waitforcompleted) { return; } // context.RemoveBookmark(bookmark.Name); var _msg = JObject.Parse(obj.ToString()); JObject payload = _msg; // Backward compatible with older version of openflow if (_msg.ContainsKey("payload")) { payload = _msg.Value <JObject>("payload"); } var state = _msg["state"].ToString(); if (!string.IsNullOrEmpty(state)) { if (state == "idle") { Log.Output("Workflow out node set to idle, so also going idle again."); context.CreateBookmark(bookmark.Name, new BookmarkCallback(OnBookmarkCallback)); return; } else if (state == "failed") { var message = "Invoke OpenFlow Workflow failed"; if (_msg.ContainsKey("error")) { message = _msg["error"].ToString(); } if (_msg.ContainsKey("_error")) { message = _msg["_error"].ToString(); } if (payload.ContainsKey("error")) { message = payload["error"].ToString(); } if (payload.ContainsKey("_error")) { message = payload["_error"].ToString(); } if (string.IsNullOrEmpty(message)) { message = "Invoke OpenFlow Workflow failed"; } throw new Exception(message); } } List <string> keys = payload.Properties().Select(p => p.Name).ToList(); if (Arguments == null || Arguments.Count == 0) { foreach (var key in keys) { PropertyDescriptor myVar = context.DataContext.GetProperties().Find(key, true); if (myVar != null) { if (myVar.PropertyType == typeof(System.Data.DataTable)) { try { var json = payload[key].ToString(); if (!string.IsNullOrEmpty(json)) { var jarray = JArray.Parse(json); myVar.SetValue(context.DataContext, jarray.ToDataTable()); } else { myVar.SetValue(context.DataContext, null); } } catch (Exception) { throw; } } else { JToken t = payload[key]; System.Reflection.MethodInfo method = typeof(JToken).GetMethod(nameof(JToken.Value)); // typeof(JToken).GetMethod(nameof(JToken.Value)); System.Reflection.MethodInfo generic = method.MakeGenericMethod(myVar.PropertyType); var value = generic.Invoke(payload, new object[] { key }); myVar.SetValue(context.DataContext, value); } } else { Log.Debug("Recived property " + key + " but no variable exists to save the value in " + payload[key]); } } } else { Dictionary <string, object> arguments = (from argument in Arguments where argument.Value.Direction != ArgumentDirection.In select argument).ToDictionary((KeyValuePair <string, Argument> argument) => argument.Key, (KeyValuePair <string, Argument> argument) => argument.Value.Get(context)); foreach (var a in arguments) { if (keys.Contains(a.Key)) { if (Arguments[a.Key].ArgumentType == typeof(System.Data.DataTable)) { try { var json = payload[a.Key].ToString(); if (!string.IsNullOrEmpty(json)) { var jarray = JArray.Parse(json); Arguments[a.Key].Set(context, jarray.ToDataTable()); } else { Arguments[a.Key].Set(context, null); } } catch (Exception) { throw; } } else { JToken t = payload[a.Key]; var testtest = t.Value <string>(); System.Reflection.MethodInfo method = typeof(JToken).GetMethod(nameof(JToken.Value)); // typeof(JToken).GetMethod(nameof(JToken.Value)); System.Reflection.MethodInfo generic = method.MakeGenericMethod(Arguments[a.Key].ArgumentType); var value = generic.Invoke(payload, new object[] { a.Key }); Arguments[a.Key].Set(context, value); } } else if (arguments.Count == 0) { try { if (Arguments[a.Key].ArgumentType.IsValueType) { Arguments[a.Key].Set(context, Activator.CreateInstance(Arguments[a.Key].ArgumentType)); } else { Arguments[a.Key].Set(context, null); } } catch (Exception ex) { Log.Error("Error setting " + a.Key + ": " + ex.Message); } } } } }
protected override void Execute(NativeActivityContext context) { bool waitforcompleted = WaitForCompleted.Get(context); string WorkflowInstanceId = context.WorkflowInstanceId.ToString(); // IDictionary<string, object> _payload = new System.Dynamic.ExpandoObject(); var param = new Dictionary <string, object>(); if (Arguments == null || Arguments.Count == 0) { var vars = context.DataContext.GetProperties(); foreach (dynamic v in vars) { var value = v.GetValue(context.DataContext); if (value != null) { //_payload.Add(v.DisplayName, value); try { var test = new { value }; if (value.GetType() == typeof(System.Data.DataView)) { continue; } if (value.GetType() == typeof(System.Data.DataRowView)) { continue; } //if (value.GetType() == typeof(System.Data.DataTable)) //{ // if (value != null) param[v.DisplayName] = ((System.Data.DataTable)value).ToJArray(); //} //else //{ // var asjson = JObject.FromObject(test); // param[v.DisplayName] = value; //} // var asjson = JObject.FromObject(test); param[v.DisplayName] = value; } catch (Exception) { } } else { param[v.DisplayName] = value; } } } else { Dictionary <string, object> arguments = (from argument in Arguments where argument.Value.Direction != ArgumentDirection.Out select argument).ToDictionary((KeyValuePair <string, Argument> argument) => argument.Key, (KeyValuePair <string, Argument> argument) => argument.Value.Get(context)); foreach (var a in arguments) { var value = a.Value; if (value != null) { if (value.GetType() == typeof(System.Data.DataView)) { continue; } if (value.GetType() == typeof(System.Data.DataRowView)) { continue; } //if (value.GetType() == typeof(System.Data.DataTable)) //{ // if (value != null) param[a.Key] = ((System.Data.DataTable)value).ToJArray(); //} //else //{ // param[a.Key] = a.Value; //} param[a.Key] = a.Value; } else { param[a.Key] = null; } } } try { var Instance = WorkflowInstance.Instances.Where(x => x.InstanceId == context.WorkflowInstanceId.ToString()).FirstOrDefault(); // , string SpanId, string ParentSpanId var workflowid = this.workflow.Get(context); var workflow = RobotInstance.instance.GetWorkflowByIDOrRelativeFilename(workflowid); if (workflow == null) { throw new ArgumentException("Failed locating workflow " + workflowid); } IWorkflowInstance instance = null; Views.WFDesigner designer = null; GenericTools.RunUI(() => { designer = RobotInstance.instance.GetWorkflowDesignerByIDOrRelativeFilename(this.workflow.Get(context)) as Views.WFDesigner; if (designer != null) { designer.BreakpointLocations = null; instance = workflow.CreateInstance(param, null, null, designer.IdleOrComplete, designer.OnVisualTracking, null, Instance.SpanId); } else { instance = workflow.CreateInstance(param, null, null, RobotInstance.instance.Window.IdleOrComplete, null, null, Instance.SpanId); } instance.caller = WorkflowInstanceId; }); Log.Verbose("InvokeOpenRPA: Run Instance ID " + instance._id); if (waitforcompleted) { context.CreateBookmark(instance._id, new BookmarkCallback(OnBookmarkCallback)); if (instance.Bookmarks == null) { instance.Bookmarks = new Dictionary <string, object>(); } instance.Bookmarks.Add(instance._id, null); //((WorkflowInstance)instance).wfApp.Persist(); } GenericTools.RunUI(() => { if (designer != null) { designer.Run(designer.VisualTracking, designer.SlowMotion, instance); } else { instance.Run(); } }); } catch (Exception ex) { Log.Error(ex.ToString()); throw; } }
void OnBookmarkCallback(NativeActivityContext context, Bookmark bookmark, object obj) { bool waitforcompleted = WaitForCompleted.Get(context); if (!waitforcompleted) { return; } // keep bookmark, incase workflow dies, and need to pickup more data when started again // context.RemoveBookmark(bookmark.Name); var command = Newtonsoft.Json.JsonConvert.DeserializeObject <Interfaces.mq.RobotCommand>(obj.ToString()); if (command.data == null) { return; } if (command.command == "invokefailed" || command.command == "error") { if (string.IsNullOrEmpty(command.data.ToString())) { throw new Exception("Invoke failed"); } Exception ex = null; try { ex = Newtonsoft.Json.JsonConvert.DeserializeObject <Exception>(command.data.ToString()); } catch (Exception) { } if (ex != null) { throw ex; } throw new Exception(command.data.ToString()); } if (command.command == "timeout") { throw new Exception("request timed out, no robot picked up the message in a timely fashion"); } if (string.IsNullOrEmpty(command.data.ToString())) { return; } var payload = JObject.Parse(command.data.ToString()); List <string> keys = payload.Properties().Select(p => p.Name).ToList(); if (Arguments == null || Arguments.Count == 0) { foreach (var key in keys) { var myVar = context.DataContext.GetProperties().Find(key, true); if (myVar != null) { if (myVar.PropertyType.Name == "DataTable") { var json = payload[key].ToString(); if (!string.IsNullOrEmpty(json)) { var jarray = JArray.Parse(json); myVar.SetValue(context.DataContext, jarray.ToDataTable()); } else { myVar.SetValue(context.DataContext, null); } } else if (myVar.PropertyType.Name == "JArray") { var json = payload[key].ToString(); var jobj = JArray.Parse(json); myVar.SetValue(context.DataContext, jobj); } else if (myVar.PropertyType.Name == "JObject") { var json = payload[key].ToString(); var jobj = JObject.Parse(json); myVar.SetValue(context.DataContext, jobj); } else { myVar.SetValue(context.DataContext, payload[key].ToString()); } } } } else { Dictionary <string, object> arguments = (from argument in Arguments where argument.Value.Direction != ArgumentDirection.In select argument).ToDictionary((KeyValuePair <string, Argument> argument) => argument.Key, (KeyValuePair <string, Argument> argument) => argument.Value.Get(context)); foreach (var a in arguments) { if (keys.Contains(a.Key)) { if (Arguments[a.Key].ArgumentType == typeof(System.Data.DataTable)) { try { var json = payload[a.Key].ToString(); if (!string.IsNullOrEmpty(json)) { var jarray = JArray.Parse(json); Arguments[a.Key].Set(context, jarray.ToDataTable()); } else { Arguments[a.Key].Set(context, null); } } catch (Exception) { throw; } } else { JToken t = payload[a.Key]; var testtest = t.Value <string>(); System.Reflection.MethodInfo method = typeof(JToken).GetMethod(nameof(JToken.Value)); // typeof(JToken).GetMethod(nameof(JToken.Value)); System.Reflection.MethodInfo generic = method.MakeGenericMethod(Arguments[a.Key].ArgumentType); var value = generic.Invoke(t, new object[] { }); Arguments[a.Key].Set(context, value); } } else { try { if (Arguments[a.Key].ArgumentType.IsValueType) { Arguments[a.Key].Set(context, Activator.CreateInstance(Arguments[a.Key].ArgumentType)); } else { Arguments[a.Key].Set(context, null); } } catch (Exception ex) { Log.Error("Error setting " + a.Key + ": " + ex.Message); } } } } }
void OnBookmarkCallback(NativeActivityContext context, Bookmark bookmark, object obj) { try { bool waitforcompleted = WaitForCompleted.Get(context); if (!waitforcompleted) { return; } // keep bookmark, incase workflow dies, and need to pickup data after being restarted // context.RemoveBookmark(bookmark.Name); var instance = obj as WorkflowInstance; if (instance == null) { throw new Exception("Bookmark returned a non WorkflowInstance"); } var workflow = RobotInstance.instance.GetWorkflowByIDOrRelativeFilename(this.workflow.Get(context)); var name = "The invoked workflow failed with "; if (workflow != null && !string.IsNullOrEmpty(workflow.name)) { name = workflow.name; } if (workflow != null && !string.IsNullOrEmpty(workflow.ProjectAndName)) { name = workflow.ProjectAndName; } if (instance.Exception != null) { throw new Exception(name + " failed with " + instance.Exception.Message, instance.Exception); } if (instance.hasError) { throw new Exception(name + " failed with " + instance.errormessage); } if (Arguments == null || Arguments.Count == 0) { foreach (var prop in instance.Parameters) { var myVar = context.DataContext.GetProperties().Find(prop.Key, true); if (myVar != null) { myVar.SetValue(context.DataContext, prop.Value); //if (myVar.PropertyType.Name == "DataTable") //{ // var json = prop.ToString(); // if(!string.IsNullOrEmpty(json)) // { // var jarray = JArray.Parse(json); // myVar.SetValue(context.DataContext, jarray.ToDataTable()); // } // else // { // myVar.SetValue(context.DataContext, null); // } //} //else //{ // //var myValue = myVar.GetValue(context.DataContext); // myVar.SetValue(context.DataContext, prop.Value); //} } else { Log.Debug("Recived property " + prop.Key + " but no variable exists to save the value in " + prop.Value); } } } else { Dictionary <string, object> arguments = (from argument in Arguments where argument.Value.Direction != ArgumentDirection.In select argument).ToDictionary((KeyValuePair <string, Argument> argument) => argument.Key, (KeyValuePair <string, Argument> argument) => argument.Value.Get(context)); foreach (var a in arguments) { if (instance.Parameters.ContainsKey(a.Key)) { Arguments[a.Key].Set(context, instance.Parameters[a.Key]); } else { try { if (Arguments[a.Key].ArgumentType.IsValueType) { Arguments[a.Key].Set(context, Activator.CreateInstance(Arguments[a.Key].ArgumentType)); } else { Arguments[a.Key].Set(context, null); } } catch (Exception ex) { Log.Error("Error setting " + a.Key + ": " + ex.Message); } } } } } catch (Exception ex) { Log.Error(ex.ToString()); throw; } }