模块测试(三)----获取华为OceanConnect数据并以图表展示

版权声明:本文为博主原创文章,未经博主允许不得转载。https://github.com/SCFMVP https://blog.csdn.net/qq_37832932/article/details/81489701

一. 功能需求

    把NB设备发送给云平台的数据获取下来并以图表的形式展示

二. 实现

    先写一个华为OceanConnect的接口类SDK, 数据解析JSON类

直接上代码:

//代码来自于网络

namespace NADemo
{
    public class NASDK
    {
        public bool isHttp = false;
        string m_logfile = "wk_na.txt";
        string m_pltip;
        string m_appid;
        string m_appsecret;
        int m_pltPort;
        string m_p12certfile;
        string m_certpassword;
        public NASDK(string platformIP, int port, string appId, string appSecret, string p12cert, string certpassword)
        {
            this.m_pltip = platformIP;
            this.m_appid = appId;
            this.m_appsecret = appSecret;
            this.m_pltPort = port;
            this.m_p12certfile = p12cert;
            this.m_certpassword = certpassword;

        }
        //获取token
        public TokenResult getToken()
        {
            TokenResult result = null;

            string apiPath = "/iocm/app/sec/v1.1.0/login";
            string body = "appId=" + this.m_appid + "&secret=" + this.m_appsecret;
            string method = "POST";
            string contenttype = "application/x-www-form-urlencoded";
            WebHeaderCollection headers = new WebHeaderCollection();
            try
            {
                ApiResult apiresult = PostUrl(apiPath, body, headers, method, contenttype, this.m_p12certfile, this.m_certpassword);
                log(apiresult.statusCode.ToString() + apiresult.result);
                TokenResult tr = JsonConvert.DeserializeObject<TokenResult>(apiresult.result);
                result = tr;
            }
            catch (Exception ex)
            {
                log(ex.Message);
                log(ex.StackTrace);

                result = null;
            }
            return result;

        }
        //注册设备
        public RegDeviceResult regDevice(string token, string verifycode)
        {
            return regDevice(token, verifycode, verifycode, null, null, 0);
        }
        public RegDeviceResult regDevice(string token, string verifycode, string nodeid, string enduserid, string psk, int timeout)
        {
            RegDeviceResult result = null;
            string apiPath = "/iocm/app/reg/v1.2.0/devices?appId=" + this.m_appid;
            RegDeviceRequest rdr = new RegDeviceRequest();
            rdr.nodeId = nodeid;
            rdr.verifyCode = verifycode;
            rdr.timeout = timeout;
            string body = JsonConvert.SerializeObject(rdr);
            string method = "POST";
            string contenttype = "application/json";
            WebHeaderCollection headers = new WebHeaderCollection();
            headers.Add("app_key", this.m_appid);
            headers.Add("Authorization", "Bearer " + token);


            try
            {
                ApiResult apiresult = PostUrl(apiPath, body, headers, method, contenttype, this.m_p12certfile, this.m_certpassword);
                log(apiresult.statusCode.ToString() + apiresult.result);
                RegDeviceResult tr = JsonConvert.DeserializeObject<RegDeviceResult>(apiresult.result);
                if (tr.deviceId.Trim() != "")
                {
                    result = tr;
                }
            }
            catch (Exception ex)
            {
                log(ex.Message);
                log(ex.StackTrace);

                result = null;
            }
            return result;
        }

