Exemple #1
0
        public static void TestNonVolatileMemory()
        {
            HeepTest nonVolatileTest = new HeepTest("Non Volatile Memory Test");

            String TestDeviceName = "Test Device";

            List <byte> ID = new List <byte>();

            for (byte i = 0; i < 4; i++)
            {
                ID.Add(i);
            }

            DeviceID   myID     = new DeviceID(ID);
            HeepDevice myDevice = new HeepDevice(myID);

            myDevice.SetDeviceName(TestDeviceName);

            List <byte> readMem = NonVolatileData.ReadMemoryFromFile();

            int    counter   = 1;
            String foundName = HeepParser.parseDeviceNameMOP(readMem, ref counter);

            nonVolatileTest.AddTest(new TestData("Device Name", TestDeviceName, foundName));

            nonVolatileTest.CheckTests();
        }
Exemple #2
0
        public static async void SendDeviceContext(HeepDevice theDevice)
        {
            string      deviceIDString = GetDeviceIDString(theDevice.GetDeviceID());
            string      project        = "heep-3cddb";
            FirestoreDb db             = FirestoreDb.Create(project);

            Console.WriteLine("Created Cloud Firestore client with project ID: {0}", project);

            string name = theDevice.GetDeviceName();
            Dictionary <string, object> user = new Dictionary <string, object>
            {
                { "Name", name },
            };
            WriteResult writeResult = await db.Collection("DeviceList").Document(deviceIDString).SetAsync(user);

            for (int i = 0; i < theDevice.GetControlList().Count; i++)
            {
                Control currentControl = theDevice.GetControlList()[i];
                Dictionary <string, object> controlDoc = new Dictionary <string, object>
                {
                    { "Name", currentControl.GetName() },
                    { "ID", currentControl.GetID() },
                    { "Type", (int)currentControl.GetControlType() },
                    { "Direction", (int)currentControl.GetControlDirection() },
                    { "HighValue", currentControl.GetHighValue() },
                    { "LowValue", currentControl.GetLowValue() }
                };

                string      controlDocName = "Control" + i;
                WriteResult controlResult  = await db.Collection("DeviceList").Document(deviceIDString).Collection("Controls").Document(controlDocName).SetAsync(controlDoc);
            }
        }
Exemple #3
0
        public static List <byte> ParseSetValueCommand(List <byte> commandBuffer, HeepDevice theDevice)
        {
            int controlID    = commandBuffer [2];
            int controlValue = commandBuffer [3];

            theDevice.SetControlByID(controlID, controlValue);

            return(AddSuccessMessageToBuffer("Successfully Set Value", theDevice));
        }
Exemple #4
0
        public static List <byte> ParseCommand(List <byte> commandBuffer, HeepDevice theDevice)
        {
            if (commandBuffer [0] == HeepLanguage.IsHeepDeviceOpCode)
            {
                return(ParseIsHeepDeviceCommand(commandBuffer, theDevice));
            }
            else if (commandBuffer [0] == HeepLanguage.SetValueOpCode)
            {
                return(ParseSetValueCommand(commandBuffer, theDevice));
            }
            else if (commandBuffer [0] == HeepLanguage.SetVertexOpCode)
            {
                return(ParseSetVertexCommand(commandBuffer, theDevice));
            }

            return(AddErrorMessageToBuffer("Invalid Command", theDevice));
        }
Exemple #5
0
        public static void StartListening(HeepDevice device, UdpClient client)
        {
            byte[] recData;
            while (true)
            {
                try
                {
                    IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
                    Debug.Log("About to receive");
                    recData = client.Receive(ref anyIP);
                    Debug.Log("Received");
                    List <byte> commandData = new List <byte>(recData);

                    if (HeepParser.isROP(commandData))
                    {
                        Debug.Log("Was ROP");
                        continue;
                    }

                    List <byte> fromparser = HeepParser.ParseCommand(commandData, device);

                    Debug.Log("IP Address: " + anyIP.Address + " Port: " + anyIP.Port);
                    Debug.Log("UDP Data Received: " + Encoding.ASCII.GetString(commandData.ToArray(), 0, commandData.Count));

                    String printableReturn = "";
                    for (int i = 0; i < fromparser.Count; i++)
                    {
                        printableReturn += fromparser[i] + " ";
                    }
                    Debug.Log("Sending: " + printableReturn);

                    UdpClient udpClientB = new UdpClient();
                    anyIP.Port = PORT;
                    udpClientB.Send(fromparser.ToArray(), fromparser.Count, anyIP);
                }
                catch (ObjectDisposedException) {
                    Debug.Log("Server disposed");
                    return;
                }
                catch (Exception err)
                {
                    Debug.Log("UDP Exception: " + err.ToString());
                }
            }
        }
