紧接着C#连接的云平台。
一、api介绍
api程序集
using NLECloudSDK;
using NLECloudSDK.Model;
using System.Collections.Generic;
namespace NLECloudSDK
{
public class NLECloudAPI
{
public NLECloudAPI(string serverUrl);
public ResultMsg<int> AddDevice(DeviceAddUpdateDTO device, string token = null);
public ResultMsg<int> AddSensor<T>(int deviceId, T sensor, string token = null) where T : SensorAddUpdateBase, new();
public Result AddSensorDatas(int deviceId, SensorDataListAddDTO data, string token = null);
public Result Cmds(int deviceId, string apiTag, object data, string token = null);
public Result DeleteDevice(int deviceId, string token = null);
public Result DeleteSensor(int deviceId, string apiTag, string token = null);
public ResultMsg<DeviceInfoDTO> GetDeviceInfo(int deviceId, string token = null);
public ResultMsg<ListPagerSet<DeviceBaseInfoDTO>> GetDevices(DeviceFuzzyQryPagingParas query, string token = null);
public ResultMsg<IEnumerable<DeviceSensorDataDTO>> GetDevicesDatas(string devids, string token = null);
public ResultMsg<IEnumerable<OnlineDataDTO>> GetDevicesStatus(string devids, string token = null);
public ResultMsg<ProjectInfoDTO> GetProjectInfo(int projectId, string token = null);
public ResultMsg<ListPagerSet<ProjectInfoDTO>> GetProjects(ProjectFuzzyQryPagingParas query, string token = null);
public ResultMsg<IEnumerable<SensorBaseInfoDTO>> GetProjectSensors(int projectId, string token = null);
public ResultMsg<SensorDataInfoDTO> GetSensorDatas(SensorDataFuzzyQryPagingParas query, string token = null);
public ResultMsg<SensorBaseInfoDTO> GetSensorInfo(int deviceId, string apiTag, string token = null);
public ResultMsg<IEnumerable<SensorBaseInfoDTO>> GetSensors(int deviceId, string apiTags = "", string token = null);
public ResultMsg<SensorDataInfoDTO> GroupingSensorDatas(SensorDataJuHeQryPagingParas query, string token = null);
public Result UpdateDevice(int deviceId, DeviceAddUpdateDTO device, string token = null);
public Result UpdateSensor<T>(int deviceId, string apiTag, T sensor, string token = null) where T : SensorAddUpdateBase, new();
public ResultMsg<AccountLoginResultDTO> UserLogin(AccountLoginDTO submitData);
}
}
- 获取传感器数值的api:
public ResultMsg<SensorBaseInfoDTO> GetSensorInfo(int deviceId, string apiTag, string token = null);
示例:
var sdk = new NLECloudAPI("http://api.nlecloud.com");
var qry = sdk.GetSensorInfo(9969, "DefenseState", TempInfo.Token);//输入设备ID,传感器标识名,accessToken
- 控制命令api
public Result Cmds(int deviceId, string apiTag, object data, string token = null);
var sdk = new NLECloudAPI("http://api.nlecloud.com");
var qry = SDK.Cmds(9969, "defense", 1, TempInfo.Token);//输入设备ID,传感器标识名,命令值,accessToken
二、编写代码
- 添加新窗口
- 布局:拉出相应的控件就行,使用到的控件有label、textBox、button。
xaml代码
<Window x:Class="ConnectToCloud.Main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ConnectToCloud"
mc:Ignorable="d"
Title="Menu" Height="450" Width="800">
<Grid>
<Label Content="设备ID:" HorizontalAlignment="Left" Margin="85.795,58.714,0,0" VerticalAlignment="Top"/>
<Label Content="数据值:" HorizontalAlignment="Left" Margin="86.461,153.129,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="tb_deviceId" HorizontalAlignment="Left" Height="23" Margin="149.462,58.714,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="86394"/>
<Button x:Name="btn_getValue" Content="获取数据值" HorizontalAlignment="Left" Margin="149.462,109.754,0,0" VerticalAlignment="Top" Width="75" Click="btn_getValue_Click"/>
<Button x:Name="btn_open" Content="开" HorizontalAlignment="Left" Margin="457.347,109.754,0,0" VerticalAlignment="Top" Width="75" Click="btn_open_Click"/>
<Label Content="传感器标识名:" HorizontalAlignment="Left" Margin="363.347,58.714,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="tb_apiTag" HorizontalAlignment="Left" Height="23" Margin="457.347,58.714,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="temperature"/>
<Label x:Name="lb_sensorValue" Content="正在获取" HorizontalAlignment="Left" Margin="166.462,153.129,0,0" VerticalAlignment="Top"/>
<Button x:Name="btn_close" Content="关" HorizontalAlignment="Left" Margin="567.429,109.754,0,0" VerticalAlignment="Top" Width="75" Click="btn_close_Click"/>
</Grid>
</Window>
- .cs代码,编写点击事件,调用SDK中的api就行
using NLECloudSDK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ConnectToCloud
{
/// <summary>
/// Main.xaml 的交互逻辑
/// </summary>
public partial class Main : Window
{
public Main()
{
InitializeComponent();
}
private void btn_getValue_Click(object sender, RoutedEventArgs e)
{
String deviceId = tb_deviceId.Text.Trim();
var apiTag = tb_apiTag.Text.Trim();
if (deviceId.Equals(""))
MessageBox.Show("请输入设备Id");
else
{
if(apiTag.Equals(""))
MessageBox.Show("请输入api名称");
else
getSensorValue(int.Parse(deviceId),apiTag);
}
}
private void btn_open_Click(object sender, RoutedEventArgs e)
{
String deviceId = tb_deviceId.Text.Trim();
var apiTag = tb_apiTag.Text.Trim();
if (deviceId.Equals(""))
MessageBox.Show("请输入设备Id");
else
{
if (apiTag.Equals(""))
MessageBox.Show("请输入api名称");
else
sendCmd(int.Parse(deviceId),apiTag,1);
}
}
private void btn_close_Click(object sender, RoutedEventArgs e)
{
String deviceId = tb_deviceId.Text.Trim();
var apiTag = tb_apiTag.Text.Trim();
if (deviceId.Equals(""))
MessageBox.Show("请输入设备Id");
else
{
if (apiTag.Equals(""))
MessageBox.Show("请输入api名称");
else
sendCmd(int.Parse(deviceId), apiTag,0);
}
}
private void getSensorValue(int deviceId, String apiTag)
{
var sdk = new NLECloudAPI("http://api.nlecloud.com");
var qry = sdk.GetSensorInfo(deviceId, apiTag, TempInfo.AccessToken); //deviceId为设备Id,mToken为登录凭据
lb_sensorValue.Content = qry.ResultObj.Value;
MessageBox.Show(qry.ResultObj.RecordTime, "最后的获取时间");
}
private void sendCmd(int deviceId, string apiTag,int type)
{
var sdk = new NLECloudAPI("http://api.nlecloud.com");
var qry = sdk.Cmds(deviceId, apiTag, type, TempInfo.AccessToken);
if(qry.Status == ResultStatus.Success)
MessageBox.Show("成功发送控制指令!");
else if(qry.Status == ResultStatus.Failure)
MessageBox.Show("发送指令失败,可能是设备未上线!");
}
}
}