概述
C# 物联网开发API接口系列(四)
#久爱物联网#MQTT#
【提纲】
1)获取设备列表(某用户的)
2)单独检查设备是否存在合法
【正文】
/// /// 获取设备列表 /// /// 令牌 /// private string getDevList(string _token) { string result = ""; int listnum = 0; StringBuilder sb = new StringBuilder(); try { if (SqlHelper.isConnected) { SqlConnection SqlConn = new SqlConnection(SqlHelper.ConnString); if (SqlConn.State != ConnectionState.Connecting) { SqlConn.Open();// 打开数据库连接 } //注意:参数是token查询用的是 uid 之前有一方法已经调用了 uid = getUidbyToken(token); SqlCommand sqlCmd = new SqlCommand("SELECT * FROM box_List WHERE bForUid=" + uid, SqlConn); SqlDataReader rd = sqlCmd.ExecuteReader();//返回的是记录集 if (rd.HasRows) { while (rd.Read()) { listnum++; sb.AppendFormat("{" + string.Format(""regpacket":"{0}","boxname":"{1}","actived":{2}", rd["bRegpacket"].ToString(), rd["bName"].ToString(),Convert.ToInt16(rd["bStatus"])) + "}");//可继续扩展其他属性 sb.AppendFormat(",");//最后一条需要处理此符号 } sb.Remove(sb.ToString().LastIndexOf(','), 1);//处理最后一位符号(如何处理最后一个逗号) } rd.Close();//统一释放资源 rd = null; sqlCmd = null; SqlConn.Close();//关闭数据库连接 SqlConn = null;//否则不更新原内容 } //当data为数组时加[] 非数组及json格式写法 if(listnum>0) result = string.Format(""data":[{0}],"code":{1},"time":"{2}"", sb.ToString().Replace("{", "{").Replace("}", "}"), 200, svrNowTime()); else result = string.Format(""data":"{0}","code":{1},"time":"{2}"", "请检查参数的合法性:" + _token, 0, svrNowTime()); } catch (Exception ex) { result = string.Format(""data":"[ERROR]:{0}","code":{1},"time":"{2}"", ex.Message.ToString(), -1, DateTime.Now); } return "{"+result+"}";//组合成标准的json格式的头尾 } /// /// 验证智能模块的合法性 /// /// 注册包 /// private string checkDevValidity(string reg) { string _retu = ""; try { SqlConnection SqlConn = new SqlConnection(SqlHelper.ConnString); if (SqlConn.State != ConnectionState.Connecting) { SqlConn.Open();// 打开数据库连接 } //bStatus 1启用0禁用-1列黑 SqlCommand sqlCmd = new SqlCommand("SELECT * FROM box_List WHERE bStatus=1 AND bForUid=" + uid + " AND bRegpacket='" + reg + "'", SqlConn); SqlDataReader rd = sqlCmd.ExecuteReader(); if (rd.HasRows) { rd.Read(); _retu = string.Format(""data":"[OK]{0}","code":{1},"time":"{2}"", rd["bRegpacket"], 200, DateTime.Now); } else { _retu = string.Format(""data":"[SORRY]{0}","code":{1},"time":"{2}"", "验证失败,请检查参数是否正确!", 0, DateTime.Now); } } catch (Exception ex) { _retu = string.Format(""data":"[ERROR]:{0}","code":{1},"time":"{2}"", ex.Message.ToString(), -1, DateTime.Now); } return "{" + _retu + "}"; //重新组合标准json格式头尾{} } //通过token获取uid private int getUidbyToken(string tok) { int _uid; try { SqlConnection SqlConn = new SqlConnection(SqlHelper.ConnString); if (SqlConn.State != ConnectionState.Connecting) { SqlConn.Open();// 打开数据库连接 } SqlCommand sqlCmd = new SqlCommand("SELECT top 1 uid,fuid,uToken,uTel,uName FROM user_List WHERE uToken='" + tok + "' AND uStatus=1", SqlConn);//uRole=8 SqlDataReader rd = sqlCmd.ExecuteReader();//返回的是记录集 AND uStatus=1 if (rd.HasRows) { if (rd.Read()) { uid = Convert.ToInt16(rd["uid"]);//***主要查询这个 fuid = Convert.ToInt16(rd["fuid"]); utoken = rd["uToken"].ToString(); utel = rd["uTel"].ToString(); uname = rd["uName"].ToString(); // ujson = "{"uid":" + uid + ","fuid":" + fuid + ","utoken":"" + utoken + "","uname":"" + uname + "","utel":"" + utel + ""}"; } _uid = uid;//返回这个 } else { _uid = -1;//没找到 } } catch (Exception ex) { _uid = 0;//错误异常时0 } return _uid; }
到此为止,主要接口及方法已经讲解一遍!举一反三,触类旁通,学会学习 就很快了!
下一步,将接口部署到服务器下,进行访问即可!
在管理客户端调用接口,根据返回json数据进行解析(了解json前端知识ajax)
进行各种操作即可!
有机会时间 会继续分享如何前端验证(ajax)解析数据并展现到页面上(H5)
最后
以上就是坦率溪流为你收集整理的web api接口开发实例_C# 物联网开发API接口系列(4)的全部内容,希望文章能够帮你解决web api接口开发实例_C# 物联网开发API接口系列(4)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复