예제 #1
0
        private void ResponseCmd(ESMonitor.Model.TaskNotice model)
        {
            DevCtrlResponseCmd cmd = new DevCtrlResponseCmd();

            bool flag = cmd.DecodeFrame(model.Data, model.Length);

            if (flag)
            {
                switch (cmd.CmdByte)
                {
                case 0x1F:     //读取CMP
                {
                    double cmp = 0;
                    cmd.DecodeCMPReadCmd(ref cmp);
                    _json.AddItem("CMPState", "1");
                    _json.AddItem("CMP", cmp.ToString(CultureInfo.CurrentCulture));
                }

                break;

                case 0x2F:
                {
                    if (cmd.CmdType == 0xFB)
                    {
                        if (cmd.Data[0] == 0xFF)
                        {
                            _json.AddItem("coordinate", "null");
                            break;
                        }
                        byte[] data = new byte[24];
                        for (int i = 0; i < 24; i++)
                        {
                            if (cmd.Data[i] == 0x00)
                            {
                                data[i] = 0x30;
                            }
                            else
                            {
                                data[i] = cmd.Data[i];
                            }
                        }
                        string coordinate     = Encoding.ASCII.GetString(data, 0, 24).Insert(12, ",");
                        string url            = string.Format("http://api.map.baidu.com/geoconv/v1/?coords={0}&from=1&to=5&ak=0DpSiAEhexZzZR7c7pkYFq7E", coordinate);
                        var    request        = (HttpWebRequest)WebRequest.Create(url);
                        var    response       = request.GetResponse();
                        var    responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                        JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
                        var    obj       = (Dictionary <string, object>)((object[])((Dictionary <string, object>)jsonSerializer.DeserializeObject(responseString))["result"])[0];
                        double longitude = double.Parse(obj["x"].ToString());
                        double latitude  = double.Parse(obj["y"].ToString());
                        coordinate = longitude.ToString("F6") + ";" + latitude.ToString("F6");

                        _json.AddItem("coordinate", coordinate);
                        break;
                    }
                    UInt16 cycleTime = 0;
                    cmd.DecodeCMPCycleSetCmd(ref cycleTime);
                    _json.AddItem("Cycle", cycleTime.ToString());
                }
                break;

                case 0x3F:
                {
                    byte state = 0;
                    cmd.DecodeCMStopCmd(ref state);
                    _json.AddItem("State", state.ToString());
                }
                break;

                case 0x4F:
                {
                    byte state = 0;
                    cmd.DecodeBGTestStartCmd(ref state);
                    _json.AddItem("State", state.ToString());
                }
                break;

                case 0x5F:
                {
                    byte state = 0;
                    cmd.DecodeBGTestStopCmd(ref state);
                    _json.AddItem("State", state.ToString());
                }
                break;

                case 0x6F:
                {
                    byte state = 0;
                    cmd.DecodeBGTestResultCmd(ref state);
                    _json.AddItem("State", state.ToString());
                }
                break;

                case 0x7F:
                {
                    byte state = 0;
                    cmd.DecodeSPANTestStartCmd(ref state);
                    _json.AddItem("State", state.ToString());
                }
                break;

                case 0x8F:
                {
                    byte state = 0;
                    cmd.DecodeSPANTestStopCmd(ref state);
                    _json.AddItem("State", state.ToString());
                }
                break;

                case 0x9F:
                {
                    byte state = 0;
                    cmd.DecodeSPANTestResultCmd(ref state);
                    _json.AddItem("State", state.ToString());
                }
                break;

                case 0xAF:
                {
                    byte state = 0;
                    cmd.DecodeSetOUT1Cmd(ref state);
                    _json.AddItem("State", state.ToString());
                }
                break;

                case 0xBF:
                {
                    byte state = 0;
                    cmd.DecodeSetOUT2Cmd(ref state);
                    _json.AddItem("State", state.ToString());
                }
                break;

                case 0x1D:
                {
                    double noise = 0;
                    cmd.DecodeUpInstNoiseOpenCmd(ref noise);
                    _json.AddItem("Noise", noise.ToString(CultureInfo.CurrentCulture));
                }
                break;

                case 0x2D:
                {
                    cmd.DecodeUpInstNoiseCloseCmd();
                }
                break;

                case 0x3D:
                {
                    double noise = 0;
                    cmd.DecodeUpInstNoiseCmd(ref noise);
                    _json.AddItem("Noise", noise.ToString(CultureInfo.CurrentCulture));
                }
                break;

                case 0x4D:
                {
                    double noise = 0;
                    cmd.DecodeUpOneSecNoiseOpenCmd(ref noise);
                    _json.AddItem("Noise", noise.ToString(CultureInfo.CurrentCulture));
                }
                break;

                case 0x5D:
                {
                    cmd.DecodeUpOneSecNoiseCloseCmd();
                }
                break;

                case 0x6D:
                {
                    double noise = 0;
                    cmd.DecodeUpOneSecNoiseCmd(ref noise);
                    _json.AddItem("NoiseState", "1");
                    _json.AddItem("Noise", noise.ToString(CultureInfo.CurrentCulture));
                }
                break;

                case 0x7D:
                {
                    bool state;
                    state = cmd.DecodeSetZWCmd();
                    _json.AddItem("State", (Convert.ToByte(state)).ToString());
                }
                break;

                case 0x8D:
                {
                    bool state = false;
                    cmd.DecodeSetCWCmd();
                    _json.AddItem("State", (Convert.ToByte(state)).ToString());
                }
                break;

                case 0x9D:
                {
                    bool state = false;
                    cmd.DecodeSetAWCmd();
                    _json.AddItem("State", (Convert.ToByte(state)).ToString());
                }
                break;

                case 0xAD:
                {
                    bool state = false;
                    cmd.DecodeSetFGCmd();
                    _json.AddItem("State", (Convert.ToByte(state)).ToString());
                }
                break;

                case 0xBD:
                {
                    bool state = false;
                    cmd.DecodeSetSGCmd();
                    _json.AddItem("State", (Convert.ToByte(state)).ToString());
                }
                break;

                case 0xCD:
                {
                    bool state = false;
                    cmd.DecodeSetIGCmd();
                    _json.AddItem("State", (Convert.ToByte(state)).ToString());
                }
                break;

                case 0x1B:
                {
                    byte state = 0;
                    flag = cmd.DecodeWindDirWriteDevAddrCmd(ref state);
                    _json.AddItem("State", state.ToString());
                }
                break;

                case 0x2B:
                {
                    byte   addr    = 0;
                    UInt16 windDir = 0;
                    cmd.DecodeReadWindDirCmd(ref addr, ref windDir);
                    _json.AddItem("WindDirState", "1");
                    _json.AddItem("WindDir", windDir.ToString());
                }
                break;

                case 0x1A:
                {
                    byte state = 0;
                    cmd.DecodeWindSpeedWriteDevAddrCmd(ref state);
                    _json.AddItem("State", state.ToString());
                }
                break;

                case 0x2A:
                {
                    byte   addr      = 0;
                    double windSpeed = 0;
                    cmd.DecodeReadWindSpeedCmd(ref addr, ref windSpeed);
                    _json.AddItem("WindSpeedState", "1");
                    _json.AddItem("WindSpeed", windSpeed.ToString());
                }
                break;

                case 0x19:
                {
                    double temperature = 0, humidity = 0;
                    flag = cmd.DecodeReadESDataCmd(ref temperature, ref humidity);
                    _json.AddItem("ESState", "1");
                    _json.AddItem("Temp", temperature.ToString());
                    _json.AddItem("Humidity", "1");
                    _json.AddItem("Humidity", humidity.ToString());
                }
                break;

                case 0x08:
                {
                    ESData esModel = new ESData();

                    cmd.DecodeReadAllDataCmd(ref esModel);

                    _json.AddItem("PMState", esModel.PmState.ToString());
                    _json.AddItem("Pm25", esModel.Pm25.ToString());
                    _json.AddItem("Pm100", esModel.Pm100.ToString());

                    _json.AddItem("CMPState", esModel.CmpState.ToString());
                    _json.AddItem("CMP", esModel.Cmp.ToString());

                    _json.AddItem("NoiseState", esModel.NoiseState.ToString());
                    _json.AddItem("Noise", esModel.Noise.ToString());

                    _json.AddItem("WindDirState", esModel.WindDirState.ToString());
                    _json.AddItem("WindDir", esModel.WindDir.ToString());

                    _json.AddItem("WindSpeedState", esModel.WindSpeedState.ToString());
                    _json.AddItem("WindSpeed", esModel.WindSpeed.ToString());

                    _json.AddItem("ESState", esModel.ESState.ToString());
                    _json.AddItem("Temp", esModel.Temperature.ToString());
                    _json.AddItem("Humidity", esModel.Humidity.ToString());
                    _json.AddItem("Time", model.UpdateTime.ToString());
                }
                break;

                case 0x17:
                {
                    byte state = 0;
                    cmd.DecodeSwitchAutoReport(ref state);
                    _json.AddItem("State", state.ToString());
                }
                break;
                }

                _json.ItemOk();
            }
            else
            {
                _json.success = false;
                _json.error   = "解析命令错误";
            }
        }