唯倚社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 32|回复: 0

【原创】基于wpf的wiot1控制程序

[复制链接]

29

主题

167

帖子

2158

积分

LV3

Rank: 3Rank: 3

积分
2158
发表于 2018-4-22 21:06:02 | 显示全部楼层 |阅读模式

轻松玩转社区

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 fsw 于 2018-4-22 21:07 编辑

唯倚论坛首发,禁止转载!
今天我们来做一个基于wpf的wiot1控制程序
1.先进行布局,首先我选择tlink的http下行控制接口来发送和接受数据https://www.tlink.io/web/develop ... 8%AD%E5%BF%83#api_3,所以我们放置三个textbox和一个checkbox。
[C#] 纯文本查看 复制代码
<Window x:Class="weiecniot.MainWindow"
        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:weiecniot"
        mc:Ignorable="d"
        Title="MainWindow" Width="450.584" Height="310.583" SizeToContent="WidthAndHeight">
    <Grid Width="auto" Height="auto" Margin="20,5,20,25" >
        <Label Content="传感器ID:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
        <Label Content="设备序列号:" HorizontalAlignment="Left" Margin="10,40,0,0" VerticalAlignment="Top" Width="79"/>
        <Label Content="用户key:" HorizontalAlignment="Left" Margin="10,70,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="tb1" Height="25" Margin="89,10,10,0" TextWrapping="Wrap" Text="200153789" VerticalAlignment="Top" VerticalContentAlignment="Center"/>
        <TextBox x:Name="tb2" Height="25" Margin="89,40,10,0" TextWrapping="Wrap" Text="SG4GUR40N9GH0C6A" VerticalAlignment="Top" VerticalContentAlignment="Center"/>
        <TextBox x:Name="tb3" Height="25" Margin="89,70,10,0" TextWrapping="Wrap" Text="ddc7028843c441da8a54c8b5e9a9e429" VerticalAlignment="Top" VerticalContentAlignment="Center"/>
        <CheckBox x:Name="cb1" Content="开关" HorizontalAlignment="Left" Margin="14,100,0,0" VerticalAlignment="Top"/>
        <Label x:Name="tip" Content="" Margin="10,120,10,0" VerticalAlignment="Top"/>
    </Grid>
</Window>

2.在xaml代码界面里给checkbox创建一个点击事件,通过点击发送数据
[C#] 纯文本查看 复制代码
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
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.Navigation;
using System.Windows.Shapes;

namespace weiecniot
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            cb1.Click += Cb1_Click;




        }
        public string Post(string url, string content)
        {
            //申明一个容器result接收数据
            string result = "";
            //首先创建一个HttpWebRequest,申明传输方式POST
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.ReadWriteTimeout = 5000;
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";

            //添加POST参数
            byte[] data = Encoding.UTF8.GetBytes(content);
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }

            //申明一个容器resp接收返回数据
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容  
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }
        tlinkdata t;
        private void Cb1_Click(object sender, RoutedEventArgs e)
        {
            tip.Content = "Loading...";
            cb1.IsEnabled = false;
            t = new tlinkdata();
            t.deviceNo = tb2.Text;
            t.sensorId = tb1.Text;
            t.userKey = tb3.Text;
            t.value = cb1.IsChecked == true ? "1" : "0";
            Thread t1 = new Thread(PostDataToTlink);
            t1.Start();
         
        }

        bool isrun=false;
        public void PostDataToTlink()
        {
            if (isrun == true)
            {
               
                return;
            }

            isrun = true;
            string jsonData = JsonConvert.SerializeObject(t);
            //mtvjson = JsonConvert.DeserializeObject<MTVSearchJson>(json);
            string json = Post("http://api.tlink.io/tlink_interface/api/device/sendSwitchValue.htm", jsonData);
            var result = JsonConvert.DeserializeObject<tlinkresult>(json);
            Dispatcher.Invoke(new Action(() =>
            {
                cb1.IsEnabled = true;
                if (result != null)
                {
                    if (result is tlinkresult)
                        tip.Content = result.msg==""?"success!":result.msg;
                }
                
            }));
            isrun = false;
        }
    }
}

3.给所需要发送和接受的json数据分别建立一个实体类,这里我们可以在https://www.sojson.com/json2entity.html在线转换。
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace weiecniot
{


    public class tlinkdata
    {
        /// <summary>
        /// 设备序列号
        /// </summary>
        public string deviceNo { get; set; }
        /// <summary>
        /// 传感器ID
        /// </summary>
        public string sensorId { get; set; }
        /// <summary>
        /// 用戶key
        /// </summary>
        public string userKey { get; set; }
        /// <summary>
        /// 下发内容 开关型0或者1
        /// </summary>
        public string value { get; set; }
    }

}

[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace weiecniot
{
    public class tlinkresult
    {
        /// <summary>
        /// 00
        /// </summary>
        public string flag { get; set; }
        /// <summary>
        /// 操作成功!
        /// </summary>
        public string msg { get; set; }
    }

}

4.为了防止线程阻塞,需要新建一个线程,当选择复选框时,数据发送会在这个线程进行。
5.进一步保证程序的稳定性,在程序中添加标记,保证线程正在进行时不被干预
这样一来我们的wpf控制程序就写完了!
*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|weiecn ( 湘ICP备14002058号 )

GMT+8, 2018-5-25 20:52 , Processed in 0.073687 second(s), 10 queries , Redis On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表