        //修改设备
        public string modifyDevice(string token, string deviceid, string mid, string devmodel)
        {
            string result = null;
            string apiPath = "/iocm/app/dm/v1.2.0/devices/" + deviceid + "?appId=" + this.m_appid;
            ModDeviceRequest mdr = new ModDeviceRequest();
            mdr.protocolType = "CoAP";
            mdr.manufacturerId = mid;
            mdr.model = devmodel;
            string body = JsonConvert.SerializeObject(mdr);
            string method = "PUT";
            string contenttype = "application/json";
            WebHeaderCollection headers = new WebHeaderCollection();
            headers.Add("app_key", this.m_appid);
            headers.Add("Authorization", "Bearer " + token);


            try
            {
                ApiResult apiresult = PostUrl(apiPath, body, headers, method, contenttype, this.m_p12certfile, this.m_certpassword);
                log(apiresult.statusCode.ToString() + apiresult.result);
                result = apiresult.statusCode.ToString();

            }
            catch (Exception ex)
            {
                log(ex.Message);
                log(ex.StackTrace);


                result = null;
            }
            return result;
        }
        //订阅消息
        public string subscribe(string token, string notifytype, string callbackurl)
        {
            string result = null;
            string apiPath = "/iocm/app/sub/v1.2.0/subscribe";

            string body = "{\"notifyType\":\"" + notifytype + "\",\"callbackurl\":\"" + callbackurl + "\"}";
            string method = "POST";
            string contenttype = "application/json";
            WebHeaderCollection headers = new WebHeaderCollection();
            headers.Add("app_key", this.m_appid);
            headers.Add("Authorization", "Bearer " + token);


            try
            {
                ApiResult apiresult = PostUrl(apiPath, body, headers, method, contenttype, this.m_p12certfile, this.m_certpassword);
                log(apiresult.statusCode.ToString() + apiresult.result);
                result = apiresult.statusCode.ToString();

            }
            catch (Exception ex)
            {
                log(ex.Message);
                log(ex.StackTrace);

                result = null;
            }
            return result;
        }
        //查询历史数据
        public HistoryDataResult queryHistoryData(string token, string deviceId, int count)
        {
            return queryHistoryData(token, deviceId, deviceId, "", "", 0, count, "", "");
        }
        public HistoryDataResult queryHistoryData(string token, string deviceId,string serviceid,int pageno,int pagesize,string starttime,string endtime)
        {
            return queryHistoryData(token, deviceId, deviceId,serviceid,"",pageno,pagesize,starttime,endtime);
        }

        public HistoryDataResult queryHistoryData(string token, string deviceId, string gatewayid, string serviceId, string property, int pageno, int pagesize, string starttime, string endtime)
        {
            HistoryDataResult result = null;
            string apiPath = string.Format("/iocm/app/data/v1.1.0/deviceDataHistory?deviceId={0}&gatewayId={1}&serviceId={2}&pageNo={3}&pageSize={4}&startTime={5}&endTime={6}&property={7}&appId={8}", deviceId, gatewayid, serviceId, pageno, pagesize, starttime, endtime, property, this.m_appid);

            string body = "";
            string method = "GET";
            string contenttype = "application/json";
            WebHeaderCollection headers = new WebHeaderCollection();
            headers.Add("app_key", this.m_appid);
            headers.Add("Authorization", "Bearer " + token);


            try
            {
                ApiResult apiresult = PostUrl(apiPath, body, headers, method, contenttype, this.m_p12certfile, this.m_certpassword);
                log(apiresult.statusCode.ToString() + apiresult.result);
                HistoryDataResult tr = JsonConvert.DeserializeObject<HistoryDataResult>(apiresult.result);

                result = tr;

            }
            catch (Exception ex)
            {
                log(ex.Message);
                log(ex.StackTrace);

                result = null;
            }
            return result;
        }
        //下发命令
        public string sendCommand(string token, string deviceId, string callbackurl, string serviceId, string commandId, List<CommandPara> lsParas)
        {
            string result = null;
            string apiPath = string.Format("/iocm/app/cmd/v1.4.0/deviceCommands?appId={0}", this.m_appid);

            SendCommandRequest scr = new SendCommandRequest();
            scr.deviceId = deviceId;
            scr.callbackUrl = callbackurl;
            scr.command = new Command();
            scr.command.method = commandId;
            scr.command.serviceId = serviceId;
            scr.command.paras = "#commandparas#";
            string body = JsonConvert.SerializeObject(scr);
            string parabody = "{";
            foreach (CommandPara item in lsParas)
            {
                if (item.isNum) { parabody += string.Format("\"{0}\":{1},", item.paraName,item.paraValue); } else {
                    parabody += string.Format("\"{0}\":\"{1}\",", item.paraName, item.paraValue);
                }
             
            }
            parabody= parabody.TrimEnd(',')+"}";
           

            body = body.Replace("\"#commandparas#\"", parabody);

            string method = "POST";
            string contenttype = "application/json";
            WebHeaderCollection headers = new WebHeaderCollection();
            headers.Add("app_key", this.m_appid);
            headers.Add("Authorization", "Bearer " + token);


            try
            {
                ApiResult apiresult = PostUrl(apiPath, body, headers, method, contenttype, this.m_p12certfile, this.m_certpassword);
                log(apiresult.statusCode.ToString() + apiresult.result);
               
                result = apiresult.statusCode.ToString();

            }
            catch (Exception ex)
            {
                log(ex.Message);
                log(ex.StackTrace);

                result = null;
            }
            return result;
        }
























