LoRaDataUploadJob.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using log4net;
  2. using Newtonsoft.Json;
  3. using Quartz;
  4. using RDIFramework.Utilities;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Globalization;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Net;
  13. using System.Security.Cryptography;
  14. using System.Text;
  15. using System.Web.Script.Serialization;
  16. namespace TimedUpload.QuartzJobs
  17. {
  18. [DisallowConcurrentExecution]
  19. public class LoRaDataUploadJob:IJob
  20. {
  21. private readonly ILog log = LogManager.GetLogger(typeof(IJob));
  22. private static DateTime timeStampStartTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  23. public void Execute(IJobExecutionContext context)
  24. {
  25. log.Info("lora抄表数据上传任务开始执行.................\r\n");
  26. #region 逻辑
  27. try
  28. {
  29. String str_CompanyID = Constants.ComPanyId;
  30. String loraUploadTime = Constants.loraUploadTime;
  31. String str_Url = Constants.newUrl;
  32. string now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
  33. //GetSecretKey(str_Url, Constants.passWord);
  34. //查询水表数据
  35. String str_Sql = "SELECT * FROM Bsc_MeterInfo where ReviseDT >= '" + loraUploadTime + "'";
  36. DataTable dt_MeterInfo = dbHelper.Fill(str_Sql);
  37. if (dt_MeterInfo.Rows.Count > 0)
  38. {
  39. List<loraDatalist> l_Data = new List<loraDatalist>();
  40. for (int i = 0; i < dt_MeterInfo.Rows.Count; i++)
  41. {
  42. loraDatalist dataList = new loraDatalist();
  43. //数据处理
  44. dataList.meterFactory = Constants.AppId;
  45. dataList.meterNo = dt_MeterInfo.Rows[i]["表地址"].ToString();
  46. dataList.endInt = Math.Round(Convert.ToDecimal(dt_MeterInfo.Rows[i]["表读数"]), 2);
  47. dataList.revInt = null;
  48. dataList.realInt = null;
  49. dataList.pressInt = null;
  50. dataList.elecInt = null;
  51. dataList.waterTime = Convert.ToDateTime(dt_MeterInfo.Rows[i]["ReviseDT"].ToString()).ToString("yyyy-MM-dd HH:mm");
  52. dataList.mon = Convert.ToInt32(Convert.ToDateTime(dt_MeterInfo.Rows[i]["ReviseDT"].ToString()).ToString("yyyyMM"));
  53. dataList.remark = "";
  54. dataList.meterType = 0;
  55. //dataList.UserNo = dt_MeterInfo.Rows[i]["UserNo"].ToString();
  56. l_Data.Add(dataList);
  57. }
  58. String timestamp = getTimeStamp10(DateTime.Now).ToString();
  59. // 原始数据转为json格式
  60. String obj = JsonHelper.ObjectToJSON(l_Data);
  61. String appSecret = Constants.appSecret;
  62. // 拼接字符串
  63. String sign = obj+ "&timestamp="+timestamp+ "&key="+appSecret;
  64. String md5Sign = md5Encript(sign);
  65. // 封装数据到实体类
  66. loraData data = new loraData();
  67. data.timestamp = timestamp;
  68. data.sign = md5Sign;
  69. data.body = l_Data;
  70. String parameter = JsonHelper.ObjectToJSON(data);
  71. str_Url = str_Url + "?sign=" + md5Sign + "&timestamp=" + timestamp;
  72. String str_Result = WebHelper.HttpWebRequest(str_Url, obj, "POST", "application/json");
  73. if (str_Result.Contains("成功"))
  74. {
  75. log.Info("lora上传成功:" + str_Result);
  76. }
  77. UpdateUploadTime(now);
  78. }
  79. //整理数据格式
  80. //调接口上传数据
  81. }
  82. catch (Exception ex)
  83. {
  84. log.Error("lora抄表数据上传任务异常:" + ex.Message);
  85. }
  86. #endregion
  87. log.InfoFormat("lora抄表数据上传任务执行结束.................\r\n");
  88. }
  89. /// <summary>
  90. /// 更新配置文件中的更新数据时间
  91. /// </summary>
  92. /// <param name="time"></param>
  93. private void UpdateUploadTime(string time)
  94. {
  95. var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  96. cfg.AppSettings.Settings["loraUploadTime"].Value = time;
  97. cfg.Save();
  98. ConfigurationManager.RefreshSection("appSettings");
  99. }
  100. public string md5Encript(String str)
  101. {
  102. MD5 md5 = new MD5CryptoServiceProvider();
  103. byte[] data = Encoding.UTF8.GetBytes(str);
  104. byte[] result = md5.ComputeHash(data);
  105. String md5Str = BitConverter.ToString(result).Replace("-", "");
  106. return md5Str;
  107. }
  108. /// <summary>
  109. /// 返回13位整数
  110. /// </summary>
  111. /// <param name="dateTime"></param>
  112. /// <returns></returns>
  113. public long getTimeStamp(DateTime dateTime)
  114. {
  115. return (long)(dateTime.ToUniversalTime() - timeStampStartTime).TotalMilliseconds;
  116. }
  117. public long getTimeStamp10(DateTime dateTime)
  118. {
  119. return (long)(dateTime.ToUniversalTime() - timeStampStartTime).TotalSeconds;
  120. }
  121. public static bool CheckTableExiste(string strTableName)
  122. {
  123. string strSql = "select * from sysobjects where name='" + strTableName + "'";
  124. if (CreateDataSet(strSql).Tables[0].Rows.Count == 0)
  125. {
  126. return false;
  127. }
  128. return true;
  129. }
  130. public static DataSet CreateDataSet(string strSql)
  131. {
  132. DataSet dataSet = new DataSet();
  133. try
  134. {
  135. dataSet = new DataSet();
  136. dbHelper.Fill(dataSet, strSql, "checkTable");
  137. }
  138. catch (Exception exception)
  139. {
  140. //log.Error("checkTable:" + exception.Message);
  141. }
  142. return dataSet;
  143. }
  144. static IDbProvider dbHelper
  145. {
  146. get
  147. {
  148. var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, Constants.SongZiLoraDB);
  149. return DbDefine;
  150. }
  151. }
  152. }
  153. public class loraResult
  154. {
  155. public String secretKey { get; set; }
  156. public String message { get; set; }
  157. public String status { get; set; }
  158. }
  159. public class loraData
  160. {
  161. public String timestamp { get; set; }
  162. public String sign { get; set; }
  163. public List<loraDatalist> body { get; set; }
  164. }
  165. public class loraDatalist
  166. {
  167. //水表厂商编号 AppId
  168. public String meterFactory { get; set; }
  169. //水表编号
  170. public String meterNo { get; set; }
  171. //正累计读数
  172. public Decimal? endInt { get; set; }
  173. //负累计读数
  174. public Decimal? revInt { get; set; }
  175. //瞬时流量读数
  176. public Decimal? realInt { get; set; }
  177. //压力
  178. public String pressInt { get; set; }
  179. //电池电量
  180. public Decimal? elecInt { get; set; }
  181. //采集时间
  182. public String waterTime { get; set; }
  183. //年月 例:202407
  184. public int mon { get; set; }
  185. //备注
  186. public String remark { get; set; }
  187. //0小表 1大表
  188. public int meterType { get; set; }
  189. ////用户号
  190. //public String UserNo { get; set; }
  191. }
  192. }