Example #1
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            MachinaBridgeSocket   ms      = null;
            List <Machina.Action> actions = new List <Machina.Action>();
            bool send = false;

            if (!DA.GetData(0, ref ms))
            {
                return;
            }
            if (!DA.GetDataList(1, actions))
            {
                return;
            }
            if (!DA.GetData(2, ref send))
            {
                return;
            }

            if (ms == null || ms.socket == null || !ms.socket.IsAlive)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Not valid Bridge connection.");
                return;
            }

            List <string> instructions = new List <string>();

            if (send)
            {
                string ins = "";

                foreach (Machina.Action a in actions)
                {
                    // "DefineTool" is now an Action on its own, so shouldn't need this!
                    //// If attaching a tool, send the tool description first.
                    //// This is quick and dirty, a result of this component not taking the robot object as an input.
                    //// How coud this be improved...? Should tool creation be an action?
                    //if (a.type == Machina.ActionType.Attach)
                    //{
                    //    ActionAttach aa = (ActionAttach)a;
                    //    ins = aa.tool.ToInstruction();
                    //    instructions.Add(ins);
                    //    ms.socket.Send(ins);
                    //}

                    ins = a.ToInstruction();
                    instructions.Add(ins);
                    ms.socket.Send(ins);
                }

                DA.SetData(0, "Sent!");
            }
            else
            {
                DA.SetData(0, "Nothing sent");
            }

            DA.SetDataList(1, instructions);
        }
Example #2
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // This stops the component from assigning nulls
            // if we don't assign anything to an output.
            DA.DisableGapLogic();

            MachinaBridgeSocket ms = null;
            bool autoUpdate        = true;
            int  millis            = 1000;

            if (!DA.GetData(0, ref ms))
            {
                return;
            }
            if (!DA.GetData(1, ref autoUpdate))
            {
                return;
            }
            if (!DA.GetData(2, ref millis))
            {
                return;
            }

            // Some sanity
            if (millis < 10)
            {
                millis = 10;
            }
            if (ms == null || ms.socket == null || !ms.socket.IsAlive)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Not valid Bridge connection.");
                return;
            }

            // Output the last received message from the last cycle
            DA.SetData(0, _lastMsg);

            // Stop triggering expiration updates if the buffer is empty
            int size = ms.BufferSize();

            if (_updateOut && size == 0)
            {
                _updateOut = false;

                // And go back to regular autoupdate
                if (autoUpdate)
                {
                    this.OnPingDocument().ScheduleSolution(millis, doc => {
                        this.ExpireSolution(false);
                    });
                }

                return;
            }

            // If there are messagges logged by the MS, trigger a chain of expiration updates
            if (size > 0)
            {
                _updateOut = true;
                _lastMsg   = ms.FetchFirst(true);

                // Schedule a new solution right away
                this.OnPingDocument().ScheduleSolution(5, doc => {
                    this.ExpireSolution(false);
                });

                return;
            }

            // Otherwise, back to reguar autoupdate
            if (autoUpdate)
            {
                this.OnPingDocument().ScheduleSolution(millis, doc =>
                {
                    this.ExpireSolution(false);
                });
            }
        }
Example #3
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            string url        = "";
            string clientName = "";
            bool   connect    = false;

            if (!DA.GetData(0, ref url))
            {
                return;
            }
            if (!DA.GetData(1, ref clientName))
            {
                return;
            }
            if (!DA.GetData(2, ref connect))
            {
                return;
            }

            url += "?name=" + clientName;

            _ms = _ms ?? new MachinaBridgeSocket(clientName);

            bool          connectedResult = false;
            List <string> msgs            = new List <string>();

            // @TODO: move all socket management inside the wrapper
            if (connect)
            {
                if (_ms.socket == null)
                {
                    _ms.socket = new WebSocket(url);
                }

                if (!_ms.socket.IsAlive)
                {
                    _ms.socket.Connect();
                    _ms.socket.OnMessage += Socket_OnMessage;
                    _ms.socket.OnClose   += Socket_OnClose;
                }

                connectedResult = _ms.socket.IsAlive;

                if (connectedResult)
                {
                    msgs.Add("Connected to Machina Bridge");
                }
                else
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Could not connect to Machina Bridge app");
                    return;
                }
            }
            else
            {
                if (_ms.socket != null)
                {
                    _ms.socket.Close(CloseStatusCode.Normal, "k thx bye!");
                    _ms.socket = null;
                    _ms.Flush();

                    msgs.Add("Disconnected from the bridge");
                }
                connectedResult = false;
            }

            DA.SetDataList(0, msgs);
            DA.SetData(1, connectedResult ? _ms : null);
        }