Esempio n. 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   = "解析命令错误";
            }
        }
Esempio n. 2
0
        public override bool ProcessCommand(byte[] buffer, int offset, int packetLen)//packetLen是总的数据长度
        {
            var res = _responseCmd.DecodeFrame(buffer, packetLen);

            if (!res)
            {
                return(false);
            }

            var devId = DevsManage.GetDevId(_responseCmd.NodeId);

            if (devId <= 0)
            {
                return(false);
            }

            //Check command
            switch ((ProtocolCmdType)_responseCmd.CmdType)
            {
            case ProtocolCmdType.SysComm:
                if (_responseCmd.CmdByte == (Byte)ProtocolCmdByte.HeartBeat)
                {
                    if (!DevsManage.IsRegistered(devId))
                    {
                        DevsManage.Register(devId);    //注册
                        DevsManage.SetDevStatus(devId, (int)CommStatus.Free);
                        EsProtocolMgr.Add(devId, this);
                        AdjustTime();
                    }
                    else
                    {
                        if (!EsProtocolMgr.IsSocketConect(devId, this))
                        {
                            EsProtocolMgr.Add(devId, this);    //此处顺序有问题
                        }
                    }

                    DevsManage.SetTimeOutCount(devId, 3);

                    //更新心跳时间
                    DevsManage.UpdateHeartBeatTime(devId);

                    DoHeartBeat();
                }
                break;

            case ProtocolCmdType.ModuleCfg:
                break;

            case ProtocolCmdType.ModuleCtrl:
                if (_responseCmd.CmdByte == (byte)ProtocolCmdByte.GetGpsInfo)
                {
                    DevsManage.UpdateRecvTime(devId);
                    AddTaskResponse(devId, buffer, packetLen);
                }
                break;

            case ProtocolCmdType.DevCtrl:
                break;

            case ProtocolCmdType.DevResponse:
                if (_responseCmd.CmdByte == (Byte)ProtocolCmdByte.AutoUploadAllEsParams)
                {
                    var esParams = new EsData();

                    if (_responseCmd.DecodeAutoUploadEsParamsCmd(ref esParams))
                    {
                        var dataId = AddEsMin(devId, esParams);
                        var model  = new ESMonitor.Model.TaskNotice();
                        try
                        {
                            model.DevId      = devId;
                            model.Data       = buffer;
                            model.Length     = packetLen;
                            model.TaskId     = dataId;
                            model.UpdateTime = DateTime.Now;
                            _tnBll.Add(model);
                        }
                        catch (Exception ex)
                        {
                            Program.Logger.ErrorFormat("保存TaskNotice错误: {0},ESMIN_ID{1},TaskNoticeID{2}", ex.Message, dataId, model.TaskId);
                            Program.Logger.Error(ex.StackTrace);
                        }
                        DevsManage.UpdateAutoUploadTime(devId);
                    }
                }
                else
                {
                    DevsManage.UpdateRecvTime(devId);
                    AddTaskResponse(devId, buffer, packetLen);
                }
                break;

            case ProtocolCmdType.DevStatus:
                if (_responseCmd.CmdByte == (byte)ProtocolCmdByte.GetGpsInfo)
                {
                    DevsManage.UpdateRecvTime(devId);
                    UpdateDevGps(_responseCmd, devId);
                }
                break;
            }

            return(true);
        }