LonglslandExhibitionCenter/LonglslandExhibitionCenter/Controllers/api/SetLightingController.cs

94 lines
3.8 KiB
C#

using DataService.api;
using Newtonsoft.Json;
using QingHaiVisualizationAPI.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
namespace LonglslandExhibitionCenter.Controllers.api
{
/// <summary>
/// 照明-控制照明
/// </summary>
public class SetLightingController : ApiController
{
static log4net.ILog log;
DataService.BLL.lighting_info bll = new DataService.BLL.lighting_info();
public HttpResponseMessage Get(string name = "", string value = "")
{
var res = new get_control_lighting();
try
{
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
{
var list = bll.GetModelList("");
var alist = list.Where(x => x.LightingId == name).FirstOrDefault();
var Number = alist.LightingNumber;
var Type = alist.LightingType;
var Id = alist.ClientId;
var code = alist.PointerCode;
//var model = new DataService.Model.lighting_info()
//{
// LightingId = alist.LightingId,
// LightingNumber = alist.LightingNumber,
// LightingName = alist.LightingName,
// LightingType = alist.LightingType,
// LightingState = value,
// LightingMalfunction = alist.LightingMalfunction,
// PointerCode = alist.PointerCode,
// ClientId = alist.ClientId,
// Reserve1 = alist.Reserve1,
// Reserve2 = alist.Reserve2,
// Reserve3 = alist.Reserve3,
// Reserve4 = alist.Reserve4,
// Reserve5 = alist.Reserve5,
//};
//bll.Update(model);
var blist = bll.GetModelList("").Where(x => x.LightingNumber == Number && x.LightingType == Type && x.ClientId == Id).ToList();
log4net.Config.XmlConfigurator.Configure();
log = log4net.LogManager.GetLogger("loginfo");
var mqqt = new MqttClientService();
data controlData = new data();
controlData.h = new HeaderData();
controlData.h.rt = Number;
// 如果需要同时设置多个数据项,可以使用字典
controlData.b = new BodyData();
controlData.b.dl = new Dictionary<string, string>();
foreach (var item in blist)
{
if (item.PointerCode == code)
{
item.LightingState = value;
}
controlData.b.dl["" + item.PointerCode + ""] = "" + item.LightingState + "";
}
mqqt.MqttClientStart();
mqqt.Publish("/gc/data/" + Id, "J" + JsonConvert.SerializeObject(controlData));
res.code = 200;
res.msg = "成功";
res.data = controlData;
}
else
{
res.code = 201;
res.msg = "参数不能为空";
}
}
catch (Exception ex)
{
res.code = 500;
res.msg = "失败," + ex.Message;
}
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(res), Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}
}
}