BacDevice.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using NPOI.HSSF.UserModel;
  2. using NPOI.SS.UserModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.IO;
  7. using System.IO.BACnet;
  8. using System.Net;
  9. using System.Text.RegularExpressions;
  10. namespace NetCore31BACNetTransfor
  11. {
  12. public class BacDevice
  13. {
  14. public BacnetAddress Address { get; set; }
  15. public uint DeviceId { get; set; }
  16. public ObservableCollection<BacProperty> Properties { get; set; }
  17. public int ScanIndex { get; set; } = 0; //扫点序号
  18. public BacDevice(BacnetAddress adr, uint device_id)
  19. {
  20. Address = adr;
  21. DeviceId = device_id;
  22. }
  23. public void LoadProperties(uint dID, Dictionary<string, Point> c)
  24. {
  25. try
  26. {
  27. if (Properties == null) Properties = new ObservableCollection<BacProperty>();
  28. foreach (var p in c)
  29. {
  30. var pro = BacProperty.getPro(p.Key, p.Value);
  31. if (pro == null) continue;
  32. pro.Device = this;
  33. if (pro.Device.DeviceId == dID)
  34. {
  35. Properties.Add(pro);
  36. }
  37. }
  38. }
  39. catch (Exception exp)
  40. {
  41. Logger.Error("BacDevice.LoadProperties: ", exp);
  42. }
  43. }
  44. public void LoadPropertiesFromExcel(string file)
  45. {
  46. try
  47. {
  48. if (!File.Exists(file)) return;
  49. if (Properties == null) Properties = new ObservableCollection<BacProperty>();
  50. using (FileStream fp = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  51. {
  52. var book = new HSSFWorkbook(fp);
  53. for (int sheetNum = 0; sheetNum < book.NumberOfSheets; sheetNum++)
  54. {
  55. var sheet = book.GetSheetAt(sheetNum);
  56. if (sheet.FirstRowNum < 0 || sheet.FirstRowNum >= sheet.LastRowNum) continue;
  57. for (var rowNum = sheet.FirstRowNum + 1; rowNum <= sheet.LastRowNum; rowNum++)
  58. {
  59. var rowContent = sheet.GetRow(rowNum);
  60. if (rowContent == null) continue;
  61. var pro = BacProperty.FromExcelRow(rowContent);
  62. if (pro == null) continue;
  63. pro.Device = this;
  64. Properties.Add(pro);
  65. }
  66. }
  67. }
  68. }
  69. catch (Exception exp)
  70. {
  71. Logger.Error("BacDevice.LoadPropertiesFromExcel: ", exp);
  72. }
  73. }
  74. }
  75. public class BacProperty
  76. {
  77. public BacDevice Device { get; set; }
  78. public BacnetObjectId ObjectId { get; set; }
  79. public string PROP_DESCRIPTION { get; set; }
  80. public string PROP_OBJECT_NAME { get; set; }
  81. public string PROP_PRESENT_VALUE { get; set; }
  82. public string building { get; set; }
  83. public string meter { get; set; }
  84. public string funcid { get; set; }
  85. public static BacProperty FromExcelRow(IRow row)
  86. {
  87. if (row == null) return null;
  88. try
  89. {
  90. BacProperty rst = new BacProperty();
  91. rst.PROP_OBJECT_NAME = row.GetCell(1)?.ToString();
  92. rst.ObjectId = new BacnetObjectId(
  93. (BacnetObjectTypes)Enum.Parse(typeof(BacnetObjectTypes), row.GetCell(2)?.ToString()),
  94. uint.Parse(row.GetCell(3)?.ToString()));
  95. rst.PROP_DESCRIPTION = row.GetCell(5)?.ToString();
  96. rst.PROP_PRESENT_VALUE = row.GetCell(6)?.ToString();
  97. return rst;
  98. }
  99. catch (Exception exp)
  100. {
  101. Logger.Error("BacProperty.FromExcelRow", exp);
  102. return null;
  103. }
  104. }
  105. public static BacProperty getPro(string key, Point point)
  106. {
  107. //Key = "192.168.20.159:2068-1968-analogInput-0"
  108. try
  109. {
  110. BacProperty rst = new BacProperty();
  111. //rst.PROP_OBJECT_NAME = row.GetCell(1).ToString();
  112. rst.ObjectId = new BacnetObjectId(getObjectType(Regex.Split(key, "-", RegexOptions.IgnoreCase)[2]),
  113. uint.Parse(Regex.Split(key, "-", RegexOptions.IgnoreCase)[3]));
  114. // rst.PROP_DESCRIPTION = row.GetCell(5).ToString();
  115. //rst.PROP_PRESENT_VALUE = row.GetCell(6).ToString();
  116. rst.building = point.buildingSign;
  117. rst.meter = point.meterSign;
  118. foreach (var funcid in point.functionList)
  119. {
  120. rst.funcid = Convert.ToString(funcid.functionID);
  121. break;
  122. }
  123. return rst;
  124. }
  125. catch (Exception exp)
  126. {
  127. Logger.Error("BacProperty.getPro", exp);
  128. return null;
  129. }
  130. }
  131. private static BacnetObjectTypes getObjectType(string type)
  132. {
  133. // TODO Auto-generated method stub
  134. if ("analogInput".Equals(type))
  135. {
  136. return BacnetObjectTypes.OBJECT_ANALOG_INPUT;
  137. }
  138. else if ("analogOutput".Equals(type))
  139. {
  140. return BacnetObjectTypes.OBJECT_ANALOG_OUTPUT;
  141. }
  142. else if ("analogValue".Equals(type))
  143. {
  144. return BacnetObjectTypes.OBJECT_ANALOG_VALUE;
  145. }
  146. else if ("binaryInput".Equals(type))
  147. {
  148. return BacnetObjectTypes.OBJECT_BINARY_INPUT;
  149. }
  150. else if ("binaryOutput".Equals(type))
  151. {
  152. return BacnetObjectTypes.OBJECT_BINARY_OUTPUT;
  153. }
  154. else if ("binaryValue".Equals(type))
  155. {
  156. return BacnetObjectTypes.OBJECT_BINARY_VALUE;
  157. }
  158. else if ("multiStateInput".Equals(type))
  159. {
  160. return BacnetObjectTypes.OBJECT_MULTI_STATE_INPUT;
  161. }
  162. else if ("multiStateOutput".Equals(type))
  163. {
  164. return BacnetObjectTypes.OBJECT_MULTI_STATE_OUTPUT;
  165. }
  166. else if ("multiStateValue".Equals(type))
  167. {
  168. return BacnetObjectTypes.OBJECT_MULTI_STATE_VALUE;
  169. }
  170. return BacnetObjectTypes.OBJECT_ANALOG_INPUT;
  171. }
  172. }
  173. }