DASCDataJob.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using log4net;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using Quartz;
  5. using RDIFramework.Utilities;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Text;
  13. using TimedUpload.utils;
  14. namespace TimedUpload.QuartzJobs
  15. {
  16. /// <summary>
  17. /// 大安水厂出厂水定时上传任务
  18. /// </summary>
  19. [DisallowConcurrentExecution]
  20. public class DASCDataJob : IJob
  21. {
  22. private readonly ILog log = LogManager.GetLogger(typeof(DASCDataJob));
  23. static IDbProvider dbHelper
  24. {
  25. get
  26. {
  27. var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, SystemConfig.pipeLineDbConn);
  28. return DbDefine;
  29. }
  30. }
  31. private string manufacturerCode = SystemConfig.pipeLineManufacturerCode;
  32. private string uploadUrl = SystemConfig.pipeLineNewUploadUrl;
  33. public void Execute(IJobExecutionContext context)
  34. {
  35. //1.获取要上传的测定信息
  36. //2.清水池 237 获取出厂累计流量1、出厂瞬时流量1
  37. //3.流量压力 236 获取出厂累计流量2、出厂瞬时流量2、二号出水压力、出厂累计流量3、出厂瞬时流量3
  38. try
  39. {
  40. string sql = "",devId,devNo,devName,lastUpdateTime;
  41. sql = "Select ID,最后更新时间,名称 From 设备信息 Where id in(237,236)";
  42. DataTable dt = dbHelper.Fill(sql);
  43. for (int row = 0; row < dt.Rows.Count; row++)
  44. {
  45. devId = dt.Rows[row]["ID"].ToString();//ID
  46. //devNo = dt.Rows[row]["考核表编号"].ToString();//考核表编号
  47. lastUpdateTime= dt.Rows[row]["最后更新时间"].ToString();
  48. devName = dt.Rows[row]["名称"].ToString();
  49. switch (devId)
  50. {
  51. case "237"://出水一
  52. DealQSC(devId, "wwkj0001", lastUpdateTime, devName);
  53. //DealQSC(devId, devNo, lastUpdateTime, devName);
  54. break;
  55. case "236":
  56. DealQSC2(devId, "", lastUpdateTime, devName);
  57. //DealQSC2(devId, devNo, lastUpdateTime, devName);
  58. break;
  59. default:
  60. log.Info("devid");
  61. break;
  62. }
  63. }
  64. }
  65. catch(Exception ex)
  66. {
  67. log.Info("异常信息:" + ex.Message);
  68. }
  69. }
  70. //处理清水池流量
  71. public void DealQSC(string devId,string devNo,string lastUpdateTime,string devName)
  72. {
  73. try
  74. {
  75. int year = Convert.ToDateTime(lastUpdateTime).Year;
  76. string sql = "";
  77. string strTableName = GetHisTable(devId, year.ToString());
  78. if (string.IsNullOrEmpty(strTableName))
  79. return;
  80. //先取数据
  81. sql = "Select top 1000 采集时间,出厂累计流量1,出厂瞬时流量1 From " + strTableName + " Where 采集时间 > '" + lastUpdateTime + "' Order By 采集时间 asc";
  82. DataTable dtDevHis = dbHelper.Fill(sql);
  83. //接口是一条一条接收数据
  84. StringBuilder dataBuilder = new StringBuilder();
  85. //log.Info("dtDevHis.Rows.Count:" + dtDevHis.Rows.Count.ToString());
  86. for (int hisRow = 0; hisRow < dtDevHis.Rows.Count; hisRow++)
  87. {
  88. FlowEntity entity = new FlowEntity()
  89. {
  90. devId=devId,
  91. devName=devName,
  92. MeterAssessmentCode= devNo,
  93. GetDateTime=dtDevHis.Rows[hisRow]["采集时间"].ToString(),
  94. NetCumulativeFlow= dtDevHis.Rows[hisRow]["出厂累计流量1"].ToString(),
  95. PositiveCumulativeFlow= dtDevHis.Rows[hisRow]["出厂累计流量1"].ToString(),
  96. NegativeCumulativeFlow = "0",
  97. InstantaneousFlow= dtDevHis.Rows[hisRow]["出厂瞬时流量1"].ToString(),
  98. Pressure= "NULL",
  99. BatteryVoltageValue= "NULL"
  100. };
  101. CompactAndSendData(entity);
  102. }
  103. //跨年判断
  104. CrossYear(year, devId);
  105. }
  106. catch(Exception ex)
  107. {
  108. log.Info("设备名称:" + devName + ",出现异常:" + ex.Message);
  109. }
  110. //end
  111. }
  112. //处理流量压力
  113. public void DealQSC2(string devId,string devNo,string lastUpdateTime,string devName)
  114. {
  115. try
  116. {
  117. //log.Info("1");
  118. int year = Convert.ToDateTime(lastUpdateTime).Year;
  119. string sql = "";
  120. string strTableName = GetHisTable(devId, year.ToString());
  121. //log.Info(strTableName);
  122. if (string.IsNullOrEmpty(strTableName))
  123. return;
  124. //先取数据
  125. sql = "Select top 1000 采集时间,出厂累计流量2,出厂瞬时流量2,二号出水压力,出厂累计流量3,出厂瞬时流量3 From " + strTableName + " Where 采集时间 > '" + lastUpdateTime + "' Order By 采集时间 asc";
  126. //log.Info(sql);
  127. DataTable dtDevHis = dbHelper.Fill(sql);
  128. //接口是一条一条接收数据
  129. StringBuilder dataBuilder = new StringBuilder();
  130. //log.Info("dtDevHis.Rows.Count:" + dtDevHis.Rows.Count.ToString());
  131. for (int hisRow = 0; hisRow < dtDevHis.Rows.Count; hisRow++)
  132. {
  133. //2号管
  134. FlowEntity entity2 = new FlowEntity()
  135. {
  136. devId = devId,
  137. devName = devName,
  138. MeterAssessmentCode = "wwkj0002",
  139. GetDateTime = dtDevHis.Rows[hisRow]["采集时间"].ToString(),
  140. NetCumulativeFlow = dtDevHis.Rows[hisRow]["出厂累计流量2"].ToString(),
  141. PositiveCumulativeFlow = dtDevHis.Rows[hisRow]["出厂累计流量2"].ToString(),
  142. NegativeCumulativeFlow = "0",
  143. InstantaneousFlow = dtDevHis.Rows[hisRow]["出厂瞬时流量2"].ToString(),
  144. Pressure = dtDevHis.Rows[hisRow]["二号出水压力"].ToString(),
  145. BatteryVoltageValue = "NULL"
  146. };
  147. CompactAndSendData(entity2);
  148. //3号管
  149. FlowEntity entity3 = new FlowEntity()
  150. {
  151. devId = devId,
  152. devName = devName,
  153. MeterAssessmentCode = "wwkj0003",//devNo,
  154. GetDateTime = dtDevHis.Rows[hisRow]["采集时间"].ToString(),
  155. NetCumulativeFlow = dtDevHis.Rows[hisRow]["出厂累计流量3"].ToString(),
  156. PositiveCumulativeFlow = dtDevHis.Rows[hisRow]["出厂累计流量3"].ToString(),
  157. NegativeCumulativeFlow = "0",
  158. InstantaneousFlow = dtDevHis.Rows[hisRow]["出厂瞬时流量3"].ToString(),
  159. Pressure = "NULL",
  160. BatteryVoltageValue = "NULL"
  161. };
  162. CompactAndSendData(entity3);
  163. }
  164. //跨年判断
  165. CrossYear(year, devId);
  166. }
  167. catch (Exception ex)
  168. {
  169. log.Info("设备名称:" + devName + ",出现异常:" + ex.Message);
  170. }
  171. //end
  172. }
  173. #region 请求接口
  174. public string postSend(string url, string param)
  175. {
  176. string strResult = "";
  177. Encoding myEncode = Encoding.GetEncoding("UTF-8");
  178. byte[] postBytes = Encoding.UTF8.GetBytes(param);
  179. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
  180. req.Method = "POST";
  181. req.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
  182. req.ContentLength = postBytes.Length;
  183. try
  184. {
  185. using (Stream reqStream = req.GetRequestStream())
  186. {
  187. reqStream.Write(postBytes, 0, postBytes.Length);
  188. }
  189. using (WebResponse res = req.GetResponse())
  190. {
  191. using (StreamReader sr = new StreamReader(res.GetResponseStream(), myEncode))
  192. {
  193. strResult = sr.ReadToEnd();
  194. return strResult;
  195. }
  196. }
  197. }
  198. catch (WebException ex)
  199. {
  200. log.Info("Post数据出错:" + ex.Message);
  201. return "";
  202. }
  203. }
  204. #endregion
  205. #region 组装数据发送数据
  206. public void CompactAndSendData(FlowEntity entity)
  207. {
  208. StringBuilder dataBuilder = new StringBuilder();
  209. dataBuilder.Clear();
  210. dataBuilder.Append("{");
  211. dataBuilder.Append("\"ManufacturerCode\":\"").Append(manufacturerCode).Append("\",");
  212. dataBuilder.Append("\"MeterAssessmentCode\":\"").Append(entity.MeterAssessmentCode).Append("\",");
  213. dataBuilder.Append("\"GetDateTime\":\"").Append(Convert.ToDateTime(DataBaseHelper.DataFormat(entity.GetDateTime)).ToString("yyyy-MM-dd HH:mm:ss")).Append("\",");
  214. dataBuilder.Append("\"NetCumulativeFlow\":\"").Append(DataBaseHelper.DataFormat(entity.NetCumulativeFlow)).Append("\",");
  215. dataBuilder.Append("\"PositiveCumulativeFlow\":\"").Append(DataBaseHelper.DataFormat(entity.PositiveCumulativeFlow)).Append("\",");
  216. dataBuilder.Append("\"NegativeCumulativeFlow\":\"").Append(DataBaseHelper.DataFormat(entity.NegativeCumulativeFlow)).Append("\",");
  217. dataBuilder.Append("\"InstantaneousFlow\":\"").Append(DataBaseHelper.DataFormat(entity.InstantaneousFlow)).Append("\",");
  218. dataBuilder.Append("\"Pressure\":\"").Append(DataBaseHelper.DataFormat(entity.Pressure)).Append("\",");
  219. dataBuilder.Append("\"BatteryVoltageValue\":\"").Append(DataBaseHelper.DataFormat(entity.BatteryVoltageValue)).Append("\",");
  220. dataBuilder.Append("\"MeterFault\":\"").Append("0").Append("\",");
  221. dataBuilder.Append("\"BoxDoorAlarm\":\"").Append("NULL").Append("\",");
  222. dataBuilder.Append("\"BatteryVoltageState\":\"").Append("NULL").Append("\",");
  223. dataBuilder.Append("\"remark1\":\"").Append("NULL").Append("\",");
  224. dataBuilder.Append("\"remark2\":\"").Append("NULL").Append("\",");
  225. dataBuilder.Append("\"remark3\":\"").Append("NULL").Append("\",");
  226. dataBuilder.Append("\"remark4\":\"").Append("NULL").Append("\",");
  227. dataBuilder.Append("\"remark5\":\"").Append("NULL").Append("\"");
  228. dataBuilder.Append("}");
  229. //log.Info(dataBuilder.ToString());
  230. string updateReuslt = postSend(uploadUrl, dataBuilder.ToString());
  231. JObject jo = (JObject)JsonConvert.DeserializeObject(updateReuslt);
  232. string execCode = jo["execCode"].ToString();//执行返回代码
  233. string execMsg = jo["execMsg"].ToString();//返回返回描述
  234. //log.Info(execMsg); //执行成功
  235. if (execCode == "0")
  236. {
  237. //更新 记录表
  238. string UpdateHisSmartSql = "UPDATE 设备信息 SET 最后更新时间 = '" + entity.GetDateTime + "' WHERE ID = " + entity.devId;
  239. int num =dbHelper.ExecuteNonQuery(UpdateHisSmartSql);
  240. //log.Info("ch插入:" + num);
  241. }
  242. else
  243. {
  244. //插入异常表
  245. log.Debug("设备名称:" + entity.devName + ",插入异常:" + execMsg);
  246. }
  247. }
  248. #endregion
  249. #region 历史记录表判断
  250. public string GetHisTable(string devId,string year)
  251. {
  252. string tableName = "";
  253. tableName="历史记录_" + ("000000" + devId).Substring(devId.Length, 6) + "_" + year;
  254. if (!DataBaseHelper.CheckTableExiste(tableName))
  255. {
  256. tableName = "";
  257. }
  258. return tableName;
  259. }
  260. #endregion
  261. #region 跨年判断
  262. public void CrossYear(int year,string devId)
  263. {
  264. int thisYear = DateTime.Now.Year;
  265. string sql = "";
  266. if (thisYear > year)
  267. {
  268. //跨年处理 记录表的更新时间 更新为新一年的 最初时间 例如"2019-01-01 00:00:00"
  269. sql = "UPDATE 设备信息 SET 最后更新时间 = '" + thisYear + "-01-01 00:00:00.000" + "' WHERE ID = " + devId;
  270. dbHelper.ExecuteNonQuery(sql);
  271. }
  272. }
  273. #endregion
  274. }
  275. }