示例#1
0
        private static void Server_OnReceivedMessage(NamedPipeConnection <SAPEvent, SAPEvent> connection, SAPEvent message)
        {
            try
            {
                if (message == null)
                {
                    return;
                }
                form.AddText("[resc] " + message.action);
                if (message.action == "beginrecord")
                {
                    try
                    {
                        recordstarting = true;
                        var recinfo = message.Get <SAPToogleRecordingEvent>();
                        var overlay = false;
                        if (recinfo != null)
                        {
                            overlay = recinfo.overlay;
                            StartMonitorMouse(recinfo.mousemove);
                        }
                        SAPHook.Instance.BeginRecord(overlay);
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                        recordstarting = false;
                    }
                    catch (Exception ex)
                    {
                        message.error = ex.Message;
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                }
                if (message.action == "endrecord")
                {
                    try
                    {
                        StopMonitorMouse();
                        SAPHook.Instance.EndRecord();
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                    catch (Exception ex)
                    {
                        message.error = ex.Message;
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                }
                if (message.action == "login")
                {
                    try
                    {
                        var login = message.Get <SAPLoginEvent>();
                        if (SAPHook.Instance.Login(login))
                        {
                            var session = SAPHook.Instance.GetSession(login.SystemName);
                            if (session == null)
                            {
                                message.error = "Login failed";
                            }
                        }
                        else
                        {
                            message.error = "Login failed";
                        }
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                    catch (Exception ex)
                    {
                        message.error = ex.Message;
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                }
                if (message.action == "getconnections")
                {
                    try
                    {
                        var result = new SAPGetSessions();
                        SAPHook.Instance.RefreshSessions();
                        result.Connections = SAPHook.Instance.Connections;
                        message.Set(result);
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                    catch (Exception ex)
                    {
                        message.error = ex.Message;
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                }

                if (message.action == "getitems")
                {
                }
                if (message.action == "getitem")
                {
                    try
                    {
                        var msg = message.Get <SAPEventElement>();
                        if (string.IsNullOrEmpty(msg.SystemName))
                        {
                            throw new ArgumentException("System Name is mandatory right now!");
                        }
                        var session = SAPHook.Instance.GetSession(msg.SystemName);
                        if (session != null)
                        {
                            GuiComponent comp = session.GetSAPComponentById <GuiComponent>(msg.Id);
                            if (comp is null)
                            {
                                throw new ArgumentException("Item with id " + msg.Id + " was not found");
                            }

                            msg.Id            = comp.Id; msg.Name = comp.Name;
                            msg.SystemName    = session.Info.SystemName;
                            msg.ContainerType = comp.ContainerType; msg.type = comp.Type;
                            var    p      = comp.Parent as GuiComponent;
                            string parent = (p != null) ? p.Id : null;
                            msg.Parent = parent;
                            // msg.LoadProperties(comp, true);
                            msg.LoadProperties(comp, false);
                            var children = new List <SAPEventElement>();
                            if (comp.ContainerType)
                            {
                                var cont = comp as GuiVContainer;

                                if (comp is GuiVContainer vcon)
                                {
                                    for (var i = 0; i < vcon.Children.Count; i++)
                                    {
                                        GuiComponent Element = vcon.Children.ElementAt(i);
                                        p      = Element.Parent as GuiComponent;
                                        parent = (p != null) ? p.Id : null;
                                        var _newchild = new SAPEventElement(Element, session.Info.SystemName, parent, false);
                                        children.Add(_newchild);
                                    }
                                }
                                else if (comp is GuiContainer con)
                                {
                                    for (var i = 0; i < con.Children.Count; i++)
                                    {
                                        GuiComponent Element = con.Children.ElementAt(i);
                                        p      = Element.Parent as GuiComponent;
                                        parent = (p != null) ? p.Id : null;
                                        children.Add(new SAPEventElement(Element, session.Info.SystemName, parent, false));
                                    }
                                }
                                else if (comp is GuiStatusbar sbar)
                                {
                                    for (var i = 0; i < sbar.Children.Count; i++)
                                    {
                                        GuiComponent Element = sbar.Children.ElementAt(i);
                                        p      = Element.Parent as GuiComponent;
                                        parent = (p != null) ? p.Id : null;
                                        children.Add(new SAPEventElement(Element, session.Info.SystemName, parent, false));
                                    }
                                }
                                else
                                {
                                    throw new Exception("Unknown container type " + comp.Type + "!");
                                }
                            }

                            msg.Children = children.ToArray();
                        }
                        else
                        {
                            message.error = "SAP not running, or session " + msg.SystemName + " not found.";
                        }
                        message.Set(msg);
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                    catch (Exception ex)
                    {
                        message.error = ex.Message;
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                }
                if (message.action == "invokemethod" || message.action == "setproperty" || message.action == "getproperty" || message.action == "highlight")
                {
                    try
                    {
                        var step = message.Get <SAPInvokeMethod>();
                        if (step != null)
                        {
                            var session = SAPHook.Instance.GetSession(step.SystemName);
                            if (session != null)
                            {
                                GuiComponent comp = session.GetSAPComponentById <GuiComponent>(step.Id);
                                if (comp == null)
                                {
                                    if (step.Id.Contains("/tbar[1]/"))
                                    {
                                        comp = session.GetSAPComponentById <GuiComponent>(step.Id.Replace("/tbar[1]/", "/tbar[0]/"));
                                    }
                                }
                                if (comp == null)
                                {
                                    throw new Exception(string.Format("Can't find component using id {0}", step.Id));
                                }
                                string typeName = _prefix + comp.Type;
                                Type   t        = SAPGuiApiAssembly.GetType(typeName);
                                if (t == null)
                                {
                                    throw new Exception(string.Format("Can't find type {0}", typeName));
                                }
                                var Parameters = Newtonsoft.Json.JsonConvert.DeserializeObject <object[]>(step.Parameters);

                                if (message.action == "invokemethod")
                                {
                                    step.Result = t.InvokeMember(step.ActionName, System.Reflection.BindingFlags.InvokeMethod, null, comp, Parameters);
                                }
                                if (message.action == "setproperty")
                                {
                                    step.Result = t.InvokeMember(step.ActionName, System.Reflection.BindingFlags.SetProperty, null, comp, Parameters);
                                }
                                if (message.action == "getproperty")
                                {
                                    step.Result = t.InvokeMember(step.ActionName, System.Reflection.BindingFlags.GetProperty, null, comp, Parameters);
                                }
                                var vcomp = comp as GuiVComponent;

                                if (message.action == "highlight" && vcomp != null)
                                {
                                    try
                                    {
                                        if (_lastHighlight != null)
                                        {
                                            _lastHighlight.Visualize(false);
                                        }
                                    }
                                    catch (Exception)
                                    {
                                    }
                                    _lastHighlight = null;
                                    _lastHighlight = comp as GuiVComponent;
                                    _lastHighlight.Visualize(true);
                                }
                            }
                            else
                            {
                                message.error = "SAP not running, or session " + step.SystemName + " not found.";
                            }
                            message.Set(step);
                        }
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                    catch (Exception ex)
                    {
                        message.error = ex.Message;
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                }
            }
            catch (Exception ex)
            {
                log(ex.ToString());
                form.AddText(ex.ToString());
            }
        }
示例#2
0
        private static void Server_OnReceivedMessage(NamedPipeConnection <SAPEvent, SAPEvent> connection, SAPEvent message)
        {
            try
            {
                if (message == null)
                {
                    return;
                }
                form.AddText("[resc] " + message.action);
                if (message.action == "beginrecord")
                {
                    try
                    {
                        recordstarting = true;
                        var recinfo = message.Get <SAPToogleRecordingEvent>();
                        var overlay = false;
                        if (recinfo != null)
                        {
                            overlay = recinfo.overlay;
                            //StartMonitorMouse(recinfo.mousemove);
                            form.AddText("StartMonitorMouse::begin");
                            StartMonitorMouse(true);
                            form.AddText("StartMonitorMouse::end");
                        }
                        form.AddText("BeginRecord::begin");
                        SAPHook.Instance.BeginRecord(overlay);
                        form.AddText("BeginRecord::end");
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                        recordstarting = false;
                    }
                    catch (Exception ex)
                    {
                        message.error = ex.Message;
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                }
                if (message.action == "endrecord")
                {
                    try
                    {
                        StopMonitorMouse();
                        SAPHook.Instance.EndRecord();
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                    catch (Exception ex)
                    {
                        message.error = ex.Message;
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                }
                if (message.action == "login")
                {
                    try
                    {
                        var  login   = message.Get <SAPLoginEvent>();
                        bool dologin = true;
                        if (SAPHook.Instance.Sessions != null)
                        {
                            foreach (var session in SAPHook.Instance.Sessions)
                            {
                                if (session.Info.SystemName.ToLower() == login.SystemName.ToLower())
                                {
                                    dologin = false; break;
                                }
                            }
                        }
                        if (dologin)
                        {
                            if (SAPHook.Instance.Login(login))
                            {
                                var session = SAPHook.Instance.GetSession(login.SystemName);
                                if (session == null)
                                {
                                    message.error = "Login failed";
                                }
                            }
                            else
                            {
                                message.error = "Login failed";
                            }
                        }
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                    catch (Exception ex)
                    {
                        message.error = ex.Message;
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                }
                if (message.action == "getconnections")
                {
                    try
                    {
                        var result = new SAPGetSessions();
                        SAPHook.Instance.RefreshSessions();
                        result.Connections = SAPHook.Instance.Connections;
                        message.Set(result);
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                    catch (Exception ex)
                    {
                        message.error = ex.Message;
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                }

                if (message.action == "getitems")
                {
                    try
                    {
                        var msg         = message.Get <SAPEventElement>();
                        var session     = SAPHook.Instance.GetSession(msg.SystemName);
                        var sapelements = new List <GuiComponent>();
                        msg.Id = StripSession(msg.Id);
                        var paths = msg.Id.Split(new string[] { "/" }, StringSplitOptions.None);
                        for (var i = 0; i < paths.Length; i++)
                        {
                            string part  = paths[i];
                            int    index = ExtractIndex(ref part);
                            if (i == 0)
                            {
                                if (index > -1)
                                {
                                    sapelements.Add(session.Children.ElementAt(index));
                                }
                                if (index == -1)
                                {
                                    for (var x = 0; x < session.Children.Count; x++)
                                    {
                                        sapelements.Add(session.Children.ElementAt(x));
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        message.error = ex.Message;
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                }
                if (message.action == "getitem")
                {
                    try
                    {
                        var msg = message.Get <SAPEventElement>();
                        // if (string.IsNullOrEmpty(msg.SystemName)) throw new ArgumentException("System Name is mandatory right now!");
                        var session = SAPHook.Instance.GetSession(msg.SystemName);
                        if (session == null)
                        {
                            msg.Id = null;
                            message.Set(msg);
                            form.AddText("[send] " + message.action);
                            pipe.PushMessage(message);
                            return;
                        }
                        // msg.Id = StripSession(msg.Id);
                        GuiComponent comp = session.GetSAPComponentById <GuiComponent>(msg.Id);
                        if (comp is null)
                        {
                            msg.Id = null;
                            message.Set(msg);
                            form.AddText("[send] " + message.action);
                            pipe.PushMessage(message);
                            return;
                        }
                        msg = new SAPEventElement(comp, session.Info.SystemName, msg.GetAllProperties, msg.Path, msg.Cell, msg.Flat, true, msg.MaxItem, msg.VisibleOnly);
                        message.Set(msg);
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                    catch (Exception ex)
                    {
                        message.error = ex.Message;
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                }
                if (message.action == "invokemethod" || message.action == "setproperty" || message.action == "getproperty" || message.action == "highlight")
                {
                    try
                    {
                        var step = message.Get <SAPInvokeMethod>();
                        if (step != null)
                        {
                            var session = SAPHook.Instance.GetSession(step.SystemName);
                            if (session != null)
                            {
                                GuiComponent comp = session.GetSAPComponentById <GuiComponent>(step.Id);
                                if (comp == null)
                                {
                                    if (step.Id.Contains("/tbar[1]/"))
                                    {
                                        comp = session.GetSAPComponentById <GuiComponent>(step.Id.Replace("/tbar[1]/", "/tbar[0]/"));
                                    }
                                }
                                if (comp == null)
                                {
                                    throw new Exception(string.Format("Can't find component using id {0}", step.Id));
                                }
                                string typeName = _prefix + comp.Type;
                                Type   t        = SAPGuiApiAssembly.GetType(typeName);
                                if (t == null)
                                {
                                    throw new Exception(string.Format("Can't find type {0}", typeName));
                                }
                                var Parameters = Newtonsoft.Json.JsonConvert.DeserializeObject <object[]>(step.Parameters);

                                if (message.action == "invokemethod")
                                {
                                    step.Result = t.InvokeMember(step.ActionName, System.Reflection.BindingFlags.InvokeMethod, null, comp, Parameters);
                                }
                                if (message.action == "setproperty")
                                {
                                    step.Result = t.InvokeMember(step.ActionName, System.Reflection.BindingFlags.SetProperty, null, comp, Parameters);
                                }
                                if (message.action == "getproperty")
                                {
                                    step.Result = t.InvokeMember(step.ActionName, System.Reflection.BindingFlags.GetProperty, null, comp, Parameters);
                                }
                                var vcomp = comp as GuiVComponent;

                                if (message.action == "highlight" && vcomp != null)
                                {
                                    try
                                    {
                                        if (_lastHighlight != null)
                                        {
                                            _lastHighlight.Visualize(false);
                                        }
                                    }
                                    catch (Exception)
                                    {
                                    }
                                    _lastHighlight = null;
                                    _lastHighlight = comp as GuiVComponent;
                                    _lastHighlight.Visualize(true);
                                }
                            }
                            else
                            {
                                message.error = "SAP not running, or session " + step.SystemName + " not found.";
                            }
                            message.Set(step);
                        }
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                    catch (Exception ex)
                    {
                        message.error = ex.Message;
                        if (ex.InnerException != null)
                        {
                            message.error = ex.InnerException.Message;
                        }
                        form.AddText("[send] " + message.action);
                        pipe.PushMessage(message);
                    }
                }
            }
            catch (Exception ex)
            {
                log(ex.ToString());
                form.AddText(ex.ToString());
            }
        }