上两篇介绍了C#中获取华为云sdk和json封装,接上篇我们完成窗体部分
话不多说直接上代码
private void Form1_Load(object sender, System.EventArgs e)
{
this.txtPltIP.Text = "****************";
this.txtAppid.Text = "*************";
this.txtAppPwd.Text = "************";
this.txtCertPwd.Text = "*************";
this.txtCertFile.Text = "*************";
panel3.Visible = true;
}
//----------------------------数据获取及展示方法集-----------------------//
//***********************************************************************//
//***********************************************************************//
//-----------------------------------------------------------------------//
/*
* 功能: API获取oceanconnect数据及展示
* SDK封装了一部分,可查阅
* 调用: 参见具体方法
*/
NASDK currsdk = null;
//-----------------检查是否已获取token----------------------//
private bool check()
{
if (currsdk == null)
{
MessageBox.Show("请先获取token");
return false;
}
return true;
}
//----------------获取token并以textbox方法保存---------------------//
public void getToken()
{
this.txtToken.Text = "......";
currsdk = new NASDK(this.txtPltIP.Text, Convert.ToInt32(this.txtPort.Text), this.txtAppid.Text, this.txtAppPwd.Text, txtCertFile.Text, txtCertPwd.Text);
TokenResult token = currsdk.getToken();
if (token == null)
{
MessageBox.Show("获取失败,请看日志");
}
else //我改的
{
panel3.Visible = false;
MessageBox.Show("配置成功!");
this.txtToken.Text = token.accessToken;
// startThread_drawmap();
}
//else //原来的
//{
// panel3.Visible = false;
// MessageBox.Show("配置成功!");
// this.txtToken.Text = token.accessToken;
// startThread_drawmap();
//}
}
//------------------------获取数据方法-------------------------//
/*
* 原函数: 选择性获取历史数据
* 参数: 第几页, 每页记录数, 设备Id, 服务Id
* 返回值: 待确定.../已确定
* PS: pageno=0, pagesize为所需记录数. serviceId=null???
*/
public HistoryDataResult getHistoryData(int PageNo, int PageSize, string DeviceId, string ServiceId)
{
if (!check()) { return null; }//null
string startTime = dtpHDQueryStart.Value.ToString("yyyyMMddTHHmmssZ");
string endTime = dtpHDQueryEnd.Value.ToString("yyyyMMddTHHmmssZ");
int pageno; int pagesize;
try
{
pageno = Convert.ToInt16(PageNo);
pagesize = Convert.ToInt16(PageSize);
}
catch (Exception ex)
{
}
HistoryDataResult result = currsdk.queryHistoryData(txtToken.Text, DeviceId, DeviceId, ServiceId,"", PageNo, PageSize, startTime, endTime);
if (result == null)
{
MessageBox.Show("获取失败,请看日志");
return result; //result
}
return result;
}
//-----------------------解析数据方法, 暂时写在这, 不采取调用方式---------------//
/*
* 返回值: 数据数组5*5
*/
public int[][] transData(HistoryDataResult result)
{
//------------------------解析数据PS: 写成方法-----------------------------//
List<DeviceDataHistoryDTOsItem> str = result.deviceDataHistoryDTOs;
string result3 = "";
//str.count 为记录条数, 等于参数PageSzie
//str[i]为该条记录的第i个值
int[] Ins_01 = new int[5]; //存储第一条记录的五个值, 不用这种方法
int[][] Five_data = new int[5][];//我们要二维数组的形式
string[] TimeStamp = new string[5];//时间戳数组
Five_data[0] = new int[5];
Five_data[1] = new int[5];
Five_data[2] = new int[5];
Five_data[3] = new int[5];
Five_data[4] = new int[5];
for (int i = 0; i < str.Count; i++)//i是记录条数, j是该条记录下的第j个值
{
int j = 0;
foreach (KeyValuePair<string, string> item in str[i].data)
{
result3 = item.Key + ":" + item.Value + "\r\n";
Five_data[i][j++] = Convert.ToInt16(item.Value);
TimeStamp[i] = str[i].timestamp;//时间戳, 判断是否是最后一次的数据
//MessageBox.Show(result3);
}
}
//------------------------下拦截线-----------------------------//
return Five_data;
}
private void button7_Click(object sender, System.EventArgs e)
{
OpenFileDialog ofg = new OpenFileDialog();
if (ofg.ShowDialog() != DialogResult.OK)
{
return;
}
txtCertFile.Text = ofg.FileName;
}
//------------------参数配置完成----------------------------//
/*
private void button8_Click(object sender, System.EventArgs e)
{
getToken();
}
private void button10_Click(object sender, System.EventArgs e)
{
string deviceId = null;
//翻译成指定设备,继续添加
// if (this.comboBox5.Text.Equals("device_Lir_01"))
deviceId = All_Devices.Lir_deviceId2;
HistoryDataResult result = getHistoryData(0, 1, deviceId, null);//获取某一设备的最新1条数据
//transData(result);
//------------------------解析数据PS: 写成方法-----------------------------//
List<DeviceDataHistoryDTOsItem> str = result.deviceDataHistoryDTOs;
string result3 = "";
//str.count 为记录条数, 等于参数PageSzie
//str[i]为该条记录的第i个值
int[][] Five_data = new int[7][];//我们要二维数组的形式
Five_data[0] = new int[7];
Five_data[1] = new int[7];
Five_data[2] = new int[7];
Five_data[3] = new int[7];
Five_data[4] = new int[7];
Five_data[5] = new int[7];
Five_data[6] = new int[7];
string[] TimeStamp = new string[7];//时间戳数组
for (int i = 0; i < str.Count; i++)//i是记录条数, j是该条记录下的第j个值
{
int j = 0;
foreach (KeyValuePair<string, string> item in str[i].data)
{
result3 = item.Key + ":" + item.Value + "\r\n";
Five_data[i][j++] = Convert.ToInt16(item.Value);
//textBox1.Text = textBox1.Text + item.Value;
TimeStamp[i] = str[i].timestamp;//时间戳, 判断是否是最后一次的数据, 这里还要解析为时间
}
}
textBox1.Text = "人员" + Five_data[0][0].ToString();
textBox2.Text = "温度" + Five_data[0][1].ToString();
textBox3.Text = "湿度" + Five_data[0][2].ToString();
textBox4.Text = "烟雾" + Five_data[0][3].ToString();
textBox5.Text = "pm25" + Five_data[0][4].ToString();
textBox6.Text = "心率" + Five_data[0][5].ToString();
}
具体实现窗口
模拟设备数据上报
c#数据获取
总结:比如今天是2020,3月2号 开始时间就应该比3月2好早 结束时间比3月2号晚 不然获取数据为空