DADeviceDataJob.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 System.Text.RegularExpressions;
  14. using TimedUpload.utils;
  15. namespace TimedUpload.QuartzJobs
  16. {
  17. /// <summary>
  18. /// 大安桑瑞大表监控数据对接
  19. /// </summary>
  20. [DisallowConcurrentExecution]
  21. public class DADeviceDataJob : IJob
  22. {
  23. private readonly ILog log = LogManager.GetLogger(typeof(DADeviceDataJob));
  24. private string manufacturerCode = SystemConfig.pipeLineManufacturerCode;
  25. private string uploadUrl = SystemConfig.pipeLineNewUploadUrl;
  26. static IDbProvider dbHelper
  27. {
  28. get
  29. {
  30. var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, SystemConfig.pipeLineDbConn);
  31. return DbDefine;
  32. }
  33. }
  34. public void Execute(IJobExecutionContext context)
  35. {
  36. //1.查询设备
  37. //2.上传数据
  38. try
  39. {
  40. decimal NetCumulativeFlow = 0;
  41. string sql = "", devId, devNo, devName, lastUpdateTime;
  42. sql = "Select MeterAssessmentId,MeterAssessmentName,GetDateTime,MeterAssessmentCode From MeterAssessmentBase WHERE DaFlag = 1";
  43. //log.Info("循环前");
  44. DataTable dt = dbHelper.Fill(sql);
  45. //log.Info(sql);
  46. for (int row = 0; row < dt.Rows.Count; row++)
  47. {
  48. devId = dt.Rows[row]["MeterAssessmentId"].ToString();//ID
  49. devNo = dt.Rows[row]["MeterAssessmentCode"].ToString();//考核表编号
  50. lastUpdateTime = Convert.ToDateTime(dt.Rows[row]["GetDateTime"]).ToString("yyyy-MM-dd HH:mm:ss");
  51. devName = dt.Rows[row]["MeterAssessmentName"].ToString();
  52. //log.Info("DevId=" + devId + ",devName=" + devName);
  53. //sql = "Select * From ARM_HistoryData Where Address='" + devNo + "' order by CreateMinute asc";
  54. sql = "select \"RealValue\",\"ForValue\",\"RevValue\",\"PressValue\",\"CelVal\",\"CreateMinute\" from \"ARM_HistoryData\" t where \"AddressCode\"='" + devNo + "' AND \"CreateMinute\" > '" + lastUpdateTime + "' order by \"CreateMinute\" asc";
  55. DataTable dtData = OracleHelper.ExecuteDataTable(sql);
  56. int rowIndex = dtData.Rows.Count;
  57. //log.Info(sql);
  58. for(int i = 0; i < rowIndex; i++)
  59. {
  60. if (dtData.Rows[i]["ForValue"] != DBNull.Value && dtData.Rows[i]["RevValue"] != DBNull.Value)
  61. {
  62. if (IsNumeric((dtData.Rows[i]["ForValue"].ToString())) && IsNumeric((dtData.Rows[i]["ForValue"].ToString())))
  63. {
  64. NetCumulativeFlow = Convert.ToDecimal(dtData.Rows[i]["ForValue"]) - Convert.ToDecimal(dtData.Rows[i]["RevValue"]);
  65. }
  66. else {
  67. continue;
  68. }
  69. }
  70. else {
  71. continue;
  72. }
  73. FlowEntity entity = new FlowEntity()
  74. {
  75. devId = devId,
  76. devName = devName,
  77. MeterAssessmentCode = devNo,
  78. GetDateTime = dtData.Rows[i]["CreateMinute"].ToString(),
  79. NetCumulativeFlow = NetCumulativeFlow.ToString(),//dtData.Rows[i]["ForValue"].ToString(),
  80. PositiveCumulativeFlow = dtData.Rows[i]["ForValue"].ToString(),
  81. NegativeCumulativeFlow = dtData.Rows[i]["RevValue"].ToString(),
  82. InstantaneousFlow = dtData.Rows[i]["RealValue"].ToString(),
  83. Pressure = dtData.Rows[i]["PressValue"].ToString(),
  84. BatteryVoltageValue = dtData.Rows[i]["CelVal"].ToString()
  85. };
  86. CompactAndSendData(entity);
  87. }
  88. }
  89. }
  90. catch (Exception ex)
  91. {
  92. log.Info("异常信息:" + ex.Message);
  93. }
  94. }
  95. #region 请求接口
  96. public string postSend(string url, string param)
  97. {
  98. string strResult = "";
  99. Encoding myEncode = Encoding.GetEncoding("UTF-8");
  100. byte[] postBytes = Encoding.UTF8.GetBytes(param);
  101. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
  102. req.Method = "POST";
  103. req.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
  104. req.ContentLength = postBytes.Length;
  105. try
  106. {
  107. using (Stream reqStream = req.GetRequestStream())
  108. {
  109. reqStream.Write(postBytes, 0, postBytes.Length);
  110. }
  111. using (WebResponse res = req.GetResponse())
  112. {
  113. using (StreamReader sr = new StreamReader(res.GetResponseStream(), myEncode))
  114. {
  115. strResult = sr.ReadToEnd();
  116. return strResult;
  117. }
  118. }
  119. }
  120. catch (WebException ex)
  121. {
  122. log.Info("Post数据出错:" + ex.Message);
  123. return "";
  124. }
  125. }
  126. #endregion
  127. #region 组装数据发送数据
  128. public void CompactAndSendData(FlowEntity entity)
  129. {
  130. StringBuilder dataBuilder = new StringBuilder();
  131. dataBuilder.Clear();
  132. dataBuilder.Append("{");
  133. dataBuilder.Append("\"ManufacturerCode\":\"").Append(manufacturerCode).Append("\",");
  134. dataBuilder.Append("\"MeterAssessmentCode\":\"").Append(entity.MeterAssessmentCode).Append("\",");
  135. dataBuilder.Append("\"GetDateTime\":\"").Append(Convert.ToDateTime(DataBaseHelper.DataFormat(entity.GetDateTime)).ToString("yyyy-MM-dd HH:mm:ss")).Append("\",");
  136. dataBuilder.Append("\"NetCumulativeFlow\":\"").Append(DataBaseHelper.DataFormat(entity.NetCumulativeFlow)).Append("\",");
  137. dataBuilder.Append("\"PositiveCumulativeFlow\":\"").Append(DataBaseHelper.DataFormat(entity.PositiveCumulativeFlow)).Append("\",");
  138. dataBuilder.Append("\"NegativeCumulativeFlow\":\"").Append(DataBaseHelper.DataFormat(entity.NegativeCumulativeFlow)).Append("\",");
  139. dataBuilder.Append("\"InstantaneousFlow\":\"").Append(DataBaseHelper.DataFormat(entity.InstantaneousFlow)).Append("\",");
  140. dataBuilder.Append("\"Pressure\":\"").Append(DataBaseHelper.DataFormat(entity.Pressure)).Append("\",");
  141. dataBuilder.Append("\"BatteryVoltageValue\":\"").Append(DataBaseHelper.DataFormat(entity.BatteryVoltageValue)).Append("\",");
  142. dataBuilder.Append("\"MeterFault\":\"").Append("0").Append("\",");
  143. dataBuilder.Append("\"BatteryVoltageState\":\"").Append("NULL").Append("\",");
  144. dataBuilder.Append("\"BoxDoorAlarm\":\"").Append("NULL").Append("\",");
  145. dataBuilder.Append("\"remark1\":\"").Append("NULL").Append("\",");
  146. dataBuilder.Append("\"remark2\":\"").Append("NULL").Append("\",");
  147. dataBuilder.Append("\"remark3\":\"").Append("NULL").Append("\",");
  148. dataBuilder.Append("\"remark4\":\"").Append("NULL").Append("\",");
  149. dataBuilder.Append("\"remark5\":\"").Append("NULL").Append("\"");
  150. dataBuilder.Append("}");
  151. //log.Info(dataBuilder.ToString());
  152. string updateReuslt = postSend(uploadUrl, dataBuilder.ToString());
  153. JObject jo = (JObject)JsonConvert.DeserializeObject(updateReuslt);
  154. string execCode = jo["execCode"].ToString();//执行返回代码
  155. string execMsg = jo["execMsg"].ToString();//返回返回描述
  156. log.Info(execMsg); //执行成功
  157. if (execCode == "0")
  158. {
  159. //更新 记录表
  160. //string UpdateHisSmartSql = "UPDATE 设备信息 SET 最后更新时间 = '" + entity.GetDateTime + "' WHERE ID = " + entity.devId;
  161. //OracleHelper.ExecuteNonQuery(UpdateHisSmartSql);
  162. }
  163. else
  164. {
  165. //插入异常表
  166. log.Debug("设备名称:" + entity.devName + ",插入异常:" + execMsg);
  167. }
  168. }
  169. #endregion
  170. public bool IsNumeric(string value)
  171. {
  172. return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
  173. }
  174. }
  175. }