SqlClientHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using Microsoft.Data.SqlClient;
  2. using Microsoft.Extensions.Logging;
  3. using PetroChinaOnlineWatchPlugin.MessageEntity.Outgoing;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Configuration;
  7. using System.Data;
  8. using System.IO;
  9. namespace PetroChinaOnlineWatchPlugin
  10. {
  11. internal class SqlClientHelper
  12. {
  13. public static List<IfsfMessageDataOut> ReadOnlineMonitorData(AppConfigV1 appConfig, ILogger logger, ref Dictionary<string, object> tempDic, DbAddressType dbAddressType)
  14. {
  15. List<IfsfMessageDataOut> dataList = null;
  16. try
  17. {
  18. var sqlSettings = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
  19. {
  20. ExeConfigFilename = Path.Combine(appConfig.MainAppDirectory, "SqlConnect.config")
  21. }, ConfigurationUserLevel.None).AppSettings.Settings;
  22. using (var connection = new SqlConnection(sqlSettings["SqlConn"].Value))
  23. {
  24. if (connection.State != ConnectionState.Open)
  25. connection.Open();
  26. string pressureDataTable = $"PressureDataNew_{ DateTime.Today.ToString("yyyyMM")}";
  27. string queryString = null;
  28. if (dbAddressType == DbAddressType.Configure)
  29. {
  30. queryString = "select top 1 StartDate, JYQS, PVZ, PVF from T_GasStationConfig";
  31. }
  32. else if (dbAddressType == DbAddressType.EnvironmentData)
  33. {
  34. queryString = "select top 1 A.Id as pdId, B.Id as alId, A.ReceiveTime, A.YGYL, A.YZYL, A.WQND, A.YGWD, B.AlrmType, B.IsAlarm " +
  35. $"from (select top 1 Id, ReceiveTime, YGYL, YZYL, WQND, YGWD, isupload_bd from {pressureDataTable} where isupload_bd=0) A " +
  36. "full outer join (select top 1 Id, AlrmType, IsAlarm, isupload from AlarmLog where isupload=0) B on A.isupload_bd=B.isupload";
  37. }
  38. else if (dbAddressType == DbAddressType.PumpData)
  39. {
  40. queryString = "select * from " +
  41. "(select * from (select top 1 Id as ogtId, ReceiveTime, MacCode, GunCode, TempRatio, MeanGasQ, TempGas, MeanOilQ, TempOil, isupload " +
  42. "from OilGunTemp_D where isupload=0) A full outer join " +
  43. "(select top 1 StartDate, JYQS, PVZ, PVF, isupload_bd from T_GasStationConfig) B on A.isupload=B.isupload_bd) D " +
  44. "full outer join (select top 1 Id as ogpId, opertime, macname, gunname, eventcode, operate, isupload " +
  45. "from OilGunOperLog where isupload=0) C on D.isupload=C.isupload";
  46. }
  47. else if (dbAddressType == DbAddressType.FaultData)
  48. {
  49. queryString = "select top 1 Id, repairTime, EquipmentType, EquipmentNumber, repairCode " +
  50. "from T_RepairRecord where isupload=0";
  51. }
  52. using (var command = new SqlCommand(queryString, connection))
  53. {
  54. using (var reader = command.ExecuteReader())
  55. {
  56. reader.Read();
  57. if (!reader.HasRows)
  58. return null;
  59. switch (dbAddressType)
  60. {
  61. case DbAddressType.Configure:
  62. var oilSettings = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
  63. {
  64. ExeConfigFilename = Path.Combine(appConfig.MainAppDirectory, "OilGun.config")
  65. }, ConfigurationUserLevel.None).AppSettings.Settings;
  66. var dataSettings = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
  67. {
  68. ExeConfigFilename = Path.Combine(appConfig.MainAppDirectory, "DataPressure.config")
  69. }, ConfigurationUserLevel.None).AppSettings.Settings;
  70. // CF_ID 01H, CF_Message - Data_Id 64H, Enable_Time - Data_Id 02H, Pump_Number - Data_Id 03H
  71. // Positive_Pressure_Value - Data_Id 04H, Negative_Pressure_Value - Data_Id 05H, Post_Treatmen_Pressuret - Data_Id 06H
  72. // Dispenser_Number - Data_Id 07H, 气液比预警下限值 - Data_Id 08H, 气液比预警上限值 - Data_Id 09H, 预警次数比例 - Data_Id 0AH
  73. // 气液比预警转换时间 - Data_Id 0BH, 环境数据采集间隔 - Data_Id 0CH, P/V 阀压力预报警转换时间 - Data_Id 0DH, 罐压预警上限值 - Data_Id 0EH
  74. // 罐压预警下限值 - Data_Id 0FH, 罐压预警时间 - Data_Id 10H, 罐压报警时间 - Data_Id 11H, 油气浓度报警阈值 - Data_Id 12H
  75. // new Bin8Message(0x07, 8),
  76. dataList = new List<IfsfMessageDataOut>() { new DateTimeMessage(0x02, reader["StartDate"]), new Bin8Message(0x03, reader["JYQS"]),
  77. new Bin8Bcd6Message(0x04, reader["PVZ"]), new Bin8Bcd6Message(0x05, reader["PVF"]), new Bin8Bcd6Message(0x06, oilSettings["CLZZQDYL"].Value),
  78. new Bin8Message(0x07, appConfig.DispenserNumber), new Bin8Bcd4Message(0x08, oilSettings["QYBMIN"].Value), new Bin8Bcd4Message(0x09, oilSettings["QYBMAX"].Value), new Bin8Bcd4Message(0x0A, oilSettings["QYBBL"].Value),
  79. new Bin8Message(0x0B, oilSettings["QYBBJ"].Value), new Bin8Message(0x0C, dataSettings["HCLDelay"].Value), new Bin8Message(0x0D, oilSettings["ZKFBJ"].Value), new Bin8Bcd6Message(0x0E, oilSettings["YGLYMAX"].Value),
  80. new Bin8Bcd6Message(0x0F, oilSettings["YGLYMIN"].Value), new Bin8Message(0x10, oilSettings["YGLYYJ"].Value), new Bin8Message(0x11, oilSettings["YGLYBJ"].Value), new Bcd4Message(0x12, oilSettings["XielouConcentration"].Value)
  81. };
  82. break;
  83. case DbAddressType.EnvironmentData:
  84. // EM_ID 11H, Monitor_Time - Data_Id 02H, Gas_Tank_Pressure - Data_Id 03H, Liquid_Resistance_Pressure - Data_Id 04H
  85. // Gas_Volume - Data_Id 05H, Concentration - Data_Id 06H, Tank_Temp - Data_Id 07H
  86. string alrmLogId = reader["alId"].ToString();
  87. updateString = $"update {pressureDataTable} set isupload_bd=1 where Id='{reader["pdId"]}';";
  88. updateString += alrmLogId == "" ? "" : $"update AlarmLog set isupload=1 where Id='{reader["alId"]}'";
  89. tempDic["LiquidResistance"] = reader["YZYL"];
  90. dataList = new List<IfsfMessageDataOut>() { new DateTimeMessage(0x02, reader["ReceiveTime"]), new Bin8Bcd6Message(0x03, reader["YGYL"]),
  91. new Bin8Bcd6Message(0x04, reader["YZYL"]), new Bin8Bcd8Message(0x05, 0m), new Bcd4Message(0x06, reader["WQND"]), new Bin8Bcd4Message(0x07, reader["YGWD"])
  92. };
  93. // EM_ID 11H, Zero_Ppressure_Alarm - Data_Id 10H, Tank_Pressure_Alarm - Data_Id 11H, Critical_Pressure_Alarm - Data_Id 12H
  94. // Post_Process_Alarm - Data_Id 13H, Airtight_Alarm - Data_Id 14H, Liquid_Resistance_Alarm - Data_Id 15H, 处理装置启动状态报警 - Data_Id 16H
  95. // 处理装置停止状态报警 - Data_Id 17H, 卸油回气管状态报警 - Data_Id 18H
  96. string alrmType = reader["AlrmType"].ToString();
  97. if (alrmTypeDataIdMap.ContainsKey(alrmType))
  98. {
  99. alrmTypeDataIdMap[alrmType][1] = 2; //2:报警
  100. }
  101. else if (alrmType == "1")
  102. {
  103. tempDic["GasLiquidRatioAlarm"] = 2; //2:报警
  104. }
  105. foreach (byte[] ar in alrmTypeDataIdMap.Values)
  106. {
  107. dataList.Add(new Bin8Message(ar[0], ar[1]));
  108. }
  109. break;
  110. case DbAddressType.PumpData:
  111. // DP_ID 21H, Monitor_Time - Data_Id 02H, Dispenser_ID - Data_Id 03H, Pump_ID - Data_Id 04H
  112. // Gas_Liquid_Ratio - Data_Id 05H, Gas_Current_Speed - Data_Id 06H, Gas_Flow - Data_Id 07H, Fueling_Speed - Data_Id 08H
  113. // Fueling_Volume - Data_Id 09H, Liquid_Resistance - Data_Id 0AH
  114. // DP_ID 21H, Monitor_Time - Data_Id 02H, Dispenser_ID - Data_Id 03H, Pump_ID - Data_Id 04H
  115. // DP_ID 21H, Gas_Liquid_Ratio_Alarm - Data_Id 14H
  116. // DP_ID 21H, Monitor_Time - Data_Id 02H, Dispenser_ID - Data_Id 03H, Pump_ID - Data_Id 04H
  117. // DP_ID 21H, Pump_Start_Stop_State - Data_Id 1EH, Operation_Type - Data_Id 1FH
  118. string ogpId = reader["ogpId"].ToString();
  119. updateString = $"update OilGunTemp_D set isupload=1 where Id='{reader["ogtId"]}';";
  120. updateString += ogpId == "" ? "" : $"update OilGunOperLog set isupload=1 where Id='{reader["ogpId"]}'";
  121. object liquidResistance = tempDic.ContainsKey("LiquidResistance") ? tempDic["LiquidResistance"] : "0";
  122. object gasLiquidRatioAlarm = tempDic.ContainsKey("GasLiquidRatioAlarm") ? tempDic["GasLiquidRatioAlarm"] : "0";
  123. dataList = new List<IfsfMessageDataOut>() { new DateTimeMessage(0x02, reader["ReceiveTime"]), new Bin8Message(0x03, reader["MacCode"]), new Bin8Message(0x04, reader["GunCode"]),
  124. new Bin8Bcd4Message(0x05, reader["TempRatio"]), new Bin8Bcd6Message(0x06, reader["MeanGasQ"]), new Bin8Bcd6Message(0x07, reader["TempGas"]),
  125. new Bin8Bcd6Message(0x08, reader["MeanOilQ"]), new Bin8Bcd8Message(0x09, reader["TempOil"]), new Bin8Bcd6Message(0x0A, liquidResistance),
  126. new Bin8Message(0x14, gasLiquidRatioAlarm),
  127. new Bin8Message(0x1E, reader["eventcode"]), new Bin8Message(0x1F, reader["operate"])
  128. };
  129. break;
  130. case DbAddressType.FaultData:
  131. // ER_ID 41H, Fault_Time - Data_Id 01H, Device_Type - Data_Id 02H, Device_ID - Data_Id 03H, Fault_ID - Data_Id 04H
  132. updateString = $"update T_RepairRecord set isupload=1 where Id='{reader["Id"]}'";
  133. int faultId = 5;
  134. //3.第3位故障码表示故障类型:1:通信故障,2:设备故障,3:软件故障,4:数据库故障,其它数字:其它故障
  135. int faultCode = int.Parse(reader["repairCode"].ToString().Substring(2, 1));
  136. if (faultCode > 0 && faultCode < 5)
  137. faultId = faultCode;
  138. dataList = new List<IfsfMessageDataOut>() { new DateTimeMessage(0x01, reader["repairTime"]), new Bin8Message(0x02, deviceTypeMap[reader["EquipmentType"].ToString()]),
  139. new Bin8Message(0x03, reader["EquipmentNumber"]), new Bin8Message(0x04, faultId)
  140. };
  141. break;
  142. default:
  143. break;
  144. }
  145. }
  146. }
  147. return dataList;
  148. }
  149. }
  150. catch (Exception ex)
  151. {
  152. logger.LogError($"ReadOnlineMonitorData error, exception detail: {ex}");
  153. return null;
  154. }
  155. }
  156. public static void UpdateOnlineMonitorData(AppConfigV1 appConfig, ILogger logger)
  157. {
  158. try
  159. {
  160. var sqlSettings = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
  161. {
  162. ExeConfigFilename = Path.Combine(appConfig.MainAppDirectory, "SqlConnect.config")
  163. }, ConfigurationUserLevel.None).AppSettings.Settings;
  164. using (var connection = new SqlConnection(sqlSettings["SqlConn"].Value))
  165. {
  166. if (connection.State != ConnectionState.Open)
  167. connection.Open();
  168. using (var command = new SqlCommand(updateString, connection))
  169. {
  170. command.ExecuteNonQuery();
  171. }
  172. }
  173. }
  174. catch (Exception ex)
  175. {
  176. logger.LogError($"updateString: {updateString}");
  177. logger.LogError($"UpdateOnlineMonitorData error, exception detail: {ex}");
  178. }
  179. }
  180. private static string updateString = null;
  181. // EM_ID 11H, Zero_Ppressure_Alarm - Data_Id 10H, Tank_Pressure_Alarm - Data_Id 11H, Critical_Pressure_Alarm - Data_Id 12H
  182. // Post_Process_Alarm - Data_Id 13H, Airtight_Alarm - Data_Id 14H, Liquid_Resistance_Alarm - Data_Id 15H, 处理装置启动状态报警 - Data_Id 16H
  183. // 处理装置停止状态报警 - Data_Id 17H, 卸油回气管状态报警 - Data_Id 18H
  184. private static Dictionary<string, byte[]> alrmTypeDataIdMap = new Dictionary<string, byte[]>() { { "11", new byte[] { 0x10, 0 } }, { "6", new byte[] { 0x11, 0 } },
  185. { "4", new byte[] { 0x12, 0 } }, { "5", new byte[] { 0x13, 0 } }, { "2", new byte[] { 0x14, 0 } }, { "3", new byte[] { 0x15, 0 } }, { "7", new byte[] { 0x16, 0 } },
  186. { "8", new byte[] { 0x17, 0 } }, { "9", new byte[] { 0x18, 0 } } };
  187. private static Dictionary<string, int> deviceTypeMap = new Dictionary<string, int>() { { "采集器", 1 }, { "油气流量传感器", 2 }, { "压力传感器", 3 },
  188. { "浓度传感器", 4 }, { "温度传感器", 5 }, { "控制台", 6 } };
  189. }
  190. }