NoiseDataUploadJob.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using Quartz;
  2. using System;
  3. using log4net;
  4. using RDIFramework.Utilities;
  5. using System.Collections.Generic;
  6. using RabbitMQ.Client;
  7. using System.Text;
  8. using Newtonsoft.Json;
  9. using System.Data;
  10. namespace TimedUpload.QuartzJobs
  11. {
  12. [DisallowConcurrentExecution]
  13. public class NoiseDataUploadJob : IJob
  14. {
  15. private readonly ILog log = LogManager.GetLogger(typeof(NoiseDataUploadJob));
  16. private const String NOISECHANNELNAME = "noiseMonitor.deviceHis";
  17. public void Execute(IJobExecutionContext context)
  18. {
  19. string[] uploadUrls = Constants.UploadUrl.Split('|');
  20. Dictionary<string, IConnection> connections = new Dictionary<string, IConnection>();
  21. Dictionary<string, IModel> channels = new Dictionary<string, IModel>();
  22. Dictionary<string, IBasicProperties> properties = new Dictionary<string, IBasicProperties>();
  23. foreach (string uploadUrl in uploadUrls)
  24. {
  25. ConnectionFactory factory = new ConnectionFactory();
  26. factory.HostName = uploadUrl;//主机名,Rabbit会拿这个IP生成一个endpoint,这个很熟悉吧,就是socket绑定的那个终结点。
  27. factory.UserName = Constants.UploadUserName;//默认用户名,用户可以在服务端自定义创建,有相关命令行
  28. factory.Password = Constants.UploadPassword;//默认密码
  29. factory.AutomaticRecoveryEnabled = true; // 链接断开会自动重连
  30. IConnection connection = factory.CreateConnection();
  31. IModel channel = connection.CreateModel();
  32. channel.QueueDeclare(NOISECHANNELNAME, true, false, false, null);//创建一个名称为kibaqueue的消息队列
  33. IBasicProperties property = channel.CreateBasicProperties();
  34. property.ContentType = "text/plain";
  35. property.DeliveryMode = 2; //持久化
  36. connections.Add(uploadUrl, connection);
  37. properties.Add(uploadUrl, property);
  38. channels.Add(uploadUrl, channel);
  39. }
  40. if (channels.Count > 0)
  41. {
  42. SendNoiseDataUploadHistory(channels, properties);
  43. }
  44. foreach (KeyValuePair<string, IConnection> item in connections)
  45. {
  46. IConnection connection = item.Value;
  47. connection.Close();
  48. }
  49. }
  50. /// <summary>
  51. /// 同步噪声设备的数据
  52. /// </summary>
  53. /// <param name="channels"></param>
  54. /// <param name="properties"></param>
  55. private void SendNoiseDataUploadHistory(Dictionary<string, IModel> channels, Dictionary<string, IBasicProperties> properties) {
  56. try
  57. {
  58. NoiseModel model = new NoiseModel();
  59. log.Info("同步噪声声设备数据...............start\r\n");
  60. String sql = "";
  61. DataTable dt = dbHelper.Fill(sql);
  62. if (dt != null)
  63. {
  64. // 数据 获取 以及 赋值 知道表结构了在去写查询和赋值
  65. for (int i = 0; i < dt.Rows.Count; i++)
  66. {
  67. model.deviceCode = "";
  68. model.readTime = DateTime.Now;
  69. model.videoUrl = "";
  70. model.weather = "";
  71. model.isLeakPoint = 0;
  72. model.csq = 0;
  73. model.psrp = 0;
  74. model.snr = 0;
  75. model.ecl = 0;
  76. model.pcl = 0;
  77. model.isBatteryUnusual = 0;
  78. model.isHitch = 0;
  79. model.singnalType = 1;
  80. string message = JsonConvert.SerializeObject(model);
  81. foreach (var item in channels)
  82. {
  83. string key = item.Key;
  84. IModel channel = item.Value;
  85. IBasicProperties property = properties[key];
  86. channel.BasicPublish(NOISECHANNELNAME, "", property, Encoding.UTF8.GetBytes(message)); //生产消息
  87. }
  88. }
  89. }
  90. log.Info("同步噪声声设备数据...............end\r\n");
  91. }
  92. catch (Exception ex)
  93. {
  94. log.Error("步噪声声设备数据错误:" + ex.Message+ "====" + ex.StackTrace + "\r\n");
  95. }
  96. }
  97. static IDbProvider dbHelper
  98. {
  99. get
  100. {
  101. var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, Constants.WaterFactoryDbConncetion);
  102. return DbDefine;
  103. }
  104. }
  105. }
  106. class NoiseModel
  107. {
  108. /// <summary>
  109. /// 设备编号
  110. /// </summary>
  111. public string deviceCode { get; set; }
  112. /// <summary>
  113. /// 上报时间
  114. /// </summary>
  115. public DateTime readTime { get; set; }
  116. /// <summary>
  117. /// 音频路径
  118. /// </summary>
  119. public string videoUrl { get; set; }
  120. /// <summary>
  121. /// 天气
  122. /// </summary>
  123. public string weather { get; set; }
  124. /// <summary>
  125. /// 是否漏点 0 不是 1 是
  126. /// </summary>
  127. public int isLeakPoint { get; set; }
  128. /// <summary>
  129. /// csq 信号强度
  130. /// CSQ指示RSSI强度,取值范围为0-31,数值越大信号越好。
  131. /// CSQ值大于5,终端即可正常工作。若CSQ值小于5即不能正常工作。如果出现99表示信道无效。
  132. /// CSQ=(RSSI<接收信号强度dBm>+113)/2
  133. /// </summary>
  134. public int csq { get; set; }
  135. /// <summary>
  136. /// psrp 无线信号强度的关键参数 RSRP 前期写错了 不好改了
  137. /// RSRP是代表无线信号强度的关键参数,反映当前信道的路径损耗强度,用于小区覆盖的测量和小区选择/重选。
  138. /// 测量频率带宽上承载参考信号的资源元素(RE)上的接收功率(以瓦为单位)的线性平均值
  139. /// RSRP的取值范围:
  140. /// -44~-140dBm,值越大越好。
  141. /// Rx≤-105,覆盖强度等级6,表示覆盖较差。业务基本无法连接。
  142. /// -105<Rx≤-95,覆盖强度等级5,表示覆盖差。室外业务能够连接,但连接成功率低,室内业务基本无法连接。
  143. /// -95<Rx≤-85,覆盖强度等级4,表示覆盖一般,室外能够连接,室内连接成功率低。
  144. /// -85<Rx≤-75,覆盖强度等级3,表示覆盖较好,室内外都能够连接。
  145. /// -75<Rx≤-65,覆盖强度等级2,表示覆盖好,室内外都能够很好的连接。
  146. /// Rx>-65,覆盖强度等级1,表示覆盖非常好。
  147. /// </summary>
  148. public int psrp { get; set; }
  149. /// <summary>
  150. /// snr 信噪比
  151. /// 信号功率与噪声功率的比值,比值越大越好
  152. /// </summary>
  153. public int snr { get; set; }
  154. /// <summary>
  155. /// ecl 覆盖等级
  156. /// ECL共分三个等级,数值从0到2,分别对应可对抗144dB、154dB、164dB的信号衰减
  157. /// 基站和UE之间会根据其所在的CEL来选择相对应的信息重发次数。
  158. /// 0表示常规覆盖,MCL<144dB,与现有GPRS覆盖一致。
  159. /// 1表示扩展覆盖,144dB<MCL<154dB,在现有GPRS覆盖的基础上提升了10dB
  160. /// 2表示极端覆盖,154dB<MCL<164dB,在现有GPRS覆盖的基础上提升了20dB。
  161. /// </summary>
  162. public int ecl { get; set; }
  163. /// <summary>
  164. /// pcl
  165. /// </summary>
  166. public int pcl { get; set; }
  167. /// <summary>
  168. /// 电池是否异常 0 正常 1 异常
  169. /// </summary>
  170. public int isBatteryUnusual { get; set; }
  171. /// <summary>
  172. /// 是否故障 0 正常 1 故障
  173. /// </summary>
  174. public int isHitch { get; set; }
  175. /// <summary>
  176. /// 信号状态 1 在线 2 离线
  177. /// </summary>
  178. public int singnalType { get; set; }
  179. }
  180. }