private bool generate(string path, out string result) { result = ""; bool flag = false; string errString = null; try { if (this.dtStationAlarm.Rows.Count <= Constant.CONFIG_ALARM_DETAILS_MAX) { fillAreaDatas(this.areas, this.dtStationInfo, this.dtStationAlarm); } else { //当条数超过大约9000条后,this.mian对象被释放,导致后续状态错误,所以必须限制。+时间开发。 result = Constant.CONFIG_STATION_ALARM + "超过了" + Constant.CONFIG_ALARM_DETAILS_MAX + "条,生成点文件受限,解锁限制请联系管理员!"; return(false); } if (null == this.pmcPoint) { this.pmcPoint = getPMCPointDataType(this.mainFormOperation.Point_type); } DataTable dt = pmcPoint.generateCSVData(areas); if (null != dt) { //进度条 string status = "生成文件中..."; startProgressBar(5); setProgressBarValue(1, status); object[,] obj = OtherUtils.convertDataTabelToArray(dt); setProgressBarValue(2, status); flag = FileUtils.createExcel(obj, Excel.XlFileFormat.xlCSV, path, out errString); setProgressBarValue(4, status); if (!flag) { MessageBox.Show(errString); } stopProgressBar(); flag = true; } } catch (Exception e) { flag = false; result = e.Message; OtherUtils.writeMsgByByte(Constant.LOG_PATH, result + "\r\n" + e.ToString()); } finally { clearMemory(); } return(flag); }
private void loadStationAlarm(List <Area> areas, DataTable dtStationAlarm, int dtStationInfoRows, int progressMaxValue) { List <PLC> plcs = null; List <Station> stations = new List <Station>(); List <AlarmDetail> alarmDetails = null; int rowNumberThisPlc = 1; //当前plc报警详细数量行,初始带标题+1 int rowNumbersAllPlc = 0; //所有plc报警详细数量行累加之和,带标题 Area area = null; PLC plc = null; Station station = null; AlarmDetail ad = null; bool readMultiPlcConfig = Global.isReadMultiAlarmConfig; int excelNumber = 0; if (null == dtStationAlarm || dtStationAlarm.Rows.Count == 0) { return; } string plcNamePrev = null; int stationNoPrev = -1; string[] fileds = new string[] { "PLC", "IP", "StationNo", "StationName", "AlarmId", "Description" }; if (!checkTitle(dtStationAlarm, fileds, 6)) { throw new MyException(Constant.CONFIG_STATION_ALARM + "标题不符合要求!"); } //2017/9/30 if (dtStationAlarm.Rows.Count == 0) { throw new MyException(Constant.CONFIG_STATION_ALARM + "读取数据为空,请检查Sheet名是否为" + Constant.SHEET_NAME); } //dtStationAlarm.Rows.Count 总数量不带标题 for (int m = 0; m < dtStationAlarm.Rows.Count; m++) { string plcName = dtStationAlarm.Rows[m]["PLC"].ToString().Trim(); string sStationNo = dtStationAlarm.Rows[m]["StationNo"].ToString().Trim(); string sAlarmIdConfig1 = dtStationAlarm.Rows[m]["AlarmId"].ToString().Trim(); string alarmDetail = dtStationAlarm.Rows[m]["Description"].ToString().Trim(); int stationNo = -1; int result = -1; bool flag; //获取plcName,不切换plc时 if (string.IsNullOrEmpty(plcName)) //支持多PLC在一个配置文件中 2017/9/21 { if (!string.IsNullOrEmpty(plcNamePrev)) { plcName = plcNamePrev; } rowNumberThisPlc++; } else { rowNumberThisPlc = (readMultiPlcConfig == true ? 2 : 1); } //设置Excel号 rowNumbersAllPlc = m + 1 + 1; //序号+1,标题+1 excelNumber = (readMultiPlcConfig == true ? rowNumberThisPlc : rowNumbersAllPlc); //获取stationNo if (!string.IsNullOrEmpty(sStationNo)) { result = -1; flag = int.TryParse(sStationNo, out result); if (!flag) { throw new MyException(Constant.CONFIG_STATION_ALARM + "文件中配置了错误的StationNo。Excel行号=" + excelNumber + ",plcName=" + plcName); } stationNo = result; } else { if (stationNoPrev > 0) { stationNo = stationNoPrev; } } //获取alarmId if (!sAlarmIdConfig1.ToUpper().Contains("ALARM")) { throw new MyException(Constant.CONFIG_STATION_ALARM + "文件中配置了错误的AlarmId。Excel行号=" + excelNumber + ",AlarmId=" + sAlarmIdConfig1 + ",plcName=" + plcName); } sAlarmIdConfig1 = sAlarmIdConfig1.Replace("ALARM", ""); string sAlarmIdConfig2 = OtherUtils.getStringBetweenBracket(sAlarmIdConfig1); flag = int.TryParse(sAlarmIdConfig2, out result); if (!flag || result < Constant.ALARM_NUMBER_COUNT_MIN || result > Constant.ALARM_NUMBER_COUNT_MAX) { throw new MyException(Constant.CONFIG_STATION_ALARM + "文件中配置了错误的AlarmId。Excel行号=" + excelNumber + ",AlarmId=" + result + ",plcName=" + plcName); } int alarmId = result; //对报警描述处理 //仅支持80个字符。 if (alarmDetail.Length > 80) { alarmDetail = alarmDetail.Substring(0, 79); } //处理特殊字符 List <string> specialChars = InterfaceFactory.getInstance(this.mainFormOperation, this.interfaceType).SpecialChar.specialChars; for (int i = 0; i < specialChars.Count; i++) { if (alarmDetail.Contains(specialChars[i])) { alarmDetail = alarmDetail.Replace(specialChars[i], ""); OtherUtils.writeMsgByByte(Constant.LOG_PATH, "检测到报警描述中含有特殊字符=" + specialChars[i] + ",已经删除。Excel行号=" + excelNumber + ",AlarmId=" + alarmId + ",plcName=" + plcName); } } bool noPlcExist = true; if (!string.IsNullOrEmpty(sStationNo)) { for (int n = 0; n < areas.Count; n++) { area = areas[n]; plcs = areas[n].Plcs; for (int o = 0; o < plcs.Count; o++) { if (plcName == plcs[o].Name) { noPlcExist = false; plc = plcs[o]; stations = plc.Stations; for (int p = 0; p < stations.Count; p++) { if (stations[p].Number == stationNo) //两个配置文件中的工位号必须保持一致 { station = stations[p]; alarmDetails = station.AlarmDetails; //直接操作了areas里的AlarmDetails ad = getAlarmDetail(alarmId, alarmDetail); alarmDetails.Add(ad); break; } } break; } } } if (noPlcExist) { throw new MyException(Constant.CONFIG_STATION_ALARM + "文件中无匹配的PLC存在。Excel行号=" + excelNumber + ",plcName=" + plcName); } plcNamePrev = plcName; stationNoPrev = stationNo; } else { //配置文件中的报警号=当前工位号,否则不加入 if (stationNo == station.Number) { ad = getAlarmDetail(alarmId, alarmDetail); alarmDetails.Add(ad); } } //判断退出之前更新进度条 int progressVlaue = dtStationInfoRows + m + 1; reportProcess(progressVlaue, "读取station alarm中... area=" + area.Name + ",plcName=" + plc.Name + ",station=" + station.Name + "alarmId=" + alarmId + ",alarm=" + alarmDetail + ",Excel行号=" + excelNumber); //最后一行退出 if (m == dtStationAlarm.Rows.Count - 1) { if (this.main.getProgressBar().Value < progressMaxValue) { this.main.stopProgressBar(); } return; } //判断空行 string plcNameNext = dtStationAlarm.Rows[m + 1]["PLC"].ToString().Trim(); string sStationNoNext = dtStationAlarm.Rows[m + 1]["StationNo"].ToString().Trim(); string sAlarmIdNext = dtStationAlarm.Rows[m + 1]["AlarmId"].ToString().Trim(); string alarmDetailNext = dtStationAlarm.Rows[m + 1]["Description"].ToString().Trim(); if (string.IsNullOrEmpty(sAlarmIdNext) && string.IsNullOrEmpty(alarmDetailNext)) { throw new MyException(Constant.CONFIG_STATION_ALARM + "文件中包含空行。Excel行号=" + (rowNumberThisPlc + 1) + ",plcName=" + plcName); } } }
private bool readConfig(string[] path, out string errorString) { errorString = ""; bool flag = false; int configCount = path.Length; int multiAlarmConfig = 0; try { //开启进度条 startProgressBar(configCount); for (int i = 0; i < configCount; i++) { DataTable dt = FileUtils.readExcelByOleDb(path[i], Constant.SHEET_NAME, true); // DataTable dt = FileUtils.readExcelByCom(path[i], 1, "", true); string status = ""; string filaName = path[i].Substring(path[i].LastIndexOf("\\") + 1); if (path[i].Contains(Constant.CONFIG_STATION_INFO)) { this.dtStationInfo = dt; flag = true; status = "读取" + filaName + "中..."; } else if (path[i].Contains(Constant.CONFIG_STATION_ALARM)) { //支持一个station_info文件,多个station_alarm文件的读取。 //若仅读取一个station_alarm,则以最后读取的有效。 if (configCount == 1) { if (this.dtStationAlarm.Rows.Count > 0) { this.dtStationAlarm.Clear(); } } this.dtStationAlarm.Merge(dt, true, MissingSchemaAction.Add); //若发现列架构不一致,则默认增加 flag = true; multiAlarmConfig++; status = "读取" + filaName + "中..."; } reportProcess(i + 1, status); } if (multiAlarmConfig > 1) { Global.isReadMultiAlarmConfig = true; } else { Global.isReadMultiAlarmConfig = false; } } catch (ArgumentNullException ex) { flag = false; errorString = ex.Message + ",请检查表名是否为" + Constant.SHEET_NAME; OtherUtils.writeMsgByByte(Constant.LOG_PATH, ex.ToString()); } catch (Exception e) { flag = false; errorString = e.Message; OtherUtils.writeMsgByByte(Constant.LOG_PATH, e.ToString()); } finally { //手动关闭进度条 if (this.main.getProgressBar().Value == this.main.getProgressBar().Maximum) { stopProgressBar(); } } return(flag); }
/// <summary> /// 获取根据配置文件生成的数据表 /// </summary> /// <param name="areas"></param> /// <returns></returns> public virtual DataTable generateCSVData(List <Area> areas) { try { int progressValue = 0; startProgressBar(areas); for (int i = 0; i < areas.Count; i++) { List <PLC> plcs = areas[i].Plcs; for (int j = 0; j < plcs.Count; j++) { string deviceId = plcs[j].DeviceId; bool isFilter = plcs[j].IsFilter; List <Station> stations = plcs[j].Stations; if (isFilter) { break; } for (int k = 0; k < stations.Count; k++) { string stationName = stations[k].Name; int stationNo = stations[k].Number; string resourceId = stations[k].ResourceId; bool isFilterStation = stations[k].IsFilter; bool isProdStation = stations[k].IsProdStation; if (isFilterStation) { break; } List <AlarmDetail> alarmDetails = stations[k].AlarmDetails; //产量点类型 if (this.GetType() == typeof(ProdStationPoint)) { if (!isProdStation) { continue; } } //非产量点类型 else if (this.GetType() == typeof(NotProdStationPoint)) { if (isProdStation) { continue; } } initSpecialPointName(stationNo); initPointAttrs(stationName, stationNo, resourceId, deviceId); generateStationInfoData(stationName, stationNo, resourceId, deviceId); for (int l = 0; l < alarmDetails.Count; l++) { int alarmNumber = alarmDetails[l].Number; string alarmDetail = alarmDetails[l].Detail; generateAlarmInfoData(stationName, stationNo, resourceId, deviceId, alarmNumber, alarmDetail); } clearSpecialPointName(); this.mainController.reportProcess(progressValue++, "读取信息加入内存中... "); } } } this.mainController.stopProgressBar(); } catch (Exception e) { OtherUtils.writeMsgByByte(Constant.LOG_PATH, e.ToString()); mCSV.Dt = null; } return(mCSV.Dt); }