using AutoMapper; using SLH.SSDMS.Common; using SLH.SSDMS.Model.DO; using SLH.SSDMS.Model.DTO; using SLH.SSDMS.RestSharp; using SLH.SSDMS.WebSocket; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SLH.SSDMS.Services.serviceImpl { public class BufferParse : IBufferParse { private static IReturnBuffer returnBuffer = new ReturnBuffer(); private static IRfidJudge rfidJudge = new RfidJudge(); private static IBaseServices baseServices = new BaseServices(); private static IBaseServices configServices = new BaseServices(); /// /// 心跳解析 /// /// /// public async Task HeartAnalysis(byte[] buffer) { try { WebSocketServer.sendMessage("01"); return returnBuffer.HeartAnalysisResult(1, buffer[3]); }catch(Exception ex) { LogHelper.Error("心跳解析异常", ex); return returnBuffer.HeartAnalysisResult(0, buffer[3]); } } /// /// 指令解析 /// /// /// public async Task ReadAnalysis(byte[] buffer) { try { Mapper.Initialize(cret => cret.CreateMap()); Base_ReadRecord readRecord = new Base_ReadRecord(); //RFID标签号 string RFIDCode = ""; //终端地址 readRecord.terminal = Convert.ToString(buffer[3], 16); //生产年 readRecord.proYear = Convert.ToString(buffer[4], 16) + Convert.ToString(buffer[5], 16); //生产月 readRecord.proMonth = Convert.ToString(buffer[6]); //生产机台 readRecord.proMachine = Convert.ToString(buffer[7], 16) + Convert.ToString(buffer[8], 16); //带强 readRecord.beltStrength = Convert.ToString(buffer[9], 16) + Convert.ToString(buffer[10], 16); switch (readRecord.beltStrength) { case "05": readRecord.beltStrength = "ST1400"; break; case "06": readRecord.beltStrength = "ST1600"; break; case "07": readRecord.beltStrength = "ST1800"; break; case "08": readRecord.beltStrength = "ST2000"; break; case "09": readRecord.beltStrength = "ST2250"; break; default: readRecord.beltStrength = "ST1800"; break; } //带宽 readRecord.beltWidth = Convert.ToString(buffer[11], 16); switch (readRecord.beltWidth) { case "5": readRecord.beltWidth = "1200mm"; break; case "6": readRecord.beltWidth = "1400mm"; break; case "7": readRecord.beltWidth = "1600mm"; break; case "8": readRecord.beltWidth = "1800mm"; break; case "9": readRecord.beltWidth = "2000mm"; break; default: readRecord.beltWidth = "1600mm"; break; } //上胶厚度 readRecord.upGlueThick = Convert.ToString(buffer[12], 16) + Convert.ToString(buffer[13], 16); switch (readRecord.upGlueThick) { case "10": readRecord.upGlueThick = "6mm"; break; case "11": readRecord.upGlueThick = "6.5mm"; break; case "12": readRecord.upGlueThick = "7mm"; break; case "13": readRecord.upGlueThick = "7.5mm"; break; case "14": readRecord.upGlueThick = "8mm"; break; default: readRecord.upGlueThick = "7mm"; break; } //连接符 ==>转为ASCII readRecord.unionSymbol = ((char)Convert.ToInt16(Convert.ToString(buffer[14], 16) + Convert.ToString(buffer[15], 16))).ToString(); //下胶厚度 readRecord.downGlueThick = Convert.ToString(buffer[16], 16) + Convert.ToString(buffer[17], 16); switch (readRecord.downGlueThick) { case "08": readRecord.downGlueThick = "5mm"; break; case "09": readRecord.downGlueThick = "5.5mm"; break; case "10": readRecord.downGlueThick = "6mm"; break; case "11": readRecord.downGlueThick = "6.5mm"; break; case "12": readRecord.downGlueThick = "7mm"; break; default: readRecord.downGlueThick = "6mm"; break; } //盖胶级别 readRecord.coverGlueLevel = Convert.ToString(buffer[18], 16) + Convert.ToString(buffer[19], 16); switch (readRecord.coverGlueLevel) { case "01": readRecord.coverGlueLevel = "L"; break; case "02": readRecord.coverGlueLevel = "D"; break; case "03": readRecord.coverGlueLevel = "H"; break; case "04": readRecord.coverGlueLevel = "T1"; break; case "05": readRecord.coverGlueLevel = "T2"; break; default: readRecord.coverGlueLevel = "T3"; break; } //卷号 readRecord.lotNum = Convert.ToString(buffer[20], 16) + Convert.ToString(buffer[21], 16); //RFID卡序号 readRecord.rfidOrder = Convert.ToString(buffer[22], 16) + Convert.ToString(buffer[23], 16); //当前RFID前间距 readRecord.preDistance = (Convert.ToString(buffer[24], 16) + Convert.ToString(buffer[25], 16)).ToInt(); //当前RFID后间距 readRecord.aftDistance = (Convert.ToString(buffer[26], 16) + Convert.ToString(buffer[27], 16)).ToInt(); //读取时间 readRecord.readTime = DateTime.Now; //时间戳 byte[] timeByte = new byte[8]; Array.Copy(buffer, 28, timeByte, 0, 8); foreach (var item in timeByte) { readRecord.timeNumber = readRecord.timeNumber + item; } //RFID标签 byte[] rfidByte = new byte[24]; Array.Copy(buffer, 4, rfidByte, 0, 24); foreach (var item in rfidByte) { RFIDCode = RFIDCode + item; } readRecord.rfidCode = RFIDCode; //判断运行顺序 readRecord.orderFlag = await rfidJudge.JudgeDirection(RFIDCode); LogHelper.Info("标签数据解析完成:" + JsonChange.ModeToJson(readRecord)); LogHelper.Info("运行方向:" + readRecord.orderFlag); if (readRecord.orderFlag < 2) { await baseServices.Add(readRecord); try { ViewModelDTO viewModelDTO = Mapper.Map(readRecord); viewModelDTO.runTime = await rfidJudge.JudgeRunTime(readRecord); viewModelDTO.speed = await rfidJudge.JudgeRunSpeed(readRecord); viewModelDTO.distance = await rfidJudge.JudgeRunDistance(readRecord); string firstTime = await rfidJudge.JudgeFirstTime(readRecord.lotNum); viewModelDTO.firstTime = firstTime.Substring(0, 10); viewModelDTO.beltSum = 11; //获取BC9F流量 Sys_Config sys_Config = await GetBC9FWorkFlow(); if(sys_Config != null) { viewModelDTO.carryFlow = Convert.ToDecimal(sys_Config.config_value); viewModelDTO.titalWorkLoad = Convert.ToDecimal(sys_Config.remark); } //websocket上传数据 WebSocketServer.sendMessage("01"); WebSocketServer.sendMessage(JsonChange.ModeToJson(viewModelDTO)); //上传无人巡检系统 Upload.sendMessage(viewModelDTO); } catch(Exception ex) { LogHelper.Info("数据上传异常:" + ex); } } } catch(Exception ex) { LogHelper.Error("指令解析异常", ex); } return returnBuffer.ReadAnalysisResult(1, buffer[3]); } /// /// 获取BC9F作业流量 /// private async Task GetBC9FWorkFlow() { Sys_Config sys_Config = await configServices.QueryById(4); LogHelper.Info($"获取BC9F数据:{JsonChange.ModeToJson(sys_Config)}"); return sys_Config; } } }