예제 #1
0
        //Allows interaction with pod control parameters
        //Can be used from the pod state classes or more
        //directly from the gui during developement
        public bool setParameters(string node, List <DataParameter> parameters)
        {
            rPodI2CTX       formatter = new rPodI2CTX();
            rPodNetworkNode n         = rPodNodeDiscovery.ActiveNodes.FirstOrDefault(x => x.NodeNamePretty == node);

            if (n == null)
            {
                return(false);
            }
            string ip = n.IP;

            if (n.RequestSocket == null)
            {
                n.RequestSocket = new RequestSocket();
            }

            n.RequestSocket.Connect("tcp://" + ip + ":6789");
            n.RequestSocket.TrySendFrame(TimeSpan.FromSeconds(1), formatter.formatTransmitParameters(parameters));
            string reply;

            if (!n.RequestSocket.TryReceiveFrameString(TimeSpan.FromSeconds(1), out reply))
            {
                n.RequestSocket.Disconnect("tcp://" + ip + ":6789");
                return(false);
            }
            else if (reply == "Got It")
            {
                return(true);
            }
            return(false);
        }
        public static void ReceiveCallback(IAsyncResult ar)
        {
            UdpClient  u = (UdpClient)((UdpState)(ar.AsyncState)).u;
            IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;

            if (Paused)
            {
                if (IsRunning)
                {
                    u.BeginReceive(new AsyncCallback(ReceiveCallback), ((UdpState)(ar.AsyncState)));
                }
                return;
            }

            Byte[] receiveBytes;

            try
            {
                receiveBytes = u.EndReceive(ar, ref e);
            }
            catch (ObjectDisposedException ex)
            {
                return;
            }

            string receiveString = Encoding.ASCII.GetString(receiveBytes);

            Console.WriteLine("Received: {0}\n", receiveString);

            rPodNetworkNode n = new rPodNetworkNode(receiveString.Substring(6), e.Address.ToString(), false);

            n.NodeAnnounceSeen();

            if (ActiveNodes == null)
            {
                ActiveNodes = new List <rPodNetworkNode>();
            }

            if (ActiveNodes.Contains(n) == false)
            {
                ActiveNodes.Add(n);
                FoundNewNode(n);
            }
            else
            {
                ActiveNodes.FirstOrDefault(x => x.Equals(n)).NodeAnnounceSeen();
            }

            if (IsRunning)
            {
                u.BeginReceive(new AsyncCallback(ReceiveCallback), ((UdpState)(ar.AsyncState)));
            }
        }
예제 #3
0
        //Called when a new node is detected on the network
        public static void NewNodeDetected(rPodNetworkNode node)
        {
            ZMQTelemetryProcessor processor = new ZMQTelemetryProcessor();

            processor.NetworkNode = node;
            processor.Ip          = node.IP;
            Thread newThread = new Thread(processor.zmqBeginListen);

            node.TelemetryProcessor = processor;
            node.NodeZMQSeen();
            runningThreads.Add(newThread);
            newThread.Start();
        }
예제 #4
0
        //Allows interaction with pod control parameters
        //Can be used from the pod state classes or more
        //directly from the gui during developement
        public static bool setParameters(string node, List <DataParameter> parameters)
        {
            rPodI2CTX formatter = new rPodI2CTX();

            //catch if that list is empty
            if (rPodNodeDiscovery.ActiveNodes == null || rPodNodeDiscovery.ActiveNodes.Count == 0)
            {
                return(false); //No nodes is definitely a failure
            }
            rPodNetworkNode n = rPodNodeDiscovery.ActiveNodes.FirstOrDefault(x => x.NodeNamePretty == node);

            if (n == null)
            {
                return(false);
            }
            string ip = n.IP;

            //Catch any wonky network exceptions and indicate that we failed
            try
            {
                if (n.RequestSocket == null)
                {
                    n.RequestSocket = new RequestSocket();
                }

                n.RequestSocket.Connect("tcp://" + ip + ":6789");
                n.RequestSocket.TrySendFrame(TimeSpan.FromSeconds(1), formatter.formatTransmitParameters(parameters));
                string reply;
                if (!n.RequestSocket.TryReceiveFrameString(TimeSpan.FromSeconds(1), out reply))
                {
                    n.RequestSocket.Disconnect("tcp://" + ip + ":6789");
                    n.RequestSocket.Close();
                    return(false);
                }
                else if (reply == "Got It")
                {
                    return(true);
                }
                return(false);
            }catch
            {
                return(false);
            }
        }
        public static void ReceiveCallback(IAsyncResult ar)
        {
            UdpClient  u = (UdpClient)((UdpState)(ar.AsyncState)).u;
            IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;

            if (Paused)
            {
                if (IsRunning)
                {
                    u.BeginReceive(new AsyncCallback(ReceiveCallback), ((UdpState)(ar.AsyncState)));
                }
                return;
            }

            Byte[] receiveBytes;

            try
            {
                receiveBytes = u.EndReceive(ar, ref e);
            }
            catch (ObjectDisposedException ex)
            {
                return;
            }

            string receiveString = Encoding.ASCII.GetString(receiveBytes);

            Console.WriteLine("Received: {0}\n", receiveString);

            string[] tokens = receiveString.Split(',');
            if (tokens.Length == 5)
            {
                if (tokens[0] == "rPod!")
                {
                    int      id      = int.Parse(tokens[1]);
                    DateTime PiTime  = DateTime.Parse(tokens[3]);
                    bool     logging = false;
                    if (tokens[4] == "yes")
                    {
                        logging = true;
                    }
                    rPodNetworkNode n = new rPodNetworkNode(tokens[2], e.Address.ToString(), false, logging, PiTime);
                    n.NodeAnnounceSeen(logging, PiTime);

                    if (ActiveNodes == null)
                    {
                        ActiveNodes = new List <rPodNetworkNode>();
                    }

                    if (ActiveNodes.Contains(n) == false)
                    {
                        ActiveNodes.Add(n);
                        if (FoundNewNode != null)
                        {
                            FoundNewNode(n);
                        }
                    }
                    else
                    {
                        ActiveNodes.FirstOrDefault(x => x.Equals(n)).NodeAnnounceSeen(logging, PiTime);
                    }
                }
            }
            if (IsRunning)
            {
                u.BeginReceive(new AsyncCallback(ReceiveCallback), ((UdpState)(ar.AsyncState)));
            }
        }