/// <summary> /// 查询 /// </summary> /// <param name="query"></param> private void Query(string query) { tbodySummary.InnerHtml = ""; List <Work> macs = new List <Work>(); using (var bll = new EquipmentBLL()) { Expression <Func <TB_Equipment, bool> > expression = PredicateExtensions.True <TB_Equipment>(); expression = expression.And(a => a.Deleted == false && a.TB_Terminal.Version == 1); if (!string.IsNullOrEmpty(query)) { string number = query; string mode = ""; int dash = query.LastIndexOf('-'); if (dash > 0) { number = query.Substring(dash + 1); mode = query.Replace(number, ""); expression = expression.And(a => a.Number.Equals(number) && a.TB_EquipmentModel.Code.Equals(mode)); } else { expression = expression.And(a => a.Number.Equals(number)); } } var list = bll.FindList(expression); if (null != list && list.Count() > 0) { foreach (var obj in list) { macs.Add(new Work() { Id = obj.id, MacId = bll.GetFullNumber(obj), Time1 = obj.RegisterTime.Value, Time2 = DateTime.Now }); } } } // 循环统计每一个设备的计算时间 foreach (var mac in macs) { Query(mac); } }
/// <summary> /// 导出设备列表到excel /// </summary> /// <param name="bll"></param> /// <param name="excel"></param> private void ExportEquipmentsToExcel(ExcelHandlerBLL bll, TB_ExcelHandler excel) { // data为保存出错时的异常数据 string source = "", data = ""; Application app = null; Workbook book = null; Worksheet sheet = null; try { app = new Application(); book = app.Workbooks.Open(EXCEL_PATH + EXCEL_EQUIPMENTS); sheet = (Worksheet)book.ActiveSheet; app.Visible = false; app.AlertBeforeOverwriting = false; app.DisplayAlerts = false; using (var ebll = new EquipmentBLL()) { int line = 3; int cnt = 0; var n = (int?)null; var list = ebll.FindList(f => f.Deleted == false); foreach (var obj in list) { var x = line + cnt; sheet.Cells[x, 1] = (cnt + 1); sheet.Cells[x, 2] = n == obj.Model ? "-" : obj.TB_EquipmentModel.TB_EquipmentType.Code; sheet.Cells[x, 3] = n == obj.Model ? "-" : ebll.GetFullNumber(obj); sheet.Cells[x, 4] = EverdigmUtils.GetEquipmentFunctional(obj.Functional.Value); sheet.Cells[x, 5] = EquipmentBLL.GetRuntime(obj.Runtime + obj.InitializedRuntime, obj.CompensatedHours.Value); sheet.Cells[x, 6] = ebll.GetEngineState(obj); sheet.Cells[x, 7] = string.IsNullOrEmpty(obj.GpsAddress) ? "-" : obj.GpsAddress; // 状态 sheet.Cells[x, 8] = obj.TB_EquipmentStatusName.Code; // customer sheet.Cells[x, 9] = n == obj.Customer ? "-" : obj.TB_Customer.Code; sheet.Cells[x, 10] = n == obj.Customer ? "-" : obj.TB_Customer.Name; // 终端 var link = EverdigmUtils.GetOnlineStyle(obj.OnlineStyle, obj.OnlineTime, false); link = link.Substring(link.IndexOf('>') + 1); link = link.Substring(0, link.IndexOf('<')); sheet.Cells[x, 11] = string.IsNullOrEmpty(link) ? "-" : link; string alarm = ebll.GetAlarmStatus(obj.Alarm); alarm = alarm.Substring(alarm.IndexOf("title=\"") + 7); alarm = alarm.Substring(0, alarm.IndexOf('"')); sheet.Cells[x, 12] = alarm.Contains("No") ? "-" : alarm; sheet.Cells[x, 13] = null == obj.LastActionTime ? "-" : obj.LastActionTime.Value.ToString("yyyy/MM/dd HH:mm"); sheet.Cells[x, 14] = n == obj.Terminal ? "-" : obj.TB_Terminal.Number; bool sat = n != obj.Terminal && n != obj.TB_Terminal.Satellite; sheet.Cells[x, 15] = sat ? obj.TB_Terminal.TB_Satellite.CardNo : "-"; sheet.Cells[x, 16] = n == obj.Warehouse ? "-" : obj.TB_Warehouse.Name; cnt++; } } // 另存为别的 var date = excel.CreateDate.Value.ToString("yyyyMMdd"); var path = Path.Combine(WEB_PATH, "files\\xls\\", date); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } source = path + "\\Equipments2Excel_" + excel.CreateDate.Value.ToString("yyyyMMdd") + ".xlsx"; if (File.Exists(source)) { File.Delete(source); } book.SaveAs(source); } catch (Exception e) { data = e.StackTrace; ShowUnhandledMessage(format("{0}Equipment to Excel handler error: {1}{2}{3}", Now, e.Message, Environment.NewLine, e.StackTrace)); } finally { // 关闭book if (null != book) { book.Close(); book = null; } // 关闭application if (null != app) { app.Quit(); app = null; } // 释放内存 GC.Collect(); GC.WaitForPendingFinalizers(); } var target = "../" + source.Replace(WEB_PATH, "").Replace("\\", "/"); bll.Update(f => f.id == excel.id, act => { act.Handled = true; act.Target = target; act.Status = (byte)(string.IsNullOrEmpty(data) ? 0 : 1); act.Data = data; }); }
/// <summary> /// 导出终端列表到excel /// </summary> /// <param name="bll"></param> /// <param name="excel"></param> private void ExportTerminalsToExcel(ExcelHandlerBLL bll, TB_ExcelHandler excel) { string source = "", data = ""; Application app = null; Workbook book = null; Worksheet sheet = null; try { app = new Application(); book = app.Workbooks.Open(EXCEL_PATH + EXCEL_TERMINALS); sheet = (Worksheet)book.ActiveSheet; app.Visible = false; app.AlertBeforeOverwriting = false; app.DisplayAlerts = false; using (var tbll = new TerminalBLL()) { using (var ebll = new EquipmentBLL()) { int line = 2; int cnt = 0; var n = (int?)null; var list = tbll.FindList(f => f.Delete == false); foreach (var obj in list) { var x = line + cnt; sheet.Cells[x, 1] = (cnt + 1); sheet.Cells[x, 2] = obj.Number; sheet.Cells[x, 3] = n == obj.Satellite ? "-" : obj.TB_Satellite.CardNo; sheet.Cells[x, 4] = string.IsNullOrEmpty(obj.Firmware) ? "-" : obj.Firmware; sheet.Cells[x, 5] = obj.Revision; sheet.Cells[x, 6] = TerminalTypes.GetTerminalType(obj.Type.Value); sheet.Cells[x, 7] = obj.ProductionDate.Value.ToString("yyyy/MM/dd"); var e = ebll.Find(d => d.Terminal == obj.id && d.Deleted == false); sheet.Cells[x, 8] = null == e ? "-" : ebll.GetFullNumber(e); var link = EverdigmUtils.GetOnlineStyle(obj.OnlineStyle, obj.OnlineTime, false); link = link.Substring(link.IndexOf('>') + 1); link = link.Substring(0, link.IndexOf('<')); sheet.Cells[x, 9] = string.IsNullOrEmpty(link) ? "-" : link; sheet.Cells[x, 10] = null == obj.OnlineTime ? "-" : obj.OnlineTime.Value.ToString("yyyy/MM/dd HH:mm"); cnt++; } } } // 另存为别的 var date = excel.CreateDate.Value.ToString("yyyyMMdd"); var path = Path.Combine(WEB_PATH, "files\\xls\\", date); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } source = path + "\\Terminals2Excel_" + excel.CreateDate.Value.ToString("yyyyMMdd") + ".xlsx"; if (File.Exists(source)) { File.Delete(source); } book.SaveAs(source); } catch (Exception e) { data = e.StackTrace; ShowUnhandledMessage(format("{0}Terminal to Excel handler error: {1}{2}{3}", Now, e.Message, Environment.NewLine, e.StackTrace)); } finally { // 关闭book if (null != book) { book.Close(); book = null; } // 关闭application if (null != app) { app.Quit(); app = null; } // 释放内存 GC.Collect(); GC.WaitForPendingFinalizers(); } var target = "../" + source.Replace(WEB_PATH, "").Replace("\\", "/"); bll.Update(f => f.id == excel.id, act => { act.Handled = true; act.Status = (byte)(string.IsNullOrEmpty(data) ? 0 : 1); act.Target = target; act.Data = data; }); }
/// <summary> /// 根据TX300数据包更新终端和设备的在线状态 /// </summary> /// <param name="tx300"></param> private void HandleTX300Status(TX300 tx300, AsyncUserDataBuffer data) { var sim = GetSimFromData(tx300); using (var ebll = new EquipmentBLL()) { var equipment = ebll.Find(f => f.TB_Terminal.Sim.Equals(sim)); var terminal = new TerminalBLL().Find(f => f.Sim.Equals(sim)); SaveTerminalData(null == terminal ? -1 : terminal.id, sim, data.PackageType, tx300.TotalLength, true, data.ReceiveTime); // 终端不存在的话,不用再继续处理数据了 if (!IsTracker(tx300.CommandID)) { if (null == terminal) { return; } } HandleOnline(sim, tx300.CommandID, data); if (tx300.CommandID != 0xAA00) { // TX10G的数据 if (tx300.CommandID >= 0x7000 && tx300.CommandID <= 0x7040) { HandleTX10G(tx300, data); } else { SaveTX300History(tx300, data.ReceiveTime, (null == equipment ? "" : ebll.GetFullNumber(equipment))); // 根据命令的不同处理各个命令详情 HandleCommand(tx300, equipment, terminal); } } } }
/// <summary> /// 处理新版的卫星通讯协议 /// </summary> /// <param name="data"></param> private void HandleIridiumNewProtocolPackage(IridiumData data) { if (data.Payload[0] == 0x01) { // 新版的卫星通讯协议 uint thisWorkTime = BitConverter.ToUInt32(data.Payload, 13); string locks = CustomConvert.GetHex(data.Payload[1]); string alarms = CustomConvert.IntToDigit(data.Payload[2], CustomConvert.BIN, 8) + CustomConvert.IntToDigit(data.Payload[3], CustomConvert.BIN, 8); IridiumLocation location = new IridiumLocation(); location.LatLng = new byte[IridiumLocation.SIZE]; Buffer.BlockCopy(data.Payload, 4, location.LatLng, 0, IridiumLocation.SIZE); location.Unpackate(); // 通过卫星的IMEI号码查找终端 using (var tbll = new TerminalBLL()) { using (var ebll = new EquipmentBLL()) { var terminal = tbll.Find(f => f.TB_Satellite.CardNo.Equals(data.IMEI)); TB_Equipment equipment = null; if (null != terminal) { // 只有第一版的终端才需要计算补偿时间 var compensated = terminal.Version == 1; tbll.Update(f => f.id == terminal.id, act => { act.OnlineStyle = (byte)LinkType.SATELLITE; // 同时更新终端的最后链接时间 act.OnlineTime = data.Time; }); equipment = ebll.Find(f => f.Terminal == terminal.id); if (null != equipment) { // 旧的运转时间 var oldRuntime = equipment.Runtime; var interval = 0 == oldRuntime ? 0 : (thisWorkTime - oldRuntime); // 粗略增加的小时数,启动时加2小时 var addMin = interval > 60 ? interval / 60 : 1 + (location.EngFlag.Equals("On") ? 1 : 0); int addHour = (int)(interval > 60 ? interval / 60 : 1);// 显示历史记录 string oldCompensated = format("WorkHours {0}, UsedHours {1}, Efficiency {2}, AddHours {3}, Compensated {4}", equipment.WorkHours, equipment.UsedHours, equipment.HourWorkEfficiency, equipment.AddedHours, equipment.CompensatedHours); string newCompensated = ""; // 更新设备的总运转时间 HandleEquipmentRuntime(equipment, thisWorkTime); if (null != equipment) { string calculated = ""; ebll.Update(f => f.id == equipment.id, act => { act.OnlineStyle = (byte)LinkType.SATELLITE; act.OnlineTime = data.Time; // 更新设备的报警状态 2015/09/10 14:04 act.Alarm = alarms; act.LastAction = "0x1000"; act.LastActionBy = "SAT"; act.LastActionTime = data.Time; // 更新定位信息 2015/09/09 23:29 if (location.Available) { act.Latitude = location.Latitude; act.Longitude = location.Longitude; } // 更新启动与否状态 2015/08/31 act.Voltage = location.EngFlag.Equals("On") ? "G2400" : "G0000"; // 更新总运转时间 act.Runtime = equipment.Runtime; act.AccumulativeRuntime = equipment.AccumulativeRuntime; // 更新 version 终端为1的版本的运转时间的补偿 if (compensated && interval > 0) { // 实际工作小时数 act.WorkHours += interval / 60.0; // 实际所用小时数 act.UsedHours += addHour; // 重新计算每小时工作效率 act.HourWorkEfficiency = act.WorkHours / act.UsedHours; // 补偿的小时数 act.AddedHours += addMin / 60.0; // 实际补偿的小时数 act.CompensatedHours = act.AddedHours * act.HourWorkEfficiency; } // 如果回来的运转时间比当前时间大则更新成为On状态 暂时 2015/09/02 //if (worktime > act.Runtime) //{ // // act.Voltage = "G2400"; // act.Runtime = (int)worktime; //} //else //{ // if (worktime > 0) // { // // 运转时间不为零的话,更新运转时间 // act.Runtime = (int)worktime; // } //} // 锁车状态 2015/08/14 if (act.LockStatus != locks) { act.LockStatus = locks; } // 判断锁车的有效状态 if (locks.Equals("40") || locks.Equals("0F") || locks.Equals("FF")) { // 锁车时还有开机状态 if (location.EngFlag.Equals("On")) { // 锁车了,但未关机,还在工作中 if (act.LockEffected == (byte)LockEffect.Locked) { act.LockEffected = (byte)LockEffect.LockedAndStillWork; } else if (act.LockEffected == (byte)LockEffect.LockedAndEngineOff) { // 锁车了且已关机了,此时再开机则需要报警(没锁住车) act.LockEffected = (byte)LockEffect.LockedAndNoEffect; } } else { // 锁车了,且已关机了 if (act.LockEffected == (byte)LockEffect.Locked) { act.LockEffected = (byte)LockEffect.LockedAndEngineOff; } } } }); if (compensated && interval > 0) { equipment = ebll.Find(f => f.id == equipment.id); newCompensated = format("WorkHours {0}, UsedHours {1}, Efficiency {2}, AddHours {3}, Compensated {4}", equipment.WorkHours, equipment.UsedHours, equipment.HourWorkEfficiency, equipment.AddedHours, equipment.CompensatedHours); calculated = format("Compensate changed(interval: {0}){1} from {2}{3} to {4}", interval, Environment.NewLine, oldCompensated, Environment.NewLine, newCompensated); } if (!string.IsNullOrEmpty(calculated)) { OnUnhandledMessage(this, new Sockets.UIEventArgs() { Message = format("{0}{1}{2}", Now, calculated, Environment.NewLine) }); } } } else { OnUnhandledMessage(this, new Sockets.UIEventArgs() { Message = format("{0} Iridium has not bind with any equipment.", Now, Environment.NewLine) }); } } else { OnUnhandledMessage(this, new Sockets.UIEventArgs() { Message = format("{0} Satellite has no terminal, data will save as terminal number: \"{1}\".{2}", Now, data.IMEI.Substring(4), Environment.NewLine) }); } // 保存TX300历史记录 SaveTX300History(new TX300() { CommandID = 0x1000, MsgContent = data.Payload, ProtocolType = ProtocolTypes.SATELLITE, // 默认装载机终端类型 2015/09/22 09:40 TerminalType = null == terminal ? TerminalTypes.LD : terminal.Type.Value, // 没有终端时,用IMEI号码的末尾11位数字表示终端号码 2015/09/22 09:40 TerminalID = null == terminal ? data.IMEI.Substring(4) : terminal.Sim, TotalLength = (ushort)data.Payload.Length }, data.Time, ebll.GetFullNumber(equipment)); try { long?posId = null; if (location.Available) { using (var posbll = new PositionBLL()) { var pos = posbll.GetObject(); pos.Latitude = location.Latitude; pos.Longitude = location.Longitude; pos.NS = location.NSI; pos.EW = location.EWI; pos.StoreTimes = null == equipment ? 0 : equipment.StoreTimes; pos.GpsTime = data.Time; pos.Equipment = null == equipment ? (int?)null : equipment.id; // 没有终端时,用IMEI号码的末尾11位数字表示终端号码 2015/09/22 09:40 pos.Terminal = null == terminal?data.IMEI.Substring(4) : (terminal.Sim.Length < 11 ? (terminal.Sim + "000") : terminal.Sim); pos.Type = location.Report + "(Eng " + location.EngFlag + ")(SAT)"; posbll.Add(pos); posId = pos.id; } } // 处理报警信息 if (alarms != ALARM) { using (var armbll = new AlarmBLL()) { var arm = armbll.GetObject(); arm.Code = alarms; arm.AlarmTime = data.Time; arm.Equipment = null == equipment ? (int?)null : equipment.id; arm.Position = posId; arm.StoreTimes = null == equipment ? 0 : equipment.StoreTimes; // 没有终端时,用IMEI号码的末尾11位数字表示终端号码 2015/09/22 09:40 arm.Terminal = null == terminal?data.IMEI.Substring(4) : (terminal.Sim.Length < 11 ? (terminal.Sim + "000") : terminal.Sim); armbll.Add(arm); } } } catch (Exception e) { OnUnhandledMessage(this, new Sockets.UIEventArgs() { Message = format("{0}{1}{2}{3}", Now, e.Message, Environment.NewLine, e.StackTrace) }); } // 更新卫星方式的命令状态(只处理命令回复的1000,其他的命令在普通命令过程中处理) if (location.ReportType == 1 && data.Payload.Length <= 17) { HandleIridiumCommandResponseed(data); } location.Dispose(); location = null; } } } }