Advertisement
Guest User

aaaaaaaa

a guest
Oct 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. using System;
  2. using ProjectWorking.Mezzi;
  3. using System.Threading;
  4. using System.Net.Http;
  5. using System.Text;
  6. using System.Net.Http.Headers;
  7. using System.Threading.Tasks;
  8. using System.Net.Http.Formatting;
  9. using CSRedis;
  10. using System.Net;
  11. using Newtonsoft.Json;
  12. using System.Net.NetworkInformation;
  13. using GMap.NET;
  14. using GMap.NET.WindowsForms;
  15. using GMap.NET.WindowsForms.Markers;
  16. using GMap.NET.MapProviders;
  17. using System.Collections.Generic;
  18. using System.Data.SQLite;
  19.  
  20.  
  21. namespace ProjectWorking
  22. {
  23. class Program
  24. {
  25. static void Main(string[] args)
  26. {
  27. PointLatLng start = new PointLatLng(-25.974134,32.593042);
  28.  
  29.  
  30. int pullmanNumber = 3;
  31.  
  32. int count = 0;
  33. //var redis = new RedisClient("192.168.101.143");
  34. Pullman p1 = new Pullman(1,70, 1);
  35. Pullman p2 = new Pullman(2,50, 2);
  36. Pullman p3 = new Pullman(3,30, 3);
  37. while(true)
  38. {
  39. Thread.Sleep(1000);
  40. p1.Update();
  41. p2.Update();
  42. p3.Update();
  43. //redis.LPush("sensors_data",JsonCreator(p1));
  44. //redis.LPush("sensors_data",JsonCreator(p2));
  45. //redis.LPush("sensors_data",JsonCreator(p3));
  46. Console.Writeline(JsonCreator(p1));
  47. /*
  48. if(ping()){
  49. if(count>0){
  50. for(int i=0 ;i<count * pullmanNumber;i++){
  51. sendToApi(redis.BLPop(30, "sensors_data"));
  52. }
  53. }
  54. for(int i = 0; i < pullmanNumber; i++){
  55. sendToApi(redis.BLPop(30, "sensors_data"));
  56. }
  57. count=0;
  58. }
  59. else{
  60. count ++;
  61. }
  62. */
  63. }
  64. }
  65.  
  66. static bool ping()
  67. {
  68. Ping pingSender = new Ping();
  69. PingReply reply = pingSender.Send("8.8.8.8");
  70. if (reply.Status == IPStatus.Success)
  71. {
  72. return true;
  73. }
  74. return false;
  75. }
  76.  
  77. static string JsonCreator(Pullman p){
  78. string json = JsonConvert.SerializeObject(p, Formatting.Indented);
  79. return json;
  80. }
  81.  
  82. static async void sendToApi(string data){
  83. try{
  84. using (var client = new HttpClient())
  85. {
  86. client.BaseAddress = new Uri("http://127.0.0.1:5000/");
  87. var content = new StringContent(data, Encoding.UTF8, "application/json");
  88. var result = await client.PostAsync("pullman", content);
  89. string resultContent = await result.Content.ReadAsStringAsync();
  90. }
  91. }catch
  92. {
  93.  
  94. }
  95. }
  96. }
  97.  
  98. class Contapersone
  99. {
  100. private static Random rndom = new Random();
  101. private int _numeroMaxPersone;
  102. private int _numeroPasseggeri;
  103.  
  104. public Contapersone(int posti)
  105. {
  106. _numeroMaxPersone = posti;
  107. _numeroPasseggeri = rndom.Next(0,_numeroMaxPersone);
  108. }
  109. public void Change()
  110. {
  111. int numero = rndom.Next(-10,10);
  112. int scelta = rndom.Next(0,2);
  113. if(_numeroPasseggeri + numero < _numeroMaxPersone && _numeroPasseggeri + numero >=0)
  114. {
  115. _numeroPasseggeri += numero;
  116. }
  117. }
  118. public int GetNumber()
  119. {
  120. return _numeroPasseggeri;
  121. }
  122. }
  123.  
  124. class Posizione
  125. {
  126.  
  127.  
  128. //PointLatLng start = new PointLatLng(-25.974134,32.593042);
  129.  
  130. private static Random rndom = new Random();
  131. private double _latitudine;
  132. private double _longitudine;
  133. public Posizione(double latitudine, double longitudine)
  134. {
  135. _latitudine = latitudine;
  136. _longitudine = longitudine;
  137. }
  138. public void Change()
  139. {
  140. _latitudine +=0.0020;
  141. _longitudine -=0.0020;
  142. _latitudine = Math.Round(_latitudine,5);
  143. _longitudine = Math.Round(_longitudine,5);
  144. }
  145. public double GetLat()
  146. {
  147. return _latitudine;
  148. }
  149. public double GetLon()
  150. {
  151. return _longitudine;
  152. }
  153. }
  154.  
  155. class Pullman
  156. {
  157. private static Random rnd = new Random();
  158.  
  159. List<Percorso> Percorsi=GeneratorlstPercorsi();
  160. private int _id;
  161. public int Id
  162. {
  163. get => _id;
  164. set => _id = value;
  165. }
  166.  
  167. private int _numeroPosti;
  168.  
  169. private bool _movimento = true;
  170. public bool Movimento{
  171. get => _movimento;
  172. }
  173.  
  174. private int _tempososta;
  175.  
  176. private Contapersone _contaPersone;
  177. public int PersoneABordo
  178. {
  179. get => _contaPersone.GetNumber();
  180. }
  181.  
  182. private Posizione _posizione;
  183. public double Longitudine{
  184. get => _posizione.GetLon();
  185. }
  186. public double Latitudine{
  187. get => _posizione.GetLat();
  188. }
  189.  
  190. private DateTime _oraInserimento;
  191. public string OraInserimento
  192. {
  193. get => _oraInserimento.ToString("MM/dd/yyyy HH:mm:ss");
  194. }
  195.  
  196. private Percorso _percorso;
  197. public int IdPercorso{
  198. get => Percorsi.IndexOf(_percorso);
  199. }
  200.  
  201. public Pullman(int id, int numeroposti, int idPercorso)
  202. {
  203. _id = id;
  204. _numeroPosti = numeroposti;
  205. _contaPersone = new Contapersone(_numeroPosti);
  206. _posizione = new Posizione(CoordsGenerator(),CoordsGenerator());
  207. _oraInserimento = DateTime.Now;
  208. _percorso=Percorsi[idPercorso];
  209. }
  210.  
  211. public void Update()
  212. {
  213. if(_movimento == true)
  214. {
  215. _posizione.Change();
  216. int random = rnd.Next(0,30);
  217. if(random == 0)
  218. {
  219. _movimento = false;
  220. _tempososta = rnd.Next(10,20);
  221. }
  222. }
  223. else if(_movimento == false)
  224. {
  225. _tempososta --;
  226. _contaPersone.Change();
  227. if(_tempososta == 0)
  228. {
  229. _movimento = true;
  230. }
  231. }
  232. _oraInserimento=DateTime.Now;
  233. }
  234.  
  235. private static double CoordsGenerator()
  236. {
  237. double minimum = -20;
  238. double maximum = 20;
  239. Random random = new Random();
  240. return Math.Round((random.NextDouble() * (maximum - minimum) + minimum),5);
  241. }
  242.  
  243. private static Percorso PercorsoGenerator()
  244. {
  245. Posizione partenza = new Posizione(CoordsGenerator(), CoordsGenerator());
  246. Posizione arrivo = new Posizione(CoordsGenerator(), CoordsGenerator());
  247.  
  248. Percorso percorso = new Percorso(partenza, arrivo);
  249.  
  250.  
  251. return percorso;
  252. }
  253.  
  254. private static List<Percorso> GeneratorlstPercorsi()
  255. {
  256. List<Percorso> lstPercorsi = new List<Percorso>();
  257.  
  258. for (int i = 0; i < 10; i++)
  259. {
  260. //lstPercorsi[i]=PercorsoGenerator();
  261. lstPercorsi.Add(PercorsoGenerator());
  262. }
  263.  
  264. return lstPercorsi;
  265. }
  266. }
  267.  
  268. class Percorso
  269. {
  270. Posizione _partenza;
  271. Posizione _arrivo;
  272.  
  273. public Percorso(Posizione partenza, Posizione arrivo)
  274. {
  275. _partenza = partenza;
  276. _arrivo = arrivo;
  277. }
  278.  
  279. public Percorso()
  280. {
  281.  
  282. }
  283.  
  284. public Posizione GetPartenza()
  285. {
  286. return _partenza;
  287. }
  288.  
  289. public Posizione GetArrivo()
  290. {
  291. return _arrivo;
  292. }
  293. }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement