Exemplo n.º 1
0
        public ReturnResult <ApiToken> RefreshAccessToken(int appId, string refreshToken)
        {
            var rst = new ReturnResult <ApiToken>();

            //根据AppId获取对应的应用服务器地址
            var appRst = new AppsBll().GetOne(new Apps()
            {
                Id = appId
            });

            if (appRst.Result)
            {
                var apps = appRst.Data;
                WebHeaderCollection headers = new WebHeaderCollection();
                headers.Add("app_key", apps.AppId);
                var tokenRst = GetDataFromAPI <TokenResult>(new RequestPara()
                {
                    ServerUrl   = apps.AppServer,
                    ApiPath     = "/iocm/app/sec/v1.1.0/refreshToken",
                    Method      = HttpClientActionMethod.POST,
                    ContentType = "application/json",
                    Headers     = headers,
                    PostData    = JsonHelper.Instance.Serialize(new
                    {
                        appId  = apps.AppId,
                        secret = apps.Secret,
                        refreshToken
                    }),
                    CertFile     = ApiConfig.CertFile,
                    CertPassword = ApiConfig.CertPassword
                });

                if (tokenRst.Result && !string.IsNullOrEmpty(tokenRst.Data.accessToken))
                {
                    //添加到数据库
                    var model = new ApiToken()
                    {
                        AppId                   = appId,
                        AccessToken             = tokenRst.Data.accessToken,
                        AccessTokenCreateTime   = DateTime.Now.AddMinutes(-2),
                        AccessTokenExpiresIn    = tokenRst.Data.expiresIn,
                        RefreshToken            = tokenRst.Data.refreshToken,
                        RefreshTokenExpiresTime = DateTime.Now.AddDays(1)
                    };
                    var bll    = new ApiTokenBll();
                    var addRst = bll.Update(model);
                    if (addRst.Result)
                    {
                        //刷新成功
                        rst.Result  = true;
                        rst.Data    = model;
                        rst.Message = tokenRst.Message;
                    }
                }
            }

            return(rst);
        }
Exemplo n.º 2
0
        public string GetList(Apps model)
        {
            var rst = new ReturnResult <List <Apps> >();
            var bll = new AppsBll();

            rst = bll.GetList(model);

            return(JsonHelper.Instance.Serialize(rst));
        }
Exemplo n.º 3
0
        public string Del(Apps model)
        {
            var rst = new ReturnResult <bool>();
            var bll = new AppsBll();

            rst = bll.Delete(model);

            return(JsonHelper.Instance.Serialize(rst));
        }
