Ejemplo n.º 1
0
        /// <summary>
        /// 查询项目所有设备的传感器
        /// </summary>
        /// <param name="projectId">项目ID</param>
        /// <param name="token">请求token</param>
        /// <returns></returns>
        public ResultMsg <IEnumerable <SensorBaseInfoDTO> > GetProjectSensors(Int32 projectId, String token = null)
        {
            var result = new ResultMsg <IEnumerable <SensorBaseInfoDTO> >();

            //验证
            var vry = PrimaryKeyVerify(projectId, ref token);

            if (!vry.IsSuccess())
            {
                vry.CopyTo(result);
                return(result);
            }

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.ProjectSensorsUrl);

            //2、根据该API接口的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            apiPath = apiPath.Replace("{projectId}", projectId.ToString());//将API地址中的{projectid}替换成真实项目ID

            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();

            req.Method = HttpMethod.GET;
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity, IEnumerable <SensorBaseInfoDTO> >(apiPath, req);

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 发送命令
        /// </summary>
        /// <param name="deviceId">设备ID</param>
        /// <param name="apiTag">传感标识名</param>
        /// <param name="data">
        /// 开关类:开=1,关=0,暂停=2
        /// 家居类:调光灯亮度=0~254,RGB灯色度=2~239,窗帘、卷闸门、幕布打开百分比=3%~100%,红外指令=1(on)2(off)
        /// 其它:integer/float/Json/String类型值
        /// </param>
        /// <param name="token"></param>
        /// <returns></returns>
        public Result Cmds(Int32 deviceId, String apiTag, Object data, String token = null)
        {
            var result = new Result();

            var vry = PrimaryKeyVerify(deviceId, ref token);

            if (!vry.IsSuccess())
            {
                vry.CopyTo(result);
                return(result);
            }

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.CmdUrl);

            apiPath += string.Format("?{0}&{1}", "deviceId=" + deviceId, "apiTag=" + apiTag);
            //2、根据该API接口的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            //apiPath = apiPath.Replace("{deviceid}", deviceId.ToString());//将API地址中的{projectid}替换成真实项目ID

            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();

            req.Method = HttpMethod.POST;
            req.Datas  = JsonFormatter.Serialize(data);
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity>(apiPath, req);

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 更新某个设备
        /// </summary>
        /// <param name="para"></param>
        /// <param name="deviceId"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ResultMsg <Result> UpdateDeviceByDeviceId(DeviceAddParas para, int deviceId, string token = null)
        {
            var result = new ResultMsg <Result>();

            //验证
            var vry = PrimaryKeyVerify(deviceId, ref token);

            if (!vry.IsSuccess())
            {
                vry.CopyTo(result);
                return(result);
            }

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.DeviceUpdateUrl);

            //2、根据该API接口的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            apiPath = apiPath.Replace("{deviceid}", deviceId.ToString());//将API地址中的{projectid}替换成真实项目ID

            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();

            req.Method = HttpMethod.PUT;
            req.Datas  = JsonFormatter.Serialize(para);
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity>(apiPath, req);

            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 批量查询设备的在线状态
        /// </summary>
        /// <param name="devids"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ResultMsg <IEnumerable <OnlineDataDTO> > GetDevicesStatus(String devids, String token = null)
        {
            var result = new ResultMsg <IEnumerable <OnlineDataDTO> >();

            var vry = TokenVerify(ref token);

            if (!vry.IsSuccess())
            {
                vry.CopyTo(result);
                return(result);
            }

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.DevicesStatusUrl);

            //2、根据该API接口的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            //apiPath = apiPath.Replace("{projectId}", projectId.ToString());//将API地址中的{projectid}替换成真实项目ID
            apiPath += string.Format("?{0}", "devids=" + devids);
            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();

            req.Method = HttpMethod.GET;
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity, IEnumerable <OnlineDataDTO> >(apiPath, req);

            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 模糊查询项目
        /// </summary>
        /// <param name="query"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ResultMsg <ListPagerSet <ProjectInfoDTO> > GetProjectsInfo(ProjectFuzzyQryPagingParas queryData, String token = null)
        {
            var result = new ResultMsg <ListPagerSet <ProjectInfoDTO> >();

            #region --验证--
            //if (String.IsNullOrEmpty(txtToken))
            var vry = TokenVerify(ref token);
            if (!vry.IsSuccess())
            {
                vry.CopyTo(result);
                return(result);
            }
            #endregion

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.ProjectsInfoUrl);

            //2、根据该API接口 的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            //apiPath = apiPath.Replace("{projectId}", projectId.ToString());//将API地址中的{gatewayTag}替换成真实设备标识

            //2、Get请求,拼接querystring
            apiPath += string.Format("?{0}&{1}", "PageSize=" + queryData.PageSize, "StartDate=" + queryData.StartDate, "EndDate=" + queryData.EndDate, "PageIndex=" + queryData.PageIndex);

            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();
            req.Method = HttpMethod.GET;
            //req.Datas = JsonFormatter.Serialize(queryData);
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity, ListPagerSet <ProjectInfoDTO> >(apiPath, req);

            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 添加一个新的传感器
        /// </summary>
        /// <param name="deviceId">设备ID</param>
        /// <param name="sensor">
        ///     传感器:为SensorAddUpdate对象时
        ///     执行器:为ActuatorAddUpdate对象时
        ///     摄像头:为CameraAddUpdate对象时
        /// </param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ResultMsg <Int32> AddSensor <T>(Int32 deviceId, T sensor, String token = null) where T : SensorAddUpdateBase, new()
        {
            var result = new ResultMsg <int>();

            //验证
            var vry = PrimaryKeyVerify(deviceId, ref token);

            if (!vry.IsSuccess())
            {
                vry.CopyTo(result);
                return(result);
            }

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.SensorsOfDeviceUrl);

            //2、根据该API接口的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            apiPath = apiPath.Replace("{deviceid}", deviceId.ToString());//将API地址中的{deviceId}替换成真实设备ID

            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();

            req.Method = HttpMethod.POST;
            req.Datas  = JsonFormatter.Serialize(sensor);
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity, int>(apiPath, req);

            return(result);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 删除某个传感器
        /// </summary>
        /// <param name="deviceId">设备ID</param>
        /// <param name="apiTag">传感标识名</param>
        /// <param name="token"></param>
        /// <returns></returns>
        public Result DeleteSensor(Int32 deviceId, String apiTag, String token = null)
        {
            var result = new Result();

            //验证
            var vry = PrimaryKeyVerify(deviceId, ref token);

            if (!vry.IsSuccess())
            {
                vry.CopyTo(result);
                return(result);
            }

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.SensorOfDeviceUrl);

            //2、根据该API接口的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            apiPath = apiPath.Replace("{deviceid}", deviceId.ToString()).Replace("{apitag}", apiTag);//将API地址中的{projectid}替换成真实项目ID

            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();

            req.Method = HttpMethod.DELETE;
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity, Result>(apiPath, req);

            return(result);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 模糊查询设备
        /// </summary>
        /// <param name="query">查询参数</param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ResultMsg <ListPagerSet <DeviceBaseInfoDTO> > GetDevices(DeviceFuzzyQryPagingParas query, String token = null)
        {
            var result = new ResultMsg <ListPagerSet <DeviceBaseInfoDTO> >();

            var vry = TokenVerify(ref token);

            if (!vry.IsSuccess())
            {
                vry.CopyTo(result);
                return(result);
            }

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.Devices);

            //2、根据该API接口 的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            //apiPath = apiPath.Replace("{projectId}", projectId.ToString());//将API地址中的{gatewayTag}替换成真实设备标识
            apiPath += string.Format("?{0}&{1}", "PageSize=" + query.PageSize, "StartDate=" + query.StartDate, "EndDate=" + query.EndDate, "PageIndex=" + query.PageIndex);
            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();

            req.Method = HttpMethod.GET;
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity, ListPagerSet <DeviceBaseInfoDTO> >(apiPath, req);

            return(result);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 添加一个新设备
        /// </summary>
        /// <param name="device">提交数据</param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ResultMsg <Int32> AddDevice(DeviceAddUpdateDTO device, String token = null)
        {
            var result = new ResultMsg <Int32>();

            var vry = TokenVerify(ref token);

            if (!vry.IsSuccess())
            {
                vry.CopyTo(result);
                return(result);
            }

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.Devices);

            //2、根据该API接口 的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            //apiPath = apiPath.Replace("{projectId}", projectId.ToString());//将API地址中的{gatewayTag}替换成真实设备标识

            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();

            req.Method = HttpMethod.POST;
            req.Datas  = JsonFormatter.Serialize(device);
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity, Int32>(apiPath, req);

            return(result);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 添加一个新设备
        /// </summary>
        /// <param name="para"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ResultMsg <int> AddDevice(DeviceAddParas para, string token = null)
        {
            var result = new ResultMsg <int>();

            #region --ToUse--
            //if (String.IsNullOrEmpty(txtToken))
            //if (!vry.IsSuccess())
            //{
            //    vry.CopyTo(result);
            //    return result;
            //}
            #endregion

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.Devices);

            //2、根据该API接口 的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            //apiPath = apiPath.Replace("{projectId}", projectId.ToString());//将API地址中的{gatewayTag}替换成真实设备标识

            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();
            req.Method = HttpMethod.POST;
            req.Datas  = JsonFormatter.Serialize(para);
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity, int>(apiPath, req);

            return(result);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="data">请求数据</param>
        /// <param name="cookie">附加请求COOKIE</param>
        /// <param name="method">请求方式</param>
        /// <param name="timeout">允许超时间,单位(秒)</param>
        /// <returns></returns>
        public static ResultMsg <HttpResEntity> Http(string url, string data, string cookie, HttpMethod method, int timeout)
        {
            HttpReqEntity reqEntity = new HttpReqEntity()
            {
                Method  = method,
                Datas   = data,
                Cookies = cookie,
            };

            return(webRequest(url, reqEntity, timeout));
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 请求
        /// </summary>
        /// <param name="url">请求的URL</param>
        /// <param name="data">请求的包实体</param>
        /// <returns></returns>
        public static ResultMsg <string> Http(string url, HttpReqEntity data)
        {
            ResultMsg <string> result = new ResultMsg <string>();

            ResultMsg <HttpResEntity> resultMsg = Http(url, data, TIMEOUT);

            if (resultMsg.Status == ResultStatus.Success)
            {
                result.ResultObj = resultMsg.ResultObj.Bodys;
            }
            else
            {
                result.SetFailure(resultMsg.Msg);
            }

            return(result);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 聚合查询传感数据
        /// </summary>
        /// <returns></returns>
        public ResultMsg <SensorDataInfoDTO> GroupingSensorDatas(SensorDataJuHeQryPagingParas query, string token = null)
        {
            var result = new ResultMsg <SensorDataInfoDTO>();

            var vry = TokenVerify(ref token);

            if (!vry.IsSuccess())
            {
                vry.CopyTo(result);
                return(result);
            }

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}/grouping", mApiHost, NLECloudAPIUrl.DatasOfSensorUrl);

            //2、根据该API接口 的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            apiPath  = apiPath.Replace("{deviceid}", query.DeviceID.ToString());//将API地址中的{gatewayTag}替换成真实设备标识
            apiPath += string.Format("?{0}&{1}", "GroupBy=" + query.GroupBy, "Func=" + query.Func);
            apiPath += string.Format("&{0}&{1}&{2}", "ApiTags=" + query.ApiTags, "StartDate=" + query.StartDate, "EndDate=" + query.EndDate);
            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();

            req.Method = HttpMethod.GET;
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity, SensorDataInfoDTO>(apiPath, req);

            if (result.IsSuccess() && result.ResultObj != null && result.ResultObj.DataPoints != null)
            {
                foreach (SensorDataAddDTO p in result.ResultObj.DataPoints)
                {
                    if (p.PointDTO != null)
                    {
                        foreach (var w in p.PointDTO)
                        {
                            ValueConvertToByteAry(w.Value);
                        }
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 批量查询设备最新数据
        /// </summary>
        /// <param name="devids">设备ID用逗号隔开, 限制100个设备 </param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ResultMsg <List <DeviceSensorDataDTO> > GetDeviceNewestDatas(string devids, string token = null)
        {
            var result = new ResultMsg <List <DeviceSensorDataDTO> >();

            //验证
            //var vry = PrimaryKeyVerify(projectId, ref token);
            //if (!vry.IsSuccess())
            //{
            //    vry.CopyTo(result);
            //    return result;
            //}

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.DevicesDatasUrl);

            //2、根据该API接口的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            //apiPath = apiPath.Replace("{projectId}", projectId.ToString());//将API地址中的{projectid}替换成真实项目ID
            apiPath += string.Format("?{0}", "devIds=" + devids);
            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();

            req.Method = HttpMethod.GET;
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity, List <DeviceSensorDataDTO> >(apiPath, req);

            if (result.IsSuccess() && result.ResultObj != null)
            {
                result.ResultObj.ForEach(p =>
                {
                    if (p.Datas != null)
                    {
                        p.Datas.ForEach(w =>
                        {
                            w.Value = ValueConvertToByteAry(w.Value);
                        });
                    }
                });
            }

            return(result);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// 查询传感数据
        /// </summary>
        /// <returns></returns>
        public ResultMsg <SensorDataInfoDTO> GetDatasOfSensors(DatasFuzzyQryPagingParas query, string token = null)
        {
            var result = new ResultMsg <SensorDataInfoDTO>();

            #region --ToUse--
            //if (String.IsNullOrEmpty(txtToken))
            //if (!vry.IsSuccess())
            //{
            //    vry.CopyTo(result);
            //    return result;
            //}
            #endregion

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.DatasOfSensorUrl);

            //2、根据该API接口 的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            apiPath  = apiPath.Replace("{deviceid}", query.deviceId.ToString());//将API地址中的{gatewayTag}替换成真实设备标识
            apiPath += string.Format("?{0}&{1}&{2}&{3}&{4}&{5}", "deviceId=" + query.deviceId, "Method=" + query.Method, "TimeAgo=" + query.TimeAgo, "Sort=" + query.Sort, "PageSize=" + query.PageSize, "PageIndex=" + query.PageIndex);
            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();
            req.Method = HttpMethod.GET;
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity, SensorDataInfoDTO>(apiPath, req);

            if (result.IsSuccess() && result.ResultObj != null && result.ResultObj.DataPoints != null)
            {
                result.ResultObj.DataPoints.ForEach(p =>
                {
                    if (p.PointDTO != null)
                    {
                        foreach (var w in p.PointDTO)
                        {
                            ValueConvertToByteAry(w.Value);
                        }
                    }
                });
            }

            return(result);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// 模糊查询传感器
        /// </summary>
        /// <param name="deviceId">设备ID</param>
        /// <param name="apiTags">传感标识名</param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ResultMsg <IEnumerable <SensorBaseInfoDTO> > GetSensors(Int32 deviceId, String apiTags = "", String token = null)
        {
            var result = new ResultMsg <IEnumerable <SensorBaseInfoDTO> >();

            //验证
            var vry = PrimaryKeyVerify(deviceId, ref token);

            if (!vry.IsSuccess())
            {
                vry.CopyTo(result);
                return(result);
            }

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.SensorsOfDeviceUrl);

            //2、根据该API接口的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            apiPath  = apiPath.Replace("{deviceid}", deviceId.ToString());//将API地址中的{deviceId}替换成真实设备ID
            apiPath += string.Format("?{0}", "apitags=" + apiTags);
            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();

            req.Method = HttpMethod.GET;
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity, IEnumerable <SensorBaseInfoDTO> >(apiPath, req);

            if (result.IsSuccess() && result.ResultObj != null)
            {
                foreach (SensorBaseInfoDTO w in result.ResultObj)
                {
                    ValueConvertToByteAry(w.Value);
                }
            }

            return(result);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// 查询单个设备
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ResultMsg <DeviceInfoDTO> GetDeviceByDeviceId(int deviceId, string token = null)
        {
            var result = new ResultMsg <DeviceInfoDTO>();

            //验证
            var vry = PrimaryKeyVerify(deviceId, ref token);

            if (!vry.IsSuccess())
            {
                vry.CopyTo(result);
                return(result);
            }

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.DeviceUrl);

            //2、根据该API接口的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            apiPath = apiPath.Replace("{deviceId}", deviceId.ToString());//将API地址中的{projectid}替换成真实项目ID

            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();

            req.Method = HttpMethod.GET;
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity, DeviceInfoDTO>(apiPath, req);
            if (result.IsSuccess() && result.ResultObj != null && result.ResultObj.Sensors != null)
            {
                result.ResultObj.Sensors.ForEach(w =>
                {
                    w.Value = ValueConvertToByteAry(w.Value);
                });
            }

            return(result);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// 查询单个传感器
        /// </summary>
        /// <param name="deviceId">设备ID</param>
        /// <param name="apiTag">传感标识名</param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ResultMsg <SensorBaseInfoDTO> GetSensorInfo(Int32 deviceId, String apiTag, String token = null)
        {
            var result = new ResultMsg <SensorBaseInfoDTO>();

            //验证
            var vry = PrimaryKeyVerify(deviceId, ref token);

            if (!vry.IsSuccess())
            {
                vry.CopyTo(result);
                return(result);
            }

            //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知
            String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.SensorOfDeviceUrl);

            //2、根据该API接口的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中
            apiPath = apiPath.Replace("{deviceid}", deviceId.ToString()).Replace("{apitag}", apiTag);//将API地址中的{projectid}替换成真实项目ID

            //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去
            HttpReqEntity req = new HttpReqEntity();

            req.Method = HttpMethod.GET;
            req.Headers.Add("AccessToken", token);

            //4、定义该API接口返回的对象
            result = RequestAPIHelper.RequestServer <HttpReqEntity, SensorBaseInfoDTO>(apiPath, req, (json) =>
            {
                var qry = new ResultMsg <SensorBaseInfoDTO>();
                if (String.IsNullOrEmpty(json))
                {
                    return(qry.SetFailure("数据请求错误,返回对象为空!"));
                }

                JObject jObject = JObject.Parse(json);
                if (jObject["ResultObj"].HasValues && jObject["ResultObj"].SelectToken("Groups") != null)
                {
                    switch (jObject["ResultObj"]["Groups"].ToString())
                    {
                    case "1":
                        {
                            var tmp       = JsonFormatter.Deserialize <ResultMsg <SensorInfoDTO> >(json);
                            qry.ResultObj = tmp.ResultObj;
                        }
                        break;

                    case "2":
                        {
                            var tmp       = JsonFormatter.Deserialize <ResultMsg <ActuatorInfoDTO> >(json);
                            qry.ResultObj = tmp.ResultObj;
                        }
                        break;

                    case "3":
                        {
                            var tmp       = JsonFormatter.Deserialize <ResultMsg <CameraInfoDTO> >(json);
                            qry.ResultObj = tmp.ResultObj;
                        }
                        break;
                    }
                    return(qry);
                }
                else
                {
                    return(qry.SetFailure("数据请求错误,返回对象为空!"));
                }
            });

            if (result.IsSuccess() && result.ResultObj != null)
            {
                result.ResultObj.Value = ValueConvertToByteAry(result.ResultObj.Value);
            }

            return(result);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// 请求
        /// </summary>
        /// <param name="data">POST时的数据</param>
        /// <param name="url">地址</param>
        /// <param name="timeout">超时时间秒</param>
        /// <returns>返回HttpResEntity</returns>
        private static ResultMsg <HttpResEntity> webRequest(string url, HttpReqEntity data, int timeout)
        {
            ResultMsg <HttpResEntity> ret      = new ResultMsg <HttpResEntity>();
            HttpWebRequest            request  = null;
            HttpWebResponse           response = null;

            if (timeout <= 0)
            {
                timeout = TIMEOUT;
            }
            TimeoutTask timeoutTask = new TimeoutTask(
                delegate
            {
                try
                {
                    request             = (HttpWebRequest)WebRequest.Create(url);
                    request.ContentType = string.IsNullOrEmpty(data.ContentType) ? "application/json" : data.ContentType;
                    request.Method      = data.Method.ToString();


                    if (data.Headers != null && data.Headers.Count > 0)
                    {
                        request.Headers.Add(data.Headers);
                    }

                    Encoding encoding = data.Encoding == null ? Encoding.UTF8 : data.Encoding;
                    //if (data.Method != HttpMethod.GET)
                    {
                        if (!string.IsNullOrEmpty(data.Datas))
                        {
                            byte[] requestData    = encoding.GetBytes(data.Datas);
                            request.ContentLength = requestData.Length;
                            using (Stream newStream = request.GetRequestStream())
                            {
                                newStream.Write(requestData, 0, requestData.Length);
                            };
                        }
                        else
                        {
                            request.ContentLength = 0;
                        }
                    }

                    if (!string.IsNullOrEmpty(data.Cookies))
                    {
                        CookieContainer co = new CookieContainer();
                        string server      = GetHost(url, true);
                        co.SetCookies(new Uri("http://" + server), data.Cookies);
                        request.CookieContainer = co;
                    }

                    //Thread.Sleep(31000);
                    response = (HttpWebResponse)request.GetResponse();
                    HttpResEntity resEntity = new HttpResEntity();
                    using (StreamReader receiveStream = new StreamReader(response.GetResponseStream(), encoding))
                    {
                        resEntity.Bodys   = receiveStream.ReadToEnd();
                        resEntity.Cookies = response.Headers.Get("Set-Cookie");
                        resEntity.Headers = response.Headers;

                        ret.SetSuccess(resEntity);
                    };
                }
                catch (Exception ex)
                {
                    ret.SetFailure(String.Format("请求出错\r请求地址:{0}\r可能原因:{1}", url, ex.Message));
                }
                finally
                {
                    CloseRequest(request, response);
                }
            },
                delegate//超时后处理事件
            {
                CloseRequest(request, response);
                ret.SetFailure(string.Format("请求出错\r请求地址:{0}\r可能原因:{1}", url, "连接超时了,网络不通或服务端无响应"));
            });

            if (timeoutTask.Wait(timeout * 1000))//不超时
            {
                return(ret);
            }

            return(ret);
        }
Ejemplo n.º 20
0
 /// <summary>
 /// 请求
 /// </summary>
 /// <param name="url">请求的URL</param>
 /// <param name="data">请求的包实体</param>
 /// <param name="timeout">允许超时间,单位(秒)</param>
 /// <returns></returns>
 public static ResultMsg <HttpResEntity> Http(string url, HttpReqEntity data, int timeout)
 {
     return(webRequest(url, data, timeout));
 }