DADeviceDataJob.cs 7.2 KB

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