jingshanDataUploadDataJob.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using log4net;
  2. using Quartz;
  3. using RabbitMQ.Client;
  4. using RDIFramework.Utilities;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. namespace TimedUpload.QuartzJobs
  13. {
  14. class jingshanDataUploadDataJob : IJob
  15. {
  16. private readonly ILog log = LogManager.GetLogger(typeof(DataUploadJob));
  17. public void Execute(IJobExecutionContext context)
  18. {
  19. string[] uploadUrls = Constants.UploadUrl.Split('|');
  20. Dictionary<string, IConnection> connections = new Dictionary<string, IConnection>();
  21. Dictionary<string, IModel> channels = new Dictionary<string, IModel>();
  22. Dictionary<string, IBasicProperties> properties = new Dictionary<string, IBasicProperties>();
  23. foreach (string uploadUrl in uploadUrls)
  24. {
  25. ConnectionFactory factory = new ConnectionFactory();
  26. factory.HostName = uploadUrl;//主机名,Rabbit会拿这个IP生成一个endpoint,这个很熟悉吧,就是socket绑定的那个终结点。
  27. factory.UserName = Constants.UploadUserName;//默认用户名,用户可以在服务端自定义创建,有相关命令行
  28. factory.Password = Constants.UploadPassword;//默认密码
  29. factory.AutomaticRecoveryEnabled = true; // 链接断开会自动重连
  30. IConnection connection = factory.CreateConnection();
  31. IModel channel = connection.CreateModel();
  32. channel.QueueDeclare("zone.device", true, false, false, null);//创建一个名称为kibaqueue的消息队列
  33. channel.QueueDeclare("zone.deviceHis", true, false, false, null);//创建一个名称为kibaqueue的消息队列
  34. IBasicProperties property = channel.CreateBasicProperties();
  35. property.ContentType = "text/plain";
  36. property.DeliveryMode = 2; //持久化
  37. connections.Add(uploadUrl, connection);
  38. properties.Add(uploadUrl, property);
  39. channels.Add(uploadUrl, channel);
  40. }
  41. if (channels.Count > 0)
  42. {
  43. SendZoneDevice(channels, properties);
  44. SendZoneDeviceHis(channels, properties);
  45. }
  46. foreach (KeyValuePair<string, IConnection> item in connections)
  47. {
  48. IConnection connection = item.Value;
  49. connection.Close();
  50. }
  51. }
  52. /// <summary>
  53. /// 大表设备添加
  54. /// </summary>
  55. /// <param name="channels"></param>
  56. /// <param name="properties"></param>
  57. private void SendZoneDevice(Dictionary<string, IModel> channels, Dictionary<string, IBasicProperties> properties)
  58. {
  59. try
  60. {
  61. String meterId = Constants.MeterId;
  62. String sql = "SELECT a.ID,a.名称,a.考核表编码,a.X坐标,a.Y坐标,b.传输协议参数,ISNULL(a.是否阀控,0) AS isValve FROM [设备信息] a left join 传输设备 b on a.传输设备ID = b.ID where a.是否启用 = '是' and a.考核表编码 is not null and a.ID > " + meterId + " order by a.ID";
  63. DataTable dt = dbHelper.Fill(sql);
  64. if (dt == null)
  65. {
  66. log.Info("大表设备同步任务查询报错.................\r\n");
  67. return;
  68. }
  69. if (dt.Rows.Count == 0)
  70. {
  71. log.Info("大表设备同步任务,没有需要同步的设备.................\r\n");
  72. return;
  73. }
  74. log.Info("大表设备同步任务开始执行.................\r\n");
  75. StringBuilder message = new StringBuilder();
  76. for (int i = 0; i < dt.Rows.Count; i++)
  77. {
  78. message.Clear();
  79. try
  80. {
  81. DataRow dr = dt.Rows[i];
  82. String iccid = "";
  83. if (!"".Equals(dr["传输协议参数"].ToString()))
  84. {
  85. iccid = dr["传输协议参数"].ToString().Split(',')[0];
  86. }
  87. String lngAndLat = "";
  88. if (!"".Equals(dr["X坐标"].ToString()) && !"".Equals(dr["Y坐标"].ToString()))
  89. {
  90. lngAndLat = dr["X坐标"].ToString() + "|" + dr["Y坐标"].ToString();
  91. }
  92. message.Append("{");
  93. message.Append("\"meterAssessmentName\": \"").Append(dr["名称"]).Append("\",");
  94. message.Append("\"iccId\": ").Append(iccid).Append(",");
  95. //message.Append("\"areaId\": 22,");
  96. message.Append("\"lngAndLat\": \"").Append(lngAndLat).Append("\",");
  97. //message.Append("\"pipeCailber\": \"DN32\",");
  98. //message.Append("\"pipeTexture\": \"PVC\",");
  99. //message.Append("\"imei\": \"77564212\",");
  100. message.Append("\"isPressucre\": 1,");
  101. message.Append("\"isFlow\": 1,");
  102. message.Append("\"isZoneMeter\": 1,");
  103. message.Append("\"isTradeMeter\": 0,");
  104. message.Append("\"isLargeUser\": 0,");
  105. message.Append("\"isLargeUser\": 0,");
  106. message.Append("\"isValve\":").Append(dr["isValve"].ToString()).Append(",");
  107. message.Append("\"meterAssessmentCode\": \"").Append(dr["考核表编码"]).Append("\",");
  108. message.Append("\"manufacturerCode\": \"").Append(Constants.ManufacturerCode).Append("\",");
  109. message.Append("\"meterTypeId\": \"2\"");
  110. message.Append("}");
  111. foreach (KeyValuePair<string, IModel> item in channels)
  112. {
  113. string key = item.Key;
  114. IModel channel = item.Value;
  115. IBasicProperties property = properties[key];
  116. channel.BasicPublish("zone.device", "", property, Encoding.UTF8.GetBytes(message.ToString())); //生产消息
  117. }
  118. meterId = dr["ID"].ToString();
  119. }
  120. catch (Exception ex)
  121. {
  122. log.Info("大表设备同步任务数据推送失败:" + message.ToString() + "\r\n");
  123. log.Error(ex.Message + "===========" + ex.StackTrace + "\r\n");
  124. }
  125. }
  126. UpdateAppConfig("MeterId", meterId);
  127. log.Info("大表设备同步任务执行结束.................\r\n");
  128. }
  129. catch (Exception ex)
  130. {
  131. log.Error("大表设备同步任务执行错误:" + ex.Message + "===========" + ex.StackTrace + "\r\n");
  132. }
  133. }
  134. /// <summary>
  135. /// 大表历史数据
  136. /// </summary>
  137. /// <param name="channels"></param>
  138. /// <param name="properties"></param>
  139. private void SendZoneDeviceHis(Dictionary<string, IModel> channels, Dictionary<string, IBasicProperties> properties)
  140. {
  141. try
  142. {
  143. log.Info("大表设备历史数据同步任务开始执行.................\r\n");
  144. String sqlMeter = "SELECT ID,考核表编码,ISNULL(是否阀控,0) AS isValve FROM [设备信息] where 是否启用 = '是' and 考核表编码 is not null order by ID";
  145. DataTable dtMeter = dbHelper.Fill(sqlMeter);
  146. //Dictionary<string, object> arguments = new Dictionary<string, object>();
  147. //arguments["x-max-length-bytes"] = 2147383648;
  148. //arguments["x-overflow"] = "reject-publish";
  149. Dictionary<String, String> uploadHis = new Dictionary<string, string>();
  150. using (StreamReader sr = new StreamReader(@"TextFile1.txt"))
  151. {
  152. String line = "";
  153. while ((line = sr.ReadLine()) != null)
  154. {
  155. if (!"".Equals(line))
  156. {
  157. String[] item = line.Split(',');
  158. uploadHis[item[0]] = item[1];
  159. }
  160. }
  161. }
  162. for (int i = 0; i < dtMeter.Rows.Count; i++)
  163. {
  164. DataRow drMeter = dtMeter.Rows[i];
  165. String meterId = drMeter["ID"].ToString();
  166. String meterCode = drMeter["考核表编码"].ToString();
  167. String lastTime = "";
  168. int isValve = Convert.ToInt32(drMeter["isValve"]);
  169. int nowYear = DateTime.Now.Year;
  170. int lastYear = uploadHis.ContainsKey(meterCode) ? Convert.ToDateTime(uploadHis[meterCode]).Year : nowYear;
  171. for (int k = lastYear; k <= nowYear; k++)
  172. {
  173. string tablename = "历史记录_" + ("000000" + meterId).Substring(meterId.Length, 6) + "_" + k;
  174. // 判断历史记录表是否存在
  175. if (!CheckTableExist(tablename))
  176. {
  177. continue;
  178. }
  179. String sqlMeterHis = "select 记录时间,采集时间,正累计流量,负累计流量,净累计流量,瞬时流量,电池电压,压力";
  180. if (isValve == 1) {// 阀控的情况
  181. sqlMeterHis += ",开度,运行模式,手动开度设定值,压力量程设定,常用压力设定,阀门调整间隔";
  182. }
  183. sqlMeterHis +=" from " + tablename;
  184. if (uploadHis.ContainsKey(meterCode)) // 添加是否在线的判断
  185. {
  186. sqlMeterHis += " where 设备状态 = '全部正常' AND 通讯状态 = '全部正常' AND 采集时间 > '" + uploadHis[meterCode] + "'";
  187. }
  188. else { // 添加是否在线的判断
  189. sqlMeterHis += "WHERE 设备状态 = '全部正常' AND 通讯状态 = '全部正常'"
  190. }
  191. sqlMeterHis += " order by 采集时间";
  192. DataTable dtMeterHis = dbHelper.Fill(sqlMeterHis);
  193. StringBuilder message = new StringBuilder();
  194. for (int j = 0; j < dtMeterHis.Rows.Count; j++)
  195. {
  196. message.Clear();
  197. try
  198. {
  199. DataRow drMeterHis = dtMeterHis.Rows[j];
  200. String getDateTime = Convert.ToDateTime(drMeterHis["采集时间"]).ToString("yyyy-MM-dd HH:mm:ss");
  201. message.Append("{");
  202. message.Append("\"meterAssessmentCode\": \"").Append(meterCode).Append("\",");
  203. message.Append("\"manufacturerCode\": ").Append(Constants.ManufacturerCode).Append(",");
  204. message.Append("\"getDateTime\": \"").Append(getDateTime).Append("\",");
  205. if (isValve == 1)
  206. {
  207. if (Convert.DBNull != drMeterHis["开度"])
  208. {
  209. message.Append("\"valveOpening\": ").Append(Convert.ToDecimal(drMeterHis["开度"])).Append(",");
  210. }
  211. if (Convert.DBNull != drMeterHis["运行模式"])
  212. {
  213. message.Append("\"operaOption\": ").Append(Convert.ToDecimal(drMeterHis["运行模式"])).Append(",");
  214. }
  215. if (Convert.DBNull != drMeterHis["手动开度设定值"])
  216. {
  217. message.Append("\"autoOpingVal\": ").Append(Convert.ToDecimal(drMeterHis["手动开度设定值"])).Append(",");
  218. }
  219. if (Convert.DBNull != drMeterHis["压力量程设定"])
  220. {
  221. message.Append("\"pressureRange\": ").Append(Convert.ToDecimal(drMeterHis["压力量程设定"])).Append(",");
  222. }
  223. if (Convert.DBNull != drMeterHis["常用压力设定"])
  224. {
  225. message.Append("\"comPressureVal\": ").Append(Convert.ToDecimal(drMeterHis["常用压力设定"])).Append(",");
  226. }
  227. if (Convert.DBNull != drMeterHis["阀门调整间隔"])
  228. {
  229. message.Append("\"changeTime\": ").Append(Convert.ToDecimal(drMeterHis["阀门调整间隔"])).Append(",");
  230. }
  231. }
  232. if (Convert.DBNull != drMeterHis["净累计流量"])
  233. {
  234. message.Append("\"netCumulativeFlow\": ").Append(Convert.ToDecimal(drMeterHis["净累计流量"])).Append(",");
  235. }
  236. if (Convert.DBNull != drMeterHis["正累计流量"])
  237. {
  238. message.Append("\"positiveCumulativeFlow\": ").Append(Convert.ToDecimal(drMeterHis["正累计流量"])).Append(",");
  239. }
  240. if (Convert.DBNull != drMeterHis["负累计流量"])
  241. {
  242. message.Append("\"negativeCumulativeFlow\": ").Append(Convert.ToDecimal(drMeterHis["负累计流量"])).Append(",");
  243. }
  244. if (Convert.DBNull != drMeterHis["瞬时流量"])
  245. {
  246. message.Append("\"instantaneousFlow\": ").Append(Convert.ToDecimal(drMeterHis["瞬时流量"])).Append(",");
  247. }
  248. if (Convert.DBNull != drMeterHis["压力"])
  249. {
  250. message.Append("\"pressure\": ").Append(Convert.ToDecimal(drMeterHis["压力"])).Append(",");
  251. }
  252. if (Convert.DBNull != drMeterHis["电池电压"])
  253. {
  254. message.Append("\"batteryVoltageValue\": ").Append(Convert.ToDecimal(drMeterHis["电池电压"])).Append(",");
  255. }
  256. message.Append("}");
  257. foreach (KeyValuePair<string, IModel> item in channels)
  258. {
  259. string key = item.Key;
  260. IModel channel = item.Value;
  261. IBasicProperties property = properties[key];
  262. channel.BasicPublish("zone.deviceHis", "", property, Encoding.UTF8.GetBytes(message.ToString())); //生产消息
  263. }
  264. lastTime = getDateTime;
  265. }
  266. catch (Exception ex)
  267. {
  268. log.Info("大表设备历史记录同步任务数据推送失败:" + message.ToString() + "\r\n");
  269. log.Error(ex.Message + "===========" + ex.StackTrace + "\r\n");
  270. }
  271. }
  272. }
  273. if (!"".Equals(lastTime))
  274. {
  275. uploadHis[meterCode] = lastTime;
  276. }
  277. }
  278. SavaUploadHis(uploadHis);
  279. log.Info("大表设备历史记录同步任务执行结束.................\r\n");
  280. }
  281. catch (Exception ex)
  282. {
  283. log.Error("大表设备历史记录同步任务执行错误:" + ex.Message + "===========" + ex.StackTrace + "\r\n");
  284. }
  285. }
  286. /// <summary>
  287. /// 更新配置文件中的值
  288. /// </summary>
  289. /// <param name="key">键</param>
  290. /// <param name="value">值</param>
  291. private void UpdateAppConfig(String key, String value)
  292. {
  293. var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  294. cfg.AppSettings.Settings[key].Value = value;
  295. cfg.Save();
  296. ConfigurationManager.RefreshSection("appSettings");
  297. }
  298. /// <summary>
  299. /// 判断历史记录表是否存在
  300. /// </summary>
  301. /// <param name="tablename"></param>
  302. /// <returns></returns>
  303. private bool CheckTableExist(string tablename)
  304. {
  305. DataTable table = dbHelper.Fill("select top 1 * from sysobjects where name='" + tablename + "' and xtype='u'");
  306. if (table == null || table.Rows.Count == 0)
  307. {
  308. return false;
  309. }
  310. return true;
  311. }
  312. /// <summary>
  313. /// 保存每块块表的上传最后一条历史记录
  314. /// </summary>
  315. /// <param name="uploadHis"></param>
  316. private void SavaUploadHis(Dictionary<String, String> uploadHis)
  317. {
  318. // 清除之前的内容
  319. FileStream stream = File.Open(@"TextFile1.txt", FileMode.OpenOrCreate, FileAccess.Write);
  320. stream.Seek(0, SeekOrigin.Begin);
  321. stream.SetLength(0);
  322. stream.Close();
  323. using (StreamWriter sw = new StreamWriter(@"TextFile1.txt"))
  324. {
  325. foreach (var item in uploadHis)
  326. {
  327. sw.WriteLine(item.Key + "," + item.Value);
  328. }
  329. }
  330. }
  331. static IDbProvider dbHelper
  332. {
  333. get
  334. {
  335. var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, Constants.DbConncetion);
  336. return DbDefine;
  337. }
  338. }
  339. }
  340. }