MessageTest.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using HengShan_Pump_TQC_IFSF.MessageEntity.Incoming;
  2. using Microsoft.CodeAnalysis.CSharp.Syntax;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Edge.Core.Parser.BinaryParser.Util;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using HengShan_Pump_TQC_IFSF.MessageEntity;
  9. using System.Text;
  10. using Edge.Core.Parser.BinaryParser.MessageEntity;
  11. using HengShan_Pump_TQC_IFSF;
  12. namespace HengShan_Pump_TQC_IFSF_Test
  13. {
  14. [TestClass]
  15. public class MessageTest
  16. {
  17. public static bool ValueEquals(IEnumerable<byte> array1, IEnumerable<byte> array2)
  18. {
  19. if (array1 == null && array2 == null)
  20. {
  21. return true;
  22. }
  23. if ((array1 == null) || (array2 == null))
  24. {
  25. return false;
  26. }
  27. if (array1.Count() != array2.Count())
  28. {
  29. return false;
  30. }
  31. if (array1.Equals(array2))
  32. {
  33. return true;
  34. }
  35. else
  36. {
  37. for (int Index = 0; Index < array1.Count(); Index++)
  38. {
  39. if (!Equals(array1.ElementAt(Index), array2.ElementAt(Index)))
  40. {
  41. return false;
  42. }
  43. }
  44. }
  45. return true;
  46. }
  47. [TestMethod]
  48. public void TestMethod1()
  49. {
  50. List<MessageTemplateBase> continueParseStack = new List<MessageTemplateBase>();
  51. HengShan_Pump_TQC_IFSF.Parser p = new HengShan_Pump_TQC_IFSF.Parser();
  52. p.Deserialized += (s, a) =>
  53. {
  54. if (a.Extra != null && a.Extra.Length > 0)
  55. {
  56. continueParseStack.Add(a.To);
  57. p.Deserialize(a.Extra);
  58. }
  59. else
  60. {
  61. continueParseStack.Add(a.To);
  62. }
  63. };
  64. var m = p.Deserialize(("02 01 01 01 00 22 " +
  65. //M_Lg 89
  66. "00 59 " +
  67. //database id 01: CALCULATOR DATABASE
  68. "01 01 " +
  69. "02 01 03 04 01 06 05 01 04 08 01 01 09 01 00 0B 01 01 0C 01 00 28 01 57 " +
  70. " 29 01 57 2A 01 35 2B 01 00 32 03 54 4F 4B 33 03 4D 50 44 34 03 54 51 43 " +
  71. " 35 0C 31 32 33 34 35 36 37 38 39 30 31 32 " +
  72. " 36 0C 30 30 30 30 30 30 30 30 31 2E 38 31 39 01 00 3A 06 00 00 00 00 02 32").ToBytes());
  73. Assert.AreEqual(true, continueParseStack.Count == 1);
  74. var dbParser = DatabaseDataParser.New().Convert(continueParseStack.Cast<IfsfMessage>().First().RawDatabaseData.ToArray());
  75. }
  76. [TestMethod]
  77. public void TestMethod2()
  78. {
  79. List<MessageTemplateBase> continueParseStack = new List<MessageTemplateBase>();
  80. HengShan_Pump_TQC_IFSF.Parser p = new HengShan_Pump_TQC_IFSF.Parser();
  81. p.Deserialized += (s, a) =>
  82. {
  83. if (a.Extra != null && a.Extra.Length > 0)
  84. {
  85. continueParseStack.Add(a.To);
  86. p.Deserialize(a.Extra);
  87. }
  88. else
  89. {
  90. continueParseStack.Add(a.To);
  91. }
  92. };
  93. var m = p.Deserialize(("01 01 02 01 02 00 00 03 01 00 01" +
  94. "" +
  95. "02 01 01 01 00 20 00 0A 01 00 01 06 00 00 00 00 01 92" +
  96. "" +
  97. "01 01 02 01 02 40 00 05 01 00 05 01 31" +
  98. "" +
  99. "02 01 01 01 00 E0 00 03 01 00 00" +
  100. "" +
  101. "01 01 02 01 02 00 00 03 01 00 05" +
  102. "" +
  103. "02 01 01 01 00 20 00 05 01 00 05 01 31" +
  104. "" +
  105. "01 01 02 01 02 40 00 05 01 00 04 01 0A" +
  106. "" +
  107. "01 01 02 01 02 40 00 05 01 00 04 01 0A" +
  108. "" +
  109. "02 01 01 01 00 E0 00 03 01 00 00" +
  110. "" +
  111. "01 01 02 01 02 00 00 03 01 00 04" +
  112. "" +
  113. "02 01 01 01 00 20 00 05 01 00 04 01 0A" +
  114. "" +
  115. "01 01 02 01 02 40 00 06 01 00 0B 02 02 01" +
  116. "" +
  117. "02 01 01 01 00 E0 00 03 01 00 00" +
  118. "" +
  119. "01 01 02 01 00 00 00 14 01 01 02 04 05 08 09 0B 0C 28 29 2A 2B 32 33 34 35 36 39 3A" +
  120. "02 01 01 01 00 22 00 59 01 01 02 01 03 04 01 06 05 01 04 08 01 01 09 01 00 0B 01 01 0C 01 00 28 01 57 29 01 57 2A 01 35 2B 01 00 32 03 54 4F 4B 33 03 4D 50 44 34 03 54 51 43 35 0C 31 32 33 34 35 36 37 38 39 30 31 32 36 0C 30 30 30 30 30 30 30 30 31 2E 38 31 39 01 00 3A 06 00 00 00 00 02 32" +
  121. "01 01 02 01 00 03 00 05 01 21 14 15 16" +
  122. "01 01 02 01 00 0C 00 05 01 22 14 15 16" +
  123. "01 01 02 01 00 15 00 05 01 23 14 15 16" +
  124. "01 01 02 01 00 1E 00 05 01 24 14 15 16" +
  125. "02 01 01 01 00 3E 00 0C 01 24 14 01 02 15 01 00 16 02 00 00" +
  126. "01 01 02 01 00 1F 00 05 01 24 14 15 16" +
  127. "01 01 02 01 00 00 00 05 01 21 14 15 16" +
  128. "02 01 01 01 00 20 00 0C 01 21 14 01 02 15 01 00 16 02 00 00" +
  129. "01 01 02 01 00 01 00 05 01 21 14 15 16" +
  130. "02 01 01 01 00 21 00 0C 01 21 14 01 02 15 01 00 16 02 00 00" +
  131. "01 01 02 01 00 02 00 03 01 21 16" +
  132. "01 01 02 01 00 0B 00 05 01 22 14 15 16" +
  133. "01 01 02 01 00 14 00 05 01 23 14 15 16" +
  134. "01 01 02 01 00 1D 00 05 01 24 14 15 16" +
  135. "02 01 01 01 00 3D 00 0C 01 24 14 01 02 15 01 00 16 02 00 00" +
  136. "01 01 02 01 00 1E 00 05 01 24 14 15 16" +
  137. "02 01 01 01 00 3E 00 0C 01 24 14 01 02 15 01 00 16 02 00 00" +
  138. "01 01 02 01 00 1F 00 03 01 24 16" +
  139. "02 01 01 01 00 3F 00 06 01 24 16 02 00 00" +
  140. "01 01 02 01 00 18 00 03 01 24 16" +
  141. "01 01 02 01 00 01 00 03 01 21 16" +
  142. "02 01 01 01 00 38 00 06 01 24 16 02 00 00" +
  143. "01 01 02 01 00 02 00 03 01 21 16" +
  144. "02 01 01 01 00 22 00 06 01 21 16 02 00 00" +
  145. "01 01 02 01 00 03 00 03 01 21 16" +
  146. "02 01 01 01 00 23 00 06 01 21 16 02 00 00" +
  147. "01 01 02 01 00 04 00 03 01 21 16" +
  148. "02 01 01 01 00 24 00 06 01 21 16 02 00 00" +
  149. "01 01 02 01 00 0D 00 05 01 22 14 15 16" +
  150. "01 01 02 01 00 16 00 05 01 23 14 15 16" +
  151. "02 01 01 01 00 36 00 0C 01 23 14 01 02 15 01 00 16 02 00 00" +
  152. "02 01 01 01 00 80 00 0C 03 23 41 04 64 00 01 01 04 05 01 02" +
  153. "02 01 01 01 00 80 00 18 01 24 64 00 14 01 01 15 01 00 16 02 00 00 50 08 00 00 00 00 00 00 00 00" +
  154. "02 01 01 01 00 80 00 18 01 21 64 00 14 01 01 15 01 00 16 02 00 00 50 08 00 00 00 00 00 00 00 00" +
  155. "02 01 01 01 00 80 00 18 01 23 64 00 14 01 02 15 01 00 16 02 00 00 50 08 00 00 00 00 00 00 00 00" +
  156. "02 01 01 01 00 80 00 18 01 24 64 00 14 01 02 15 01 00 16 02 00 00 50 08 00 00 00 00 00 00 00 00" +
  157. "02 01 01 01 00 80 00 0C 03 23 41 04 64 00 01 01 04 05 01 02" +
  158. "02 01 01 01 00 80 00 18 01 21 64 00 14 01 02 15 01 00 16 02 00 00 50 08 00 00 00 00 00 00 00 00" +
  159. "02 01 01 01 00 80 00 0C 03 24 41 04 64 00 01 01 04 05 01 02" +
  160. "02 01 01 01 00 80 00 0C 03 21 41 04 64 00 01 01 04 05 01 02" +
  161. "02 01 01 01 00 80 00 18 01 22 64 00 14 01 01 15 01 00 16 02 00 00 50 08 00 00 00 00 00 00 00 00").ToBytes());
  162. Assert.AreEqual(true, continueParseStack.Count != 1);
  163. for (int i = 0; i < continueParseStack.Count; i++)
  164. {
  165. Console.WriteLine($"[{i}]: {continueParseStack.ElementAt(i).ToLogString()}");
  166. var dbParser = DatabaseDataParser.New().Convert(continueParseStack.Cast<IfsfMessage>().ElementAt(i).RawDatabaseData.ToArray());
  167. Console.WriteLine($"DatabaseType: {dbParser.DatabaseType}: " +
  168. $"{dbParser.DataIds.Select(di => $"dataEleId: {di.Item1}, dValue: (len: {di.Item2.Length}) {di.Item2.Select(d => d.ToString()).Aggregate("", (acc, n) => acc + " " + n)}").Aggregate((acc, n) => acc + " | " + n)}");
  169. Console.WriteLine();
  170. Console.WriteLine();
  171. }
  172. }
  173. [TestMethod]
  174. public void LonWorksHeartbeatSerializeTestMethod1()
  175. {
  176. var heartbeat = new LonWorksHeartbeat(1, 2, 1);
  177. heartbeat.DeviceStates = new[] { HeartbeatDeviceStatus.EverythingIsFine };
  178. var parser = new Parser();
  179. var actual = parser.Serialize(heartbeat);
  180. var expect = new byte[] { 0x01, 0x02, 0x01, 0x01, 0x00 };
  181. Assert.IsTrue(ValueEquals(actual, expect));
  182. }
  183. [TestMethod]
  184. public void LonWorksHeartbeatSerializeTestMethod2()
  185. {
  186. var heartbeat = new LonWorksHeartbeat(1, 3, 5);
  187. heartbeat.DeviceStates = new[] { HeartbeatDeviceStatus.EverythingIsFine };
  188. var parser = new Parser();
  189. var actual = parser.Serialize(heartbeat);
  190. var expect = new byte[] { 0x01, 0x03, 0x05, 0x01, 0x00 };
  191. Assert.IsTrue(ValueEquals(actual, expect));
  192. }
  193. }
  194. }