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