Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using ProjectWorking.Mezzi;
- using System.Threading;
- using System.Net.Http;
- using System.Text;
- using System.Net.Http.Headers;
- using System.Threading.Tasks;
- using System.Net.Http.Formatting;
- using CSRedis;
- using System.Net;
- using Newtonsoft.Json;
- using System.Net.NetworkInformation;
- using GMap.NET;
- using GMap.NET.WindowsForms;
- using GMap.NET.WindowsForms.Markers;
- using GMap.NET.MapProviders;
- using System.Collections.Generic;
- using System.Data.SQLite;
- namespace ProjectWorking
- {
- class Program
- {
- static void Main(string[] args)
- {
- PointLatLng start = new PointLatLng(-25.974134,32.593042);
- int pullmanNumber = 3;
- int count = 0;
- //var redis = new RedisClient("192.168.101.143");
- Pullman p1 = new Pullman(1,70, 1);
- Pullman p2 = new Pullman(2,50, 2);
- Pullman p3 = new Pullman(3,30, 3);
- while(true)
- {
- Thread.Sleep(1000);
- p1.Update();
- p2.Update();
- p3.Update();
- //redis.LPush("sensors_data",JsonCreator(p1));
- //redis.LPush("sensors_data",JsonCreator(p2));
- //redis.LPush("sensors_data",JsonCreator(p3));
- Console.Writeline(JsonCreator(p1));
- /*
- if(ping()){
- if(count>0){
- for(int i=0 ;i<count * pullmanNumber;i++){
- sendToApi(redis.BLPop(30, "sensors_data"));
- }
- }
- for(int i = 0; i < pullmanNumber; i++){
- sendToApi(redis.BLPop(30, "sensors_data"));
- }
- count=0;
- }
- else{
- count ++;
- }
- */
- }
- }
- static bool ping()
- {
- Ping pingSender = new Ping();
- PingReply reply = pingSender.Send("8.8.8.8");
- if (reply.Status == IPStatus.Success)
- {
- return true;
- }
- return false;
- }
- static string JsonCreator(Pullman p){
- string json = JsonConvert.SerializeObject(p, Formatting.Indented);
- return json;
- }
- static async void sendToApi(string data){
- try{
- using (var client = new HttpClient())
- {
- client.BaseAddress = new Uri("http://127.0.0.1:5000/");
- var content = new StringContent(data, Encoding.UTF8, "application/json");
- var result = await client.PostAsync("pullman", content);
- string resultContent = await result.Content.ReadAsStringAsync();
- }
- }catch
- {
- }
- }
- }
- class Contapersone
- {
- private static Random rndom = new Random();
- private int _numeroMaxPersone;
- private int _numeroPasseggeri;
- public Contapersone(int posti)
- {
- _numeroMaxPersone = posti;
- _numeroPasseggeri = rndom.Next(0,_numeroMaxPersone);
- }
- public void Change()
- {
- int numero = rndom.Next(-10,10);
- int scelta = rndom.Next(0,2);
- if(_numeroPasseggeri + numero < _numeroMaxPersone && _numeroPasseggeri + numero >=0)
- {
- _numeroPasseggeri += numero;
- }
- }
- public int GetNumber()
- {
- return _numeroPasseggeri;
- }
- }
- class Posizione
- {
- //PointLatLng start = new PointLatLng(-25.974134,32.593042);
- private static Random rndom = new Random();
- private double _latitudine;
- private double _longitudine;
- public Posizione(double latitudine, double longitudine)
- {
- _latitudine = latitudine;
- _longitudine = longitudine;
- }
- public void Change()
- {
- _latitudine +=0.0020;
- _longitudine -=0.0020;
- _latitudine = Math.Round(_latitudine,5);
- _longitudine = Math.Round(_longitudine,5);
- }
- public double GetLat()
- {
- return _latitudine;
- }
- public double GetLon()
- {
- return _longitudine;
- }
- }
- class Pullman
- {
- private static Random rnd = new Random();
- List<Percorso> Percorsi=GeneratorlstPercorsi();
- private int _id;
- public int Id
- {
- get => _id;
- set => _id = value;
- }
- private int _numeroPosti;
- private bool _movimento = true;
- public bool Movimento{
- get => _movimento;
- }
- private int _tempososta;
- private Contapersone _contaPersone;
- public int PersoneABordo
- {
- get => _contaPersone.GetNumber();
- }
- private Posizione _posizione;
- public double Longitudine{
- get => _posizione.GetLon();
- }
- public double Latitudine{
- get => _posizione.GetLat();
- }
- private DateTime _oraInserimento;
- public string OraInserimento
- {
- get => _oraInserimento.ToString("MM/dd/yyyy HH:mm:ss");
- }
- private Percorso _percorso;
- public int IdPercorso{
- get => Percorsi.IndexOf(_percorso);
- }
- public Pullman(int id, int numeroposti, int idPercorso)
- {
- _id = id;
- _numeroPosti = numeroposti;
- _contaPersone = new Contapersone(_numeroPosti);
- _posizione = new Posizione(CoordsGenerator(),CoordsGenerator());
- _oraInserimento = DateTime.Now;
- _percorso=Percorsi[idPercorso];
- }
- public void Update()
- {
- if(_movimento == true)
- {
- _posizione.Change();
- int random = rnd.Next(0,30);
- if(random == 0)
- {
- _movimento = false;
- _tempososta = rnd.Next(10,20);
- }
- }
- else if(_movimento == false)
- {
- _tempososta --;
- _contaPersone.Change();
- if(_tempososta == 0)
- {
- _movimento = true;
- }
- }
- _oraInserimento=DateTime.Now;
- }
- private static double CoordsGenerator()
- {
- double minimum = -20;
- double maximum = 20;
- Random random = new Random();
- return Math.Round((random.NextDouble() * (maximum - minimum) + minimum),5);
- }
- private static Percorso PercorsoGenerator()
- {
- Posizione partenza = new Posizione(CoordsGenerator(), CoordsGenerator());
- Posizione arrivo = new Posizione(CoordsGenerator(), CoordsGenerator());
- Percorso percorso = new Percorso(partenza, arrivo);
- return percorso;
- }
- private static List<Percorso> GeneratorlstPercorsi()
- {
- List<Percorso> lstPercorsi = new List<Percorso>();
- for (int i = 0; i < 10; i++)
- {
- //lstPercorsi[i]=PercorsoGenerator();
- lstPercorsi.Add(PercorsoGenerator());
- }
- return lstPercorsi;
- }
- }
- class Percorso
- {
- Posizione _partenza;
- Posizione _arrivo;
- public Percorso(Posizione partenza, Posizione arrivo)
- {
- _partenza = partenza;
- _arrivo = arrivo;
- }
- public Percorso()
- {
- }
- public Posizione GetPartenza()
- {
- return _partenza;
- }
- public Posizione GetArrivo()
- {
- return _arrivo;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement