Пример #1
0
        /// <summary>
        /// 保存指定设备ID的对应表数据集,使用前创建DeviceTableInfo对象,并复制DevId属性
        /// </summary>
        /// <param name="deviceTableInfo"></param>
        /// <returns></returns>
        public bool SaveTableToDB(DeviceTableInfo deviceTableInfo)
        {
            if (deviceTableInfo == null)
            {
                return(false);
            }
            if (deviceTableInfo.TableList.Count <= 0)
            {
                return(false);
            }
            StreamWriter recWriter = null;

            try
            {
                string filePath = this.GetFilePath(deviceTableInfo.DevId.ToString());
                recWriter = new StreamWriter(filePath, true);
                //recWriter.BaseStream.Seek(0, SeekOrigin.End);
                foreach (TableInfo recInfo in deviceTableInfo.TableList.Values)
                {
                    string recInfoJson = JsonConvert.SerializeObject(recInfo);
                    recWriter.WriteLine(recInfoJson);
                    recWriter.Flush();
                }
                recWriter.Close();
                return(true);
            }
            catch (Exception e)
            {
                RunLog.Log(string.Format("保存楼层对应表失败,设备ID:{0},错误:{1}", deviceTableInfo.DevId, e.Message));
                return(false);
            }
        }
        /// <summary>
        /// 获取命令报文
        /// </summary>
        /// <param name="devId"></param>
        /// <param name="commandWord"></param>
        /// <param name="sendData"></param>
        /// <returns></returns>
        protected string GetWriteReport(int devId, string cmdWord, string sendData)
        {
            string strCmdData = string.Empty;

            int ctrlNo  = devId;
            int groupID = 0;

            if (devId == 0)
            {
                groupID = 0;
                ctrlNo  = 0;
            }
            else
            {
                groupID = ((ctrlNo - 1) / 0xFF) + 1;
                ctrlNo  = (ctrlNo % 0xFF);
                //解决255的倍数问题
                if (ctrlNo == 0)
                {
                    ctrlNo = 0xFF;
                }
            }

            //设备组号
            string strDevGroupID = StrUtils.IntToHex(groupID, 2);
            //设备地址
            string strDevCtrlID = StrUtils.IntToHex(ctrlNo, 2);
            //设备类型
            string strDevType = StrUtils.IntToHex((int)this.DevType, 2);
            //流水号
            string strCmdSerialNo = this.GetCmdNumber();
            //命令字
            string strCmdWord = cmdWord;

            //生成固定部分的命令报文
            strCmdData = strDevGroupID + strDevCtrlID + StrUtils.IntToHex((int)DevType, 2) + strCmdSerialNo + strCmdWord + sendData;
            // 计算校验位
            string strXorCheck = StrUtils.GetXORCheck(strCmdData);

            RunLog.Log("************CheckXor strCmdData : " + strCmdData);
            // 命令报文+校验位
            strCmdData = strCmdData + strXorCheck;

            // 格式化命令报文,如果报文中出现>= 0xF0的数据,则做特定处理
            // 特别注意:校验位也需检测

            strCmdData = CommandProcesserHelper.AddF0Escape(strCmdData);

            RunLog.Log("************ AddF0Escape strCmdData :" + strCmdData);


            // 组合报文,加上起始字和结束字
            strCmdData = CommandProcesserHelper.CMD_START_FLAG + strCmdData + CommandProcesserHelper.CMD_END_FLAG;

            IsDownLoadDevLength = strCmdData;
            RunLog.Log(strCmdData);
            return(strCmdData);
        }
Пример #3
0
        /// <summary>
        /// 加载dll依赖
        /// </summary>
        private void LoadDllFiles()
        {
            string strPath = Path.Combine(Application.StartupPath, @"DependentFiles\Delphi");

            if (!Directory.Exists(strPath))
            {
                string strMsg = string.Format("{0} not exists!", strPath);
                RunLog.Log(strMsg, LogType.ltError);
                HintProvider.ShowConfirmDialog(null, strMsg, buttons: ConfirmFormButtons.OK);
                return;
            }
            UtilityTool.LoadDllFile(strPath, TripleDESIntf.TRIDES_DLL);
        }
