恒石成品库WCS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

439 lines
20 KiB

using CommLayerFactory;
using ICommLayer;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using WcfControlMonitorLib;
namespace WcfControlMonitorWebLib.Controllers
{
/// <summary>
/// 设备状态
/// </summary>
[ApiController, Route("api/device")]
public class DeviceController: ControllerBase
{
DBFactory.DBOperator dbo = CStaticClass.dbo;
DBFactory.DBOperator dboMan = CStaticClass.dboM;
StringBuilder sql = new StringBuilder();
//List<Model.MDevice> deviceList = new List<Model.MDevice>();
List<Model.ClientMDevice> deviceList = new List<Model.ClientMDevice>();
//SControlMonitor scontrol = new SControlMonitor();
/// <summary>
/// 获取初始化设备状态
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult GetList()
{
//sql.Remove(0, sql.Length);
//sql.Append("select T_Base_Device.F_DeviceIndex from T_Base_Device, T_Base_Device_Kind " +
// "where T_Base_Device.F_DeviceKindIndex = T_Base_Device_Kind.F_DeviceKindIndex" +
// " and T_Base_Device_Kind.F_GoodsMoveKindIndex = 1 and T_Base_Device.F_DeviceKindIndex not in (12) ");//not in 是为了把虚拟关键设备拿掉
////T_Base_Device_Kind里面关键设备的F_GoodsMoveKindIndex =1 没动数据库配置 就在这排除一下 虚拟关键设备客户端没画 会报错
///
sql.Remove(0, sql.Length);
sql.Append("select T_Base_Device.F_DeviceIndex from T_Base_Device, T_Base_Device_Kind " +
"where T_Base_Device.F_DeviceKindIndex = T_Base_Device_Kind.F_DeviceKindIndex" +
" and (T_Base_Device_Kind.F_GoodsMoveKindIndex = 1 or T_Base_Device_Kind.F_DeviceKindIndex=7) and T_Base_Device.F_SUBSCRIBECLIENT =1 and T_Base_Device.F_DeviceKindIndex not in (12,22,35,10,8,37) ");//not in 是为了把虚拟关键设备拿掉 F_DeviceKindIndex=7是扫码器
//T_Base_Device_Kind里面关键设备的F_GoodsMoveKindIndex =1 没动数据库配置 就在这排除一下 虚拟关键设备客户端没画 会报错
DataView dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
if (dv.Count > 0)
{
int iDevice = 0;
for (int i = 0; i < dv.Count; i++)
{
iDevice = int.Parse(dv[i]["F_DeviceIndex"].ToString());
//方案一 简化一下给客户端发
Model.MDevice mDevice = Model.CGetInfo.GetDeviceInfo(iDevice);
deviceList.Add(Model.CGetInfo.SendMessageToClient(mDevice));
#region
//Model.MDevice mDevice= Model.CGetInfo.GetDeviceInfo(iDevice);
//if (mDevice.LogicHaveGoods)
//{
// mDevice.HaveGoodsMeg = "逻辑有货";
//}
//else
//{
// mDevice.HaveGoodsMeg = "逻辑无货";
//}
////处理数据库查询相关的
//#region 目标位置 和条码
//sql.Remove(0, sql.Length);
//sql.Append("SELECT F_MonitorIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam2,F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6, F_TxtParam FROM T_Monitor_Task WHERE F_MonitorIndex =").Append(mDevice.TaskNo);
//DataView dvdv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
//if (dvdv.Count > 0)
//{
// int order = Convert.ToInt32(dvdv[0]["F_DeviceCommandIndex"]);
// string info = string.Empty;
// int Z = Convert.ToInt32(dvdv[0]["F_NumParam1"]);
// int X = Convert.ToInt32(dvdv[0]["F_NumParam2"]);
// int Y = Convert.ToInt32(dvdv[0]["F_NumParam3"]);
// int Z1 = Convert.ToInt32(dvdv[0]["F_NumParam4"]);
// int X1 = Convert.ToInt32(dvdv[0]["F_NumParam5"]);
// int Y1 = Convert.ToInt32(dvdv[0]["F_NumParam6"]);
// if (mDevice.DeviceKind == 1)
// {
// switch (order)
// {
// case 1://回原点
// info = string.Format("回原点:01列01层");
// break;
// case 2://将取
// info = string.Format("将取:{0:D2}排-{1:D2}列-{2:D2}层", Z, X, Y);
// break;
// case 3://将送
// info = string.Format("将送:{0:D2}排-{1:D2}列-{2:D2}层", Z1, X1, Y1);
// break;
// case 4://取
// info = string.Format("取货:{0:D2}排-{1:D2}列-{2:D2}层", Z, X, Y);
// break;
// case 5://送
// info = string.Format("送货:{0:D2}排-{1:D2}列-{2:D2}层", Z1, X1, Y1);
// break;
// case 6:
// info = string.Format("取送货:{0:D2}排-{1:D2}列-{2:D2}层 到 {3:D2}排-{4:D2}列-{5:D2}层", Z, X, Y, Z1, X1, Y1);
// break;
// }
// }
// else if (mDevice.DeviceKind == 2)
// {
// switch (order)
// {
// case 1://正转
// info = string.Format("正转");
// break;
// case 2://反转
// info = string.Format("反转");
// break;
// case 6://送出
// info = string.Format("{0}送出到{1}", Z, Z1);
// break;
// }
// }
// else if (mDevice.DeviceKind == 4)
// {
// switch (order)
// {
// case 7://运动
// info = string.Format("运动:{0}", Z);
// break;
// case 8://取
// info = string.Format("取货:{0}", Z);
// break;
// case 9://送
// info = string.Format("送货:{0}", Z);
// break;
// }
// }
// mDevice.ArrowLocation = info;
// mDevice.Barcode = dvdv[0]["F_TxtParam"].ToString();
//}
//else
//{
// //20140109
// if (mDevice.DeviceKind == 2)
// {
// // add for CATL 动作完成状态
// if (mDevice.RunState == 5 && mDevice.SplitByte_0 == 1)
// {
// //条码号
// object bcode = dbo.GetSingle(string.Format("SELECT F_PALLETBARCODE FROM T_Base_Device WHERE (F_DeviceIndex = {0})", mDevice.DeviceIndex));
// if (bcode != null)
// {
// mDevice.Barcode = bcode.ToString();
// }
// else
// {
// mDevice.Barcode = "";
// }
// }
// else
// {
// //条码号
// mDevice.Barcode = "";
// }
// }
// else
// {
// //条码号
// mDevice.Barcode = "";
// }
// //目标位置
// mDevice.ArrowLocation = "";
//}
//#endregion
//#region 获取报警信息
//设备状态
switch (mDevice.RunState)
{
case 0:
mDevice.ErrorMessage = "空闲";
break;
case 1:
mDevice.ErrorMessage = "运行";
break;
case 2:
mDevice.ErrorMessage = "故障";
break;
case 3:
mDevice.ErrorMessage = "离线";
break;
case 4:
mDevice.ErrorMessage = "停用";
break;
case 5://20090920
mDevice.ErrorMessage = "动作完成";
break;
}
if (mDevice.ErrorCode >= 30)
{
Model.MError err = Model.CGetInfo.GetErrorInfo(Convert.ToInt32(mDevice.DeviceKind.ToString() + mDevice.ErrorCode.ToString()));
if (err != null)
{
mDevice.ErrorMessage = err.ErrorName + ",代码:" + mDevice.ErrorCode.ToString();
}
else
{
mDevice.ErrorMessage = mDevice.ErrorCode.ToString();
}
}
else
{
mDevice.ErrorMessage = "";
}
//#endregion
//#region 获取管理任务号
////sql.Remove(0, sql.Length);
////sql.Append("");
////dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
//#endregion
//deviceList.Add(mDevice);
#endregion
}
}
return StatusCode(200, deviceList);
}
/// <summary>
/// 补码
/// </summary>
[HttpPost]
[Route("barcode")]
public IActionResult BarcodeApplyCreate(
[FromMember] int deviceIndex,
[FromMember] string newBarcode,
string session
)
{
Model.MDevice dev = Model.CGetInfo.GetDeviceInfo(deviceIndex);
StringBuilder dtime = new StringBuilder(DateTime.Now.ToString("u"));
dtime.Remove(dtime.Length - 1, 1);
int apptype = 0;
int intBarDevice = 0;
string warehouse = "1";
string applyStation = "CW";
DataView dvapp = dbo.ExceSQL(string.Format("SELECT F_MANAGEASKKIND,F_DEVICEINDEX,F_WareHouse,F_BindingDevice FROM T_BASE_PLC_ASK WHERE F_DEVICEINDEX = {0}", deviceIndex)).Tables[0].DefaultView;
if (dvapp.Count > 0)
{
apptype = Convert.ToInt32(dvapp[0]["F_MANAGEASKKIND"]);
intBarDevice = Convert.ToInt32(dvapp[0]["F_DEVICEINDEX"]);
warehouse = Convert.ToString(dvapp[0]["F_WareHouse"]);
applyStation = Convert.ToString(dvapp[0]["F_BindingDevice"]);
}
string barcode = newBarcode.ToString();
DataView dvbcApply = new DataView();
dvbcApply = dboMan.ExceSQL(string.Format("SELECT DEVICE_CODE,STOCK_BARCODE FROM IO_CONTROL_APPLY WHERE APPLY_TASK_STATUS<2 and (STOCK_BARCODE = '{0}')", barcode)).Tables[0].DefaultView;
if (dvbcApply.Count > 0)
{
return StatusCode(409);
//tbBarcode.Text = "";
//MessageBox.Show("当前条码已存在申请!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
DataView dvbc = new DataView();
dvbc = dboMan.ExceSQL(string.Format("select CONTROL_ID from IO_CONTROL where CONTROL_STATUS<>999 and CONTROL_STATUS<>300 and STOCK_BARCODE = '{0}'", barcode)).Tables[0].DefaultView;//管理系统有可能还没处理已经报告完成的任务
if (dvbc.Count > 0)
{
return StatusCode(410);
//tbBarcode.Text = "";
//MessageBox.Show("当前条码已经生成控制任务!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
DataView dvbcc = new DataView();
dvbcc = dbo.ExceSQL(string.Format("select fid from T_Manage_Task where FPALLETBARCODE = '{0}'", barcode)).Tables[0].DefaultView;
if (dvbcc.Count > 0)
{
return StatusCode(422);
//tbBarcode.Text = "";
//MessageBox.Show("当前条码存在调度任务!", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
int row = 0;
string remake = "补码";
object[] ob = new object[7] { apptype, applyStation, barcode, 0, dtime, remake, warehouse };
//oracle
//row = dboMan.ExecuteSql(string.Format("INSERT INTO IO_CONTROL_APPLY ( CONTROL_APPLY_ID,CONTROL_APPLY_TYPE,DEVICE_CODE, STOCK_BARCODE, APPLY_TASK_STATUS, CREATE_TIME,CONTROL_APPLY_REMARK,WAREHOUSE_CODE)VALUES ( IO_CONTROL_APPLY_SEQ.Nextval,{0},'{1}','{2}',{3},'{4}','{5}','{6}')", ob));
row = dboMan.ExecuteSql(string.Format("INSERT INTO IO_CONTROL_APPLY (CONTROL_APPLY_TYPE,DEVICE_CODE, STOCK_BARCODE, APPLY_TASK_STATUS, CREATE_TIME,CONTROL_APPLY_REMARK,WAREHOUSE_CODE)VALUES ({0},'{1}','{2}',{3},'{4}','{5}','{6}')", ob));
string sql = "UPDATE T_BASE_PLC_ASK SET F_BARCODE ='" + barcode + "' WHERE F_DEVICEINDEX =" + deviceIndex;
dbo.ExceSQL(sql);
if (row == 1)
{
object[] ob1 = new object[7] { apptype, applyStation, barcode, 0, dtime, remake, warehouse.ToString() };
//dbo.ExecuteSql(string.Format("INSERT INTO IO_CONTROL_APPLY_WCS (CONTROL_APPLY_ID,CONTROL_APPLY_TYPE,DEVICE_CODE, STOCK_BARCODE, APPLY_TASK_STATUS, CREATE_TIME,CONTROL_APPLY_PARA01,WAREHOUSE_CODE)VALUES (IO_CONTROL_APPLY_WCS_SEQ.Nextval,{0},'{1}','{2}',{3},'{4}','{5}','{6}')", ob1));
dbo.ExecuteSql(string.Format("INSERT INTO IO_CONTROL_APPLY_WCS (CONTROL_APPLY_TYPE,DEVICE_CODE, STOCK_BARCODE, APPLY_TASK_STATUS, CREATE_TIME,CONTROL_APPLY_REMARK,WAREHOUSE_CODE)VALUES ({0},'{1}','{2}',{3},'{4}','{5}','{6}')", ob1));
CommonClassLib.CCarryConvert.WriteDarkCasket("ClientBacorde", "客户端补码", barcode.ToString().ToUpper(), "补码站台和账户:" + deviceIndex.ToString() + "|" + session);
dev.Barcode = barcode;
Model.CGetInfo.SetDeviceInfo(dev);
}
return Ok();
}
/// <summary>
/// 设备操作
/// </summary>
/// <param name="Device"></param>
/// <param name="Action">4.设备复位;2.完成应答;1.逻辑有无物;3.逻辑预约锁解锁</param>
/// <param name="value"></param>
[HttpPut]
public void Put(
[FromQuery]int Device,
[FromQuery]int Action,
[FromQuery]int? value,
string session)//这个session就能拿到用户名
{
//string a = session;
string o = string.Empty;
Model.MDevice dev = Model.CGetInfo.GetDeviceInfo(Device);
ISendDeviceOrder sdo;
sdo = CommModeCreate.CreateSendDeviceOrder(Device);
switch (Action)
{
case 1:
if (value.HasValue)
{
if (value == 1)
{
dbo.ExceSQL(string.Format("UPDATE T_Base_Device SET F_HaveGoods = 1 where F_DeviceIndex ={0}", Device));
dev.LogicHaveGoods = true;
}
else if (value == 0)
{
dbo.ExceSQL(string.Format("UPDATE T_Base_Device SET F_HaveGoods = 0 where F_DeviceIndex ={0}", Device));
dev.LogicHaveGoods = false;
}
Model.CGetInfo.SetDeviceInfo(dev);
}
break;
case 2:
if ((dev.DeviceKind == 1) || (dev.DeviceKind == 6))
{
sdo.SendDeviceOrder(2, 0, 0, Device, 0, 0, 0, 0, 0, 0);
}
else
{
//// add for CATL 联机停车值判断
//if (deviceKind == 40)
//{
// sdo.SendDeviceOrder(ResetOrder, 0, ResetOrder, Device, 0);
//}
//else
//{
sdo.SendDeviceOrder(2, 0, 0, Device, 0);
//}
}
dev.SendMessage = null;
Model.CGetInfo.SetDeviceInfo(dev);
break;
case 3:
int count=dbo.ExecuteSql(string.Format("UPDATE T_Base_Device SET F_ManTaskReserve = 0 where F_ManTaskReserve != 0 and F_DeviceIndex ={0}", Device));
if (count > 0)
{
Model.CGetInfo.SetDeviceInfo(dev);
}
break;
case 4:
if ((dev.DeviceKind == 1) || (dev.DeviceKind == 6))
{
sdo.SendDeviceOrder(4, 0, 0, Device, 0, 0, 0, 0, 0, 0);
}
else
{
//// add for CATL 联机停车值判断
//if (deviceKind == 40)
//{
// sdo.SendDeviceOrder(ResetOrder, 0, ResetOrder, Device, 0);
//}
//else
//{
sdo.SendDeviceOrder(4, 0, 0, Device, 0);
//}
}
dev.SendMessage = null;
Model.CGetInfo.SetDeviceInfo(dev);
break;
default:
break;
}
}
/// <summary>
/// 设备上位机急停
/// </summary>
/// <param name="device"></param>
/// <param name="deviceaction"></param>
/// <returns></returns>
[HttpGet("stopMoveDevice")]
public IActionResult StopMoveDevice([FromQuery] string device, [FromQuery] string deviceaction)
{
//联机停车
try
{
Model.MDevice dev = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(device));
ISendDeviceOrder sdo;
StringBuilder[] wv = { new StringBuilder("2") };
StringBuilder[] witemnames = { new StringBuilder("") };
sdo = CommModeCreate.CreateSendDeviceOrder(Convert.ToInt32(device));
witemnames[0].Clear();
witemnames[0].Append(Model.CGeneralFunction.DBSend).Append(".").Append(Convert.ToString(dev.Dbw1Address + 19)).Append(",b");//20130510
wv[0].Clear();
wv[0].Append(deviceaction.ToString());
sdo = CommModeCreate.CreateSendDeviceOrder(Convert.ToInt32(device));
sdo.WriteDBData(witemnames, wv, dev.S7Connection);
return Ok();
}
catch (Exception ex)
{
return Ok();
}
}
}
}