Ejemplo n.º 1
0
        //对输入的请求消息进行解码,解码信息保存在format中
        static public int DeRequest(byte[] Msg, out RequestFormat Format)
        {
            Format = new RequestFormat();
            int i = 0;
            int j = 0;

            //检查请求消息长度
            if (Msg == null | Msg.Length < MIN_REQUEST_MSGSIZE)
            {
                return(SENDER_FAULT);
            }

            //对操作类型进行解码
            byte temp = Msg[0];

            Format.FunCode = (byte)(temp >> 5);
            if (Format.FunCode > 0x03)
            {
                return(FUNCODE_INVALID);
            }

            //对操作设备进行解码
            temp          = Msg[0];
            Format.Device = (byte)((byte)((temp >> 1) & 0x0f));
            if (Format.Device > 0x06)
            {
                return(DEVICECODE_INVALID);
            }

            //对二值参数进行解码
            temp         = Msg[0];
            Format.State = ((byte)(temp & 0x01)) == 0x01 ? true : false;

            //对整型参数进行解码
            byte[] value = new byte[4];
            for (i = 0, j = 1; j < 5; i++, j++)
            {
                value[i] = Msg[j];
            }
            Format.Value = BitConverter.ToInt32(value, 0);

            if (Msg.Length > MIN_REQUEST_MSGSIZE)    //如果消息长度超过5字节,对可选项进行解码
            {
                value[0] = Msg[5];
                value[1] = Msg[6];
                value[2] = Msg[7];
                value[3] = Msg[8];
                int lenght = BitConverter.ToInt32(value, 0);    //取得可选数组的长度
                if ((lenght + 1) * 4 != Msg.Length - MIN_REQUEST_MSGSIZE)
                {
                    return(SENDER_FAULT);
                }
                Format.Config    = new int[lenght + 1];
                Format.Config[0] = lenght;
                for (i = 1, j = 9; i <= lenght; i++, j = j + 4)    //对可选数组进行解码
                {
                    value[0]         = Msg[j];
                    value[1]         = Msg[j + 1];
                    value[2]         = Msg[j + 2];
                    value[3]         = Msg[j + 3];
                    Format.Config[i] = BitConverter.ToInt32(value, 0);
                }
            }
            return(FORMAT_OK);
        }
Ejemplo n.º 2
0
 //对输入的请求消息结构体进行编码,存放在msgFormat数组中,返回编码后的消息长度(字节数)
 static public int EnRequest(RequestFormat Format, out byte[] MsgFormat)
 {
     return(EnRequest(Format.FunCode, Format.Device, Format.State, Format.Value, Format.Config, out MsgFormat));
 }