Пример #4
0
        /// <summary>
        /// 根据设备ID获取对应的对应表数据集,使用前创建DeviceTableInfo对象,并复制DevId属性
        /// </summary>
        /// <param name="deviceTableInfo"></param>
        /// <param name="strErr"></param>
        /// <returns></returns>
        public bool GetTableFromDB(DeviceTableInfo deviceTableInfo)
        {
            if (deviceTableInfo == null)
            {
                return(false);
            }
            FileStream   readerFileStream = null;
            StreamReader recReader        = null;

            try
            {
                string filePath = this.GetFilePath(deviceTableInfo.DevId.ToString());
                readerFileStream = new FileStream(filePath, FileMode.OpenOrCreate);
                recReader        = new StreamReader(readerFileStream);
                while (true)
                {
                    string recInfoJson = recReader.ReadLine();
                    if (string.IsNullOrWhiteSpace(recInfoJson))
                    {
                        break;
                    }
                    TableInfo tableInfo = JsonConvert.DeserializeObject <TableInfo>(recInfoJson);
                    if (tableInfo == null)
                    {
                        recReader.Close();
                        readerFileStream.Close();
                        File.Delete(filePath);
                        return(false);
                    }
                    deviceTableInfo.TableList.Add(tableInfo.AuthId, tableInfo);
                }
                if (deviceTableInfo.TableList.Count <= 0)
                {
                    deviceTableInfo.InitDeviceTableInfoList();
                }
                recReader.Close();
                readerFileStream.Close();
                return(true);
            }
            catch (Exception e)
            {
                RunLog.Log(string.Format("获取楼层对应表失败,设备ID:{0},错误:{1}", deviceTableInfo.DevId, e.Message));
                return(false);
            }
        }
        public static string GetMacEx(string data)
        {
            try
            {
                string hexPadding = "8000000000000000";
                int    padLength  = 16 - data.Length % 16;
                byte[] dataByte   = StrUtils.HexStrToBytes(data + StrUtils.CopySubStr(hexPadding, 0, padLength));
                byte[] keyByte    = StrUtils.HexStrToBytes(GetDecryKey());
                byte[] macRes     = GetMAC(dataByte, keyByte);

                return(StrUtils.CopySubStr(StrUtils.BytesToHexStr(macRes), 0, 8));
            }
            catch (Exception ex)
            {
                return("12A254AF");

                RunLog.Log(ex.ToString());
            }
        }
        public void RecvCallBack(UdpClient client, IPEndPoint ipEndPoint, byte[] recvBuf)
        {
            if (!this.IsHandleCreated)
            {
                return;
            }

            string recvData = StrUtils.BytesToHexStr(recvBuf);

            //if (IsDownLoadDevLength.Length > 1200)
            //{
            //    recvData = IsDownLoadDevLength;
            //    //return;
            //    IsDownLoadDevLength = string.Empty;
            //}
            RunLog.Log("Rcv:" + recvData);
            //string recvData = StrUtils.BytesToHexStr(recvBuf);
            this.BeginInvoke(new Action <string>(this.AnalysisRecvData), recvData);
        }
        //获取项目编号
        private void Btn_ReadcloudFloorTable_GetData_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextEdit_ProNo.Text.Length < 8)
                {
                    HintProvider.ShowAutoCloseDialog(null, string.Format("请输入8位项目编号"));
                    return;
                }
                //string HttpAddress = "15w07q0502.51mypc.cn:15234/smartCard/syncdata/device/elevator/getCloudElevatorList?";//http地址
                string _uSysCode = TextEdit_ProNo.Text;//传递的参数  00000126;

                //string _sAppKey = "";
                DateTime currentTime = System.DateTime.Now;
                string   createTime  = currentTime.ToString("yyyyMMddHHmmss");
                string   _MD5        = "projectId" + createTime + "1177BE55278MMN5412365UHBN85214BE";
                string   _sSign      = Md5Func(_MD5);

                string postString = "projectId=" + _uSysCode + "&sign=" + _sSign + "&createTime=" + createTime; //传递的参数  00000126
                byte[] postData   = Encoding.UTF8.GetBytes(postString);                                         //编码

                //RunLog.Log("获取项目请求:  ccccccccccccc POSTString  " + postString);

                iniFileControl ReadIniConfig = new iniFileControl(Application.StartupPath + @"\CloudAdd.ini");
                string         socketHttp    = ReadIniConfig.IniReadValue("云服务配置", "地址");
                //string url = "http://test.smartcard.jia-r.com/smartCard/syncdata/device/elevator/getCloudElevatorList";//地址
                string url = socketHttp;

                WebClient webClient = new WebClient();
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); //采取POST方式
                byte[] responseData = webClient.UploadData(url, "POST", postData);          //得到返回字符流
                string srcString    = Encoding.UTF8.GetString(responseData);                //解码

                //RunLog.Log("接收项目:  ccccccccccccc srcString  " + srcString);

                //srcString = CommonUtils.ToHex(srcString, "utf-8", true);
                //CloudFloorTableBaseInfo FloorTableSerializer = JsonConvert.DeserializeObject<CloudFloorTableBaseInfo>(srcString);

                //string Test = Md5Func("projectId201903091355181177BE55278MMN5412365UHBN85214BE");



                comboBox_FloorName.Items.Clear(); //清空列表

                for (int i = 0; i <= 112; i++)
                {
                    try
                    {
                        string JsonData = cutJson(srcString, "data", i);

                        if (JsonData == null)
                        {
                            return;
                        }

                        CloudFloorTableBaseInfo FloorTable = JsonConvert.DeserializeObject <CloudFloorTableBaseInfo>(JsonData);
                        string f_deviceName = FloorTable.deviceName;
                        f_deviceUnique = FloorTable.deviceUnique;

                        //comboBox_FloorName
                        // 添加项目
                        comboBox_FloorName.DropDownStyle = ComboBoxStyle.DropDownList;
                        comboBox_FloorName.Items.Add(f_deviceName);
                        //添加list
                        var Listueq = new { ID = i, deviceName = f_deviceName, deviceUnique = f_deviceUnique };

                        list.Add(Listueq);
                    }
                    catch (Exception ex)
                    {
                        RunLog.Log(ex);
                    }
                }
            }
            catch (Exception elog)
            {
                string errMsg = "获取项目编号失败";
                HintProvider.ShowAutoCloseDialog(null, string.Format("通讯失败,错误:{0}", errMsg));

                RunLog.Log(elog);
            }


            //string JsonData = cutJson(srcString, "data", 1);
            //CloudFloorTableBaseInfo FloorTable = JsonConvert.DeserializeObject<CloudFloorTableBaseInfo>(JsonData);
        }
        private void Btn_ReadcloudFloorTable_OK_Click(object sender, EventArgs e)
        {
            try
            {
                string devUnique = string.Empty;
                if (list != null)
                {
                    foreach (var one in list)
                    {
                        PropertyDescriptorCollection pdc    = TypeDescriptor.GetProperties(one);
                        PropertyDescriptor           pdID   = pdc.Find("deviceUnique", true);
                        string             lhcodeunique     = pdID.GetValue(one).ToString();
                        PropertyDescriptor pdDevName        = pdc.Find("deviceName", true);
                        string             lhcodedeviceName = pdDevName.GetValue(one).ToString();

                        if (lhcodedeviceName == comboBox_FloorName.Text.ToString())
                        {
                            devUnique = lhcodeunique;
                        }
                    }
                }
                string   _uSysCode   = devUnique;//传递的参数  0000012610080;
                DateTime currentTime = System.DateTime.Now;
                string   createTime  = currentTime.ToString("yyyyMMddHHmmss");

                string _MD5   = "deviceUnique" + createTime + "1177BE55278MMN5412365UHBN85214BE";
                string _sSign = Md5Func(_MD5);
                RunLog.Log("_MD5" + _MD5);
                string postString = /*"projectId=" + TextEdit_ProNo.Text.ToString() + */ "deviceUnique=" + _uSysCode + "&sign=" + _sSign + "&createTime=" + createTime; //传递的参数  00000126
                byte[] postData   = Encoding.UTF8.GetBytes(postString);                                                                                                 //编码

                //RunLog.Log("请求:  ccccccccccccc deviceUnique  " + _uSysCode);

                //RunLog.Log("请求:  ccccccccccccc POSTString  " + postString);

                iniFileControl ReadIniConfig = new iniFileControl(Application.StartupPath + @"\CloudAdd.ini");
                string         socketHttp    = ReadIniConfig.IniReadValue("云楼层对应表配置", "地址");
                //string url = "http://test.smartcard.jia-r.com/smartCard/syncdata/device/elevator/getElevatorFloorConfig";//地址
                string url = socketHttp.ToString();

                WebClient webClient = new WebClient();
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); //采取POST方式
                byte[] responseData = webClient.UploadData(url, "POST", postData);          //得到返回字符流
                string srcString    = Encoding.UTF8.GetString(responseData);                //解码


                RunLog.Log("1 云接收 :ccccccccccccc srcString  " + srcString + "Length = " + srcString.Length);
                if (srcString.Length == 50)
                {
                    HintProvider.ShowAutoCloseDialog(null, string.Format("获取楼层映射表异常,请前往物业后台重置。"));
                    this.Close();
                    return;
                }

                ResponseInfo FloorTable = JsonConvert.DeserializeObject <ResponseInfo>(srcString);

                RunLog.Log("2 云接收 :ccccccccccccc srcString  " + srcString);

                this.Close();


                SendCloudFloorHex = FloorTable; //
                RunLog.Log("Read 实际楼层对比:SendCloudFloorHex ********* :" + FloorTable.data.logicFloor);

                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                string errMsg = "获取楼层对应表失败";
                HintProvider.ShowAutoCloseDialog(null, string.Format("通讯失败,错误:{0}", errMsg));

                RunLog.Log(ex);
            }
        }
        private void AnalysisRecvData(string strData)
        {
            //非忙碌状态,不解析数据
            //if(!this.IsBusy)
            //{
            //    return;
            //}
            //去F0
            string strCmdStr = CommandProcesserHelper.DelF0Escape(strData);

            //判断报文长度
            if (strCmdStr.Length < 18)
            {
                RunLog.Log(string.Format("返回的报文长度错误,报文:{0}", strCmdStr));
                HintProvider.ShowAutoCloseDialog(null, string.Format("返回的报文校验错误,错误:{0}", strCmdStr), HintIconType.Err);
                return;
            }
            //判断校验码
            string requiredCheckValue = StrUtils.GetXORCheck(strCmdStr, 2, strCmdStr.Length - 6).ToUpper();
            string factCheckValue     = StrUtils.CopySubStr(strCmdStr, strCmdStr.Length - 4, 2);

            if (string.Compare(requiredCheckValue, factCheckValue, true) != 0)
            {
                RunLog.Log(string.Format("返回的报文校验错误,报文:{0}", strCmdStr));
                HintProvider.ShowAutoCloseDialog(null, string.Format("返回的报文校验错误,错误:{0}", strCmdStr), HintIconType.Err);

                return;
            }
            //判断返回的命令状态
            string strCmdStatus = StrUtils.CopySubStr(strCmdStr, 12, 2);

            //string strCmdStatus = StrUtils.CopySubStr(strCmdStr, 10, 2);
            if (StrUtils.HexStrToInt(strCmdStatus) != CommonUtils.RES_OK)
            {
                if (strCmdStatus == "7F")
                {
                    string strCmdStatus2 = StrUtils.CopySubStr(strCmdStr, 18, 2);
                    if (strCmdStatus2 == "02")
                    {
                        HintProvider.ShowAutoCloseDialog(null, string.Format("需要初始化设备,才能重新设置网络参数"));
                        return;
                    }
                }

                RunLog.Log(string.Format("命令状态错误:{0}", CommonUtils.GetErrMsgByCode(StrUtils.HexStrToInt(strCmdStatus))));

                string Error = string.Format("命令状态错误:{0}", CommonUtils.GetErrMsgByCode(StrUtils.HexStrToInt(strCmdStatus)));
                HintProvider.ShowAutoCloseDialog(null, string.Format(Error), HintIconType.Err);

                return;
            }
            else if (StrUtils.HexStrToInt(strCmdStatus) == CommonUtils.RES_OK)
            {
                string strDevID = CommandProcesserHelper.GetDevIDByCmdInfo(StrUtils.CopySubStr(strCmdStr, 2, 4)).ToString();
                if (IsDownParm || AppConst.IsDownParmCloudLinkage)
                {
                    HintProvider.ShowAutoCloseDialog(null, string.Format("下载成功"));
                    AppConst.IsDownParmCloudLinkage = false;
                    IsDownParm = false;
                }
            }

            DeviceType devType = CommandProcesserHelper.GetDevTypeByCmdInfo(StrUtils.CopySubStr(strCmdStr, 6, 2));

            if (devType == DeviceType.CloudLinkageInfoCtrl)
            {
                //云联动器
                string strCmdStatusLink = StrUtils.CopySubStr(strCmdStr, 15, 4);

                if (strCmdStatusLink == "0121" || strCmdStatusLink == "012E" || strCmdStatusLink == "0100")
                {
                    HintProvider.ShowAutoCloseDialog(null, "下载成功", HintIconType.OK);
                    Thread.Sleep(1000);
                }
            }

            //重置
            IsDownLoadDevLength = string.Empty;

            this.AnalysisRecvDataEx(strCmdStr);
        }