コード例 #1
0
        protected override Task ProcessRequestAsync(HttpContext context)
        {
            var checker = Throttle.check("dpsLast", Global.throttle1, context);

            if (!checker.CheckResult)
            {
                Errors.GetError().p406(context);
                return(context.Response.Output.WriteAsync(string.Empty));
            }

            context.Response.ContentType = "application/json";

            var appKey = context.Request.Headers["U-ApiKey"];

            if (appKey != "appkey")
            {
                Errors.GetError().p412(context);
                return(context.Response.Output.WriteAsync(string.Empty));
            }

            var    meth        = context.Request.HttpMethod;
            var    routeValues = context.Request.RequestContext.RouteData.Values;
            string dvid        = routeValues["dvid"].ToString();
            string ssid        = routeValues["ssid"].ToString();

            //查看设备信息
            if (meth == "GET")
            {
                int dv;
                int ss;
                if (int.TryParse(dvid, out dv) && int.TryParse(ssid, out ss))
                {
                    var result = SensorsController.GetSensors().GetSensorType(dv, ss);
                    if (!result.hasError)
                    {
                        if (result.type == em_SensorType.gen)
                        {
                            return(context.Response.Output.WriteAsync(
                                       DataPointController.GetDataPoints().GetGenLast(dv, ss, result.type)));
                        }
                        else if (result.type == em_SensorType.gps || result.type == em_SensorType.photo || result.type == em_SensorType.value)
                        {
                            return(context.Response.Output.WriteAsync(
                                       DataPointController.GetDataPoints().GetGpsValImgLast(dv, ss, result.type)));
                        }
                    }
                    else
                    {
                        return(context.Response.Output.WriteAsync(result.error));
                    }
                }
                else
                {
                    return(context.Response.Output.WriteAsync(Errors.e7002));
                }
            }

            Errors.GetError().p404(context);
            return(context.Response.Output.WriteAsync(string.Empty));
        }
コード例 #2
0
        public void TestGetSensors_ReturnSensorsObjects()
        {
            // Arrange
            var sensors = fixture.Create <Sensor[]>();

            sensorService.Setup(ss => ss.GetSensors()).Returns(sensors.AsEnumerable());

            // Act
            var result = sensorsController.GetSensors();

            // Assert
            Assert.NotNull(result);
            OkObjectResult okObjResult = Assert.IsType <OkObjectResult>(result);

            Assert.Equal(200, okObjResult.StatusCode);
            Assert.NotNull(okObjResult.Value);
            Assert.IsType <Sensor[]>(okObjResult.Value);
        }
コード例 #3
0
        public void GetSensors_Returns_CollectionOfSensorDTO()
        {
            // Arrange
            var sensorServiceMock = new Mock <ISensorService>();

            sensorServiceMock.Setup(service => service
                                    .GetAllSensorsAsync())
            .Returns(Task.FromResult(GetAllSensors()));

            var loggerMock = new Mock <ILogger <SensorsController> >();

            var controller = new SensorsController(sensorServiceMock.Object, loggerMock.Object);

            // Act
            var result = controller.GetSensors().GetAwaiter().GetResult();

            // Assert
            Assert.IsType <List <SensorDTO> >(result);
        }
コード例 #4
0
        public async Task Test_GetSensors_WhenResults_Successful()
        {
            _mocksensorDataProvider.Setup(a => a.GetSensors()).ReturnsAsync(new List <Sensor>()
            {
                new Sensor()
                {
                    SensorId = 1, CreateDt = DateTime.Now, FarmId = 1, State = API.Helpers.SensorState.Dead
                }
            });

            _mockmapper.Setup(a => a.Map <IReadOnlyList <SensorDto> >(It.IsAny <IReadOnlyList <Sensor> >())).Returns(new List <SensorDto>()
            {
                new SensorDto()
                {
                    SensorId = 1, Farm = "Rich", State = API.Helpers.SensorState.Dead.ToString()
                }
            });

            var result = await sensorsController.GetSensors();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Microsoft.AspNetCore.Mvc.ActionResult <IReadOnlyList <SensorDto> >));
        }
