Form1.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using Dijkstra.NET.Graph;
  12. using Dijkstra.NET.Graph.Simple;
  13. using Dijkstra.NET.ShortestPath;
  14. using GSG.NET.Concurrent;
  15. using GSG.NET.Logging;
  16. using Newtonsoft.Json;
  17. using Newtonsoft.Json.Linq;
  18. namespace OHVLoggerTest
  19. {
  20. public partial class Form1 : Form
  21. {
  22. Logger frontWheelLogger = Logger.GetLogger( "PhysicalCheckup_Front" );
  23. Logger rearWheelLogger = Logger.GetLogger( "PhysicalCheckup_Rear" );
  24. Logger logger = Logger.GetLogger();
  25. ThreadCancel threadCancel = new ThreadCancel();
  26. List<point> Points = null;
  27. List<segment> Segments = null;
  28. Graph<int, string> graph = new Graph<int, string>();
  29. ZmqManager zmq = null;
  30. NetMq mq = null;
  31. public Form1()
  32. {
  33. InitializeComponent();
  34. this.Load += Form1_Load;
  35. this.FormClosing += Form1_FormClosing;
  36. }
  37. private void Form1_FormClosing( object sender, FormClosingEventArgs e )
  38. {
  39. this.threadCancel.Cancel();
  40. this.threadCancel.StopWaitAll();
  41. Redis.Instance.Dispose();
  42. }
  43. private void Form1_Load( object sender, EventArgs e )
  44. {
  45. Redis.Instance.Init();
  46. this.button1_Click( this, null );
  47. this.threadCancel.AddGo( Thread_Dowork );
  48. this.threadCancel.AddGo( Thread_DriveRun );
  49. this.zmq = new ZmqManager();
  50. //this.mq = new NetMq();
  51. }
  52. void Thread_Dowork()
  53. {
  54. while ( !this.threadCancel.Canceled )
  55. {
  56. try
  57. {
  58. LockUtils.Wait( 1000 );
  59. if ( Redis.Instance.GetMoveStart() )
  60. {
  61. Redis.Instance.ResetMoveStart();
  62. this.RunDrive();
  63. }
  64. }
  65. catch ( Exception e)
  66. {
  67. }
  68. }
  69. }
  70. bool isDriveRun = false;
  71. void Thread_DriveRun()
  72. {
  73. while ( !this.threadCancel.Canceled )
  74. {
  75. try
  76. {
  77. LockUtils.Wait( 2000 );
  78. }
  79. catch ( Exception )
  80. {
  81. }
  82. }
  83. }
  84. void AddNode()
  85. {
  86. this.Points.ForEach( p =>
  87. {
  88. this.graph.AddNode( p.ID );
  89. logger.D( $"AddNode : {p.ID}" );
  90. } );
  91. this.graph.Connect( 20, 19, 20, null );
  92. this.graph.Connect( 1, 21, 10, null );
  93. this.graph.Connect( 2, 22, 10, null);
  94. this.graph.Connect( 3, 2, 10, null );
  95. this.graph.Connect( 4, 3, 10, null );
  96. this.graph.Connect( 5, 23, 10, null );
  97. this.graph.Connect( 6, 5, 10, null );
  98. this.graph.Connect( 7, 6, 20, null );
  99. this.graph.Connect( 8, 24, 10, null );
  100. this.graph.Connect( 9, 8, 10, null );
  101. this.graph.Connect( 10, 25, 10, null );
  102. this.graph.Connect( 10, 4, 20, null );
  103. this.graph.Connect( 11, 26, 10, null );
  104. this.graph.Connect( 12, 11, 10, null );
  105. this.graph.Connect( 13, 12, 10, null );
  106. this.graph.Connect( 14, 13, 10, null );
  107. this.graph.Connect( 15, 14, 10, null );
  108. this.graph.Connect( 16, 15, 10, null );
  109. this.graph.Connect( 17, 27, 10, null );
  110. this.graph.Connect( 18, 17, 10, null );
  111. this.graph.Connect( 19, 18, 10, null );
  112. this.graph.Connect( 20, 19, 10, null );
  113. this.graph.Connect( 21, 20, 10, null );
  114. this.graph.Connect( 22, 1, 10, null );
  115. this.graph.Connect( 23, 4, 10, null );
  116. this.graph.Connect( 24, 7, 10, null );
  117. this.graph.Connect( 25, 9, 10, null );
  118. this.graph.Connect( 26, 10, 10, null );
  119. this.graph.Connect( 27, 16, 10, null );
  120. this.graph.Connect( 27, 21, 20, null );
  121. //this.Segments.ForEach( s =>
  122. //{
  123. // if ( s.Middle == null )
  124. // {
  125. // this.graph.Connect( (uint)s.Start.pointID, (uint)s.End.pointID, 10, $"Segment ID {s.ID}" );
  126. // logger.D( $"Connect : {s.Start.pointID} -> {s.End.pointID} / SegmentID [{s.ID}]" );
  127. // }
  128. // else
  129. // {
  130. // this.graph.Connect( (uint)s.Start.pointID, (uint)s.Middle.pointID, 10, $"Segment ID {s.ID}" );
  131. // logger.D( $"Connect : {s.Start.pointID} -> {s.Middle.pointID} / SegmentID [{s.ID}]" );
  132. // this.graph.Connect( (uint)s.Middle.pointID, (uint)s.End.pointID, 10, $"Segment ID {s.ID}" );
  133. // logger.D( $"Connect : {s.Middle.pointID} -> {s.End.pointID} / SegmentID [{s.ID}]" );
  134. // }
  135. //} );
  136. }
  137. void RunDrive( )
  138. {
  139. var redis = Redis.Instance;
  140. var currentPoint = redis.CurrentPointID();
  141. var targetPoint = redis.GetMovePoint();
  142. List<int> routeIDList = new List<int>();
  143. routeIDList.Add( currentPoint );
  144. var cuttentSegment = this.Segments.Where( x => x.Start.pointID == currentPoint ).FirstOrDefault();
  145. uint f = (uint)currentPoint;
  146. uint t = (uint)targetPoint;
  147. ShortestPathResult result = this.graph.Dijkstra( f, t );
  148. var route = result.GetPath();
  149. //segment preS = null;
  150. //int noPoint = currentPoint;
  151. //foreach ( var s in this.Segments )
  152. //{
  153. // if ( preS == null )
  154. // {
  155. // if ( s.Start.pointID == currentPoint )
  156. // preS = s;
  157. // }
  158. // foreach ( var ss in this.Segments )
  159. // {
  160. // if ( ss.Start.pointID == noPoint )
  161. // {
  162. // noPoint = ss.End.pointID;
  163. // routeIDList.Add( ss.Start.pointID );
  164. // break;
  165. // }
  166. // else if ( ss.Middle != null )
  167. // {
  168. // if ( ss.Middle.pointID == noPoint )
  169. // {
  170. // noPoint = ss.End.pointID;
  171. // routeIDList.Add( ss.Start.pointID );
  172. // break;
  173. // }
  174. // }
  175. // else { }
  176. // }
  177. // if ( s.End.pointID == 3 )
  178. // break;
  179. //}
  180. var ll = route.ToList();
  181. redis.SetDriveMove();
  182. foreach ( var point in ll )
  183. {
  184. LockUtils.Wait( 1000 );
  185. redis.SetCurrentPointID( (int)point );
  186. }
  187. redis.ResetDriveMove();
  188. }
  189. private void button1_Click( object sender, EventArgs e )
  190. {
  191. if ( !System.IO.File.Exists( Application.StartupPath + @"\Json\ohv_testmap7.json" ) )
  192. return;
  193. using ( StreamReader r = new StreamReader( Application.StartupPath + @"\Json\ohv_testmap7.json" ) )
  194. {
  195. string json = r.ReadToEnd();
  196. var o = JObject.Parse( json );
  197. this.Points = o["points"].ToObject<List<point>>();
  198. this.Segments = o["segments"].ToObject<List<segment>>();
  199. }
  200. AddNode();
  201. }
  202. private void button2_Click( object sender, EventArgs e )
  203. {
  204. //RunDrive( 0 );
  205. }
  206. }
  207. public class point
  208. {
  209. public int ID { get; set; }
  210. public int x { get; set; }
  211. public int y { get; set; }
  212. public int barcode { get; set; }
  213. }
  214. public class segment
  215. {
  216. public int ID { get; set; }
  217. public string route_type { get; set; }
  218. public double speed { get; set; }
  219. public int steering { get; set; }
  220. [JsonProperty( "start" )]
  221. public segmentPart Start { get; set; }
  222. [JsonProperty( "middle" )]
  223. public segmentPart Middle { get; set; }
  224. [JsonProperty( "end" )]
  225. public segmentPart End { get; set; }
  226. }
  227. public class segmentPart
  228. {
  229. public int pointID { get; set; }
  230. public int skew { get; set; }
  231. public int slope { get; set; }
  232. }
  233. }