Пример #1
0
        public ServiceResult <Panel> RegisterReading(string panelId, DateTime time, double wattage)
        {
            var panel     = _panelRepository.GetById(panelId);
            var kiloWatts = wattage;

            if (wattage <= 0)
            {
                return(ServiceResult <Panel> .ErrorResult("Valor incorrecto"));
            }
            if (panel.MeasuringUnit == MeasuringUnit.Watt)
            {
                kiloWatts = wattage / 1000;
            }

            var lastReading = _electricityReadingService.GetLastReading().Result;
            var timeDelta   = time - lastReading.ReadingDateTime;

            switch (panel.PanelType)
            {
            case PanelType.Regular:
                if (timeDelta.TotalHours > 1)
                {
                    _electricityReadingService.RegisterReading(panelId, time, kiloWatts);
                    return(ServiceResult <Panel> .SuccessResult(panel));
                }
                return(ServiceResult <Panel> .ErrorResult("No ha pasado una hora desde el ultimo registro"));

            case PanelType.Limited:
                if (timeDelta.TotalDays > 1 && kiloWatts > 5)
                {
                    _electricityReadingService.RegisterReading(panelId, time, kiloWatts);
                    return(ServiceResult <Panel> .SuccessResult(panel));
                }
                return(ServiceResult <Panel> .ErrorResult("No ha pasado un dia desde el ultimo registro o potencia es menor a 5W"));

            case PanelType.Ultimate:
                if (timeDelta.TotalMinutes > 1 && kiloWatts < 5)
                {
                    _electricityReadingService.RegisterReading(panelId, time, kiloWatts);
                    return(ServiceResult <Panel> .SuccessResult(panel));
                }
                return(ServiceResult <Panel> .ErrorResult("No ha pasado un minuto desde el ultimo registro o potencia es mayor a 5W"));

            default:
                return(ServiceResult <Panel> .ErrorResult("Internal server error"));
            }
        }
Пример #2
0
        public ActionResult <ElectricityReadingDto> RegisterReading([FromBody] ElectricityReading electricityReading)
        {
            //.c
            if (electricityReading == null | electricityReading.KiloWatt <= 0)
            {
                return(BadRequest("Objeto nulo o valor incorrecto."));
            }

            //.a
            electricityReading.ReadingDateTime = DateTime.UtcNow;

            //.b
            if (electricityReading.Panel.MeasuringUnit == MeasuringUnit.Watt)
            {
                electricityReading.KiloWatt /= 1000;
            }

            //.d
            var MostRecentReading = _electricityReadingService.GetRecentReadingByPanelId(electricityReading.PanelId);

            switch (electricityReading.Panel.PanelType)
            {
            case PanelType.Regular:
            {
                TimeSpan timeSpan = DateTime.UtcNow.Subtract(MostRecentReading.Result.ReadingDateTime);
                if (timeSpan.Hours < 1)
                {
                    return(BadRequest("No se puede realizar la medición."));
                }
            }
            break;

            case PanelType.Limited:
            {
                TimeSpan timeSpan = DateTime.UtcNow.Subtract(MostRecentReading.Result.ReadingDateTime);
                if (timeSpan.Days < 1)
                {
                    return(BadRequest("No se puede realizar la medición."));
                }
            }
            break;

            case PanelType.Ultimate:
            {
                TimeSpan timeSpan = DateTime.UtcNow.Subtract(MostRecentReading.Result.ReadingDateTime);
                if (timeSpan.Minutes < 1)
                {
                    return(BadRequest("No se puede realizar la medición."));
                }
            }
            break;

            default:
                return(BadRequest("Error indefinidio."));
            }

            var ServiceResult = _electricityReadingService.RegisterReading(electricityReading);

            if (ServiceResult.ResponseCode != ResponseCode.Success)
            {
                return(BadRequest(ServiceResult.Error));
            }

            var result = new ElectricityReadingDto
            {
                NumeroDeSerie   = ServiceResult.Result.PanelId,
                Panel           = ServiceResult.Result.Panel,
                KiloWatt        = ServiceResult.Result.KiloWatt,
                ReadingDateTime = DateTime.UtcNow
            };

            return(Ok(result));
        }