コード例 #5
0
        protected override Task ProcessRequestAsync(HttpContext context)
        {
            var checker = Throttle.check("dpsPost", Global.throttle1, context);

            if (!checker.CheckResult)
            {
                Errors.GetError().p406(context);
                return(context.Response.Output.WriteAsync(string.Empty));
            }

            context.Response.ContentType = "application/json";

            var appKey = context.Request.Headers["U-ApiKey"];

            if (appKey != "appkey")
            {
                Errors.GetError().p412(context);
                return(context.Response.Output.WriteAsync(string.Empty));
            }

            var    meth        = context.Request.HttpMethod;
            var    routeValues = context.Request.RequestContext.RouteData.Values;
            string dvid        = routeValues["dvid"].ToString();
            string ssid        = routeValues["ssid"].ToString();
            string skey        = routeValues["key"].ToString();

            //判断是否有模拟put,delete请求
            var req = context.Request.QueryString["method"];

            if (!string.IsNullOrEmpty(req))
            {
                var mt = req.ToUpper();
                if (Enum.GetNames(typeof(em_HttpMeth)).Contains(mt))
                {
                    meth = mt;
                }
                else
                {
                    Errors.GetError().p417(context);
                    return(context.Response.Output.WriteAsync(string.Empty));
                }
            }

            //查看设备信息
            if (meth == "GET")
            {
                int dv;
                int ss;
                if (int.TryParse(dvid, out dv) && int.TryParse(ssid, out ss))
                {
                    var result = SensorsController.GetSensors().GetSensorType(dv, ss);
                    if (!result.hasError)
                    {
                        var type = result.type;
                        if (type == em_SensorType.gen)
                        {
                            return(context.Response.Output.WriteAsync(
                                       DataPointController.GetDataPoints().GetGen(dv, ss, type, skey)));
                        }
                        else if (type == em_SensorType.gps || type == em_SensorType.value || type == em_SensorType.photo)
                        {
                            DateTime dt;
                            try
                            {
                                dt = DateTime.ParseExact(skey, "yyyy-MM-ddTHH:mm:ss",
                                                         new CultureInfo("zh-CN"), DateTimeStyles.AssumeUniversal);
                            }
                            catch
                            {
                                return(context.Response.Output.WriteAsync(Errors.e7002));
                            }
                            return(context.Response.Output.WriteAsync(
                                       DataPointController.GetDataPoints().GetGpsValImg(dv, ss, type, dt)));
                        }
                    }
                    else
                    {
                        return(context.Response.Output.WriteAsync(result.error));
                    }
                }
                else
                {
                    return(context.Response.Output.WriteAsync(Errors.e7002));
                }
            }

            //修改设备信息
            if (meth == "PUT")
            {
                int dv;
                int ss;
                if (int.TryParse(dvid, out dv) && int.TryParse(ssid, out ss))
                {
                    string netjson;
                    using (var reader = new StreamReader(context.Request.InputStream))
                    {
                        netjson = reader.ReadToEnd();
                    }
                    var result = SensorsController.GetSensors().GetSensorType(dv, ss);
                    if (!result.hasError)
                    {
                        var type = result.type;
                        if (type == em_SensorType.gen)
                        {
                            return(context.Response.Output.WriteAsync(
                                       DataPointController.GetDataPoints().EditGen(dv, ss, type, skey, netjson)));
                        }
                        else if (type == em_SensorType.gps)
                        {
                            DateTime dt;
                            try
                            {
                                dt = DateTime.ParseExact(skey, "yyyy-MM-ddTHH:mm:ss",
                                                         new CultureInfo("zh-CN"), DateTimeStyles.AssumeUniversal);
                            }
                            catch
                            {
                                return(context.Response.Output.WriteAsync(Errors.e7002));
                            }
                            return(context.Response.Output.WriteAsync(
                                       DataPointController.GetDataPoints().EditGps(dv, ss, type, dt, netjson)));
                        }
                        else if (type == em_SensorType.value)
                        {
                            DateTime dt;
                            try
                            {
                                dt = DateTime.ParseExact(skey, "yyyy-MM-ddTHH:mm:ss",
                                                         new CultureInfo("zh-CN"), DateTimeStyles.AssumeUniversal);
                            }
                            catch
                            {
                                return(context.Response.Output.WriteAsync(Errors.e7002));
                            }
                            return(context.Response.Output.WriteAsync(
                                       DataPointController.GetDataPoints().EditVal(dv, ss, type, dt, netjson)));
                        }
                        else if (type == em_SensorType.photo)
                        {
                            DateTime dt;
                            try
                            {
                                dt = DateTime.ParseExact(skey, "yyyy-MM-ddTHH:mm:ss",
                                                         new CultureInfo("zh-CN"), DateTimeStyles.AssumeUniversal);
                            }
                            catch
                            {
                                return(context.Response.Output.WriteAsync(Errors.e7002));
                            }
                            return(context.Response.Output.WriteAsync(
                                       DataPointController.GetDataPoints().EditImg(dv, ss, type, dt, netjson)));
                        }
                    }
                    else
                    {
                        return(context.Response.Output.WriteAsync(result.error));
                    }
                }
                else
                {
                    return(context.Response.Output.WriteAsync(Errors.e7002));
                }
            }

            //删除设备
            if (meth == "DELETE")
            {
                int dv;
                int ss;
                if (int.TryParse(dvid, out dv) && int.TryParse(ssid, out ss))
                {
                    var result = SensorsController.GetSensors().GetSensorType(dv, ss);
                    if (!result.hasError)
                    {
                        var type = result.type;
                        if (type == em_SensorType.gen)
                        {
                            return(context.Response.Output.WriteAsync(
                                       DataPointController.GetDataPoints().DelGen(dv, ss, type, skey)));
                        }
                        else if (type == em_SensorType.gps || type == em_SensorType.value || type == em_SensorType.photo)
                        {
                            DateTime dt;
                            try
                            {
                                dt = DateTime.ParseExact(skey, "yyyy-MM-ddTHH:mm:ss",
                                                         new CultureInfo("zh-CN"), DateTimeStyles.AssumeUniversal);
                            }
                            catch
                            {
                                return(context.Response.Output.WriteAsync(Errors.e7002));
                            }
                            return(context.Response.Output.WriteAsync(
                                       DataPointController.GetDataPoints().DelGpsValImg(dv, ss, type, dt)));
                        }
                    }
                    else
                    {
                        return(context.Response.Output.WriteAsync(result.error));
                    }
                }
                else
                {
                    return(context.Response.Output.WriteAsync(Errors.e7002));
                }
            }

            Errors.GetError().p404(context);
            return(context.Response.Output.WriteAsync(string.Empty));
        }
