using Hanhe.iWCS.Common;
using HH.AMS.BLL.MongoDB;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
namespace HH.AMS.WebApi.Controllers
{
public class LEDCommandPackte
{
byte _FC;
byte[] _DataArry;
byte _Data;
byte _LEN;
byte[] ResultData;
public LEDCommandPackte(byte FC, byte Data)
{
_FC = FC;
_LEN = 1;
_Data = Data;
}
public LEDCommandPackte(byte FC, byte[] Data)
{
_FC = FC;
_LEN = (byte)Data.Length;
_DataArry = Data;
}
public byte[] ToByte()
{
byte Index = 2;
ResultData = new byte[_LEN + 3];
ResultData[0] = 0xFD;
ResultData[1] = _FC;
if (_LEN == 1)
{ ResultData[Index] = _Data; Index++; }
else
{
Array.Copy(_DataArry, 0, ResultData, Index, _LEN); Index += _LEN;
}
ResultData[Index] = GetAccValue(ResultData, _LEN + 2);
return ResultData;
}
public bool Parsing(byte[] Data, out byte[] Value)
{
bool st = false;
Value = null;
if (Data.Length > 4)
{
byte value = GetAccValue(Data, Data.Length - 1);
if (value == Data[Data.Length - 1])
{
_FC = Data[1];
Value = new byte[Data.Length - 3];
Array.Copy(Data, 2, Value, 0, Value.Length);
st = true;
}
}
return st;
}
///
/// 累加和计算
///
/// 需要校验的数据
/// 需要校验的数据长度 从第0个字节开始计算
/// 累加和
private byte GetAccValue(byte[] NeedVerifyData, int AccLenght)
{
byte Result = 0;
if (NeedVerifyData != null)
{
for (int i = 0; i < AccLenght; i++)
{
Result = (byte)(Result + NeedVerifyData[i]);
}
}
return Result;
}
}
[RoutePrefix("api/NanTong")]
///
/// 本接口用作南通项目使用
///
public class NanTongController : ApiController
{
public string GetIP()
{
string uip = "";
if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
uip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else
{
uip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
return uip;
}
#region 车辆识别结果API
///
/// 车辆识别结果API
///
///
///
[Route("Plateresult")]
[HttpPost]
public HttpResponseMessage Plateresult([FromBody] PlateresultViewModel interfaceParas)
{
CMMLog.Info("【车辆识别结果】:" + interfaceParas);
string resultValue = string.Empty;
ResponseViewModel rv = new ResponseViewModel();
if (interfaceParas != null)
{
int channel = interfaceParas.AlarmInfoPlate.channel;//通道
string deviceName = interfaceParas.AlarmInfoPlate.deviceName;
string ipaddr = interfaceParas.AlarmInfoPlate.ipaddr;
string seriaIno = interfaceParas.AlarmInfoPlate.serialno;
string license = interfaceParas.AlarmInfoPlate.result.PlateResult.license;//车牌号信息
var where = Query.And(Query.EQ("license", license), Query.EQ("state", 0));
MongodbPlateresultViewModel pvm = MongoDBSingleton.Instance.FindOne(where);
if (pvm == null)
{
pvm = new MongodbPlateresultViewModel();
pvm.channel = channel;
pvm.deviceName = deviceName;
pvm.ipaddr = ipaddr;
pvm.seriaIno = seriaIno;
pvm.license = license;
pvm.state = 0;
pvm.area = "";
pvm.itemType = "";
pvm.createTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
MongoDBSingleton.Instance.Insert(pvm);
}
}
rv.Response_AlarmInfoPlate.info = "ok";
resultValue = Newtonsoft.Json.JsonConvert.SerializeObject(rv);
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(resultValue, Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}
#endregion
#region Comet轮询心跳
///
/// http心跳
///
///
///
[Route("Receivede")]
[HttpPost]
public HttpResponseMessage Receivede()
{
string resultValue = string.Empty;
ResponseViewModel rcv = new ResponseViewModel();
string license = "";//车牌信息
string weight = "";//重量信息
string area = "";//区域
string itemType = "";//物料种类
string ext1 = "";
string ext2 = "";
var addressIP = GetIP();
//获取车牌采集信息
var where = Query.And(Query.EQ("state", 0), Query.EQ("ipaddr", addressIP));
MongodbPlateresultViewModel _mpv = MongoDBSingleton.Instance.FindOne(where);
if (_mpv != null && !string.IsNullOrEmpty(_mpv.area) && !string.IsNullOrEmpty(_mpv.itemType))
{
license = _mpv.license;
area = _mpv.area;
itemType = _mpv.itemType;
ext1 = _mpv.ext1;
ext2 = _mpv.ext2;
}
//增加地磅重量透传反馈
where = Query.And(Query.EQ("portName", "COM4"), Query.EQ("state", 0));
MongodbWMViewModel _wm = MongoDBSingleton.Instance.FindOne(where);
if (_wm != null && _wm.bit3_8 != "")
{
weight = _wm.bit3_8;
}
if (license != "" && weight != "" && area != "" && itemType != "")
{
//resultValue = CarIOProcess(1, 1, "皖A915X2", "108kg", "A区卸货", 99);
if (itemType == "空车")
{
resultValue = CarIOProcess(1, 1, 1, license, weight + "KG", area, 255);
}
else
{
resultValue = CarIOProcess(1, 1, 1, license, weight + "KG", area, 255);
}
//通知成功 将state 改成1
where = Query.EQ("_id", _mpv._id);
UpdateBuilder updateBuider = Update.Set("state", 1);
MongoDBSingleton.Instance.Update(where, updateBuider, UpdateFlags.None);
where = Query.EQ("_id", _wm._id);
updateBuider = Update.Set("state", 1);
MongoDBSingleton.Instance.Update(where, updateBuider, UpdateFlags.None);
}
else if (ext1 == "XS" && ext2 == "XS" && license != "" && weight != "")
{
resultValue = CarIOProcess(1, 1, 1, license, weight + "KG", "销售出库、等待人工确认!", 255);
//通知成功 将state 改成1
where = Query.EQ("_id", _mpv._id);
UpdateBuilder updateBuider = Update.Set("state", 1);
MongoDBSingleton.Instance.Update(where, updateBuider, UpdateFlags.None);
where = Query.EQ("_id", _wm._id);
updateBuider = Update.Set("state", 1);
MongoDBSingleton.Instance.Update(where, updateBuider, UpdateFlags.None);
}
else
{
rcv.Response_AlarmInfoPlate.info = "err";
resultValue = Newtonsoft.Json.JsonConvert.SerializeObject(rcv);
}
#region 定制内容语音提示
var eqWhere = Query.EQ("ipaddr", addressIP);
var showModel = MongoDBSingleton.Instance.FindOne(eqWhere, typeof(MongodbPlateresultShowMessageModel).Name);
if (showModel != null)
{
resultValue = CarIOProcess(1, 1, 1, showModel.license, showModel.ext1+"KG", showModel.showContent, 255);
var removeWhere= Query.EQ("_id", showModel._id);
MongoDBSingleton.Instance.Remove(removeWhere, typeof(MongodbPlateresultShowMessageModel).Name);
}
#endregion
CMMLog.Info("心跳回复内容:" + resultValue);
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(resultValue, Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}
#endregion
#region 屏幕串口通信
///
/// 调用串口获取待回复的JSON字符串
///
///
/// 屏幕串口
/// 字体颜色 1或2或3
/// 车牌号 如:皖A91789
/// 车辆类型 如:临时车
/// 显示内容
/// 显示持续时间
/// true:开闸 false:屏幕显示透传
///
public string CarIOProcess(byte ChanelIndex, byte port, byte Color, string Plate, string VehicleType, string Message, byte DispTime, bool OpenGate = false)
{
byte Index = 0;
byte[] PlateArray = Encoding.GetEncoding("GBK").GetBytes(Plate);
byte[] VehicleTypeArray = Encoding.GetEncoding("GBK").GetBytes(VehicleType);
byte[] MessageArray = Encoding.GetEncoding("GBK").GetBytes(Message);
byte[] DataArry = new byte[PlateArray.Length + VehicleTypeArray.Length + MessageArray.Length + 5];
DataArry[Index] = (byte)Color; Index++;
DataArry[Index] = (byte)PlateArray.Length; Index++;
Array.Copy(PlateArray, 0, DataArry, Index, PlateArray.Length); Index += (byte)PlateArray.Length;
DataArry[Index] = (byte)VehicleTypeArray.Length;
Index++;
Array.Copy(VehicleTypeArray, 0, DataArry, Index, VehicleTypeArray.Length); Index += (byte)VehicleTypeArray.Length;
DataArry[Index] = (byte)MessageArray.Length; Index++;
Array.Copy(MessageArray, 0, DataArry, Index, MessageArray.Length); Index += (byte)MessageArray.Length;
DataArry[Index] = DispTime;
LEDCommandPackte LP = new LEDCommandPackte(5, DataArry);
byte[] PacketArry = LP.ToByte();
return SendDataToSerial(ChanelIndex, port, PacketArry, OpenGate);
}
public string SendDataToSerial(byte ChanelIndex, byte Port, byte[] SendData, bool OpenGate = false)
{
ResponseViewModel rcv = new ResponseViewModel();
if (OpenGate == true)
{
rcv.Response_AlarmInfoPlate.info = "ok";
//rcv.Response_AlarmInfoPlate.content = "...";
rcv.Response_AlarmInfoPlate.is_pay = "true";
}
else
{
rcv.Response_AlarmInfoPlate.info = "Send";
//rcv.Response_AlarmInfoPlate.content = "...";
rcv.Response_AlarmInfoPlate.is_pay = "False";
}
serialDataClass serialData = new serialDataClass();
serialData.serialChannel = Port - 1;
serialData.data = Convert.ToBase64String(SendData);
serialData.dataLen = SendData.Length;
rcv.Response_AlarmInfoPlate.serialData.Add(serialData);
string resulrstr = Newtonsoft.Json.JsonConvert.SerializeObject(rcv);
return resulrstr;
}
#endregion
}
#region 车牌识别结果视图Model
///
/// 车牌识别结果视图
///
public class PlateresultViewModel
{
public PlateresultViewModel() {
AlarmInfoPlate = new AlarmInfoPlateViewModel();
}
public AlarmInfoPlateViewModel AlarmInfoPlate { get; set; }
}
public class AlarmInfoPlateViewModel
{
public AlarmInfoPlateViewModel()
{
result = new AlarmInfoPlateResult();
}
public int channel { get; set; }
public string deviceName { get; set; }
public string ipaddr { get; set; }
public AlarmInfoPlateResult result { get; set; }
public string serialno { get; set; }
}
public class AlarmInfoPlateResult
{
public AlarmInfoPlateResult()
{
PlateResult = new PlateResultClass();
}
public PlateResultClass PlateResult { get; set; }
}
public class PlateResultClass
{
public int bright { get; set; }
public int carBright { get; set; }
public int carColor { get; set; }
public int colorType { get; set; }
public int colorValue { get; set; }
public int confidence { get; set; }
public int direction { get; set; }
public string imagePath { get; set; }
public string license { get; set; }
public locationClass location = new locationClass();
public TimeStampClass timeStamp = new TimeStampClass();
public int timeUsed { get; set; }
public int triggerType { get; set; }
public int type { get; set; }
}
public class locationClass
{
public RECTClass RECT = new RECTClass();
}
public class TimeStampClass
{
public TimevalClass Timeval = new TimevalClass();
}
public class RECTClass
{
public int bottom { get; set; }
public int left { get; set; }
public int right { get; set; }
public int top { get; set; }
}
public class TimevalClass
{
public Int64 sec { get; set; }
public Int64 usec { get; set; }
}
#endregion
#region 识别结果反馈握手视图
///
/// 识别结果反馈握手视图
///
public class ResponseViewModel
{
public Response_AlarmInfoPlateClass Response_AlarmInfoPlate = new Response_AlarmInfoPlateClass();
}
public class Response_AlarmInfoPlateClass
{
///
/// ok=开闸
///
public string info { get; set; }
///
/// 默认为0
///
public int channelNum { get; set; }
public string is_pay { get; set; }
public List serialData = new List();
}
public class serialDataClass
{
public int serialChannel { get; set; }
public string data { get; set; }
public int dataLen { get; set; }
}
#endregion
#region Comet轮询心跳反馈结果
public class ResponseCometViewModel
{
public Response_AlarmInfoPlateClass Response_SerialData = new Response_AlarmInfoPlateClass();
}
#endregion
#region 车牌号识别结果(Mongodb存储类)
///
/// 车牌号识别结果(Mongodb存储类)
///
public class MongodbPlateresultViewModel
{
public ObjectId _id { get; set; }
public int channel { get; set; }
public string deviceName { get; set; }
public string ipaddr { get; set; }
public string seriaIno { get; set; }
///
/// 车牌号信息
///
public string license { get; set; }
///
/// 创建时间
///
public string createTime { get; set; }
///
/// 状态(默认为0)
///
public int state { get; set; }
///
/// 区域
///
public string area { get; set; }
///
/// 物料种类
///
public string itemType { get; set; }
public string ext1 { get; set; }
public string ext2 { get; set; }
public string ext3 { get; set; }
public string ext4 { get; set; }
}
#endregion
#region 地磅采集信息
///
/// 地磅采集信息
///
public class MongodbWMViewModel
{
public ObjectId _id { get; set; }
///
/// COM串口名称
///
public string portName { get; set; }
///
/// 接收到串口的数据
///
public string receiveData { get; set; }
///
/// 状态(默认为0)
///
public int state { get; set; }
///
/// 创建时间
///
public string createTime { get; set; }
public string bit1 { get; set; }
public string bit2 { get; set; }
///
/// 数据位
///
public string bit3_8 { get; set; }
///
/// 小数位数
///
public string bit9 { get; set; }
public string bit10 { get; set; }
public string bit11 { get; set; }
public string bit12 { get; set; }
public string ext1 { get; set; }
public string ext2 { get; set; }
public string ext3 { get; set; }
}
#endregion
#region 车牌采集提示语音
///
/// 屏卡显示内容及提示语音内容
///
public class MongodbPlateresultShowMessageModel
{
public ObjectId _id { get; set; }
public string ipaddr { get; set; }
public string license { get; set; }
public string seriaIno { get; set; }
public string showContent { get; set; }
public string createTime { get; set; }
public string ext1 { get; set; }
public string ext2 { get; set; }
public string ext3 { get; set; }
}
#endregion
}