private static void saveInFile2(string port, t_vehicle_msg tv, byte[] imageBytes, byte[] plateimageBytes, int overWeightDataState) { string fileName = tv.id_local + ".csv"; string path = Application.StartupPath + "\\dataTemp\\"; string fullname = ""; try { fullname = Path.Combine(path, fileName); } catch (ArgumentException e) { SysLog.WriteError("[文件写入错误]路径拼接失败:path=" + path + "filename=" + fileName + "\r\ne.Message"); Console.WriteLine("[文件写入错误]路径拼接失败:path=" + path + "filename=" + fileName + "\r\ne.Message"); return; } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } StringBuilder sb = new StringBuilder(); sb.Append("\"" + tv.id_local + "\","); sb.Append("\"" + tv.Id + "\","); sb.Append("\"" + tv.Station_Id + "\","); sb.Append("\"" + tv.Evt_Time + "\","); sb.Append("\"" + tv.Msg_Time + "\","); sb.Append("\"" + tv.Lane_No + "\","); sb.Append("\"" + tv.Plate + "\","); sb.Append("\"" + tv.Plate_Color + "\","); sb.Append("\"" + tv.Class_Index + "\","); sb.Append("\"" + tv.Length + "\","); sb.Append("\"" + tv.Speed + "\","); sb.Append("\"" + tv.Direction + "\","); sb.Append("\"" + tv.Axles_Count + "\","); sb.Append("\"" + tv.Total_Weight + "\","); sb.Append("\"" + tv.Axle1 + "\","); sb.Append("\"" + tv.Axle2 + "\","); sb.Append("\"" + tv.Axle3 + "\","); sb.Append("\"" + tv.Axle4 + "\","); sb.Append("\"" + tv.Axle5 + "\","); sb.Append("\"" + tv.Axle6 + "\","); sb.Append("\"" + tv.Axle7 + "\","); sb.Append("\"" + tv.Axle8 + "\","); sb.Append("\"" + tv.Axle9 + "\","); sb.Append("\"" + tv.Axle10 + "\","); sb.Append("\"" + tv.Axle_space1 + "\","); sb.Append("\"" + tv.Axle_space2 + "\","); sb.Append("\"" + tv.Axle_space3 + "\","); sb.Append("\"" + tv.Axle_space4 + "\","); sb.Append("\"" + tv.Axle_space5 + "\","); sb.Append("\"" + tv.Axle_space6 + "\","); sb.Append("\"" + tv.Axle_space7 + "\","); sb.Append("\"" + tv.Axle_space8 + "\","); sb.Append("\"" + tv.Axle_space9 + "\","); sb.Append("\"" + tv.Is_Straddle + "\","); sb.Append("\"" + tv.Temperature + "\","); sb.Append("\"" + tv.Over_Weight + "\","); sb.Append("\"" + tv.Over_Weight_Ratio + "\","); sb.Append("\"" + tv.WIM_Id + "\","); sb.Append("\"" + tv.LPR_id + "\","); sb.Append("\"" + tv.Station_IP + "\","); sb.Append("\"" + tv.Gap + "\","); sb.Append("\"" + tv.TimeGap + "\","); sb.Append("\"" + tv.Headway + "\","); sb.Append("\"" + tv.ValidityCode + "\","); sb.Append("\"" + tv.Wheelbase + "\","); sb.Append("\"" + overWeightDataState + "\""); object lockThis = new object(); StreamWriter f = null; lock (lockThis) { try { SysLog.WriteLog("[写入本地]数据库连接异常2,数据写入本地文件:" + fileName); Console.WriteLine("[写入本地]数据库连接异常2,数据写入本地文件:" + fileName); f = new StreamWriter(fullname, true); f.WriteLine(sb); } catch (Exception ef) { SysLog.WriteError("[写入本地]文件写入失败:" + fileName); Console.WriteLine("[写入本地]文件写入失败:" + fileName); } finally { f.Close(); } } }
private static void SaveImage2(byte[] imageBytes, string port, string id, string imgType, t_vehicle_msg tv) { int year = tv.Evt_Time.Year; string monthAndDay = tv.Evt_Time.ToString("MMdd"); string path = "E:\\Program Files\\DTCZ\\DTCZimage\\port" + port + "\\" + year + "\\" + monthAndDay + "\\"; string filename = id + "-" + imgType + ".jpg"; string fullname = Path.Combine(path, filename); string imgBase64 = Convert.ToBase64String(imageBytes); WebClient webClient = new WebClient(); //拼接post字符串 NameValueCollection postValues = new NameValueCollection(); postValues.Add("saveDiskUrl", fullname); postValues.Add("imgCode", imgBase64); System.Net.ServicePointManager.Expect100Continue = false; //ssl证书取消验证 ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; string response = string.Empty; byte[] rByte; try { rByte = webClient.UploadValues("https://139.198.176.196:8071/down/saveImg", postValues); if (System.Text.Encoding.ASCII.GetString(rByte).Equals("true")) { Console.WriteLine($"图片已上传"); } else { Console.WriteLine($"图片上传失败,将保存到本地"); SaveImageLocal(imageBytes, port, id, imgType, tv); } } catch (Exception ei) { Console.WriteLine($"图片上传失败,将保存到本地"); SaveImageLocal(imageBytes, port, id, imgType, tv); SysLog.WriteError("图片上传失败,将保存到本地"); SysLog.WriteError(ei.Message); } return; }
private static void SaveImageLocal(byte[] imageBytes, string port, string id, string imgType, t_vehicle_msg tv) { int year = tv.Evt_Time.Year; string monthAndDay = tv.Evt_Time.ToString("MMdd"); string imageFileName = port + "$" + year + "$" + monthAndDay + "$" + id + "-" + imgType + ".jpg"; string imgPath = Application.StartupPath + "\\dataImageTemp\\"; imageFileName = Path.Combine(imgPath, imageFileName); if (!Directory.Exists(imgPath)) { Directory.CreateDirectory(imgPath); } using (FileStream fs = File.Create(imageFileName)) { fs.Write(imageBytes, 0, imageBytes.Length); fs.Close(); } }
private static void saveintoDatabase(object oht) { try { Hashtable ht = (Hashtable)oht; VehicleMsg vMsg = (VehicleMsg)ht["vMsg"]; string IP = (string)ht["IP"]; string port = (string)ht["port"]; //tv是全部车辆数据 t_vehicle_msg tv = new t_vehicle_msg(); //tvt是当日车辆数据,数据库每天零点会清空 t_vehicle_msg_today tvt = null; //tvot是超重超载车辆数据,当车重符合条件并超过规定数额时,增加一条 t_vehicle_overweight_temp tvot = null; t_vehicle_overweight tvo = null; t_vehicle_msg_abnormal tva = null; //tv.id_local = System.Guid.NewGuid().ToString("N");//用UUID作为当前一条数据的ID。 tv.id_local = vMsg.Id; tv.Id = vMsg.Id; tv.Station_Id = vMsg.StationId; tv.Evt_Time = vMsg.EvtTime; tv.Msg_Time = vMsg.MsgTime; tv.Lane_No = (sbyte)vMsg.LaneNo; tv.Plate = vMsg.Plate; tv.Plate_Color = (sbyte)vMsg.PlateColor; tv.Class_Index = (sbyte)vMsg.ClassIndex; tv.Length = (short)vMsg.Length; tv.Speed = vMsg.Speed; tv.Direction = (sbyte)vMsg.Direction; tv.Axles_Count = (sbyte)vMsg.AxlesCount; tv.Total_Weight = (int)vMsg.TotalWeight; tv.Is_Straddle = (sbyte)vMsg.IsStraddle; tv.Temperature = (sbyte)vMsg.Temperature; tv.Over_Weight = (int)vMsg.OverWeight; tv.Over_Weight_Ratio = vMsg.OverWeightRatio; tv.Axle1 = vMsg.AxleWeights[0]; tv.Axle2 = vMsg.AxleWeights[1]; tv.Axle3 = vMsg.AxleWeights[2]; tv.Axle4 = vMsg.AxleWeights[3]; tv.Axle5 = vMsg.AxleWeights[4]; tv.Axle6 = vMsg.AxleWeights[5]; tv.Axle7 = vMsg.AxleWeights[6]; tv.Axle8 = vMsg.AxleWeights[7]; tv.Axle9 = vMsg.AxleWeights[8]; tv.Axle10 = vMsg.AxleWeights[9]; tv.Axle_space1 = vMsg.axleSpaces[0]; tv.Axle_space2 = vMsg.axleSpaces[1]; tv.Axle_space3 = vMsg.axleSpaces[2]; tv.Axle_space4 = vMsg.axleSpaces[3]; tv.Axle_space5 = vMsg.axleSpaces[4]; tv.Axle_space6 = vMsg.axleSpaces[5]; tv.Axle_space7 = vMsg.axleSpaces[6]; tv.Axle_space8 = vMsg.axleSpaces[7]; tv.Axle_space9 = vMsg.axleSpaces[8]; tv.Gap = vMsg.Gap; tv.TimeGap = vMsg.TimeGap; tv.Headway = vMsg.Headway; tv.ValidityCode = vMsg.ValidityCode; tv.Wheelbase = vMsg.Wheelbase; tv.Station_IP = port; tv.WIM_Id = ""; tv.LPR_id = ""; if (vMsg.OverWeightDataState == -1) { tva = new t_vehicle_msg_abnormal(); tva.id_local = tv.id_local;//用UUID作为当前一条数据的ID。 tva.Id = tv.Id; tva.Station_Id = tv.Station_Id; tva.Evt_Time = tv.Evt_Time; tva.Msg_Time = tv.Msg_Time; tva.Lane_No = tv.Lane_No; tva.Plate = tv.Plate; tva.Plate_Color = tv.Plate_Color; tva.Class_Index = tv.Class_Index; tva.Length = tv.Length; tva.Speed = tv.Speed; tva.Direction = tv.Direction; tva.Axles_Count = tv.Axles_Count; tva.Total_Weight = tv.Total_Weight; tva.Is_Straddle = tv.Is_Straddle; tva.Temperature = tv.Temperature; tva.Over_Weight = tv.Over_Weight; tva.Over_Weight_Ratio = tv.Over_Weight_Ratio; tva.Axle1 = tv.Axle1; tva.Axle2 = tv.Axle2; tva.Axle3 = tv.Axle3; tva.Axle4 = tv.Axle4; tva.Axle5 = tv.Axle5; tva.Axle6 = tv.Axle6; tva.Axle7 = tv.Axle7; tva.Axle8 = tv.Axle8; tva.Axle9 = tv.Axle9; tva.Axle10 = tv.Axle10; tva.Axle_space1 = tv.Axle_space1; tva.Axle_space2 = tv.Axle_space2; tva.Axle_space3 = tv.Axle_space3; tva.Axle_space4 = tv.Axle_space4; tva.Axle_space5 = tv.Axle_space5; tva.Axle_space6 = tv.Axle_space6; tva.Axle_space7 = tv.Axle_space7; tva.Axle_space8 = tv.Axle_space8; tva.Axle_space9 = tv.Axle_space9; tva.Gap = tv.Gap; tva.TimeGap = tv.TimeGap; tva.Headway = tv.Headway; tva.ValidityCode = tv.ValidityCode; tva.Wheelbase = tv.Wheelbase; tva.Station_IP = tv.Station_IP; tva.WIM_Id = tv.WIM_Id; tva.LPR_id = tv.LPR_id; } if ((DateTime.Compare(DateTime.Now.Date, tv.Evt_Time.Date) == 0)) { tvt = new t_vehicle_msg_today(); tvt.id_local = tv.id_local;//用UUID作为当前一条数据的ID。 tvt.Id = tv.Id; tvt.Station_Id = tv.Station_Id; tvt.Evt_Time = tv.Evt_Time; tvt.Msg_Time = tv.Msg_Time; tvt.Lane_No = tv.Lane_No; tvt.Plate = tv.Plate; tvt.Plate_Color = tv.Plate_Color; tvt.Class_Index = tv.Class_Index; tvt.Length = tv.Length; tvt.Speed = tv.Speed; tvt.Direction = tv.Direction; tvt.Axles_Count = tv.Axles_Count; tvt.Total_Weight = tv.Total_Weight; tvt.Is_Straddle = tv.Is_Straddle; tvt.Temperature = tv.Temperature; tvt.Over_Weight = tv.Over_Weight; tvt.Over_Weight_Ratio = tv.Over_Weight_Ratio; tvt.Axle1 = tv.Axle1; tvt.Axle2 = tv.Axle2; tvt.Axle3 = tv.Axle3; tvt.Axle4 = tv.Axle4; tvt.Axle5 = tv.Axle5; tvt.Axle6 = tv.Axle6; tvt.Axle7 = tv.Axle7; tvt.Axle8 = tv.Axle8; tvt.Axle9 = tv.Axle9; tvt.Axle10 = tv.Axle10; tvt.Axle_space1 = tv.Axle_space1; tvt.Axle_space2 = tv.Axle_space2; tvt.Axle_space3 = tv.Axle_space3; tvt.Axle_space4 = tv.Axle_space4; tvt.Axle_space5 = tv.Axle_space5; tvt.Axle_space6 = tv.Axle_space6; tvt.Axle_space7 = tv.Axle_space7; tvt.Axle_space8 = tv.Axle_space8; tvt.Axle_space9 = tv.Axle_space9; tvt.Gap = tv.Gap; tvt.TimeGap = tv.TimeGap; tvt.Headway = tv.Headway; tvt.ValidityCode = tv.ValidityCode; tvt.Wheelbase = tv.Wheelbase; tvt.Station_IP = tv.Station_IP; tvt.WIM_Id = tv.WIM_Id; tvt.LPR_id = tv.LPR_id; } /* if (tv.Total_Weight >= 80000) * { * tvot = new t_vehicle_overweight_temp(); * tvot.id_local = tv.id_local; * tvot.Id = tv.Id; * tvot.Station_Id = tv.Station_Id; * tvot.Evt_Time = tv.Evt_Time; * tvot.Msg_Time = tv.Msg_Time; * tvot.Lane_No = tv.Lane_No; * tvot.Plate = tv.Plate; * tvot.Plate_Color = tv.Plate_Color; * tvot.Class_Index = tv.Class_Index; * tvot.Length = tv.Length; * tvot.Speed = tv.Speed; * tvot.Direction = tv.Direction; * tvot.Axles_Count = tv.Axles_Count; * tvot.Total_Weight = tv.Total_Weight; * tvot.Is_Straddle = tv.Is_Straddle; * tvot.Temperature = tv.Temperature; * tvot.Over_Weight = tv.Over_Weight; * tvot.Over_Weight_Ratio = tv.Over_Weight_Ratio; * * tvot.Axle1 = tv.Axle1; * tvot.Axle2 = tv.Axle2; * tvot.Axle3 = tv.Axle3; * tvot.Axle4 = tv.Axle4; * tvot.Axle5 = tv.Axle5; * tvot.Axle6 = tv.Axle6; * tvot.Axle7 = tv.Axle7; * tvot.Axle8 = tv.Axle8; * tvot.Axle9 = tv.Axle9; * tvot.Axle10 = tv.Axle10; * * tvot.Axle_space1 = tv.Axle_space1; * tvot.Axle_space2 = tv.Axle_space2; * tvot.Axle_space3 = tv.Axle_space3; * tvot.Axle_space4 = tv.Axle_space4; * tvot.Axle_space5 = tv.Axle_space5; * tvot.Axle_space6 = tv.Axle_space6; * tvot.Axle_space7 = tv.Axle_space7; * tvot.Axle_space8 = tv.Axle_space8; * tvot.Axle_space9 = tv.Axle_space9; * tvot.Station_IP = tv.Station_IP; * tvot.WIM_Id = tv.WIM_Id; * tvot.LPR_id = tv.LPR_id; * } */ if (tv.Over_Weight != 0 && vMsg.OverWeightDataState != -1) { tvo = new t_vehicle_overweight(); tvo.id_local = tv.id_local; tvo.Id = tv.Id; tvo.Station_Id = tv.Station_Id; tvo.Evt_Time = tv.Evt_Time; tvo.Msg_Time = tv.Msg_Time; tvo.Lane_No = tv.Lane_No; tvo.Plate = tv.Plate; tvo.Plate_Color = tv.Plate_Color; tvo.Class_Index = tv.Class_Index; tvo.Length = tv.Length; tvo.Speed = tv.Speed; tvo.Direction = tv.Direction; tvo.Axles_Count = tv.Axles_Count; tvo.Total_Weight = tv.Total_Weight; tvo.Is_Straddle = tv.Is_Straddle; tvo.Temperature = tv.Temperature; tvo.Over_Weight = tv.Over_Weight; tvo.Over_Weight_Ratio = tv.Over_Weight_Ratio; tvo.Axle1 = tv.Axle1; tvo.Axle2 = tv.Axle2; tvo.Axle3 = tv.Axle3; tvo.Axle4 = tv.Axle4; tvo.Axle5 = tv.Axle5; tvo.Axle6 = tv.Axle6; tvo.Axle7 = tv.Axle7; tvo.Axle8 = tv.Axle8; tvo.Axle9 = tv.Axle9; tvo.Axle10 = tv.Axle10; tvo.Axle_space1 = tv.Axle_space1; tvo.Axle_space2 = tv.Axle_space2; tvo.Axle_space3 = tv.Axle_space3; tvo.Axle_space4 = tv.Axle_space4; tvo.Axle_space5 = tv.Axle_space5; tvo.Axle_space6 = tv.Axle_space6; tvo.Axle_space7 = tv.Axle_space7; tvo.Axle_space8 = tv.Axle_space8; tvo.Axle_space9 = tv.Axle_space9; tvo.Gap = tv.Gap; tvo.TimeGap = tv.TimeGap; tvo.Headway = tv.Headway; tvo.ValidityCode = tv.ValidityCode; tvo.Wheelbase = tv.Wheelbase; tvo.Station_IP = tv.Station_IP; tvo.WIM_Id = tv.WIM_Id; tvo.LPR_id = tv.LPR_id; } dtczEntities1 de = null; try { de = new dtczEntities1(); //先判断是否为异常数据,不是异常数据正常插入,判断是否超重。异常数据直接插入异常表 if (vMsg.OverWeightDataState == 1) { de.t_vehicle_msg.Add(tv); if (tvt != null) { de.t_vehicle_msg_today.Add(tvt); } if (tvot != null) { if (vMsg.ImgData.Length > 0) { SysLog.WriteLog($"图片id名称:{tv.id_local}--Image,图片大小:{vMsg.ImgData.Length}"); Console.WriteLine($"图片id名称:{tv.id_local},图片大小:{vMsg.ImgData.Length}"); SaveImage2(vMsg.ImgData, port, tv.id_local, "Image", tv); } SysLog.WriteLog($"车辆超重超载将计入数据库"); Console.WriteLine($"车辆超重超载将计入数据库"); de.t_vehicle_overweight_temp.Add(tvot); } if (tvo != null) { if (vMsg.ImgData.Length > 0) { SysLog.WriteLog($"图片id名称:{tv.id_local}--Image,图片大小:{vMsg.ImgData.Length}"); Console.WriteLine($"图片id名称:{tv.id_local},图片大小:{vMsg.ImgData.Length}"); SaveImage2(vMsg.ImgData, port, tv.id_local, "Image", tv); } SysLog.WriteLog($"车辆超重超载将计入数据库"); Console.WriteLine($"车辆超重超载将计入数据库"); de.t_vehicle_overweight.Add(tvo); //以下是不发送条件 if (vMsg == null || vMsg.PlateImgData == null || vMsg.ImgData == null || tvo.Station_IP == null || port == null || tvo == null || tvo.Plate.Equals("无车牌") || tvo.Station_IP.Equals("10015") || tvo.Station_IP.Equals("10016") || Convert.ToInt32(port) > 10014 || vMsg.ImgData.Length == 0 || vMsg.PlateImgData.Length == 0) { } //满足条件后发给json,最后到执法局 else if (tvo.Over_Weight_Ratio >= 0.05 && tvo.Axles_Count < 8 && tvo.Length < 2200 && tvo.Axle1 < 9000 && tvo.Speed <= 100) { string imgBase64 = Convert.ToBase64String(vMsg.ImgData); string plate_imgBase64 = Convert.ToBase64String(vMsg.PlateImgData); SysLog.WriteLog($"发送出json数据"); Console.WriteLine($"发送出json数据"); sendTvoJson(tvo, plate_imgBase64, imgBase64); } } } else { if (vMsg.ImgData.Length > 0) { SysLog.WriteLog($"图片id名称:{tv.id_local}--Image,图片大小:{vMsg.ImgData.Length}"); Console.WriteLine($"图片id名称:{tv.id_local},图片大小:{vMsg.ImgData.Length}"); SaveImage2(vMsg.ImgData, port, tv.id_local, "Image", tv); } SysLog.WriteLog($"车辆超重超载数据异常,将计入异常数据库"); Console.WriteLine($"车辆超重超载数据异常,将计入异常数据库"); de.t_vehicle_msg_abnormal.Add(tva); } } catch (Exception e) { Console.WriteLine("错误处507行" + e.Message); SysLog.WriteError("错误处507行" + e.Message); } try { int c = de.SaveChanges(); SysLog.WriteLog($"车辆信息写入成功,1条记录"); Console.WriteLine($"车辆信息写入成功,1条记录"); } catch (DbUpdateException due) { SysLog.WriteError($"车辆信息存入数据库出错,数据重复:{due.InnerException.InnerException.Message}"); Console.WriteLine($"车辆信息存入数据库出错,数据重复:{due.InnerException.InnerException.Message}"); Console.WriteLine("--------------------------------------------------------------------------------"); return; } catch (Exception err) { SysLog.WriteError($"车辆信息存入数据库出错,将写入本地:{err.Message}"); Console.WriteLine($"车辆信息存入数据库出错,将写入本地:{err.Message}"); //Console.WriteLine($"车辆信息存入数据库出错,将写入本地:{err.InnerException.InnerException.Message}"); saveInFile2(tv.Station_IP, tv, vMsg.ImgData, vMsg.PlateImgData, vMsg.OverWeightDataState); } if (vMsg.PlateImgData.Length > 0) { SysLog.WriteLog($"图片id名称:{tv.id_local}--PlateImg,图片大小:{vMsg.PlateImgData.Length}"); Console.WriteLine($"图片id名称:{tv.id_local},图片大小:{vMsg.PlateImgData.Length}"); SaveImage2(vMsg.PlateImgData, port, tv.id_local, "PlateImg", tv); } if (vMsg.LaserImgData.Length > 0) { SysLog.WriteLog($"图片id名称:{tv.id_local}--LaserImg,图片大小:{vMsg.LaserImgData.Length}"); Console.WriteLine($"图片id名称:{tv.id_local},图片大小:{vMsg.LaserImgData.Length}"); SaveImage2(vMsg.LaserImgData, port, tv.id_local, "LaserImg", tv); } Console.WriteLine("--------------------------------------------------------------------------------"); vMsg = null; de = null; tv = null; tvt = null; tvot = null; tvo = null; } catch (Exception e) { Console.WriteLine("saveintoDatabase里的错误" + e.Message); SysLog.WriteError("saveintoDatabase里的错误" + e.Message); } }