コード例 #6
0
        protected override Task ProcessRequestAsync(HttpContext context)
        {
            var checker = Throttle.check("dpsPost", Global.throttle1, context);

            if (!checker.CheckResult)
            {
                Errors.GetError().p406(context);
                return(context.Response.Output.WriteAsync(string.Empty));
            }

            context.Response.ContentType = "application/json";

            var appKey = context.Request.Headers["U-ApiKey"];

            if (appKey != "appkey")
            {
                Errors.GetError().p412(context);
                return(context.Response.Output.WriteAsync(string.Empty));
            }

            var    meth        = context.Request.HttpMethod;
            var    routeValues = context.Request.RequestContext.RouteData.Values;
            string dvid        = routeValues["dvid"].ToString();
            string ssid        = routeValues["ssid"].ToString();

            //添加设备
            if (meth == "POST")
            {
                if (context.Request.InputStream.Length == 0)
                {
                    Errors.GetError().p204(context);
                    return(context.Response.Output.WriteAsync(string.Empty));
                }
                int dv;
                int ss;
                if (int.TryParse(dvid, out dv) && int.TryParse(ssid, out ss))
                {
                    string netjson;
                    using (var reader = new StreamReader(context.Request.InputStream))
                    {
                        netjson = reader.ReadToEnd();
                    }
                    var result = SensorsController.GetSensors().GetSensorType(dv, ss);
                    if (!result.hasError)
                    {
                        var type = result.type;
                        if (type == em_SensorType.gen)
                        {
                            return(context.Response.Output.WriteAsync(
                                       DataPointController.GetDataPoints().CreateGen(dv, ss, type, netjson)));
                        }
                        else if (type == em_SensorType.gps)
                        {
                            return(context.Response.Output.WriteAsync(
                                       DataPointController.GetDataPoints().CreateGps(dv, ss, type, netjson)));
                        }
                        else if (type == em_SensorType.value)
                        {
                            if (netjson.StartsWith("{"))
                            {
                                return(context.Response.Output.WriteAsync(
                                           DataPointController.GetDataPoints().CreateValOne(dv, ss, type, netjson)));
                            }
                            else if (netjson.StartsWith("["))
                            {
                                return(context.Response.Output.WriteAsync(
                                           DataPointController.GetDataPoints().CreateValMultible(dv, ss, type, netjson)));
                            }
                        }
                        else if (type == em_SensorType.photo)
                        {
                            //有另外专属接口处理图片管理
                            //return context.Response.Output.WriteAsync(
                            //    DataPointController.GetDataPoints().CreateImg(dv, ss, type, netjson));
                            return(context.Response.Output.WriteAsync(Errors.e7009));
                        }
                    }
                    else
                    {
                        return(context.Response.Output.WriteAsync(result.error));
                    }
                }
                else
                {
                    return(context.Response.Output.WriteAsync(Errors.e7002));
                }
            }

            Errors.GetError().p404(context);
            return(context.Response.Output.WriteAsync(string.Empty));
        }