Form1.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 OHVDriveSimulator
  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. //Logger lvwLogger = Logger.GetLogger( "lvwLogger" );
  26. ThreadCancel threadCancel = new ThreadCancel();
  27. List<point> Points = null;
  28. List<segment> Segments = null;
  29. Graph<int, string> graph = new Graph<int, string>();
  30. ZmqManager zmq = null;
  31. NetMq mq = null;
  32. public Form1()
  33. {
  34. InitializeComponent();
  35. this.listLog.Columns.Add( "Time" );
  36. this.listLog.Columns.Add( "Name" );
  37. this.listLog.Columns.Add( "Level" );
  38. this.listLog.Columns.Add( "message" );
  39. this.listLog.Columns.Add( "renderMessage" );
  40. this.Load += Form1_Load;
  41. this.FormClosing += Form1_FormClosing;
  42. }
  43. private void Form1_FormClosing( object sender, FormClosingEventArgs e )
  44. {
  45. this.threadCancel.Cancel();
  46. this.threadCancel.StopWaitAll();
  47. this.mq.Dispose();
  48. //Redis.Instance.Dispose();
  49. }
  50. private void Form1_Load( object sender, EventArgs e )
  51. {
  52. ListViewAppender.SetListView( this.listLog );
  53. logger.I( "Start Simulator" );
  54. mq = new NetMq();
  55. mq.Init();
  56. mq.OnRequestMoveStart += Mq_OnRequestMoveStart;
  57. //Redis.Instance.Init();
  58. this.button1_Click( this, null );
  59. this.threadCancel.AddGo( Thread_Dowork );
  60. this.threadCancel.AddGo( Thread_DriveRun );
  61. //this.zmq = new ZmqManager();
  62. //this.mq = new NetMq();
  63. }
  64. int currentTag = 2;
  65. bool isStartMove = false;
  66. TaskCancel taskCancel = new TaskCancel();
  67. private void Mq_OnRequestMoveStart()
  68. {
  69. if ( this.isStartMove )
  70. return;
  71. uint f = (uint)currentTag;
  72. uint t = uint.Parse( this.mq.TargetTag );
  73. ShortestPathResult result = this.graph.Dijkstra( f, t );
  74. var route = result.GetPath();
  75. var ll = route.ToList();
  76. this.taskCancel.Cancel();
  77. this.taskCancel.WaitAll();
  78. var task = Task.Run( () =>
  79. {
  80. mq.SetMoving();
  81. logger.D( "Start Move" );
  82. foreach ( var point in ll )
  83. {
  84. if ( this.taskCancel.Canceled )
  85. break;
  86. LockUtils.Wait( 2000 );
  87. mq.SetCurrentTag( point.ToString(), "0" );
  88. this.currentTag = (int)point;
  89. logger.I( $"[Tag Changed] - {point}" );
  90. }
  91. mq.ResetMoving();
  92. logger.D( "Stop Move" );
  93. } );
  94. taskCancel.Add( task );
  95. }
  96. void Thread_Dowork()
  97. {
  98. while ( !this.threadCancel.Canceled )
  99. {
  100. try
  101. {
  102. LockUtils.Wait( 5000 );
  103. //if ( Redis.Instance.GetMoveStart() )
  104. //{
  105. // Redis.Instance.ResetMoveStart();
  106. // this.RunDrive();
  107. //}
  108. }
  109. catch ( Exception e )
  110. {
  111. }
  112. }
  113. }
  114. bool isDriveRun = false;
  115. void Thread_DriveRun()
  116. {
  117. while ( !this.threadCancel.Canceled )
  118. {
  119. try
  120. {
  121. LockUtils.Wait( 2000 );
  122. }
  123. catch ( Exception )
  124. {
  125. }
  126. }
  127. }
  128. void AddNode()
  129. {
  130. this.Points.ForEach( p =>
  131. {
  132. this.graph.AddNode( p.ID );
  133. //logger.D( $"AddNode : {p.ID}" );
  134. } );
  135. this.graph.Connect( 20, 19, 20, null );
  136. this.graph.Connect( 1, 21, 10, null );
  137. this.graph.Connect( 1, 16, 20, null );
  138. this.graph.Connect( 2, 22, 10, null );
  139. this.graph.Connect( 3, 2, 10, null );
  140. this.graph.Connect( 4, 3, 10, null );
  141. this.graph.Connect( 5, 23, 10, null );
  142. this.graph.Connect( 6, 5, 10, null );
  143. this.graph.Connect( 7, 6, 20, null );
  144. this.graph.Connect( 8, 24, 10, null );
  145. this.graph.Connect( 9, 8, 10, null );
  146. this.graph.Connect( 10, 25, 10, null );
  147. this.graph.Connect( 10, 4, 20, null );
  148. this.graph.Connect( 11, 26, 10, null );
  149. this.graph.Connect( 12, 11, 10, null );
  150. this.graph.Connect( 13, 12, 10, null );
  151. this.graph.Connect( 14, 13, 10, null );
  152. this.graph.Connect( 15, 14, 10, null );
  153. this.graph.Connect( 16, 15, 10, null );
  154. this.graph.Connect( 17, 27, 10, null );
  155. this.graph.Connect( 18, 17, 10, null );
  156. this.graph.Connect( 19, 18, 10, null );
  157. this.graph.Connect( 20, 19, 10, null );
  158. this.graph.Connect( 21, 20, 10, null );
  159. this.graph.Connect( 22, 1, 10, null );
  160. this.graph.Connect( 23, 4, 10, null );
  161. this.graph.Connect( 24, 7, 10, null );
  162. this.graph.Connect( 25, 9, 10, null );
  163. this.graph.Connect( 26, 10, 10, null );
  164. this.graph.Connect( 27, 16, 10, null );
  165. this.graph.Connect( 27, 21, 20, null );
  166. //this.Segments.ForEach( s =>
  167. //{
  168. // if ( s.Middle == null )
  169. // {
  170. // this.graph.Connect( (uint)s.Start.pointID, (uint)s.End.pointID, 10, $"Segment ID {s.ID}" );
  171. // logger.D( $"Connect : {s.Start.pointID} -> {s.End.pointID} / SegmentID [{s.ID}]" );
  172. // }
  173. // else
  174. // {
  175. // this.graph.Connect( (uint)s.Start.pointID, (uint)s.Middle.pointID, 10, $"Segment ID {s.ID}" );
  176. // logger.D( $"Connect : {s.Start.pointID} -> {s.Middle.pointID} / SegmentID [{s.ID}]" );
  177. // this.graph.Connect( (uint)s.Middle.pointID, (uint)s.End.pointID, 10, $"Segment ID {s.ID}" );
  178. // logger.D( $"Connect : {s.Middle.pointID} -> {s.End.pointID} / SegmentID [{s.ID}]" );
  179. // }
  180. //} );
  181. }
  182. void RunDrive()
  183. {
  184. var redis = Redis.Instance;
  185. var currentPoint = redis.CurrentPointID();
  186. var targetPoint = redis.GetMovePoint();
  187. List<int> routeIDList = new List<int>();
  188. routeIDList.Add( currentPoint );
  189. var cuttentSegment = this.Segments.Where( x => x.Start.pointID == currentPoint ).FirstOrDefault();
  190. uint f = (uint)currentPoint;
  191. uint t = (uint)targetPoint;
  192. ShortestPathResult result = this.graph.Dijkstra( f, t );
  193. var route = result.GetPath();
  194. //segment preS = null;
  195. //int noPoint = currentPoint;
  196. //foreach ( var s in this.Segments )
  197. //{
  198. // if ( preS == null )
  199. // {
  200. // if ( s.Start.pointID == currentPoint )
  201. // preS = s;
  202. // }
  203. // foreach ( var ss in this.Segments )
  204. // {
  205. // if ( ss.Start.pointID == noPoint )
  206. // {
  207. // noPoint = ss.End.pointID;
  208. // routeIDList.Add( ss.Start.pointID );
  209. // break;
  210. // }
  211. // else if ( ss.Middle != null )
  212. // {
  213. // if ( ss.Middle.pointID == noPoint )
  214. // {
  215. // noPoint = ss.End.pointID;
  216. // routeIDList.Add( ss.Start.pointID );
  217. // break;
  218. // }
  219. // }
  220. // else { }
  221. // }
  222. // if ( s.End.pointID == 3 )
  223. // break;
  224. //}
  225. var ll = route.ToList();
  226. redis.SetDriveMove();
  227. foreach ( var point in ll )
  228. {
  229. LockUtils.Wait( 1000 );
  230. redis.SetCurrentPointID( (int)point );
  231. }
  232. redis.ResetDriveMove();
  233. }
  234. private void button1_Click( object sender, EventArgs e )
  235. {
  236. if ( !System.IO.File.Exists( Application.StartupPath + @"\Json\ohv_testmap7.json" ) )
  237. return;
  238. using ( StreamReader r = new StreamReader( Application.StartupPath + @"\Json\ohv_testmap7.json" ) )
  239. {
  240. string json = r.ReadToEnd();
  241. var o = JObject.Parse( json );
  242. this.Points = o["points"].ToObject<List<point>>();
  243. this.Segments = o["segments"].ToObject<List<segment>>();
  244. }
  245. AddNode();
  246. }
  247. private void button2_Click( object sender, EventArgs e )
  248. {
  249. //RunDrive( 0 );
  250. }
  251. }
  252. public class point
  253. {
  254. public int ID { get; set; }
  255. public int x { get; set; }
  256. public int y { get; set; }
  257. public int barcode { get; set; }
  258. }
  259. public class segment
  260. {
  261. public int ID { get; set; }
  262. public string route_type { get; set; }
  263. public double speed { get; set; }
  264. public int steering { get; set; }
  265. [JsonProperty( "start" )]
  266. public segmentPart Start { get; set; }
  267. [JsonProperty( "middle" )]
  268. public segmentPart Middle { get; set; }
  269. [JsonProperty( "end" )]
  270. public segmentPart End { get; set; }
  271. }
  272. public class segmentPart
  273. {
  274. public int pointID { get; set; }
  275. public int skew { get; set; }
  276. public int slope { get; set; }
  277. }
  278. }