/// <summary> /// 更加2.0实时数据基本码表动态解析设备实时数据 /// </summary> public void analysis() { message.messageHeader.hasData = false;//设备数据是头部标识为无数据,不再处理 //deviceData="2900010000001E3B0A1A060C050003031900000000010305190000000001010619000000000101071900000000010108190000000001"; //地址即新协议中的公共地址 u16 base.deviceAddress = SystemCode.HexNumberToDenary(deviceData.Substring(2 * 2, 2 * 2), true, 16, 'u').ToString(); //设备版本 int largeVersion = 2; // (int)SystemCode.HexNumberToDenary(deviceData.Substring(3 * hexbytecharnum, 1 * hexbytecharnum), true, 8, 'u'); int smallVersion = 0; // (int)SystemCode.HexNumberToDenary(deviceData.Substring(4 * hexbytecharnum, 1 * hexbytecharnum), true, 8, 'u'); base.deviceVersion = largeVersion + "." + smallVersion; string infoData = deviceData.Substring(14 * 2); //解析多个信息体 //每个信息体开始下标 int startIndex = 0; //每个信息体结束下标 int endIndex = 0; int infotype = 0; //逐个取出信息体 string info; string[] infoResult; int mcode; base.deviceType = -1; while (infoData.Length > 0) { //取得不同信息单元地址对应的解析规则 infotype = InfoBodyUtil.getInfotype(infoData); endIndex = InfoBodyUtil.getInfotypeLen(infotype); info = infoData.Substring(startIndex * 2, endIndex * 2); //没有数据就退出 if (string.IsNullOrEmpty(info)) { break; } InfoUnitAddress infoUnitAddress = InfoBodyUtil.getInfoUnitAddress(info); //没有解析规则跳过 if (infoUnitAddress == null) { infoData = infoData.Substring(endIndex * 2); continue; } //if (infoUnitAddress.address == 203) //{ // Console.WriteLine(""); //} //解析单个信息体 infoResult = InfoBodyUtil.analyze(info, infotype, infoUnitAddress); mcode = infoUnitAddress.mcode; if (infoResult.Length > 0) { if (infoResult.Length > 2)//是故障 { faultList.Add(infoResult); } else { realMonitorMap[mcode] = infoResult[1]; if (MonitorType.historyMonitorList.Contains(mcode)) { historyMonitorMap[mcode] = infoResult[1]; } if (mcode == MonitorType.MIC_DETECTOR_SUNLINGHT) { //最近环境监测仪数据到header数据中 message.realMonitorMap[MonitorType.PLANT_MONITORITEM_LINGT_CODE] = infoResult[1]; message.historyMonitorMap[MonitorType.PLANT_MONITORITEM_LINGT_CODE] = infoResult[1]; message.messageHeader.issub = true; } if (mcode == MonitorType.MIC_DETECTOR_ENRIONMENTTEMPRATURE) { //最近环境监测仪数据到header数据中 message.realMonitorMap[MonitorType.PLANT_MONITORITEM_AMBIENTTEMP_CODE] = infoResult[1]; message.historyMonitorMap[MonitorType.PLANT_MONITORITEM_AMBIENTTEMP_CODE] = infoResult[1]; message.messageHeader.issub = true; } //记录日发电量,用于年月汇总 if (mcode == MonitorType.MIC_INVERTER_TODAYENERGY) { base.todayEnergy = double.Parse(infoResult[1]); } //记录日累计增量日照,用于年月汇总 if (mcode == MonitorType.MIC_DETECTOR_DAYRADIATION) { base.todaySunshine = double.Parse(infoResult[1]); } } } infoData = infoData.Substring(endIndex * 2); tableType = TableUtil.getTableNamebyDeviceType(infoUnitAddress.deviceType); base.deviceType = infoUnitAddress.deviceType; } }
/// <summary> /// 覆写解析方法 /// </summary> /// <param name="header"></param> public override void analyze(string header) { this.issub = false; this.hasData = true; this._Header = header; _CollectorCode = _CollectorCode.Replace("\0", "0");//临时处理非法测试数据 if (debug_collector.Equals(_CollectorCode)) { Console.WriteLine(""); } string ssss = this._Header.Substring(6 * 2, 6 * 2); int year = 2000 + (int)SystemCode.HexNumberToDenary(ssss.Substring(10, 2), false, 8, 'u') % 99; int moth = (int)SystemCode.HexNumberToDenary(ssss.Substring(8, 2), false, 8, 'u'); if (moth > 12) { moth = 12; } int day = (int)SystemCode.HexNumberToDenary(ssss.Substring(6, 2), false, 8, 'u'); if (day > 31) { day = 28; } int hh = (int)SystemCode.HexNumberToDenary(ssss.Substring(4, 2), false, 8, 'u'); if (hh > 23) { hh = 23; } int mm = (int)SystemCode.HexNumberToDenary(ssss.Substring(2, 2), false, 8, 'u'); if (mm > 60) { mm = 59; } int ss = (int)SystemCode.HexNumberToDenary(ssss.Substring(0, 2), false, 8, 'u'); if (ss > 60) { ss = 59; } this.TimeNow = new DateTime(year, moth, day, hh, mm, ss); if (DateTime.Now.AddDays(2) < this.TimeNow) //超过服务器时间两天的实时数据抛弃 { this.hasData = false; return; } string infoData = header.Substring(14 * 2); //解析多个信息体 //每个信息体开始下标 int startIndex = 0; //每个信息体结束下标 int endIndex = 0; int infotype = 0; //逐个取出信息体 string info; string[] infoResult; base.hasData = false; while (infoData.Length > 0) { infotype = InfoBodyUtil.getInfotype(infoData); endIndex = InfoBodyUtil.getInfotypeLen(infotype); info = infoData.Substring(startIndex * 2, endIndex * 2); //没有数据就退出 if (string.IsNullOrEmpty(info)) { break; } InfoUnitAddress infounitAddress = InfoBodyUtil.getInfoUnitAddress(info); //没有解析规则跳过 if (infounitAddress == null) { infoData = infoData.Substring(endIndex * 2); continue; } //解析单个信息体 infoResult = InfoBodyUtil.analyze(info, infotype, infounitAddress); if (infoResult.Length > 0) { switch (int.Parse(infoResult[0])) { //发电量 case DataType.plant_dayenergy: this.DayEnergy = float.Parse(infoResult[1]); LogUtil.info("collector energy " + _dayEnergy); break; //总发电量 case DataType.plant_totalenergy: try { this.TotalEnergy = float.Parse(infoResult[1]); } catch (Exception e) { this.TotalEnergy = 0; } break; //功率 case DataType.plant_power: _power = float.Parse(infoResult[1]); //_power = float.Parse(Math.Round(_power / 1000, 3).ToString());//新协议无需换算成kw break; //日照 case DataType.plant_sunlight: try { this.sunStrength = int.Parse(infoResult[1]); } catch (Exception e) { this.sunStrength = null; } break; //环境温度 case DataType.plant_temperature: try { this.temperature = float.Parse(infoResult[1]); } catch (Exception e) { this.temperature = null; } break; default: break; } base.hasData = true; } infoData = infoData.Substring(endIndex * 2); } //string totalstr = SystemCode.ReversionAll(this._Header.Substring(35 * 2, 4 * 2)); //_totalEnergy = SystemCode.HexNumberToDenary(totalstr, false, 32, 'U'); _DevicesNum = 0; BugNum = 0; }