using log4net; using Newtonsoft.Json; using Quartz; using RDIFramework.Utilities; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Globalization; using System.IO; using System.Linq; using System.Net; using System.Security.Cryptography; using System.Text; using System.Web.Script.Serialization; namespace TimedUpload.QuartzJobs { [DisallowConcurrentExecution] public class LoRaDataUploadJob:IJob { private readonly ILog log = LogManager.GetLogger(typeof(IJob)); private static DateTime timeStampStartTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); public void Execute(IJobExecutionContext context) { log.Info("lora抄表数据上传任务开始执行.................\r\n"); #region 逻辑 try { String str_CompanyID = Constants.ComPanyId; String loraUploadTime = Constants.loraUploadTime; String str_Url = Constants.newUrl; string now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo); //GetSecretKey(str_Url, Constants.passWord); //查询水表数据 String str_Sql = "SELECT * FROM Bsc_MeterInfo where ReviseDT >= '" + loraUploadTime + "'"; DataTable dt_MeterInfo = dbHelper.Fill(str_Sql); if (dt_MeterInfo.Rows.Count > 0) { List l_Data = new List(); for (int i = 0; i < dt_MeterInfo.Rows.Count; i++) { loraDatalist dataList = new loraDatalist(); //数据处理 dataList.meterFactory = Constants.AppId; dataList.meterNo = dt_MeterInfo.Rows[i]["表地址"].ToString(); dataList.endInt = Math.Round(Convert.ToDecimal(dt_MeterInfo.Rows[i]["表读数"]), 2); dataList.revInt = null; dataList.realInt = null; dataList.pressInt = null; dataList.elecInt = null; dataList.waterTime = Convert.ToDateTime(dt_MeterInfo.Rows[i]["ReviseDT"].ToString()).ToString("yyyy-MM-dd HH:mm"); dataList.mon = Convert.ToInt32(Convert.ToDateTime(dt_MeterInfo.Rows[i]["ReviseDT"].ToString()).ToString("yyyyMM")); dataList.remark = ""; dataList.meterType = 0; //dataList.UserNo = dt_MeterInfo.Rows[i]["UserNo"].ToString(); l_Data.Add(dataList); } String timestamp = getTimeStamp10(DateTime.Now).ToString(); // 原始数据转为json格式 String obj = JsonHelper.ObjectToJSON(l_Data); String appSecret = Constants.appSecret; // 拼接字符串 String sign = obj+ "×tamp="+timestamp+ "&key="+appSecret; String md5Sign = md5Encript(sign); // 封装数据到实体类 loraData data = new loraData(); data.timestamp = timestamp; data.sign = md5Sign; data.body = l_Data; String parameter = JsonHelper.ObjectToJSON(data); str_Url = str_Url + "?sign=" + md5Sign + "×tamp=" + timestamp; String str_Result = WebHelper.HttpWebRequest(str_Url, obj, "POST", "application/json"); if (str_Result.Contains("成功")) { log.Info("lora上传成功:" + str_Result); } UpdateUploadTime(now); } //整理数据格式 //调接口上传数据 } catch (Exception ex) { log.Error("lora抄表数据上传任务异常:" + ex.Message); } #endregion log.InfoFormat("lora抄表数据上传任务执行结束.................\r\n"); } /// /// 更新配置文件中的更新数据时间 /// /// private void UpdateUploadTime(string time) { var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); cfg.AppSettings.Settings["loraUploadTime"].Value = time; cfg.Save(); ConfigurationManager.RefreshSection("appSettings"); } public string md5Encript(String str) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] data = Encoding.UTF8.GetBytes(str); byte[] result = md5.ComputeHash(data); String md5Str = BitConverter.ToString(result).Replace("-", ""); return md5Str; } /// /// 返回13位整数 /// /// /// public long getTimeStamp(DateTime dateTime) { return (long)(dateTime.ToUniversalTime() - timeStampStartTime).TotalMilliseconds; } public long getTimeStamp10(DateTime dateTime) { return (long)(dateTime.ToUniversalTime() - timeStampStartTime).TotalSeconds; } public static bool CheckTableExiste(string strTableName) { string strSql = "select * from sysobjects where name='" + strTableName + "'"; if (CreateDataSet(strSql).Tables[0].Rows.Count == 0) { return false; } return true; } public static DataSet CreateDataSet(string strSql) { DataSet dataSet = new DataSet(); try { dataSet = new DataSet(); dbHelper.Fill(dataSet, strSql, "checkTable"); } catch (Exception exception) { //log.Error("checkTable:" + exception.Message); } return dataSet; } static IDbProvider dbHelper { get { var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, Constants.SongZiLoraDB); return DbDefine; } } } public class loraResult { public String secretKey { get; set; } public String message { get; set; } public String status { get; set; } } public class loraData { public String timestamp { get; set; } public String sign { get; set; } public List body { get; set; } } public class loraDatalist { //水表厂商编号 AppId public String meterFactory { get; set; } //水表编号 public String meterNo { get; set; } //正累计读数 public Decimal? endInt { get; set; } //负累计读数 public Decimal? revInt { get; set; } //瞬时流量读数 public Decimal? realInt { get; set; } //压力 public String pressInt { get; set; } //电池电量 public Decimal? elecInt { get; set; } //采集时间 public String waterTime { get; set; } //年月 例:202407 public int mon { get; set; } //备注 public String remark { get; set; } //0小表 1大表 public int meterType { get; set; } ////用户号 //public String UserNo { get; set; } } }