Beispiel #1
0
            public CropCyclePresenter(CropCycle inCropCycle)
            {
                CropCycle = inCropCycle;
                StartDate = inCropCycle.StartDate.ToString("dd MMM");
                EndDate   = inCropCycle.EndDate?.ToString("dd MMM") ?? "Now";

                CropType = inCropCycle.CropTypeName.Substring(0, Math.Min(13, inCropCycle.CropTypeName.Length));
                if (inCropCycle.CropTypeName.Length > 13)
                {
                    CropType += "...";
                }

                CropVariety = inCropCycle.CropVariety.Substring(0, Math.Min(10, inCropCycle.CropVariety.Length));
                if (inCropCycle.CropVariety.Length > 10)
                {
                    CropVariety += "...";
                }

                LocationName = inCropCycle.Location.Name.Substring(0, Math.Min(10, inCropCycle.Location.Name.Length));
                if (inCropCycle.Location.Name.Length > 10)
                {
                    LocationName += "...";
                }


                Duration =
                    Math.Round(((inCropCycle.EndDate ?? DateTimeOffset.Now) - inCropCycle.StartDate).TotalDays, 0) +
                    " days";
            }
Beispiel #2
0
 /// <summary>Updates the properties of this viewmodel with data from POCO.</summary>
 /// <param name="cropRun">Requires CropType (for Variety) and Location (for name) to be included.</param>
 public void Update(CropCycle cropRun)
 {
     CropRunId    = cropRun.ID;
     CropName     = cropRun.CropTypeName;
     VarietyName  = cropRun.CropVariety;
     PlantingDate = cropRun.StartDate.ToString("dd/MM/yyyy");
     BoxName      = cropRun.Location.Name;
     Yield        = $"{cropRun.Yield}kg";
     _dashboardViewModel.Update(cropRun);
 }
        public void Update(CropCycle cropCycle)
        {
            CropName   = cropCycle.CropTypeName;
            BoxName    = cropCycle.Location.Name;
            IconLetter = CropName.Substring(0, 1);
            CropViewModel.Update(cropCycle);

            IsAlerted = cropCycle.Location.Devices.SelectMany(s => s.Sensors).Any(s => s.Alarmed);

            _cropViewModel.Update(cropCycle);
        }
        public async void CreateNewCropRun()
        {
            ChosenIsVacant = false;
            var settings = SettingsService.Instance;

            using (var db = new MainDbContext())
            {
                //TODO: BUG here! Does not check if the CropType already exists correctly, tries to create new one regardless
                var cropType = _cropTypeCache.FirstOrDefault(ct => ct.Key.Equals(UserCropType.ToLower())).Value;

                if (cropType == null)
                {
                    cropType = new CropType
                    {
                        Name      = UserCropType,
                        Approved  = false,
                        CreatedAt = DateTimeOffset.Now,
                        CreatedBy = settings.CredStableSid
                    };

                    db.Add(cropType);
                }
                else
                {
                    db.Attach(cropType);
                }


                var cropCycle = new CropCycle
                {
                    ID           = Guid.NewGuid(),
                    Name         = "Unnamed",
                    Yield        = 0,
                    CropType     = cropType,
                    CropTypeName = cropType.Name,
                    CropVariety  = CropVariety,
                    LocationID   = _chosenPlace.Location.ID,
                    CreatedAt    = DateTimeOffset.Now,
                    UpdatedAt    = DateTimeOffset.Now,
                    StartDate    = DateTimeOffset.Now,
                    EndDate      = null,
                    Deleted      = false,
                    Version      = new byte[32]
                };

                db.CropCycles.Add(cropCycle);
                await db.SaveChangesAsync();
            }
            await BroadcasterService.Instance.TablesChanged.Invoke(string.Empty);
        }
        /// <summary>Refreshed Cache</summary>
        private async void LoadCache()
        {
            var dbFetchTask = Task.Run(() =>
            {
                var dbLocationsRet =
                    _db.Locations.Include(loc => loc.CropCycles).Include(loc => loc.Devices).AsNoTracking().ToList();

                var sensorListRet =
                    _db.Sensors.Include(sen => sen.SensorType)
                    .Include(sen => sen.SensorType.Place)
                    .Include(sen => sen.SensorType.Param)
                    .Include(sen => sen.SensorType.Subsystem)
                    .AsNoTracking()
                    .ToList();     //Need to edit

                return(new Tuple <List <Location>, List <Sensor> >(dbLocationsRet, sensorListRet));
            });

            var result      = await dbFetchTask;
            var dbLocations = result.Item1;
            var sensorList  = result.Item2;


            var cache = new List <CroprunTuple>();


            foreach (var crop in dbLocations.SelectMany(loc => loc.CropCycles))
            {
                var cacheItem = new CroprunTuple(crop, crop.Location);

                var deviceIDs = crop.Location.Devices.Select(dev => dev.ID).ToList();
                foreach (var sensor in sensorList)
                {
                    if (deviceIDs.Contains(sensor.DeviceID))
                    {
                        cacheItem.sensors.Add(sensor);
                    }
                }

                cache.Add(cacheItem);
            }
            if (_selectedCropCycle == null)
            {
                _selectedCropCycle = cache.FirstOrDefault().cropCycle;
            }
            Cache = cache;
        }
