Esempio n. 1
0
        public byte[] HandlerClientData(byte[] buff)
        {
            int resInt = 0, machineId = 0;

            byte[]           byteResp    = { 0, 0, 0 };
            DbTableDbContext db          = new DbTableDbContext();
            DeviceSetting    outInfo     = this.DecodeData(buff);
            FactoryRoom      tempRoom    = db.FactoryRoom.FirstOrDefault(item => item.RoomNumber == outInfo.RoomNumber);
            Machines         tempMachine = db.Machines.FirstOrDefault(item => item.Number == outInfo.DeviceNumber);
            Machines         innerInfo;

            if (outInfo.DeviceId < 1)
            {
                resInt = tempRoom == null ? 3 : (tempMachine != null ? 4 : 0);
                if (resInt != 0)
                {
                    byteResp[0] = (byte)resInt;
                    return(byteResp);
                }
                innerInfo = this.exchangeData(tempRoom, outInfo);
                db.FactoryRoom.Attach(tempRoom);
                tempRoom.MachineCount += 1;
                //记录原始数据
                db.Machines.Add(innerInfo);
                //最近心跳设备记录
                db.LastHeartBreak.Add(new LastHeartBreak()
                {
                    DateRefresh = DateTime.Now,
                    FactoryName = tempRoom.FactoryName,
                    MachineId   = innerInfo.ID,
                    RoomID      = tempRoom.RoomID,
                    RoomName    = tempRoom.RoomName,
                    MachineName = outInfo.DeviceNumber
                });
            }
            else
            {
                innerInfo = db.Machines.FirstOrDefault(item => (item.IpAddress == outInfo.IPAddress || item.Number == outInfo.DeviceNumber) && item.ID != outInfo.DeviceId);
                if (innerInfo != null)
                {
                    byteResp[0] = (byte)4;
                    return(byteResp);
                }
                innerInfo = db.Machines.Find(outInfo.DeviceId);
                db.Machines.Attach(innerInfo);
                this.exchangeData(innerInfo, tempRoom, outInfo);
                machineId = innerInfo.ID;
            }
            db.SaveChanges();
            machineId = innerInfo.ID;

            //生成返回结果
            byte[] byteID = ConvertHelper.Int16ToBytes(machineId, true);
            Array.Copy(byteID, 0, byteResp, 1, 2);
            return(byteResp);
        }
Esempio n. 2
0
 /// <summary>
 /// 内外部数据转换
 /// </summary>
 /// <param name="innerInfo"></param>
 /// <param name="room"></param>
 /// <param name="info"></param>
 private void exchangeData(Machines innerInfo, FactoryRoom room, DeviceSetting info)
 {
     innerInfo.RoomID     = room.RoomID;
     innerInfo.RoomName   = room.RoomName;
     innerInfo.RoomNumber = room.RoomNumber;
     innerInfo.IpAddress  = info.IPAddress;
     innerInfo.Number     = info.DeviceNumber;
     innerInfo.Name       = info.DeviceName;
     innerInfo.Status     = enumMachineStatus.Normal;
 }
Esempio n. 3
0
 /// <summary>
 /// 内外部数据转换
 /// </summary>
 /// <param name="room"></param>
 /// <param name="info"></param>
 /// <returns></returns>
 private Machines exchangeData(FactoryRoom room, DeviceSetting info)
 {
     return(new Machines()
     {
         RoomID = room.RoomID,
         RoomName = room.RoomName,
         RoomNumber = room.RoomNumber,
         IpAddress = info.IPAddress,
         Number = info.DeviceNumber,
         Name = info.DeviceName,
         Status = enumMachineStatus.Normal
     });
 }
Esempio n. 4
0
        /// <summary>
        /// 客户端返回消息解码
        /// </summary>
        /// <param name="buff"></param>
        /// <returns></returns>
        private DeviceSetting DecodeData(byte[] buff)
        {
            DeviceSetting info = new DeviceSetting();

            byte[] tempData = buff;

            //机器码
            int locIdx = 0;

            info.DeviceId = ConvertHelper.BytesToInt16(tempData, locIdx, true);
            locIdx       += 2;
            //操作类型
            info.OperateType = tempData[locIdx++];
            int tempLen = tempData[locIdx++];

            //设备编码
            byte[] numberByte = new byte[tempLen];
            Array.Copy(tempData, locIdx, numberByte, 0, tempLen);
            info.DeviceNumber = System.Text.Encoding.GetEncoding("GBK").GetString(numberByte);
            locIdx           += tempLen;
            //车间号
            tempLen = 4;
            byte[] roomByte = new byte[tempLen];
            Array.Copy(tempData, locIdx, roomByte, 0, tempLen);
            info.RoomNumber = ConvertHelper.BytesToInt32(roomByte).ToString();
            locIdx         += tempLen;
            //IP地址
            StringBuilder builderIP = new StringBuilder();

            for (int i = 0; i < 4; i++)
            {
                builderIP.Append(tempData[locIdx++]);
                builderIP.Append('.');
            }
            info.IPAddress = builderIP.ToString().Substring(0, builderIP.Length - 1);

            return(info);
        }