WorkmanshipDataUploadJob.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using log4net;
  2. using Newtonsoft.Json;
  3. using Quartz;
  4. using RabbitMQ.Client;
  5. using RDIFramework.Utilities;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.Text;
  10. namespace TimedUpload.QuartzJobs
  11. {
  12. [DisallowConcurrentExecution]
  13. public class WorkmanshipDataUploadJob : IJob
  14. {
  15. private readonly ILog log = LogManager.GetLogger(typeof(WorkmanshipDataUploadJob));
  16. public void Execute(IJobExecutionContext context)
  17. {
  18. string[] uploadUrls = Constants.UploadUrl.Split('|');
  19. Dictionary<string, IConnection> connections = new Dictionary<string, IConnection>();
  20. Dictionary<string, IModel> channels = new Dictionary<string, IModel>();
  21. Dictionary<string, IBasicProperties> properties = new Dictionary<string, IBasicProperties>();
  22. foreach (string uploadUrl in uploadUrls)
  23. {
  24. ConnectionFactory factory = new ConnectionFactory();
  25. factory.HostName = uploadUrl;//主机名,Rabbit会拿这个IP生成一个endpoint,这个很熟悉吧,就是socket绑定的那个终结点。
  26. factory.UserName = Constants.UploadUserName;//默认用户名,用户可以在服务端自定义创建,有相关命令行
  27. factory.Password = Constants.UploadPassword;//默认密码
  28. factory.AutomaticRecoveryEnabled = true; // 链接断开会自动重连
  29. IConnection connection = factory.CreateConnection();
  30. IModel channel = connection.CreateModel();
  31. channel.QueueDeclare("workmanship", true, false, false, null);//创建一个名称为kibaqueue的消息队列
  32. IBasicProperties property = channel.CreateBasicProperties();
  33. property.ContentType = "text/plain";
  34. property.DeliveryMode = 2; //持久化
  35. connections.Add(uploadUrl, connection);
  36. properties.Add(uploadUrl, property);
  37. channels.Add(uploadUrl, channel);
  38. }
  39. if (channels.Count > 0)
  40. {
  41. SendSeconddaryPumpData(channels, properties);
  42. SendWaterWellData(channels, properties);
  43. SendWaterFactoryData(channels, properties);
  44. }
  45. foreach (KeyValuePair<string, IConnection> item in connections)
  46. {
  47. IConnection connection = item.Value;
  48. connection.Close();
  49. }
  50. }
  51. /// <summary>
  52. /// 二供数据
  53. /// </summary>
  54. /// <param name="channel"></param>
  55. private void SendSeconddaryPumpData(Dictionary<string, IModel> channels, Dictionary<string, IBasicProperties> properties)
  56. {
  57. try
  58. {
  59. log.Info("二供工艺图数据同步任务开始执行.................\r\n");
  60. string factorySql = "select 编码 DeviceCode,1 Type,RTRIM(日期) + ' ' + RTRIM(时间) ReadTime,* from (select row_number() over(partition by 编码 order by 日期 desc, 时间 desc) as rid,* from 二供通用泵站 where 编码 is not null) as tt where tt.rid=1";
  61. DataTable dtDevice = dbHelper.Fill(factorySql);
  62. if (dtDevice == null || dtDevice.Rows.Count == 0)
  63. {
  64. return;
  65. }
  66. DataColumnCollection cols = dtDevice.Columns;
  67. // 处理设备列表
  68. for (int i = 0; i < dtDevice.Rows.Count; i++)
  69. {
  70. DataRow dr = dtDevice.Rows[i];
  71. string deviceCode = dr["DeviceCode"].ToString();
  72. string type = dr["Type"].ToString();
  73. string realData = CreateJsonParameters(cols, dr);
  74. Dictionary<string, object> deviceMap = new Dictionary<string, object>();
  75. deviceMap["DeviceCode"] = deviceCode;
  76. deviceMap["Type"] = type;
  77. deviceMap["RealData"] = realData;
  78. string message = JsonConvert.SerializeObject(deviceMap);
  79. foreach (KeyValuePair<string, IModel> item in channels)
  80. {
  81. string key = item.Key;
  82. IModel channel = item.Value;
  83. IBasicProperties property = properties[key];
  84. channel.BasicPublish("workmanship", "", property, Encoding.UTF8.GetBytes(message)); //生产消息
  85. }
  86. }
  87. log.Info("二供工艺图数据同步任务开始执行.................\r\n");
  88. }
  89. catch (Exception ex)
  90. {
  91. log.Error("二供工艺图数据同步任务错误:" + ex.Message + "===========" + ex.StackTrace + "\r\n");
  92. }
  93. }
  94. /// <summary>
  95. /// 水源井数据
  96. /// </summary>
  97. /// <param name="channel"></param>
  98. private void SendWaterWellData(Dictionary<string, IModel> channels, Dictionary<string, IBasicProperties> properties)
  99. {
  100. try
  101. {
  102. log.Info("水源井工艺图数据同步任务开始执行.................\r\n");
  103. string factorySql = "select 编码 DeviceCode,2 Type,RTRIM(日期) + ' ' + RTRIM(时间) ReadTime,* from (select row_number() over(partition by 编码 order by 日期 desc, 时间 desc) as rid,* from 水源井数据 where 编码 is not null) as tt where tt.rid=1";
  104. DataTable dtDevice = dbHelper.Fill(factorySql);
  105. if (dtDevice == null || dtDevice.Rows.Count == 0)
  106. {
  107. return;
  108. }
  109. DataColumnCollection cols = dtDevice.Columns;
  110. // 处理设备列表
  111. for (int i = 0; i < dtDevice.Rows.Count; i++)
  112. {
  113. DataRow dr = dtDevice.Rows[i];
  114. string deviceCode = dr["DeviceCode"].ToString();
  115. string type = dr["Type"].ToString();
  116. string realData = CreateJsonParameters(cols, dr);
  117. Dictionary<string, object> deviceMap = new Dictionary<string, object>();
  118. deviceMap["DeviceCode"] = deviceCode;
  119. deviceMap["Type"] = type;
  120. deviceMap["RealData"] = realData;
  121. string message = JsonConvert.SerializeObject(deviceMap);
  122. foreach (KeyValuePair<string, IModel> item in channels)
  123. {
  124. string key = item.Key;
  125. IModel channel = item.Value;
  126. IBasicProperties property = properties[key];
  127. channel.BasicPublish("workmanship", "", property, Encoding.UTF8.GetBytes(message)); //生产消息
  128. }
  129. }
  130. log.Info("水源井工艺图数据同步任务结束.................\r\n");
  131. }
  132. catch (Exception ex)
  133. {
  134. log.Error("水源井工艺图数据同步任务执行错误:" + ex.Message + "===========" + ex.StackTrace + "\r\n");
  135. }
  136. }
  137. /// <summary>
  138. /// 水厂数据
  139. /// </summary>
  140. /// <param name="channel"></param>
  141. private void SendWaterFactoryData(Dictionary<string, IModel> channels, Dictionary<string, IBasicProperties> properties)
  142. {
  143. try
  144. {
  145. log.Info("水厂工艺图数据同步任务开始执行.................\r\n");
  146. string factorySql = "SELECT a.FactoryCode DeviceCode,3 Type,RTRIM(b.日期) + ' ' + RTRIM(b.时间) ReadTime,b.*,c.* FROM [水厂信息] a,";
  147. factorySql += "(select * from (select row_number() over(partition by FactoryId order by 日期 desc, 时间 desc) as rid,* from 水厂过滤间GGD where FactoryId is not null) as tt where tt.rid=1) b,";
  148. factorySql += "(select * from (select row_number() over(partition by FactoryId order by 日期 desc, 时间 desc) as rid,* from 水厂泵房2 where FactoryId is not null) as tt where tt.rid=1) c";
  149. factorySql += " where a.FactoryId = b.FactoryId and a.FactoryId = c.FactoryId";
  150. DataTable dtDevice = dbHelper.Fill(factorySql);
  151. if (dtDevice == null || dtDevice.Rows.Count == 0)
  152. {
  153. return;
  154. }
  155. DataColumnCollection cols = dtDevice.Columns;
  156. // 处理设备列表
  157. for (int i = 0; i < dtDevice.Rows.Count; i++)
  158. {
  159. DataRow dr = dtDevice.Rows[i];
  160. string deviceCode = dr["DeviceCode"].ToString();
  161. string type = dr["Type"].ToString();
  162. string realData = CreateJsonParameters(cols, dr);
  163. Dictionary<string, object> deviceMap = new Dictionary<string, object>();
  164. deviceMap["DeviceCode"] = deviceCode;
  165. deviceMap["Type"] = type;
  166. deviceMap["RealData"] = realData;
  167. string message = JsonConvert.SerializeObject(deviceMap);
  168. foreach (KeyValuePair<string, IModel> item in channels)
  169. {
  170. string key = item.Key;
  171. IModel channel = item.Value;
  172. IBasicProperties property = properties[key];
  173. channel.BasicPublish("workmanship", "", property, Encoding.UTF8.GetBytes(message)); //生产消息
  174. }
  175. }
  176. log.Info("水厂工艺图数据同步任务执行结束.................\r\n");
  177. }
  178. catch (Exception ex)
  179. {
  180. log.Error("水厂工艺图数据同步任务执行错误:" + ex.Message + "===========" + ex.StackTrace + "\r\n");
  181. }
  182. }
  183. private static string CreateJsonParameters(DataColumnCollection cols,DataRow dr)
  184. {
  185. StringBuilder JsonString = new StringBuilder();
  186. JsonString.Append("{");
  187. for (int j = 0; j < cols.Count; j++)
  188. {
  189. if (j < cols.Count - 1)
  190. {
  191. JsonString.Append("\"" + cols[j].ColumnName.ToString() + "\":" + "\"" + dr[j].ToString().Trim() + "\",");
  192. }
  193. else if (j == cols.Count - 1)
  194. {
  195. JsonString.Append("\"" + cols[j].ColumnName.ToString() + "\":" + "\"" + dr[j].ToString().Trim() + "\"");
  196. }
  197. }
  198. JsonString.Append("}");
  199. return JsonString.ToString();
  200. }
  201. static IDbProvider dbHelper
  202. {
  203. get
  204. {
  205. var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, Constants.WaterFactoryDbConncetion);
  206. return DbDefine;
  207. }
  208. }
  209. }
  210. }