Beispiel #6
0
 /// <summary>All the data in this ViewModel is pumped in from the ShellListViewModel, the only thing
 /// that will ever call this contructor. As a result this is a very simple bindable datamodel.</summary>
 /// <param name="cropCycle"></param>
 public CropViewModel(CropCycle cropCycle)
 {
     _id = cropCycle.ID;
     _dashboardViewModel = new DashboardViewModel(cropCycle);
     Update(cropCycle);
 }
 public ShellListViewModel(CropCycle cropCycle)
 {
     CropRunId     = cropCycle.ID;
     CropViewModel = new CropViewModel(cropCycle);
     Update(cropCycle);
 }
 public CroprunTuple(CropCycle inCropCycle, Location inLocation)
 {
     cropCycle = inCropCycle;
     location  = inLocation;
     sensors   = new List <Sensor>();
 }
        /// <summary>Updates are externally imposed after a bigger database query.</summary>
        /// <param name="run">CropCycle including Location.Devices.Sensors.Params</param>
        public void Update(CropCycle run)
        {
            var sensors        = run.Location.Devices.SelectMany(device => device.Sensors);
            var missingSensors = _cards.Where(c => sensors.FirstOrDefault(s => s.ID == c.Id) == null).ToList();

            foreach (var missingSensor in missingSensors)
            {
                _cards.Remove(missingSensor);
                if (MainCards.Contains(missingSensor))
                {
                    MainCards.Remove(missingSensor);
                }
                if (AmbientCards.Contains(missingSensor))
                {
                    AmbientCards.Remove(missingSensor);
                }
            }

            // Add, remove or update for each sensor.
            foreach (var sensor in sensors)
            {
                var existing = _cards.FirstOrDefault(c => c.Id == sensor.ID);
                if (existing == null)
                {
                    if (!sensor.Deleted)
                    {
                        Cards.Add(new LiveCardViewModel(sensor));
                    }
                }
                else
                {
                    if (sensor.Deleted)
                    {
                        _cards.Remove(existing);
                    }
                    else
                    {
                        existing.Update(sensor);
                    }
                }
            }
            //WE chose these two sensors for plantID's. The system we have does not have a good selection right now
            var mainIds    = new long[] { 8, 13, 19, 4, 16 };
            var ambientIds = new long[] { 5, 6, 11 };

            var mainItems    = Cards.Where(c => mainIds.Contains(c.SensorTypeID));
            var ambientItems = Cards.Where(c => ambientIds.Contains(c.SensorTypeID));

            foreach (var item in mainItems)
            {
                if (!MainCards.Contains(item))
                {
                    MainCards.Add(item);
                }
            }
            foreach (var item in ambientItems)
            {
                if (!AmbientCards.Contains(item))
                {
                    AmbientCards.Add(item);
                }
            }
        }
 /// <summary>All data in this model trickles up from the DashboardViewModel, making this a simple data
 /// model class.</summary>
 /// <param name="run"></param>
 public DashboardViewModel([NotNull] CropCycle run)
 {
     CropId = run.ID;
     Update(run);
 }