Exemplo n.º 4
0
        public ReturnResult <ApiToken> Login(int appId)
        {
            var rst = new ReturnResult <ApiToken>();
            //根据AppId获取对应的应用服务器地址
            var appRst = new AppsBll().GetOne(new Apps()
            {
                Id = appId
            });

            if (appRst.Result)
            {
                var apps = appRst.Data;

                var tokenRst = GetDataFromAPI <TokenResult>(new RequestPara()
                {
                    ServerUrl    = apps.AppServer,
                    ApiPath      = "/iocm/app/sec/v1.1.0/login",
                    Method       = HttpClientActionMethod.POST,
                    ContentType  = "application/x-www-form-urlencoded",
                    PostData     = $"appId={apps.AppId}&secret={apps.Secret}",
                    CertFile     = ApiConfig.CertFile,
                    CertPassword = ApiConfig.CertPassword
                });

                if (tokenRst.Result && !string.IsNullOrEmpty(tokenRst.Data.accessToken))
                {
                    //添加到数据库
                    var model = new ApiToken()
                    {
                        AppId                   = appId,
                        AccessToken             = tokenRst.Data.accessToken,
                        AccessTokenCreateTime   = DateTime.Now.AddMinutes(-2),
                        AccessTokenExpiresIn    = tokenRst.Data.expiresIn,
                        RefreshToken            = tokenRst.Data.refreshToken,
                        RefreshTokenExpiresTime = DateTime.Now.AddMonths(1).AddDays(-1)
                    };
                    var bll    = new ApiTokenBll();
                    var addRst = bll.Add(model);
                    if (addRst.Result)
                    {
                        //添加成功
                        rst.Result  = true;
                        rst.Data    = model;
                        rst.Message = tokenRst.Message;
                    }
                }
            }

            return(rst);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 2.7.2 查询设备指令
        /// </summary>
        /// <param name="pageNo">(可选)查询的页码,大于等于 0,默认值:0。</param>
        /// <param name="pageSize">(可选)查询每页信息的数量</param>
        /// <param name="deviceId">(可选)指定查询命令的设备 ID</param>
        /// <param name="startTime">(可选)查询开始时间  格式: yyyyMMdd'T'HHmmss'Z',如20151212T121212Z</param>
        /// <param name="endTime">(可选)查询结束时间</param>
        /// <param name="appId">数据库主键</param>
        /// <returns></returns>
        public ReturnResult <SelectDeviceResult> selectDeviceResult(int pageNo, int pageSize, string deviceId, string startTime, string endTime, int appId)
        {
            var sdr = new ReturnResult <SelectDeviceResult>();
            //获取token
            var tokenRst = new TokenApi().GetAccessToken(appId);
            var token    = tokenRst.Data.AccessToken;

            if (string.IsNullOrEmpty(token))
            {
                sdr.Message = "获取token失败";
                return(sdr);
            }

            var appRst = new AppsBll().GetOne(new Apps()
            {
                Id = appId
            });

            if (appRst.Result)
            {
                var apps = appRst.Data;
                WebHeaderCollection webHeaderCollection = new WebHeaderCollection();
                webHeaderCollection.Add("app_key", apps.AppId);
                webHeaderCollection.Add("Authorization", "Bearer" + token);

                //添加可选参数
                StringBuilder param = null;
                if (pageNo >= 0 || (pageSize >= 1 && pageSize <= 1000) ||
                    string.IsNullOrEmpty(deviceId) || string.IsNullOrEmpty(startTime) ||
                    string.IsNullOrEmpty(endTime) || string.IsNullOrEmpty(apps.AppId))
                {
                    param.Append("?");
                    param.Append($"pageNo={pageNo}&pageSize={pageSize}&deviceId={deviceId}&startTime={startTime}&endTime={endTime}&appId={apps.AppId}");
                }
                //发送且返回结果集
                sdr = GetDataFromAPI <SelectDeviceResult>(new RequestPara()
                {
                    ServerUrl    = apps.AppServer,
                    Method       = HttpClientActionMethod.GET,
                    ContentType  = "application/json",
                    ApiPath      = "/iocm/app/cmd/v1.4.0/deviceCommands" + param,
                    Headers      = webHeaderCollection,
                    CertFile     = ApiConfig.CertFile,
                    CertPassword = ApiConfig.CertPassword
                });
            }
            return(sdr);
        }
Exemplo n.º 6
0
        public ReturnResult <ApiResult> UpdateDevice(int appId, string deviceId, string name, Profile profile, string protocolType)
        {
            var rst = new ReturnResult <ApiResult>();

            //根据AppId获取对应的应用服务器地址
            var appRst = new AppsBll().GetOne(new Apps()
            {
                Id = appId
            });

            if (appRst.Result)
            {
                var apps = appRst.Data;

                var tokenRst = new TokenApi().GetAccessToken(appId);
                var token    = tokenRst.Data.AccessToken;
                if (string.IsNullOrEmpty(token))
                {
                    return(rst);
                }
                WebHeaderCollection headers = new WebHeaderCollection();
                headers.Add("app_key", apps.AppId);
                headers.Add("Authorization", "Bearer " + token);
                rst = GetDataFromAPI <ApiResult>(new RequestPara()
                {
                    ServerUrl   = apps.AppServer,
                    ApiPath     = $"/iocm/app/dm/v1.4.0/devices/{deviceId}?appId={apps.AppId}",
                    Method      = HttpClientActionMethod.PUT,
                    ContentType = "application/json",
                    Headers     = headers,
                    PostData    = JsonHelper.Instance.Serialize(new
                    {
                        appId = apps.AppId,
                        deviceId,
                        name,
                        manufacturerId   = profile.ManufacturerId,
                        manufacturerName = profile.ManufacturerName,
                        model            = profile.Model,
                        deviceType       = profile.DeviceType,
                        protocolType
                    }),
                    CertFile     = ApiConfig.CertFile,
                    CertPassword = ApiConfig.CertPassword
                });
            }

            return(rst);
        }
Exemplo n.º 7
0
        public string Add(Apps model)
        {
            var rst = new ReturnResult <bool>();
            var bll = new AppsBll();

            if (model.Id == 0)
            {
                rst = bll.Add(model);
            }
            else
            {
                rst = bll.Update(model);
            }

            return(JsonHelper.Instance.Serialize(rst));
        }
Exemplo n.º 8
0
        /// <summary>
        /// 2.7.4 创建设备命令撤销任务
        /// </summary>
        /// <param name="appid">表主键</param>
        /// <param name="deviceId">待撤销设备命令的设备 ID</param>
        /// <returns></returns>
        public ReturnResult <CreateDevCommCancel> createDeviceCommandCancel(int appid, string deviceId)
        {
            var ccc = new ReturnResult <CreateDevCommCancel>();
            //获取token
            var tokenRst = new TokenApi().GetAccessToken(appid);
            var token    = tokenRst.Data.AccessToken;

            if (string.IsNullOrEmpty(token))
            {
                ccc.Message = "获取token失败";
                return(ccc);
            }
            var appRst = new AppsBll().GetOne(new Apps()
            {
                Id = appid
            });

            if (appRst.Result)
            {
                var apps = appRst.Data;
                WebHeaderCollection webHeaderCollection = new WebHeaderCollection();
                webHeaderCollection.Add("app_key", apps.AppId);
                webHeaderCollection.Add("Authorization", "Bearer" + token);

                //添加可选参数
                StringBuilder param = new StringBuilder();
                if (!string.IsNullOrEmpty(appRst.Data.AppId))
                {
                    param.Append($"?appId={appRst.Data.AppId}");
                }
                //发送并返回结果集
                ccc = GetDataFromAPI <CreateDevCommCancel>(new RequestPara()
                {
                    ServerUrl   = apps.AppServer,
                    Method      = HttpClientActionMethod.PUT,
                    ContentType = "application/json",
                    ApiPath     = "/iocm/app/cmd/v1.4.0/deviceCommandCancelTasks" + param,
                    Headers     = webHeaderCollection,
                    PostData    = JsonHelper.Instance.Serialize(new
                    {
                        deviceId, //撤销
                    })
                });
            }
            return(ccc);
        }
Exemplo n.º 9
0
        public ReturnResult <RegDeviceResult> RegDevice(RegDeviceRequest model)
        {
            var rst = new ReturnResult <RegDeviceResult>();

            //根据AppId获取对应的应用服务器地址
            var appRst = new AppsBll().GetOne(new Apps()
            {
                Id = model.appId
            });

            if (appRst.Result)
            {
                var apps = appRst.Data;

                var tokenRst = new TokenApi().GetAccessToken(model.appId);
                var token    = tokenRst.Data.AccessToken;
                if (string.IsNullOrEmpty(token))
                {
                    return(rst);
                }
                WebHeaderCollection headers = new WebHeaderCollection();
                headers.Add("app_key", apps.AppId);
                headers.Add("Authorization", "Bearer " + token);
                rst = GetDataFromAPI <RegDeviceResult>(new RequestPara()
                {
                    ServerUrl   = apps.AppServer,
                    ApiPath     = $"/iocm/app/reg/v1.2.0/devices?appId={apps.AppId}",
                    Method      = HttpClientActionMethod.POST,
                    ContentType = "application/json",
                    Headers     = headers,
                    PostData    = JsonHelper.Instance.Serialize(new
                    {
                        //endUserId,
                        model.verifyCode,
                        model.nodeId,
                        //psk,
                        model.timeout
                    }),
                    CertFile     = ApiConfig.CertFile,
                    CertPassword = ApiConfig.CertPassword
                });
            }

            return(rst);
        }
Exemplo n.º 10
0
        public ReturnResult <SubScriptionBusinessRequest> SubscriptionBusinessData(int appId, string notifyType, string callbackUrl, bool?ownerFlag = true)
        {
            var rst = new ReturnResult <SubScriptionBusinessRequest>();
            //根据AppId获取对应的应用服务器地址
            var appRst = new AppsBll().GetOne(new Apps()
            {
                Id = appId
            });

            if (appRst.Result)
            {
                var apps = appRst.Data;

                var tokenRst = new TokenApi().GetAccessToken(appId);
                var token    = tokenRst.Data.AccessToken;
                if (string.IsNullOrEmpty(token))
                {
                    return(rst);
                }
                WebHeaderCollection headers = new WebHeaderCollection();
                headers.Add("app_key", apps.AppId);
                headers.Add("Authorization", "Bearer " + token);
                rst = GetDataFromAPI <SubScriptionBusinessRequest>(new RequestPara()
                {
                    ServerUrl   = apps.AppServer,
                    ApiPath     = $"/iocm/app/sub/v1.2.0/subscriptions?ownerFlag={ownerFlag}",
                    Method      = HttpClientActionMethod.POST,
                    ContentType = "application/json",
                    Headers     = headers,
                    PostData    = JsonHelper.Instance.Serialize(new
                    {
                        appId = apps.AppId,
                        notifyType,
                        callbackUrl
                    }),
                    CertFile     = ApiConfig.CertFile,
                    CertPassword = ApiConfig.CertPassword
                });
            }

            return(rst);
        }
Exemplo n.º 11
0
        /// <summary>
        ///  2.7.1 命令下发
        /// </summary>
        /// <param name="scr"></param>
        /// <param name="appId"></param>
        /// <returns></returns>
        public ReturnResult <DeviceCommandResp> DeviceCommands(SendCommandRequest scr, int appId)
        {
            var rst = new ReturnResult <DeviceCommandResp>();

            var tokenRst = new TokenApi().GetAccessToken(appId);
            var token    = tokenRst.Data.AccessToken;

            if (string.IsNullOrEmpty(token))
            {
                rst.Message = "获取token失败";
                return(rst);
            }

            //根据AppId获取对应的应用服务器地址
            var appRst = new AppsBll().GetOne(new Apps()
            {
                Id = appId
            });

            if (appRst.Result)
            {
                var apps = appRst.Data;
                WebHeaderCollection webHeaderCollection = new WebHeaderCollection();
                webHeaderCollection.Add("app_key", apps.AppId);
                webHeaderCollection.Add("Authorization", "Bearer " + token);
                rst = GetDataFromAPI <DeviceCommandResp>(new RequestPara()
                {
                    ServerUrl    = apps.AppServer,
                    Method       = HttpClientActionMethod.POST,
                    ContentType  = "application/json",
                    ApiPath      = "/iocm/app/cmd/v1.4.0/deviceCommands",
                    Headers      = webHeaderCollection,
                    PostData     = JsonHelper.Instance.Serialize(scr),
                    CertFile     = ApiConfig.CertFile,
                    CertPassword = ApiConfig.CertPassword
                });
            }

            return(rst);
        }
Exemplo n.º 12
0
        public ReturnResult <ApiResult> DelDevice(int appId, string deviceId)
        {
            var rst = new ReturnResult <ApiResult>();

            //根据AppId获取对应的应用服务器地址
            var appRst = new AppsBll().GetOne(new Apps()
            {
                Id = appId
            });

            if (appRst.Result)
            {
                var apps = appRst.Data;

                var tokenRst = new TokenApi().GetAccessToken(appId);
                var token    = tokenRst.Data.AccessToken;
                if (string.IsNullOrEmpty(token))
                {
                    return(rst);
                }
                WebHeaderCollection headers = new WebHeaderCollection();
                headers.Add("app_key", apps.AppId);
                headers.Add("Authorization", "Bearer " + token);
                rst = GetDataFromAPI <ApiResult>(new RequestPara()
                {
                    ServerUrl    = apps.AppServer,
                    ApiPath      = $"/iocm/app/dm/v1.4.0/devices/{deviceId}?appId={apps.AppId}",
                    Method       = HttpClientActionMethod.DELETE,
                    ContentType  = "application/json",
                    Headers      = headers,
                    CertFile     = ApiConfig.CertFile,
                    CertPassword = ApiConfig.CertPassword
                });
            }

            return(rst);
        }