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 NewDataUploadJob: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("NB表抄表数据上传任务开始执行.................\r\n"); #region 逻辑 try { String str_CompanyID = Constants.ComPanyId; String uploadTime = Constants.uploadTime; 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 ElecAddress AS MeterNo,ISNULL( NowReading, 0 ) AS EndInt,ISNULL( Total_down, 0 ) AS RevInt,ISNULL( Instance, 0 ) AS RealInt,ISNULL( BatteryVoltage , 0) as ElecInt,NowRadingDT,UserNo from V_WaterMeters where UserNo is not null and CompanyID in ('" + str_CompanyID + "') and DeleteMark =0 and NowRadingDT >= '"+ uploadTime+"'"; 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++) { newDatalist dataList = new newDatalist(); //数据处理 dataList.meterFactory = Constants.AppId; dataList.meterNo = dt_MeterInfo.Rows[i]["MeterNo"].ToString(); dataList.endInt = Math.Round(Convert.ToDecimal(dt_MeterInfo.Rows[i]["EndInt"]),2); dataList.revInt = Math.Round(Convert.ToDecimal(dt_MeterInfo.Rows[i]["RevInt"]), 2); dataList.realInt = Math.Round(Convert.ToDecimal(dt_MeterInfo.Rows[i]["RealInt"]), 2); dataList.pressInt = null; dataList.elecInt = Math.Round(Convert.ToDecimal(dt_MeterInfo.Rows[i]["ElecInt"]), 2); dataList.waterTime = Convert.ToDateTime(dt_MeterInfo.Rows[i]["NowRadingDT"].ToString()).ToString("yyyy-MM-dd HH:mm"); dataList.mon = Convert.ToInt32(Convert.ToDateTime(dt_MeterInfo.Rows[i]["NowRadingDT"].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); // 封装数据到实体类 newData data = new newData(); 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("NB表上传成功:" + str_Url); } UpdateUploadTime(now); } //整理数据格式 //调接口上传数据 } catch (Exception ex) { log.Error("NB表抄表数据上传任务异常:" + ex.Message); } #endregion log.InfoFormat("NB表抄表数据上传任务执行结束.................\r\n"); } /// /// 更新配置文件中的更新数据时间 /// /// private void UpdateUploadTime(string time) { var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); cfg.AppSettings.Settings["uploadTime"].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.DBUrl); return DbDefine; } } } public class newResult { public String secretKey { get; set; } public String message { get; set; } public String status { get; set; } } public class newData { public String timestamp { get; set; } public String sign { get; set; } public List body { get; set; } } public class newDatalist { //水表厂商编号 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; } } }