using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using 服务API.Model;
using 服务API.Tool.IOT;
using 服务API.Tool.控制台显示;
namespace 服务API.Controllers
{
///
/// 老IOT设备查询API接口
///
public class IOTDeviceController :BaseController
{
///
/// 根据设备名称查询设备状态
///
[HttpGet]
public JsonMsg GetDeviceProperty(string name)
{
string st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
if (string.IsNullOrEmpty(name))
{
return JsonMsg.Error(null,st,"设备名称不能为空"); ;
}
else
{
//DataVModel dataV = SqlContext.Base.GetInfo(name);
//return JsonMsg.OK(Tools.JsonToObjectTools(dataV?.json),st);
return JsonMsg.Error(null,st,"设备名称不能为空"); ;
}
}
catch (System.Exception ex)
{
return JsonMsg.Error(null,ex.Message);
}
}
///
/// 命令设置设备属性或命令控制
///
[HttpPost]
public JsonMsg SetDeviceProperty([FromBody] CommandModel usersModel)
{
string st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(Tools.JsonConvertTools(usersModel),Encoding.GetEncoding("UTF-8"),"application/json") };
if (usersModel == null || string.IsNullOrEmpty(usersModel.deviceName))
{
return JsonMsg.Error(null,st,"设备名称不能为空"); ;
}
else
{
string PubTopic = "/broadcast/" + "grgpECHSL7q" + "/" + usersModel.deviceName + "_SetDevice";
ConsoleHelper.WriteWarningLine("发送数据 " + PubTopic + Tools.JsonConvertTools(usersModel));
IOTDevServer.GetInstance().IOT_Publish(PubTopic,Tools.JsonConvertTools(usersModel));
return JsonMsg.OK(usersModel,st);
}
}
catch (System.Exception ex)
{
return JsonMsg.Error(null,st,ex.Message); ;
}
}
///
/// 命令设置设备属性或命令控制
///
/// 设备名称
/// 命令名称:0 控制类 1 设置属性 2 通知信息类
/// 命令变量:执行变量 key为属性或时间
/// 命令变量:value为值或者消息
///
[HttpGet]
public JsonMsg SetDeviceProperty(string deviceName,int CommandName,string CommandKey,string CommandValue)
{
string st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
if (string.IsNullOrEmpty(deviceName))
{
return JsonMsg.Error(null,st,"设备名称不能为空"); ;
}
else
{
string PubTopic = "/broadcast/" + "grgpECHSL7q" + "/" + deviceName + "_SetDevice";
Dictionary keyValuePair = new Dictionary();
keyValuePair.Add(CommandKey,CommandValue);
CommandModel command = new CommandModel() { CommandName = CommandName,deviceName = deviceName,CommandValue = keyValuePair };
ConsoleHelper.WriteWarningLine("发送数据 " + PubTopic + Tools.JsonConvertTools(command));
IOTDevServer.GetInstance().IOT_Publish(PubTopic,Tools.JsonConvertTools(command));
return JsonMsg.OK(command,st);
}
}
catch (System.Exception ex)
{
return JsonMsg.Error(null,st,ex.Message); ;
}
}
}
}