Пример #1
0
        public static void SendError()
        {
            string dummyError = "ERR0, Testing Error Message and Error Message Value!";

            SQLHelper getCityControl = new SQLHelper("SELECT ip FROM units WHERE unit_id='" + cityID + "'"); //get city IP

            getCityControl.Run_Cmd();
            getCityControl.SetIPs();

            List <string> city_ip_list = getCityControl.Get_List(); // shove city ip in a list (probably un-necessary)

            foreach (var ip in city_ip_list)
            {
                var client = new TcpClient(ip, portNum);                   // connect to the city ip

                NetworkStream ns = client.GetStream();                     // establish the network stream

                byte[] outgoing_msg = Encoding.ASCII.GetBytes(dummyError); // turn our message into bytes

                try
                {
                    ns.Write(outgoing_msg, 0, outgoing_msg.Length); // try to send it
                }

                catch (Exception e)
                {
                    Console.WriteLine(e.ToString()); // if any errors, print them and break
                    break;
                }
            }
        }
Пример #2
0
        public static void FindCity()
        {
            // Set ACK message
            string ackmsg = "CCNCS";

            // Fetch ips in a list
            SQLHelper getips = new SQLHelper("SELECT ip FROM units");

            getips.Run_Cmd();
            getips.SetIPs();

            // Fetch unit ids in a list
            SQLHelper getids = new SQLHelper("SELECT unit_id FROM units");

            getids.Run_Cmd();
            getids.SetIDs();

            // Make a new list for the ips and ids
            List <string> iplist = getips.Get_List();
            List <string> idlist = getids.Get_List();

            // Now go through down each ip and id in the lists and request the data
            foreach (var address in iplist)
            {
                foreach (var id in idlist)
                {
                    // Create a new TCP Client with the address and default port number
                    var client = new TcpClient(address, portNum);

                    // Establish a network Stream
                    NetworkStream ns = client.GetStream();

                    // Setup a byte array
                    byte[] incoming_msg = new byte[1024];

                    // Read the bytes from the network stream into the array
                    int bytesRead = ns.Read(incoming_msg, 0, incoming_msg.Length);

                    // Format to string
                    string received = Encoding.ASCII.GetString(incoming_msg, 0, bytesRead);

                    // if the message that is received is our defined acknowledement message, we have the town control.
                    if (received == ackmsg)
                    {
                        //Change the unit_ID to CCNCS
                        SQLHelper changeID = new SQLHelper("UPDATE units SET unit_id='CCNCS' WHERE ip= '" + address + "'");
                        changeID.Run_Cmd();

                        // Then send the city ACK message
                        byte[] outgoing_msg = new byte[1024];

                        // populate the byte array with our acknowledgement message
                        byte[] byte_cityack = Encoding.ASCII.GetBytes(townackmsg);

                        // try to write the acknowledgement message
                        try
                        {
                            ns.Write(byte_cityack, 0, byte_cityack.Length);
                            ns.Close();
                            client.Close();
                        }

                        catch (Exception e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                    }
                }
            }
        }
Пример #3
0
        public static void TcpTownClient()
        {
            // Fetch ips in a list
            Console.WriteLine("Fetching IPs");
            SQLHelper getips = new SQLHelper("SELECT ip FROM units");

            getips.Run_Cmd();
            getips.SetIPs();

            // Fetch unit ids in a list
            Console.WriteLine("Fetching IDs");
            SQLHelper getids = new SQLHelper("SELECT unit_id FROM units");

            getids.Run_Cmd();
            getids.SetIDs();

            // Make a new list for the ips and ids
            List <string> iplist = getips.Get_List();
            List <string> idlist = getids.Get_List();

            // Now go through down each ip and id in the lists and request the data
            foreach (var address in iplist)
            {
                foreach (var id in idlist)
                {
                    // if the id is not our city identifier, proceed with asking for levels
                    if (id.ToString() != cityID) // !potential logic error != continues code, == skips... no idea why?
                    {
                        //debugging
                        //var checkme = id;

                        // Tell who we are connecting to
                        Console.WriteLine("Connecting to Unit" + id + "...");

                        // Create a new TCP Client with the address and default port number
                        var client = new TcpClient(address, portNum);

                        // Establish a network Stream
                        NetworkStream ns = client.GetStream();

                        // Setup a byte array
                        byte[] bytes = new byte[1024];

                        // Read the bytes from the network stream into the array
                        int bytesRead = ns.Read(bytes, 0, bytes.Length);

                        // Format to string
                        string received = Encoding.ASCII.GetString(bytes, 0, bytesRead);

                        // Turn the input levels to an array
                        string[] levels = received.Split(',');

                        // Turn each level into a double
                        double wat = Convert.ToDouble(levels[0]);
                        double sew = Convert.ToDouble(levels[1]);
                        double pow = Convert.ToDouble(levels[2]);

                        // Generate a timestamp
                        Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

                        // Insert values with associated unit id into Database
                        SQLHelper insertcmd = new SQLHelper("INSERT INTO status VALUES(" + unixTimestamp + "," + "'" + id + "'," + wat + "," + sew + "," + pow + ")");
                        insertcmd.Run_Cmd();

                        // Report levels
                        Console.WriteLine("Unit " + id + " reports " + wat + " water, " + sew + " sewage, " + pow + " power");
                    }

                    // if we are dealing with a city, we need to send the acknowledgement message - sp spin up a server
                    else
                    {
                        // echo we're ignoring
                        Console.WriteLine("Skipping City...");
                    }
                }
            }
        }