using log4net; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Quartz; using RDIFramework.Utilities; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Net; using System.Text; using TimedUpload.utils; namespace TimedUpload.QuartzJobs { /// /// 大安桑瑞大表监控数据对接 /// [DisallowConcurrentExecution] public class DADeviceDataJob : IJob { private readonly ILog log = LogManager.GetLogger(typeof(DADeviceDataJob)); private string manufacturerCode = SystemConfig.pipeLineManufacturerCode; private string uploadUrl = SystemConfig.pipeLineNewUploadUrl; static IDbProvider dbHelper { get { var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, SystemConfig.pipeLineDbConn); return DbDefine; } } public void Execute(IJobExecutionContext context) { //1.查询设备 //2.上传数据 try { string sql = "", devId, devNo, devName, lastUpdateTime; sql = "Select MeterAssessmentId,MeterAssessmentName,GetDateTime,MeterAssessmentCode From MeterAssessmentBase"; DataTable dt = dbHelper.Fill(sql); for (int row = 0; row < dt.Rows.Count; row++) { devId = dt.Rows[row]["MeterAssessmentId"].ToString();//ID devNo = dt.Rows[row]["MeterAssessmentCode"].ToString();//考核表编号 lastUpdateTime = dt.Rows[row]["GetDateTime"].ToString(); devName = dt.Rows[row]["MeterAssessmentName"].ToString(); sql = "Select * From ARM_HistoryData Where Address='" + devNo + "' order by CreateMinute asc"; DataTable dtData = OracleHelper.ExecuteDataTable(sql); int rowIndex = dtData.Rows.Count; for(int i = 0; i < rowIndex; i++) { FlowEntity entity = new FlowEntity() { devId = devId, devName = devName, MeterAssessmentCode = devNo, GetDateTime = dtData.Rows[i]["CreateMinute"].ToString(), NetCumulativeFlow = dtData.Rows[i]["ForValue"].ToString(), PositiveCumulativeFlow = dtData.Rows[i]["ForValue"].ToString(), NegativeCumulativeFlow = dtData.Rows[i]["RevValue"].ToString(), InstantaneousFlow = dtData.Rows[i]["RealValue"].ToString(), Pressure = dtData.Rows[i]["PressValue"].ToString(), BatteryVoltageValue = dtData.Rows[i]["CelVal"].ToString() }; CompactAndSendData(entity); } } } catch (Exception ex) { log.Error("异常信息:" + ex.Message); } } #region 请求接口 public string postSend(string url, string param) { string strResult = ""; Encoding myEncode = Encoding.GetEncoding("UTF-8"); byte[] postBytes = Encoding.UTF8.GetBytes(param); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; req.ContentLength = postBytes.Length; try { using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(postBytes, 0, postBytes.Length); } using (WebResponse res = req.GetResponse()) { using (StreamReader sr = new StreamReader(res.GetResponseStream(), myEncode)) { strResult = sr.ReadToEnd(); return strResult; } } } catch (WebException ex) { log.Error("Post数据出错:" + ex.Message); return ""; } } #endregion #region 组装数据发送数据 public void CompactAndSendData(FlowEntity entity) { StringBuilder dataBuilder = new StringBuilder(); dataBuilder.Clear(); dataBuilder.Append("{"); dataBuilder.Append("\"ManufacturerCode\":\"").Append(manufacturerCode).Append("\","); dataBuilder.Append("\"MeterAssessmentCode\":\"").Append(entity.MeterAssessmentCode).Append("\","); dataBuilder.Append("\"GetDateTime\":\"").Append(Convert.ToDateTime(DataBaseHelper.DataFormat(entity.GetDateTime)).ToString("yyyy-MM-dd HH:mm:ss")).Append("\","); dataBuilder.Append("\"NetCumulativeFlow\":\"").Append(DataBaseHelper.DataFormat(entity.NetCumulativeFlow)).Append("\","); dataBuilder.Append("\"PositiveCumulativeFlow\":\"").Append(DataBaseHelper.DataFormat(entity.PositiveCumulativeFlow)).Append("\","); dataBuilder.Append("\"NegativeCumulativeFlow\":\"").Append(DataBaseHelper.DataFormat(entity.NegativeCumulativeFlow)).Append("\","); dataBuilder.Append("\"InstantaneousFlow\":\"").Append(DataBaseHelper.DataFormat(entity.InstantaneousFlow)).Append("\","); dataBuilder.Append("\"Pressure\":\"").Append(DataBaseHelper.DataFormat(entity.Pressure)).Append("\","); dataBuilder.Append("\"BatteryVoltageValue\":\"").Append(DataBaseHelper.DataFormat(entity.BatteryVoltageValue)).Append("\","); dataBuilder.Append("\"MeterFault\":\"").Append("0").Append("\","); dataBuilder.Append("\"BoxDoorAlarm\":\"").Append("NULL").Append("\","); dataBuilder.Append("\"remark1\":\"").Append("NULL").Append("\","); dataBuilder.Append("\"remark2\":\"").Append("NULL").Append("\","); dataBuilder.Append("\"remark3\":\"").Append("NULL").Append("\","); dataBuilder.Append("\"remark4\":\"").Append("NULL").Append("\","); dataBuilder.Append("\"remark5\":\"").Append("NULL").Append("\""); dataBuilder.Append("}"); string updateReuslt = postSend(uploadUrl, dataBuilder.ToString()); JObject jo = (JObject)JsonConvert.DeserializeObject(updateReuslt); string execCode = jo["execCode"].ToString();//执行返回代码 string execMsg = jo["execMsg"].ToString();//返回返回描述 //执行成功 if (execCode == "0") { //更新 记录表 //string UpdateHisSmartSql = "UPDATE 设备信息 SET 最后更新时间 = '" + entity.GetDateTime + "' WHERE ID = " + entity.devId; //OracleHelper.ExecuteNonQuery(UpdateHisSmartSql); } else { //插入异常表 log.Debug("设备名称:" + entity.devName + ",插入异常:" + execMsg); } } #endregion } }