using Quartz; using System; using log4net; using RDIFramework.Utilities; using System.Collections.Generic; using RabbitMQ.Client; using System.Text; using Newtonsoft.Json; using System.Data; namespace TimedUpload.QuartzJobs { [DisallowConcurrentExecution] public class NoiseDataUploadJob : IJob { private readonly ILog log = LogManager.GetLogger(typeof(NoiseDataUploadJob)); private const String NOISECHANNELNAME = "noiseMonitor.deviceHis"; public void Execute(IJobExecutionContext context) { string[] uploadUrls = Constants.UploadUrl.Split('|'); Dictionary connections = new Dictionary(); Dictionary channels = new Dictionary(); Dictionary properties = new Dictionary(); foreach (string uploadUrl in uploadUrls) { ConnectionFactory factory = new ConnectionFactory(); factory.HostName = uploadUrl;//主机名,Rabbit会拿这个IP生成一个endpoint,这个很熟悉吧,就是socket绑定的那个终结点。 factory.UserName = Constants.UploadUserName;//默认用户名,用户可以在服务端自定义创建,有相关命令行 factory.Password = Constants.UploadPassword;//默认密码 factory.AutomaticRecoveryEnabled = true; // 链接断开会自动重连 IConnection connection = factory.CreateConnection(); IModel channel = connection.CreateModel(); channel.QueueDeclare(NOISECHANNELNAME, true, false, false, null);//创建一个名称为kibaqueue的消息队列 IBasicProperties property = channel.CreateBasicProperties(); property.ContentType = "text/plain"; property.DeliveryMode = 2; //持久化 connections.Add(uploadUrl, connection); properties.Add(uploadUrl, property); channels.Add(uploadUrl, channel); } if (channels.Count > 0) { SendNoiseDataUploadHistory(channels, properties); } foreach (KeyValuePair item in connections) { IConnection connection = item.Value; connection.Close(); } } /// /// 同步噪声设备的数据 /// /// /// private void SendNoiseDataUploadHistory(Dictionary channels, Dictionary properties) { try { NoiseModel model = new NoiseModel(); log.Info("同步噪声声设备数据...............start\r\n"); String sql = ""; DataTable dt = dbHelper.Fill(sql); if (dt != null) { // 数据 获取 以及 赋值 知道表结构了在去写查询和赋值 for (int i = 0; i < dt.Rows.Count; i++) { model.deviceCode = ""; model.readTime = DateTime.Now; model.videoUrl = ""; model.weather = ""; model.isLeakPoint = 0; model.csq = 0; model.psrp = 0; model.snr = 0; model.ecl = 0; model.pcl = 0; model.isBatteryUnusual = 0; model.isHitch = 0; model.singnalType = 1; string message = JsonConvert.SerializeObject(model); foreach (var item in channels) { string key = item.Key; IModel channel = item.Value; IBasicProperties property = properties[key]; channel.BasicPublish(NOISECHANNELNAME, "", property, Encoding.UTF8.GetBytes(message)); //生产消息 } } } log.Info("同步噪声声设备数据...............end\r\n"); } catch (Exception ex) { log.Error("步噪声声设备数据错误:" + ex.Message+ "====" + ex.StackTrace + "\r\n"); } } static IDbProvider dbHelper { get { var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, Constants.WaterFactoryDbConncetion); return DbDefine; } } } class NoiseModel { /// /// 设备编号 /// public string deviceCode { get; set; } /// /// 上报时间 /// public DateTime readTime { get; set; } /// /// 音频路径 /// public string videoUrl { get; set; } /// /// 天气 /// public string weather { get; set; } /// /// 是否漏点 0 不是 1 是 /// public int isLeakPoint { get; set; } /// /// csq 信号强度 /// CSQ指示RSSI强度,取值范围为0-31,数值越大信号越好。 /// CSQ值大于5,终端即可正常工作。若CSQ值小于5即不能正常工作。如果出现99表示信道无效。 /// CSQ=(RSSI<接收信号强度dBm>+113)/2 /// public int csq { get; set; } /// /// psrp 无线信号强度的关键参数 RSRP 前期写错了 不好改了 /// RSRP是代表无线信号强度的关键参数,反映当前信道的路径损耗强度,用于小区覆盖的测量和小区选择/重选。 /// 测量频率带宽上承载参考信号的资源元素(RE)上的接收功率(以瓦为单位)的线性平均值 /// RSRP的取值范围: /// -44~-140dBm,值越大越好。 /// Rx≤-105,覆盖强度等级6,表示覆盖较差。业务基本无法连接。 /// -105-65,覆盖强度等级1,表示覆盖非常好。 /// public int psrp { get; set; } /// /// snr 信噪比 /// 信号功率与噪声功率的比值,比值越大越好 /// public int snr { get; set; } /// /// ecl 覆盖等级 /// ECL共分三个等级,数值从0到2,分别对应可对抗144dB、154dB、164dB的信号衰减 /// 基站和UE之间会根据其所在的CEL来选择相对应的信息重发次数。 /// 0表示常规覆盖,MCL<144dB,与现有GPRS覆盖一致。 /// 1表示扩展覆盖,144dB public int ecl { get; set; } /// /// pcl /// public int pcl { get; set; } /// /// 电池是否异常 0 正常 1 异常 /// public int isBatteryUnusual { get; set; } /// /// 是否故障 0 正常 1 故障 /// public int isHitch { get; set; } /// /// 信号状态 1 在线 2 离线 /// public int singnalType { get; set; } } }