Ejemplo n.º 3
0
        //对相关设备进行相关操作
        public bool Operate(OPERATE Operate, DEVICE Device, ref bool State, ref int Value, ref int[] Config, ref string erro)
        {
            RequestFormat  request = new RequestFormat(); //将请求封装成Socket请求信息
            ResponseFormat response;                      //Socket响应信息用于接受响应消息
            int            value = Value;                 //用于暂时存放参数Value值,Value值即可能是传进参数又可能是传出参数
            bool           state = State;                 //用于暂时存放参数State值,State值即可能是传进参数又可能是传出参数

            int[] config = Config;                        //用于暂时存放参数Config值,Config值即可能是传进参数又可能是传出参数
            Config          = null;
            request.FunCode = (byte)((int)Operate);
            request.Device  = (byte)((int)Device);
            switch (Operate)           //根据操作,用相应的参数对request进行封装
            {
            case OPERATE.MODIFY_STATE: //该操作为更改开关状态操作,参数State表示请求打开还是关闭
            {
                if (Device == DEVICE.VEDIO)
                {
                    request.State = state;
                    request.Value = value;
                }
                else if (Device == DEVICE.CAMERA)
                {
                    request.State     = state;
                    request.Value     = value;
                    request.Config    = new int[config.Length + 1];
                    request.Config[0] = config.Length;
                    int j = 1;
                    foreach (int i in config)
                    {
                        request.Config[j] = i;
                        j++;
                    }
                }
                else
                {
                    erro += "你选择的设备不能进行更改开关状态操作!";
                    return(false);
                }
                break;
            }

            case OPERATE.QUERY_PARAM:    //该操作为查询操作,根据Value来判断,如何Value为0说明是查状态,如何Value为1说明是查参数,如何Value为2说明是查配置信息
            {
                request.Value = value;   //需要做出是否合理的判断
                break;
            }

            case OPERATE.ADJUST_PARAM:       //该操作为调节参数操作
            {
                if (Device == DEVICE.FILTER) //调节空调操作,参数为State
                {
                    if (state == true)
                    {
                        request.Value = value;
                    }
                    else
                    {
                        request.Value = -1;
                    }
                }
                else if (Device == DEVICE.VEDIO)
                {
                    request.State     = state;
                    request.Value     = value;
                    request.Config    = new int[config.Length + 1];
                    request.Config[0] = config.Length;
                    int j = 1;
                    foreach (int i in config)
                    {
                        request.Config[j] = i;
                        j++;
                    }
                }
                else
                {
                    erro += "你选择的设备不能进行调节参数操作!";
                    return(false);
                }
                break;
            }

            case OPERATE.CONFIG_PARAM:    //该操作为配置操作
            {
                if (config == null)
                {
                    erro += "配置操作时,传入参数出错!";
                    return(false);
                }
                if (Device == DEVICE.ALL)              //全局配置
                {
                    if (config.Length != ConfigAllNum) //全局配置时,传入参数只有一个
                    {
                        erro += "全局配置时,传入参数出错!";
                        return(false);
                    }
                    else
                    {
                        request.Config    = new int[config.Length + 1];
                        request.Config[0] = config.Length;
                        int j = 1;
                        foreach (int i in config)
                        {
                            request.Config[j] = i;
                            j++;
                        }
                    }
                }
                else
                {
                    if (Device == DEVICE.VEDIO || Device == DEVICE.FILTER)
                    {
                        if (config.Length != 1)        //单一配置时,传入参数只有一个
                        {
                            erro += "视频与滤片配置,传入参数出错!";
                            return(false);
                        }
                        else
                        {
                            request.Value = config[0];
                        }
                    }
                    else
                    {
                        erro += "你选择的设备不能进行配置操作!";
                        return(false);
                    }
                }
                break;
            }

            default:
            {
                erro += "选择的操作有误!";
                return(false);
            }
            }

            int ret = SendAnAcceptControlMsg(request, ref erro, out response);//发送请求消息和接受响应消息

            if (ret == -1 && ret == 4)
            {
                erro += "Socket传输错误!";
                return(false);//出错
            }
            else
            {
                if (response.IsSucceed == true)//该操作成功
                {
                    erro = Encoding.ASCII.GetString(response.Info);
                    switch (Operate)
                    {
                    case OPERATE.MODIFY_STATE:    //该操作为更改开关状态操作
                    {
                        if (Device == DEVICE.CAMERA || Device == DEVICE.VEDIO)
                        {
                            Value = response.Value;
                        }
                        return(true);
                    }

                    case OPERATE.QUERY_PARAM:  //该操作为查询参数操作
                    {
                        if (value == 0)        //查询设备状态
                        {
                            if (response.Value == 1)
                            {
                                State = true;
                            }
                            else
                            {
                                State = false;
                            }
                            return(true);
                        }
                        else if (value == 1)        //查询设备参数
                        {
                            if (Device == DEVICE.CAMERA)
                            {
                                if (response.Config == null || response.Config[0] != 6)        //全局查询配置时,返回值有8个
                                {
                                    erro += "查询图像采集配置时,返回值有误!";
                                    return(false);
                                }
                                else
                                {
                                    Config = new int[6];
                                    int j = 0;
                                    for (int i = 1; i < 6; i++)
                                    {
                                        Config[j] = response.Config[i];
                                        j++;
                                    }
                                    return(true);
                                }
                            }
                            else if (Device == DEVICE.VEDIO)
                            {
                                if (response.Config == null || response.Config[0] != 6)        //全局查询配置时,返回值有8个
                                {
                                    erro += "查询视频采集配置时,返回值有误!";
                                    return(false);
                                }
                                else
                                {
                                    Config = new int[6];
                                    int j = 0;
                                    for (int i = 1; i < 6; i++)
                                    {
                                        Config[j] = response.Config[i];
                                        j++;
                                    }
                                    return(true);
                                }
                            }
                            else if (Device == DEVICE.FILTER)
                            {
                                Value = response.Value;
                            }
                            return(true);
                        }
                        else if (value == 2)        //查询全局配置信息操作
                        {
                            if (Device == DEVICE.ALL)
                            {
                                if (response.Config == null || response.Config[0] != 16)        //全局查询配置时,返回值有8个
                                {
                                    erro += "全局查询配置信息时,返回值有误!";
                                    return(false);
                                }
                                else
                                {
                                    Config = new int[16];
                                    int j = 0;
                                    for (int i = 1; i < 16; i++)
                                    {
                                        Config[j] = response.Config[i];
                                        j++;
                                    }
                                    return(true);
                                }
                            }
                            else
                            {
                                Config    = new int[1];
                                Config[0] = response.Value;
                                return(true);
                            }
                        }
                        else
                        {
                            erro += "查询操作时,传出的Value标志有误!";
                            return(false);
                        }
                    }

                    case OPERATE.ADJUST_PARAM:    //该操作为调节参数操作
                    {
                        Value = response.Value;
                        return(true);
                    }

                    case OPERATE.CONFIG_PARAM:    //该操作为配置操作
                    {
                        return(true);
                    }

                    default:
                    {
                        erro += "!@#$%^&*()";
                        return(false);
                    }
                    }
                }
                else//该操不成功
                {
                    if (response.Info == null)
                    {
                        erro += "板子崩毁!";
                    }
                    else
                    {
                        erro = Encoding.ASCII.GetString(response.Info);
                    }
                    return(false);
                }
            }
        }