Пример #1
0
        public static TunnelInfo GetTunnelInfo(int tunnelId)
        {
            TunnelInfo tunnelInfo = new TunnelInfo {
                //TunelInformacje
                tunnel = Tunnel.GetTunnel(tunnelId),
                //Hodowle
                cultivations = Cultivation.GetCultivationsInTunnel(tunnelId),
                //Online
                online = false,
                //Temperature
                temperature = null
            };

            try {
                tunnelInfo.online = (DateTime.Now - (DateTime)AvrDevice.GetAvrDevicesInTunnel(tunnelId)[0].last_update).TotalSeconds < 60;
                if (tunnelInfo.online)
                {
                    tunnelInfo.temperature = Measurement.GetTemperatureInTunnel(tunnelId).value;
                    tunnelInfo.isAlarm     = GetTunnelAlarmInfo(tunnelId);
                }
            } catch {
                ;
            }


            return(tunnelInfo);
        }
Пример #2
0
        public static Measurement AddMeasurement(int measurement_type, int avr_device, double value)
        {
            DateTime date = DateTime.Now;

            try {
                string sql = $"insert into measurement.measurements (measurement_type,avr_device,value,date_time) " +
                             $"values ({measurement_type},{avr_device},{value.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)},'{date}')";

                pgSqlSingleManager.ExecuteSQL(sql);
                //to po dacie byc nie moze
                var measurementResult = pgSqlSingleManager.ExecuteSQL($"select * from measurement.measurements where" +
                                                                      $" measurement_type = {measurement_type} and avr_device = {avr_device} and date_time = '{date}'");

                Measurement newMeasurement = new Measurement {
                    id               = Int32.Parse(measurementResult[0]["id"]),
                    value            = Double.Parse(measurementResult[0]["value"]),
                    avr_device       = Int32.Parse(measurementResult[0]["avr_device"]),
                    measurement_type = Int32.Parse(measurementResult[0]["measurement_type"]),
                    date_time        = DateTime.Parse(measurementResult[0]["date_time"])
                };

                AvrDevice.Update(newMeasurement.avr_device);

                return(newMeasurement);
            } catch { }


            return(null);
        }
Пример #3
0
 public static Measurement GetTemperatureInTunnel(int tunnelId)
 {
     foreach (var avrDevice in AvrDevice.GetAvrDevicesInTunnel(tunnelId))
     {
         //ogolnie proponuje wszystkie temperatury dla wszystkich urzadzen - srednia
         //ale na razie tylko na podstawie jednego urzadzenia
         return(GetMeasurements($"select * from measurement.measurements where avr_device = {avrDevice.id} and measurement_type = 1 order by date_time desc limit 1 ")[0]);
     }
     return(null);
 }
Пример #4
0
        public static List <AvrDeviceInfo> GetAvrDeviceInfo()
        {
            List <AvrDeviceInfo> avrDevicesInfoList = new List <AvrDeviceInfo>();

            foreach (var avrDevice in AvrDevice.GetAvrDevices())
            {
                avrDevicesInfoList.Add(GetAvrDeviceInfo(avrDevice.id));
            }

            return(avrDevicesInfoList);
        }
Пример #5
0
        public static CultivationInfoBasic GetCultivationInfo(int cultivationId)
        {
            CultivationInfoBasic cultivationInfo = new CultivationInfoBasic {
                cultivation = Cultivation.GetCultivation(cultivationId),
                //Online
                online = false
            };

            try {
                cultivationInfo.online = (DateTime.Now - (DateTime)AvrDevice.GetAvrDevicesInTunnel(cultivationInfo.cultivation.tunnel.id)[0].last_update).TotalSeconds < 60;
            } catch {
                ;
            }

            return(cultivationInfo);
        }
Пример #6
0
 public IActionResult Create([FromBody] AvrDevice avrDevice)
 {
     if (avrDevice.ip == null || avrDevice.tunnel == null)
     {
         return(BadRequest());
     }
     else
     {
         AvrDevice avrChanged = AvrDevice.CreateAvrDevice(avrDevice.ip, avrDevice.tunnel.id);
         if (avrChanged.ip.Equals(avrDevice.ip))
         {
             return(CreatedAtRoute("GetTunnel", new { avrChanged.id }, avrChanged));
         }
     }
     return(BadRequest());
 }
Пример #7
0
        public IActionResult PrepareDataBase(bool prepare)
        {
            ClearDataBase();

            if (prepare)
            {
                Random gen = new Random();

                List <string> plantsNames = new List <string>();
                plantsNames.Add("Awokado");
                plantsNames.Add("Lychee");
                plantsNames.Add("Cytryna");
                plantsNames.Add("Pomarańcza");
                plantsNames.Add("Mango");

                foreach (string plantName in plantsNames)
                {
                    int newPlantId = Plant.CreatePlant(plantName).id;

                    for (int i = 0; i < 5; i++)
                    {
                        Variety.CreateVariety(PlantSpecies.GetPlantSpeciesByName(plantName).id, $"Odmiana {i}");
                    }
                }

                for (int i = 0; i < 4; i++)
                {
                    int id = Tunnel.CreateTunnel($"Tunnel {i}").id;
                    AvrDevice.CreateAvrDevice($"localhost/espSim/{i + 10}", id);

                    for (int j = 0; j < gen.Next(1, 6); j++)
                    {
                        List <Plant> plants          = Plant.GetPlants();
                        int          plantTableIndex = gen.Next(0, plants.Count - 1);


                        int breedingId = Cultivation.CreateCultivation($"Hodowla {i}/{j}", plants[plantTableIndex].id, i + i * j + 1, id, DateTime.Now).id;
                        for (int k = 0; k < gen.Next(0, 10); k++)
                        {
                            CultivationComment.AddCultivationComents($"Komentarz do hodowli { i + i * j * k}", breedingId);
                        }
                    }
                }
            }

            return(new NoContentResult());
        }
