using CronExpressionDescriptor;
using LangGuan.Command.Model;
using LangGuan.Command.Model.EntityModel;
using LangGuan.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LangGuan.Controllers
{
///
///
///
[ApiVersion("1.0")]
[Route("api/[controller]/v{version:apiVersion}")]
[ApiController]
public class DeviceController : ControllerBase
{
private DeviceSerive _deviceSerive;
private PlanService _planService;
///
///
///
///
public DeviceController(DeviceSerive deviceSerive, PlanService planService)
{
_deviceSerive = deviceSerive;
_planService = planService;
}
///
/// Add
///
///
///
[HttpPost]
public async Task Add(Device input)
{
var result = await _deviceSerive.CreateAsync(input);
return Ok(new ApiResult() { code = 0 });
}
///
/// 删除
///
///
///
[HttpPost("remove")]
public async Task remove(IEnumerable ids)
{
foreach (var item in ids)
{
await _deviceSerive.RemoveAsync(item);
}
return Ok(new ApiResult() { code = 0 });
}
///
/// 列表
///
///
///
[HttpGet("list")]
public async Task list([FromQuery] RqeustPaging request)
{
var result = await _deviceSerive.GetList(request);
return Ok(result);
}
///
/// 更新
///
///
///
[HttpPost("update")]
public async Task update(Device request)
{
var result = await _deviceSerive.Update(request);
return Ok(result);
}
///
///
///
///
[HttpGet("GetListByGroupState")]
public async Task GetListByGroupState(bool Ingroup)
{
var result = await _deviceSerive.GetlistByGroupState(Ingroup);
return Ok(result);
}
///
/// 设备角度调整
///
///
///
[HttpPost("SetVal")]
public async Task SetVal(KeyValuePair input)
{
return Ok(new ApiResult() { code = 0, msg = "" });
}
///
/// 启动
///
///
/// true,摇摆,false,定点
///
[HttpGet("start")]
public async Task start(string id, bool point = false)
{
var result = await _deviceSerive.startdevice(id, point);
return Ok(new ApiResult() { code = 0, msg = "", data = result });
}
[HttpGet("stop")]
public async Task stop(string id)
{
var result = await _deviceSerive.stopDevice(id);
return Ok(new ApiResult() { code = 0, msg = "", data = result });
}
///
/// 判断设备是否在计划中
///
///
///
[HttpGet("existPlan")]
public async Task existPlan(string id)
{
var result = await _planService.existsPlan(id);
return Ok(new ApiResult() { code = 0, msg = "", data = result });
}
[HttpGet("getval")]
public async Task getval(string id)
{
var result = await _deviceSerive.getval(id);
return Ok(new ApiResult() { code = 0, msg = "", data = result });
}
[HttpGet("setval")]
public async Task setval(string id, bool val)
{
var result = await _deviceSerive.setval(id, val);
return Ok(new ApiResult() { code = 0, msg = "" });
}
[HttpGet("readkey")]
public async Task readkey(string ip, string key)
{
var result = await _deviceSerive.readkey(ip, key);
return Ok(new ApiResult() { code = 0, msg = "", data = result });
}
[HttpGet("writekey")]
public async Task writekey(string ip, string key, int val)
{
await _deviceSerive.wrirekey(ip, key, val);
return Ok(new ApiResult() { code = 0, msg = "" });
}
///
/// 设置上下俯仰,t=1上,t=2下,val=true,开始,false,停止
///
///
/// 1上,2下
///
///
[HttpGet("upordown")]
public async Task upordown(string id, int t = 1, bool val = true)
{
await _deviceSerive.setUporDown(id, t, val);
return Ok(new ApiResult() { code = 0, msg = "" });
}
///
/// 归零
///
///
///
///
[HttpGet("zero")]
public async Task Zero(string id, bool val = true)
{
await _deviceSerive.Zero(id, val);
return Ok(new ApiResult() { code = 0, msg = "" });
}
///
/// 获取上下角度
///
///
///
[HttpGet("getupordown")]
public async Task getupordown(string id)
{
var result = await _deviceSerive.GetUpOrDown(id);
return Ok(new ApiResult() { code = 0, msg = "", data = result });
}
///
/// 左右,跟上下一样
///
///
///
///
///
[HttpGet("leftorright")]
public async Task leftorright(string id, int t = 1, bool val = true)
{
await _deviceSerive.setLeftorRight(id, t, val);
return Ok(new ApiResult() { code = 0, msg = "" });
}
///
/// 获取角度
///
///
///
[HttpGet("getleftorright")]
public async Task getleftorright(string id)
{
var result = await _deviceSerive.GetLeftOrRight(id);
return Ok(new ApiResult() { code = 0, msg = "", data = result });
}
[HttpGet("cron")]
public async Task cron(string cron)
{
var result = ExpressionDescriptor.GetDescription(cron, new Options() { Locale = "zh-CN" });
return Ok(new ApiResult() { code = 0, msg = "", data = result });
}
}
}