jingshanDataUploadDataJob.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 meterCodeTemp = meterCode;
  168. String lastTime = "";
  169. int isValve = Convert.ToInt32(drMeter["isValve"]);
  170. int nowYear = DateTime.Now.Year;
  171. int lastYear = uploadHis.ContainsKey(meterCode) ? Convert.ToDateTime(uploadHis[meterCode]).Year : nowYear;
  172. for (int k = lastYear; k <= nowYear; k++)
  173. {
  174. string tablename = "历史记录_" + ("000000" + meterId).Substring(meterId.Length, 6) + "_" + k;
  175. // 判断历史记录表是否存在
  176. if (!CheckTableExist(tablename))
  177. {
  178. continue;
  179. }
  180. String sqlMeterHis = "select 记录时间,采集时间,正累计流量,负累计流量,净累计流量,瞬时流量,电池电压,压力,设备状态,通讯状态";
  181. if (isValve == 1) {// 阀控的情况
  182. sqlMeterHis += ",开度,运行模式,手动开度设定值,压力量程设定,常用压力设定上限,步进间隔,常用压力设定下限,步进刻度";
  183. }
  184. if(isValve == 2) { // 控阀控泵的情况
  185. sqlMeterHis += ",[运行模式],[阀1开到位延时],[阀2开到位延时],[阀3开到位延时],[真空泵开启时间],[排气间隔],[控制方式],[真空泵故障],[阀1状态],[阀2状态],[阀3状态]";
  186. }
  187. sqlMeterHis +=" from " + tablename;
  188. if (uploadHis.ContainsKey(meterCode)) // 添加是否在线的判断
  189. {
  190. sqlMeterHis += " where 设备状态 = '全部正常' AND 通讯状态 = '全部正常' AND 采集时间 > '" + uploadHis[meterCode] + "'";
  191. // sqlMeterHis += " where 采集时间 > '" + uploadHis[meterCode] + "'";
  192. }
  193. else { // 添加是否在线的判断
  194. sqlMeterHis += " WHERE 设备状态 = '全部正常' AND 通讯状态 = '全部正常'";
  195. }
  196. sqlMeterHis += " order by 采集时间";
  197. DataTable dtMeterHis = dbHelper.Fill(sqlMeterHis);
  198. StringBuilder message = new StringBuilder();
  199. for (int j = 0; j < dtMeterHis.Rows.Count; j++)
  200. {
  201. message.Clear();
  202. try
  203. {
  204. DataRow drMeterHis = dtMeterHis.Rows[j];
  205. String getDateTime = Convert.ToDateTime(drMeterHis["采集时间"]).ToString("yyyy-MM-dd HH:mm:ss");
  206. if (!(meterId == "625" || meterId == "633"))
  207. {
  208. message.Append("{");
  209. message.Append("\"meterAssessmentCode\": \"").Append(meterCode).Append("\",");
  210. message.Append("\"manufacturerCode\": ").Append(Constants.ManufacturerCode).Append(",");
  211. message.Append("\"getDateTime\": \"").Append(getDateTime).Append("\",");
  212. if (isValve == 1)
  213. {
  214. if (Convert.DBNull != drMeterHis["开度"])
  215. {
  216. message.Append("\"valveOpening\": ").Append(Convert.ToDecimal(drMeterHis["开度"])).Append(",");
  217. }
  218. if (Convert.DBNull != drMeterHis["运行模式"])
  219. {
  220. message.Append("\"operaOption\": ").Append(Convert.ToDecimal(drMeterHis["运行模式"])).Append(",");
  221. }
  222. if (Convert.DBNull != drMeterHis["手动开度设定值"])
  223. {
  224. message.Append("\"autoOpingVal\": ").Append(Convert.ToDecimal(drMeterHis["手动开度设定值"])).Append(",");
  225. }
  226. if (Convert.DBNull != drMeterHis["压力量程设定"])
  227. {
  228. message.Append("\"pressureRange\": ").Append(Convert.ToDecimal(drMeterHis["压力量程设定"])).Append(",");
  229. }
  230. if (Convert.DBNull != drMeterHis["常用压力设定上限"])
  231. {
  232. message.Append("\"comPressureVal\": ").Append(Convert.ToDecimal(drMeterHis["常用压力设定上限"])).Append(",");
  233. }
  234. if (Convert.DBNull != drMeterHis["步进间隔"])
  235. {
  236. message.Append("\"changeTime\": ").Append(Convert.ToDecimal(drMeterHis["步进间隔"])).Append(",");
  237. }
  238. if (Convert.DBNull != drMeterHis["常用压力设定下限"])
  239. {
  240. message.Append("\"comPressureValDown\": ").Append(Convert.ToDecimal(drMeterHis["常用压力设定下限"])).Append(",");
  241. }
  242. if (Convert.DBNull != drMeterHis["步进刻度"])
  243. {
  244. message.Append("\"stepInterval\": ").Append(Convert.ToDecimal(drMeterHis["步进刻度"])).Append(",");
  245. }
  246. }
  247. if(isValve == 2) { // 控阀控泵
  248. if (Convert.DBNull != drMeterHis["运行模式"])
  249. {
  250. message.Append("\"OperaOption\": ").Append(drMeterHis["运行模式"]).Append(",");
  251. }
  252. if (Convert.DBNull != drMeterHis["阀1开到位延时"])
  253. {
  254. message.Append("\"valveOneOpenDelayed\": ").Append(Convert.ToDecimal(drMeterHis["阀1开到位延时"])).Append(",");
  255. }
  256. if (Convert.DBNull != drMeterHis["阀2开到位延时"])
  257. {
  258. message.Append("\"valveTwoOpenDelayed\": ").Append(Convert.ToDecimal(drMeterHis["阀2开到位延时"])).Append(",");
  259. }
  260. if (Convert.DBNull != drMeterHis["阀3开到位延时"])
  261. {
  262. message.Append("\"valveThreeOpenDelayed\": ").Append(Convert.ToDecimal(drMeterHis["阀3开到位延时"])).Append(",");
  263. }
  264. if (Convert.DBNull != drMeterHis["真空泵开启时间"])
  265. {
  266. message.Append("\"vacuumPumpOpenTime\": ").Append(Convert.ToDecimal(drMeterHis["真空泵开启时间"])).Append(",");
  267. }
  268. if (Convert.DBNull != drMeterHis["排气间隔"])
  269. {
  270. message.Append("\"exhaustTime\": ").Append(Convert.ToDecimal(drMeterHis["排气间隔"])).Append(",");
  271. }
  272. if (Convert.DBNull != drMeterHis["控制方式"])
  273. {
  274. message.Append("\"runModule\": \"").Append(drMeterHis["控制方式"]).Append("\",");
  275. }
  276. if (Convert.DBNull != drMeterHis["真空泵故障"])
  277. {
  278. message.Append("\"vacuumPumpFault\": \"").Append(drMeterHis["真空泵故障"]).Append("\",");
  279. }
  280. if (Convert.DBNull != drMeterHis["阀1状态"])
  281. {
  282. message.Append("\"valveOneState\": \"").Append(drMeterHis["阀1状态"]).Append("\",");
  283. }
  284. if (Convert.DBNull != drMeterHis["阀2状态"])
  285. {
  286. message.Append("\"valveTwoState\": \"").Append(drMeterHis["阀2状态"]).Append("\",");
  287. }
  288. if (Convert.DBNull != drMeterHis["阀3状态"])
  289. {
  290. message.Append("\"valveThreeState\": \"").Append(drMeterHis["阀3状态"]).Append("\"");
  291. }
  292. }
  293. if (Convert.DBNull != drMeterHis["净累计流量"])
  294. {
  295. message.Append("\"netCumulativeFlow\": ").Append(Convert.ToDecimal(drMeterHis["净累计流量"])).Append(",");
  296. }
  297. if (Convert.DBNull != drMeterHis["正累计流量"])
  298. {
  299. message.Append("\"positiveCumulativeFlow\": ").Append(Convert.ToDecimal(drMeterHis["正累计流量"])).Append(",");
  300. }
  301. if (Convert.DBNull != drMeterHis["负累计流量"])
  302. {
  303. message.Append("\"negativeCumulativeFlow\": ").Append(Convert.ToDecimal(drMeterHis["负累计流量"])).Append(",");
  304. }
  305. if (Convert.DBNull != drMeterHis["瞬时流量"])
  306. {
  307. message.Append("\"instantaneousFlow\": ").Append(Convert.ToDecimal(drMeterHis["瞬时流量"])).Append(",");
  308. }
  309. if (Convert.DBNull != drMeterHis["压力"])
  310. {
  311. message.Append("\"pressure\": ").Append(Convert.ToDecimal(drMeterHis["压力"])).Append(",");
  312. }
  313. if (Convert.DBNull != drMeterHis["电池电压"])
  314. {
  315. message.Append("\"batteryVoltageValue\": ").Append(Convert.ToDecimal(drMeterHis["电池电压"]));
  316. }
  317. //int isOnline = 0; // 0 不在线 1 在线
  318. //if(drMeterHis["设备状态"] == "全部正常" && drMeterHis["通讯状态"] == "全部正常")
  319. //{
  320. // isOnline = 1;
  321. //}
  322. //message.Append("\"isOnline\": ").Append(isOnline).Append("");
  323. message.Append("}");
  324. }
  325. else {
  326. if(meterId == "625") {
  327. message.Append("{");
  328. message.Append("\"meterAssessmentCode\": \"").Append(meterCode).Append("\",");
  329. message.Append("\"manufacturerCode\": ").Append(Constants.ManufacturerCode).Append(",");
  330. message.Append("\"getDateTime\": \"").Append(getDateTime).Append("\",");
  331. if (Convert.DBNull != drMeterHis["压力"])
  332. {
  333. message.Append("\"pressure\": ").Append(Convert.ToDecimal(drMeterHis["压力"])).Append(",");
  334. }
  335. if (Convert.DBNull != drMeterHis["电池电压"])
  336. {
  337. message.Append("\"batteryVoltageValue\": ").Append(Convert.ToDecimal(drMeterHis["电池电压"]));
  338. }
  339. message.Append("}");
  340. String str = message.ToString();
  341. foreach (KeyValuePair<string, IModel> item in channels)
  342. {
  343. string key = item.Key;
  344. IModel channel = item.Value;
  345. IBasicProperties property = properties[key];
  346. channel.BasicPublish("zone.deviceHis", "", property, Encoding.UTF8.GetBytes(str)); //生产消息
  347. }
  348. message.Clear();
  349. if(meterId == "625") {
  350. meterCode = "jingshan015";
  351. }
  352. else {
  353. continue;
  354. }
  355. message.Append("{");
  356. message.Append("\"meterAssessmentCode\": \"").Append(meterCode).Append("\",");
  357. message.Append("\"manufacturerCode\": ").Append(Constants.ManufacturerCode).Append(",");
  358. message.Append("\"getDateTime\": \"").Append(getDateTime).Append("\",");
  359. if (Convert.DBNull != drMeterHis["净累计流量"])
  360. {
  361. message.Append("\"netCumulativeFlow\": ").Append(Convert.ToDecimal(drMeterHis["净累计流量"])).Append(",");
  362. }
  363. if (Convert.DBNull != drMeterHis["正累计流量"])
  364. {
  365. message.Append("\"positiveCumulativeFlow\": ").Append(Convert.ToDecimal(drMeterHis["正累计流量"])).Append(",");
  366. }
  367. if (Convert.DBNull != drMeterHis["负累计流量"])
  368. {
  369. message.Append("\"negativeCumulativeFlow\": ").Append(Convert.ToDecimal(drMeterHis["负累计流量"])).Append(",");
  370. }
  371. if (Convert.DBNull != drMeterHis["瞬时流量"])
  372. {
  373. message.Append("\"instantaneousFlow\": ").Append(Convert.ToDecimal(drMeterHis["瞬时流量"])).Append(",");
  374. }
  375. if (Convert.DBNull != drMeterHis["电池电压"])
  376. {
  377. message.Append("\"batteryVoltageValue\": ").Append(Convert.ToDecimal(drMeterHis["电池电压"]));
  378. }
  379. message.Append("}");
  380. }
  381. else if(meterId == "633") {
  382. continue;
  383. }
  384. }
  385. foreach (KeyValuePair<string, IModel> item in channels)
  386. {
  387. string key = item.Key;
  388. IModel channel = item.Value;
  389. IBasicProperties property = properties[key];
  390. channel.BasicPublish("zone.deviceHis", "", property, Encoding.UTF8.GetBytes(message.ToString())); //生产消息
  391. }
  392. lastTime = getDateTime;
  393. meterCode = meterCodeTemp;
  394. }
  395. catch (Exception ex)
  396. {
  397. log.Info("大表设备历史记录同步任务数据推送失败:" + message.ToString() + "\r\n");
  398. log.Error(ex.Message + "===========" + ex.StackTrace + "\r\n");
  399. }
  400. }
  401. }
  402. if (!"".Equals(lastTime))
  403. {
  404. uploadHis[meterCode] = lastTime;
  405. uploadHis[meterCodeTemp] = lastTime;
  406. }
  407. }
  408. SavaUploadHis(uploadHis);
  409. log.Info("大表设备历史记录同步任务执行结束.................\r\n");
  410. }
  411. catch (Exception ex)
  412. {
  413. log.Error("大表设备历史记录同步任务执行错误:" + ex.Message + "===========" + ex.StackTrace + "\r\n");
  414. }
  415. }
  416. /// <summary>
  417. /// 更新配置文件中的值
  418. /// </summary>
  419. /// <param name="key">键</param>
  420. /// <param name="value">值</param>
  421. private void UpdateAppConfig(String key, String value)
  422. {
  423. var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  424. cfg.AppSettings.Settings[key].Value = value;
  425. cfg.Save();
  426. ConfigurationManager.RefreshSection("appSettings");
  427. }
  428. /// <summary>
  429. /// 判断历史记录表是否存在
  430. /// </summary>
  431. /// <param name="tablename"></param>
  432. /// <returns></returns>
  433. private bool CheckTableExist(string tablename)
  434. {
  435. DataTable table = dbHelper.Fill("select top 1 * from sysobjects where name='" + tablename + "' and xtype='u'");
  436. if (table == null || table.Rows.Count == 0)
  437. {
  438. return false;
  439. }
  440. return true;
  441. }
  442. /// <summary>
  443. /// 保存每块块表的上传最后一条历史记录
  444. /// </summary>
  445. /// <param name="uploadHis"></param>
  446. private void SavaUploadHis(Dictionary<String, String> uploadHis)
  447. {
  448. // 清除之前的内容
  449. FileStream stream = File.Open(@"TextFile1.txt", FileMode.OpenOrCreate, FileAccess.Write);
  450. stream.Seek(0, SeekOrigin.Begin);
  451. stream.SetLength(0);
  452. stream.Close();
  453. using (StreamWriter sw = new StreamWriter(@"TextFile1.txt"))
  454. {
  455. foreach (var item in uploadHis)
  456. {
  457. sw.WriteLine(item.Key + "," + item.Value);
  458. }
  459. }
  460. }
  461. static IDbProvider dbHelper
  462. {
  463. get
  464. {
  465. var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, Constants.DbConncetion);
  466. return DbDefine;
  467. }
  468. }
  469. }
  470. }