export02.ashx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <%@ WebHandler Language="C#" Class="export02" %>
  2. using System;
  3. using System.Web;
  4. using System.Data;
  5. using Curtain.DataAccess;
  6. using DK.XuWei.WebMes;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. public class export02 : IHttpHandler
  10. {
  11. public void ProcessRequest(HttpContext context)
  12. {
  13. context.Response.ContentType = "text/plain";
  14. using (IDataAccess conn = DataAccess.Create())
  15. {
  16. DataTable dtDevice = conn.ExecuteDatatable(@"
  17. SELECT
  18. 'HGQ' || p.PLC_ID AS deviceCode,
  19. '烘干区' || P.PLC_NAME AS name
  20. FROM
  21. T_XT_PLC_V V
  22. INNER JOIN T_XT_PLC P ON P.PLC_ID = V.PLC_ID
  23. WHERE
  24. V.CREATETIME >= to_date( to_char( SYSDATE, 'yyyy-mm-dd hh' ), 'yyyy-mm-dd hh:mi:ss' )
  25. AND V.CREATETIME < to_date( to_char( SYSDATE + 1 / 24, 'yyyy-mm-dd hh' ), 'yyyy-mm-dd hh:mi:ss' )
  26. GROUP BY
  27. p.PLC_ID,
  28. P.PLC_NAME
  29. ORDER BY
  30. P.PLC_NAME"
  31. );
  32. DataTable dtDeviceData = conn.ExecuteDatatable(@"
  33. SELECT
  34. t.deviceCode,
  35. t.name,
  36. t.FNTEMPERATURE_房内温度,
  37. t.SDTEMPERATURE_设定温度,
  38. t.FNHUMIDITY_房内湿度,
  39. t.SDHUMIDITY_设定湿度,
  40. t.RSSTEMPERATURE_燃烧室温度,
  41. t.YRTEMPERATURE_余热温度
  42. FROM
  43. (
  44. SELECT
  45. 'HGQ' || p.PLC_ID AS deviceCode,
  46. '烘干区' || P.PLC_NAME AS name,
  47. nvl( V.V100, 0 ) AS FNTEMPERATURE_房内温度,
  48. nvl( V.V104, 0 ) AS SDTEMPERATURE_设定温度,
  49. nvl( V.V108, 0 ) AS FNHUMIDITY_房内湿度,
  50. nvl( V.V112, 0 ) AS SDHUMIDITY_设定湿度,
  51. nvl( V.V120, 0 ) AS RSSTEMPERATURE_燃烧室温度,
  52. nvl( V.V124, 0 ) AS YRTEMPERATURE_余热温度,
  53. rank ( ) over ( PARTITION BY P.PLC_ID ORDER BY V.VID DESC ) AS rk
  54. FROM
  55. T_XT_PLC_V V
  56. INNER JOIN T_XT_PLC P ON P.PLC_ID = V.PLC_ID
  57. WHERE
  58. V.CREATETIME >= to_date( to_char( SYSDATE, 'yyyy-mm-dd hh' ), 'yyyy-mm-dd hh:mi:ss' )
  59. AND V.CREATETIME < to_date( to_char( SYSDATE + 1 / 24, 'yyyy-mm-dd hh' ), 'yyyy-mm-dd hh:mi:ss' )
  60. ) t
  61. WHERE
  62. t.rk <= 1
  63. ORDER BY
  64. t.name"
  65. );
  66. if (dtDevice.Rows.Count == 0 || dtDeviceData.Rows.Count == 0)
  67. {
  68. context.Response.Write("当前时段无数据");
  69. return;
  70. }
  71. // 拼接主体
  72. JObject data = new JObject();
  73. data.Add(new JProperty("industryName", "广东区域"));
  74. data.Add(new JProperty("companyName", "佛山恒洁卫浴有限公司"));
  75. data.Add(new JProperty("productName", "烘干区"));
  76. data.Add(new JProperty("productCode", "FSHJHGQ"));
  77. data.Add(new JProperty("protocol", "HTTP"));
  78. data.Add(new JProperty("classfy", "陶瓷生产设备"));
  79. data.Add(new JProperty("lever", "低端"));
  80. data.Add(new JProperty("dimension", "工业品"));
  81. // 拼接device
  82. JArray arrDevice = new JArray();
  83. JObject device = null;
  84. foreach (DataRow row in dtDevice.Rows)
  85. {
  86. device = new JObject();
  87. device.Add(new JProperty("name", row["name"]));
  88. device.Add(new JProperty("deviceCode", "FSHJ" + row["deviceCode"]));
  89. device.Add(new JProperty("province", "广东省"));
  90. device.Add(new JProperty("city", "佛山市"));
  91. device.Add(new JProperty("address", "三水区乐平镇中油大道 3 号"));
  92. device.Add(new JProperty("worth", "0"));
  93. device.Add(new JProperty("latitude", "0"));
  94. device.Add(new JProperty("longitude", "0"));
  95. device.Add(new JProperty("manufacture", "2021-10-13"));
  96. arrDevice.Add(device);
  97. }
  98. data.Add("device", arrDevice);
  99. // 拼接deviceData
  100. JArray arrDeviceData = new JArray();
  101. JObject deviceData = null;
  102. string[] colNames;
  103. // 时间戳
  104. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  105. string timestamp = Convert.ToInt64(ts.TotalMilliseconds).ToString();
  106. foreach (DataRow row in dtDeviceData.Rows)
  107. {
  108. foreach (DataColumn col in dtDeviceData.Columns)
  109. {
  110. if (col.ColumnName.Equals("DEVICECODE") || col.ColumnName.Equals("NAME"))
  111. {
  112. continue;
  113. }
  114. colNames = col.ColumnName.Split('_');
  115. deviceData = new JObject();
  116. deviceData.Add(new JProperty("deviceCode", "FSHJ" + row["deviceCode"]));
  117. deviceData.Add(new JProperty("name", colNames[1]));
  118. deviceData.Add(new JProperty("mark", colNames[0]));
  119. deviceData.Add(new JProperty("value", row[col].ToString()));
  120. deviceData.Add(new JProperty("timestamp", timestamp));
  121. arrDeviceData.Add(deviceData);
  122. }
  123. }
  124. data.Add("deviceData", arrDeviceData);
  125. //上报数据
  126. //string url = "http://edge.cosmoplat.com:30152/company/device/access";// 测试
  127. string url = "http://edge.cosmoplat.com:30032/company/device/access";// 生产
  128. string message = JsonClient.Post(url, data.ToString());
  129. context.Response.Write(message);
  130. context.Response.Write(data.ToString());
  131. }
  132. }
  133. public bool IsReusable
  134. {
  135. get
  136. {
  137. return false;
  138. }
  139. }
  140. }