private void fillAreaDatasTest() { string areaName = "MC__"; string plcName = "MC010_"; string deviceId = "W103_UR___AB_UR020__0022"; string stationName = "UR___SYUR020_"; string resource = "UR___TLUR020_R"; int stationNo = 1; int alarmNumber = 1; string alarmDetail = "报警测试..."; AlarmDetail ad = new AlarmDetail(); ad.Number = alarmNumber; ad.Detail = alarmDetail; Station s = new Station(); s.Number = stationNo; s.Name = stationName; s.ResourceId = resource; s.AlarmDetails.Add(ad); PLC plc = new PLC(); plc.Name = plcName; plc.DeviceId = deviceId; plc.Stations.Add(s); Area area = new Area(); area.Name = areaName; area.Plcs.Add(plc); this.areas.Add(area); }
private AlarmDetail getAlarmDetail(int alarmId, string detail) { AlarmDetail alarmDetail = new AlarmDetail(); alarmDetail.Number = alarmId; alarmDetail.Detail = detail; return(alarmDetail); }
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); } } }