using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Reflection;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Security.Cryptography;
namespace user
{
public partial class FormIOT : Form
{
private static log4net.ILog log = log4net.LogManager.GetLogger(typeof(FormIOT));
string strToken = “”;
public static string access_token = “”;
public static DateTime token_last_time;
public FormIOT()
{
InitializeComponent();
}
private void bt_sent_Click(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(this.tb_content.Text.Trim()))
{
if (string.IsNullOrEmpty(access_token))
{
getToken();
}
if (!string.IsNullOrEmpty(access_token))
{
updateLedContent(this.tb_content.Text.Trim(), access_token);
}
else
{
MessageBox.Show("Token为空");
}
}
else
{
MessageBox.Show("请输入内容!");
this.tb_content.Focus();
}
}
/// <summary>
/// 获得Token
/// </summary>
public static void getToken()
{
string href = "http://localhost/api/v1/login";
LoginInfo user = new LoginInfo();
user.account = "admin";
user.password = "admin";
string json = JsonConvert.SerializeObject(user);
string result = GetResponse(href, null, json);
FirstResult<DataResult> uresult = DeserializeJsonToObject<FirstResult<DataResult>>(result);
//成功
if (uresult.status == 0)
{
access_token = uresult.data.token;
token_last_time = DateTime.Now;
}
}
/// <summary>
/// 获得Token
/// </summary>
public static void updateLedContent(string content,string token)
{
string url = "https://www.bekingai.com";
string result = GetResponse(url, token, null);
FirstResult<DataResult> uresult = DeserializeJsonToObject<FirstResult<DataResult>>(result);
//成功
if (uresult.status == 0)
{
//获取推送的url
url = uresult.data.commandServerAddr;
string deviceSN = "104238";
string href = "/v1.0/tranion/" + deviceSN;
CommandInfo command_content = new CommandInfo();
byte[] b = GetByteArray(getLEDCommandString(content));
转成 Base64 形式的 System.String
//command_content.data = content;
string json = JsonConvert.SerializeObject(command_content);