Exemple #6
0
        private static List <byte> AddErrorMessageToBuffer(String message, HeepDevice theDevice)
        {
            List <byte> outputBuf = new List <byte>();

            outputBuf.Add(HeepLanguage.ErrorOpCode);
            HeepLanguage.AddDeviceIDToMemory(outputBuf, theDevice.GetDeviceID());

            byte stringLength = (byte)message.Length;

            outputBuf.Add(stringLength);

            for (int i = 0; i < message.Length; i++)
            {
                outputBuf.Add((byte)message [i]);
            }

            return(outputBuf);
        }
Exemple #7
0
        public static List <byte> ParseSetVertexCommand(List <byte> commandBuffer, HeepDevice theDevice)
        {
            int counter = 1;

            HeepLanguage.GetNumberFromBuffer(commandBuffer, ref counter, 1);
            DeviceID txID      = HeepLanguage.GetDeviceIDFromBuffer(commandBuffer, ref counter);
            DeviceID rxID      = HeepLanguage.GetDeviceIDFromBuffer(commandBuffer, ref counter);
            int      txControl = HeepLanguage.GetNumberFromBuffer(commandBuffer, ref counter, 1);

            int rxControl = HeepLanguage.GetNumberFromBuffer(commandBuffer, ref counter, 1);

            IPAddress destIP = HeepLanguage.GetIPAddrFromBuffer(commandBuffer, counter);

            Vertex newVertex = new Vertex(rxID, txID, rxControl, txControl, destIP);

            theDevice.AddVertex(newVertex);

            return(AddSuccessMessageToBuffer("Vertex Set", theDevice));;
        }
Exemple #8
0
        public static void StartListening(HeepDevice device, UdpClient client)
        {
            byte[] recData;
            while (true)
            {
                try
                {
                    IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
                    recData = client.Receive(ref anyIP);
                    List <byte> commandData = new List <byte>(recData);

                    if (HeepParser.isROP(commandData))
                    {
                        continue;
                    }

                    List <byte> fromparser = HeepParser.ParseCommand(commandData, device);

                    String printableReturn = "";
                    for (int i = 0; i < fromparser.Count; i++)
                    {
                        printableReturn += fromparser[i] + " ";
                    }

                    UdpClient udpClientB = new UdpClient();
                    anyIP.Port = PORT;
                    udpClientB.Send(fromparser.ToArray(), fromparser.Count, anyIP);
                }
                catch (ObjectDisposedException) {
                    return;
                }
                catch (Exception err)
                {
                }
            }
        }
Exemple #9
0
 public static List <byte> ParseIsHeepDeviceCommand(List <byte> commandBuffer, HeepDevice theDevice)
 {
     return(theDevice.GetMemoryDump());
 }
Exemple #10
0
        public static void StartHeepServer(HeepDevice device, UdpClient client)
        {
            Thread t = new Thread(() => StartListening(device, client));

            t.Start();
        }
Exemple #11
0
        public static void StartListening(HeepDevice device)
        {
            // Data buffer for incoming data.
            byte[] bytes = new Byte[1024];

            byte []    IPAddrArray   = { 0, 0, 0, 0 };
            IPAddress  theAddr       = new IPAddress(IPAddrArray);
            IPEndPoint localEndPoint = new IPEndPoint(theAddr, 5000);

            // Create a TCP/IP socket.
            Socket listener = new Socket(AddressFamily.InterNetwork,
                                         SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and
            // listen for incoming connections.
            try {
                listener.Bind(localEndPoint);
                listener.Listen(10);

                // Start listening for connections.
                while (true)
                {
                    Console.WriteLine("Waiting for a connection...");
                    // Program is suspended while waiting for an incoming connection.
                    Socket handler = listener.Accept();

                    Console.WriteLine("Data accepted");

                    data = null;

                    bytes = new byte[1024];
                    int bytesRec = handler.Receive(bytes);
                    Console.WriteLine(bytesRec);
                    data += Encoding.ASCII.GetString(bytes, 0, bytesRec);

                    // Show the data on the console.
                    Console.WriteLine("Text received : {0}", data);

                    List <byte> commandData = new List <byte> ();
                    for (int i = 0; i < bytesRec; i++)
                    {
                        commandData.Add(bytes[i]);
                    }

                    // Echo the data back to the client.
//					byte[] msg = Encoding.ASCII.GetBytes(data);
                    List <byte> fromparser = HeepParser.ParseCommand(commandData, device);
                    byte[]      msg        = new byte[fromparser.Count];

                    for (int i = 0; i < fromparser.Count; i++)
                    {
                        msg[i] = fromparser[i];
                    }


                    handler.Send(msg);
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }
            } catch (Exception e) {
                Console.WriteLine(e.ToString());
            }

            Console.WriteLine("\nPress ENTER to continue...");
            Console.Read();
        }
Exemple #12
0
        public static void StartHeepServer(HeepDevice device)
        {
            Thread t = new Thread(() => StartListening(device));

            t.Start();
        }