        private void log(string msg)
        {
            File.AppendAllText(this.m_logfile, msg);
        }
        public class ApiResult
        {
            public int statusCode;
            public string result;
            public string errcode;
            public string memo;
        }
        private ApiResult PostUrl(string apiPath, string postData, WebHeaderCollection headers, string method, string contenttype, string p12certfile, string cerpassword)
        {
            string url = string.Format("https://{0}:{1}{2}", this.m_pltip, this.m_pltPort, apiPath);
            if (isHttp)
            {
                url = string.Format("http://{0}:{1}{2}", this.m_pltip, this.m_pltPort, apiPath);
            }
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
            HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(url);

            X509Certificate2 cerCaiShang = new X509Certificate2(p12certfile, cerpassword);
            if (!isHttp)
            {
                httpRequest.ClientCertificates.Add(cerCaiShang);
            }
            httpRequest.Method = method;
            httpRequest.ContentType = contenttype;
            httpRequest.Referer = null;
            httpRequest.AllowAutoRedirect = true;
            httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            httpRequest.Accept = "*/*";
            for (int i = 0; i < headers.Count; i++)
            {
                for (int j = 0; j < headers.GetValues(i).Length; j++)
                {
                    httpRequest.Headers.Add(headers.Keys[i], headers.GetValues(i)[j]);
                }

            }

            if (isHttp) { httpRequest.ServicePoint.Expect100Continue = false; }
            //
            if (method != "GET")
            {
                Stream requestStem = httpRequest.GetRequestStream();
                StreamWriter sw = new StreamWriter(requestStem);
                sw.Write(postData);
                sw.Close();
            }


            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

            Stream receiveStream = httpResponse.GetResponseStream();

            string result = string.Empty;
            using (StreamReader sr = new StreamReader(receiveStream))
            {
                result = sr.ReadToEnd();
            }
            ApiResult r = new ApiResult();
            r.result = result;
            r.statusCode = (int)httpResponse.StatusCode;

            return r;
        }
        public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)

        {

            if (sslPolicyErrors == SslPolicyErrors.None)

                return true;

            return true;

        }



    }
}

JSON解析类:

using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NADemo
{
    /// <summary>
    /// 设备profile:data1 int   data2 string(8,32)  ret1 int ret2 string(8,32) para1 para2 para12 para22  
    /// 0xf0上报0xff命令相应0x91 cmd1 0x92 cmd2
    /// </summary>
    public class TokenResult
    {
        /// <summary>
        /// 
        /// </summary>
        public string accessToken { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string tokenType { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string refreshToken { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int expiresIn { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string scope { get; set; }
    }

    public class RegDeviceRequest
    {
        /// <summary>
        /// 
        /// </summary>
        public string verifyCode { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string nodeId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int timeout { get; set; }
    }

    public class RegDeviceResult
    {
        /// <summary>
        /// 
        /// </summary>
        public string deviceId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string verifyCode { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int timeout { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string psk { get; set; }
    }


    public class ModDeviceRequest
    {
        /// <summary>
        /// 
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string endUser { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string manufacturerId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string model { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string protocolType { get; set; }
    }




    public class DeviceDataHistoryDTOsItem
    {
        /// <summary>
        /// 
        /// </summary>
        public string deviceId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string gatewayId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string appId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string serviceId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Dictionary<string, string> data { get; set; }
        public string DataString {
              get  {
                string result = "";
                foreach (KeyValuePair<string,string> item in data)
                {
                    result += item.Key + ":" + item.Value + "\r\n";
                }
                return result;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string timestamp { get; set; }
    }

    public class HistoryDataResult
    {
        /// <summary>
        /// 
        /// </summary>
        public int totalCount { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int pageNo { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int pageSize { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<DeviceDataHistoryDTOsItem> deviceDataHistoryDTOs { get; set; }
    }

    
    public class CommandPara
    {
        public string paraName { get; set; }
        public string paraValue { get; set; }
        public bool isNum { get; set; }
    }
    public class Command
    {
        /// <summary>
        /// 
        /// </summary>
        public string serviceId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string method { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string paras { get; set; }
    }

    public class SendCommandRequest
    {
        /// <summary>
        /// 
        /// </summary>
        public string deviceId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Command command { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string callbackUrl { get; set; }
    }


}

三. 效果展示

猜你喜欢

转载自blog.csdn.net/qq_37832932/article/details/81489701