Exemplo n.º 1
0
        public ComData GenerateErrorMessage(int originalsender, int errorcode, string message)
        {
            ComData error = new ComData();

            //set the sender ID
            error.Set(0, 1);//1 = server

            //set the receiverID
            error.Set(1, originalsender);

            //set the corresponding command ID so it can be interpreted as 'error/warning'
            error.Set(2, 2);

            //supply error code
            if (errorcode < 0)
            {
                errorcode = 0;
            }
            error.Set(3, errorcode);

            //supply message
            if (message != null && message.Length > 0)
            {
                error.Set(4, message);
            }

            return(error);
        }
Exemplo n.º 2
0
        public void dealWithInput(string str)
        {
            //Data between main brackets have been entered "{<buffer>}"

            StringEater eater = new StringEater();    //initialise string eater object

            comdata.Reset();

            //set string to string eater
            eater.String(str);

            int index = 0;

            //iterate only till all parameter indexes have been filled
            while ((eater.String().Length != 0))    //conditions: index < MAX_PARAM or
            {
                eater.eatUntil(BracketEater_conditions);

                //when there is content in the buffer
                eater.buffer.Trim();
                if (eater.buffer.Length > 0)
                {
                    //cleanup trailing and leading whitespaces

                    //if parameter was a string, then remove quotes
                    if (eater.buffer[0] == '"' && eater.buffer[eater.buffer.Length - 1] == '"')
                    {
                        eater.buffer = eater.buffer.Substring(1, eater.buffer.Length - 2);

                        comdata.Set(index, eater.buffer);
                    }
                    else
                    {
                        switch (eater.buffer[0])
                        {
                        case 'i':
                        {
                            int number;
                            Int32.TryParse(eater.buffer, out number);

                            comdata.Set(index, number);
                            break;
                        }

                        case 'd':
                        {
                            double number;
                            double.TryParse(eater.buffer, out number);

                            comdata.Set(index, number);
                            break;
                        }

                        case 'f':
                        {
                            float number;
                            float.TryParse(eater.buffer, out number);

                            comdata.Set(index, number);
                            break;
                        }

                        case 'c':
                        {
                            if (eater.buffer.Length > 1)
                            {
                                comdata.Set(index, eater.buffer[1]);
                            }
                            break;
                        }

                        default:                        //automatic
                        {
                            double number;
                            double.TryParse(eater.buffer, out number);

                            comdata.Set(index, number);

                            //is a rounded number?
                            if (number != 0 || eater.buffer == "0")
                            {
                                if (Math.Round(number) == number)
                                {                                                //int
                                    if (number > 4294967295)
                                    {
                                        comdata.Set(index, number);                                                        //array fits only in double
                                    }
                                    else
                                    if (number > 2147483647)
                                    {
                                        comdata.Set(index, (ulong)number);
                                    }
                                    else
                                    if (number > 65535)
                                    {
                                        comdata.Set(index, (long)number);
                                    }
                                    else
                                    if (number > 32767)
                                    {
                                        comdata.Set(index, (uint)number);
                                    }
                                    else
                                    if (number < -4294967295)
                                    {
                                        comdata.Set(index, number);                                                        //array fits only in double
                                    }
                                    else
                                    if (number < -32767)
                                    {
                                        comdata.Set(index, (long)number);
                                    }
                                    else
                                    {
                                        comdata.Set(index, (int)number);
                                    }
                                }
                                else
                                {                                                //just a double
                                    comdata.Set(index, number);
                                }
                            }
                            else
                            {
                                comdata.Set(index, eater.buffer);
                            }
                            break;
                        }
                        }
                    }
                }

                //and count up parameter index
                index++;
            }
            //ELSE the buffer is empty, meaning no content between separator, like ',,,' (3x nothing)
            //do nothing

            usableInput = true;
        }
Exemplo n.º 3
0
        public void Add(ComData data, Communication handler)
        {
            if (!Program.MasterServer.Connected)//Then if connection with master server is there
            {
                return;
            }

            int target = 0;

            data.Get(0, ref target);

            int sender = 0;

            data.Get(1, ref sender);

            int commandid = 0;

            data.Get(2, ref commandid);

            //Reporter.AddReport(4, handler.DeviceName+ ": "+ data.ToString());

            switch (handler.devicetype.ToLower())
            {
            case "arduino":
            case "i/o":
            {
                if (target == -1 || target == Program.MasterServer.serverID)
                {
                    Program.MasterServer.Send(data);
                    return;
                }
                else if (target == handler.comID || sender == handler.comID)        //coming or going from device
                {
                    executable(data, handler);
                }

                if (target == handler.comID)
                {
                    if (commandid != 3 || target != sender)
                    {
                        handler.Send(data);
                    }
                }
                break;
            }

            case "server":
            case "pc":
            {
                if (target == -1)        //is -1 when
                {
                    foreach (Communication device in Program.LocalDevices)
                    {
                        if (device.Connected)
                        {
                            data.Set(0, device.comID);
                            device.unhandledcommands.executable(data, device);
                        }
                    }
                }
                else if (target == handler.comID)
                {
                    executable(data, handler);
                }
                else
                {
                    foreach (Communication device in Program.LocalDevices)
                    {
                        if (device.Connected && target == device.comID)
                        {
                            device.unhandledcommands.executable(data, device);
                        }
                    }
                }

                break;
            }
            }
        }