Пример #8
0
        public static List <Measurement> GetTemperatureInTunnel(int tunnelId, DateTime startDate, DateTime endDate)
        {
            int differentBetweenMeasurement = 20;

            List <Measurement> measurements = new List <Measurement>();

            foreach (var avrDevice in AvrDevice.GetAvrDevicesInTunnel(tunnelId))
            {
                string query = $"select * from measurement.measurements where avr_device = {avrDevice.id} and measurement_type = 1 " +
                               $"and date_time >= '{startDate}' and date_time <= '{endDate}' order by date_time";

                measurements = GetMeasurements(query);

                if (measurements != null)
                {
                    List <Measurement> newMeasurementList = new List <Measurement>();

                    Measurement lastMeasurement = new Measurement();
                    bool        isStart         = true;

                    foreach (Measurement measurement in measurements)
                    {
                        if (isStart)
                        {
                            newMeasurementList.Add(measurement);
                            lastMeasurement = measurement;
                            isStart         = false;
                        }
                        else
                        {
                            if (Math.Abs((measurement.date_time - lastMeasurement.date_time).TotalMinutes) > differentBetweenMeasurement)
                            {
                                newMeasurementList.Add(measurement);
                                lastMeasurement = measurement;
                            }
                        }
                    }

                    return(newMeasurementList);
                }
            }

            return(measurements);
        }
Пример #9
0
        public static AvrDeviceInfo GetAvrDeviceInfo(int avrDeviceId)
        {
            //TYMCZASOWE LOSOWANIE ONLINE
            Random gen  = new Random();
            int    prob = gen.Next(100);
            ////////////////////////////////

            AvrDeviceInfo avrDeviceInfo = new AvrDeviceInfo {
                avrDevice = AvrDevice.GetAvrDevice(avrDeviceId),
                online    = false
            };

            try {
                avrDeviceInfo.online = (DateTime.Now - (DateTime)AvrDevice.GetAvrDevicesInTunnel(avrDeviceInfo.avrDevice.tunnel.id)[0].last_update).TotalSeconds < 60;
            } catch {
                ;
            }

            return(avrDeviceInfo);
        }
Пример #10
0
        private void ClearDataBase()
        {
            foreach (Tunnel tunnel in Tunnel.GetTunnels())
            {
                foreach (Cultivation cultivation in Cultivation.GetCultivationsInTunnel(tunnel.id))
                {
                    CultivationComment.DeleteFromBreeding(cultivation.id);
                    Cultivation.DeleteFromTunnel(cultivation.id);
                }

                foreach (AvrDevice avr_device in AvrDevice.GetAvrDevicesInTunnel(tunnel.id))
                {
                    Measurement.DeleteMeasurements();
                    AvrDevice.DeleteAvrDevice(avr_device.id);
                }

                Tunnel.DeleteTunnel(tunnel.id);
            }

            Plant.DeletePlants();
        }
Пример #11
0
        public static void Main(string[] args)
        {
            List <AVRDeviceReader> avrDeviceList = new List <AVRDeviceReader>();

            SerialPortReader serialPortReader = new SerialPortReader();

            new Thread(() => { serialPortReader.Update(); }).Start();


            while (true)
            {
                foreach (AvrDevice avrDevice in AvrDevice.GetAvrDevicesSimple())
                {
                    if (avrDeviceList.Find(x => x.id == avrDevice.id) == null)
                    {
                        AVRDeviceReader avrDeviceReader = new AVRDeviceReader(avrDevice.id, avrDevice.ip);
                        avrDeviceReader.startReading(30000);
                        avrDeviceList.Add(avrDeviceReader);
                    }
                    else
                    {
                        avrDeviceList.Find(x => x.id == avrDevice.id).ip = avrDevice.ip;
                    }
                }

                /*foreach (var avrDevice in avrDeviceList) {
                 *  Console.WriteLine($"{avrDevice.id} {avrDevice.ip}");
                 *  Console.WriteLine($"temperature: {avrDevice.temperature}");
                 *  foreach (var adcValue in avrDevice.adc) {
                 *      Console.Write($"adc: {adcValue}");
                 *  }
                 * }*/

                Thread.Sleep(3000);
            }
        }
Пример #12
0
 public IActionResult Get(int id)
 {
     return(new ObjectResult(AvrDevice.GetAvrDevice(id)));
 }