public byte[] HandResetDeviceCommand(MasProtocol masProtocol, ResetDeviceCommandRequest resetDevice, ref string deviceCode, ref int commPort)
        {
            DeviceInfo                deviceinfoobject = null;
            DeviceTypeInfo            devicetypeobject = null;
            NetworkDeviceInfo         netmacobject     = null;
            ResetDeviceRequestCommand commandobject    = new ResetDeviceRequestCommand();

            byte[] sendbuff = null;
            if (resetDevice == null)
            {
                return(sendbuff);
            }
            if (!GetDeviceObject(ref deviceinfoobject, ref devicetypeobject, ref netmacobject, ProtocolType.ResetDeviceCommandRequest, resetDevice.DeviceCode))
            {
                return(sendbuff);
            }
            if (netmacobject.NetID > 0)
            {
                commPort          = deviceinfoobject.K3;
                deviceCode        = netmacobject.IP;                                                                                                 //向下的发送的MAC地址
                commandobject     = Basic.Framework.Common.ObjectConverter.Copy <ResetDeviceCommandRequest, ResetDeviceRequestCommand>(resetDevice); //得到基础数据
                commandobject.def = deviceinfoobject;                                                                                                //得到分站的对象必须是深度拷贝
                sendbuff          = commandobject.ToBytes();                                                                                         //得到通讯命令的返回Buffer数组
            }
            return(sendbuff);
        }
        public byte[] HandModificationDeviceAdress(MasProtocol masProtocol, ModificationDeviceAdressRequest modificationDeviceAdress, ref string deviceCode, ref int commPort)
        {
            DeviceInfo        deviceinfoobject = null;
            DeviceTypeInfo    devicetypeobject = null;
            NetworkDeviceInfo netmacobject     = null;
            ModificationDeviceAdressRequestCommand commandobject = new ModificationDeviceAdressRequestCommand();

            byte[] sendbuff = null;
            if (masProtocol == null || modificationDeviceAdress == null)
            {
                return(sendbuff);
            }
            if (!GetDeviceObject(ref deviceinfoobject, ref devicetypeobject, ref netmacobject, ProtocolType.ModificationDeviceAdressRequest, modificationDeviceAdress.DeviceCode))
            {
                return(sendbuff);
            }
            if (netmacobject.NetID > 0)
            {
                commPort          = deviceinfoobject.K3;
                deviceCode        = netmacobject.IP;                                                                                                                                 //向下的发送的MAC地址
                commandobject     = Basic.Framework.Common.ObjectConverter.Copy <ModificationDeviceAdressRequest, ModificationDeviceAdressRequestCommand>(modificationDeviceAdress); //得到基础数据
                commandobject.def = deviceinfoobject;                                                                                                                                //得到分站的对象必须是深度拷贝
                sendbuff          = commandobject.ToBytes();                                                                                                                         //得到通讯命令的返回Buffer数组
            }
            return(sendbuff);
        }
        /// <summary>
        /// 获取指定IP下分站队列的挂接情况
        /// </summary>
        /// <param name="mac"></param>
        /// <returns></returns>
        ///
        //public static byte[] GetNetDeviceNumber(NetworkDeviceInfo mac)
        //{
        //    string[] strfzh = null;
        //    string str = mac.Bz1;
        //    byte[] deviceNumber = new byte[8];
        //    for (int i = 0; i < deviceNumber.Length; i++)
        //    {
        //        deviceNumber[i] = 0;
        //    }
        //    if (!string.IsNullOrEmpty(str))
        //    {
        //        strfzh = str.Split('|');
        //        for (int i = 0; i < strfzh.Length; i++)
        //        {
        //            if (i > 7)
        //            {
        //                break;
        //            }
        //            deviceNumber[i] = byte.Parse(strfzh[i]);
        //        }
        //    }
        //    return deviceNumber;
        //}

        /// <summary>
        /// 判断网络模块是否有分站
        /// </summary>
        /// <param name="net">传入的网络模块</param>
        /// <returns></returns>
        public static bool CheckNetHaveFzh(NetworkDeviceInfo net)
        {
            bool result = false;

            if (net.Bz1 != null && net.Bz1.Trim() != "")
            {
                string[] strValue = net.Bz1.Split(';');
                string[] strFzh   = null;
                for (int i = 0; i < strValue.Length; i++)
                {
                    strFzh = strValue[i].Split('|');
                    for (int j = 0; j < strFzh.Length; j++)
                    {
                        if (Convert.ToInt32(strFzh[j]) > 0)
                        {
                            result = true;
                            break;
                        }
                    }
                    if (result)
                    {
                        break;
                    }
                }
            }
            return(result);
        }
        public byte[] HandQueryHistoryRealData(MasProtocol masProtocol, QueryHistoryRealDataRequest queryHistoryRealData, ref string deviceCode, ref int commPort)
        {
            DeviceInfo        deviceinfoobject = null;
            DeviceTypeInfo    devicetypeobject = null;
            NetworkDeviceInfo netmacobject     = null;
            QueryHistoryRealDataRequestCommand commandobject = new QueryHistoryRealDataRequestCommand();

            byte[] sendbuff = null;
            if (queryHistoryRealData == null)
            {
                return(sendbuff);
            }
            if (!GetDeviceObject(ref deviceinfoobject, ref devicetypeobject, ref netmacobject, ProtocolType.QueryHistoryRealDataRequest, queryHistoryRealData.DeviceCode))
            {
                return(sendbuff);
            }
            if (netmacobject.NetID > 0)
            {
                commPort                   = deviceinfoobject.K3;
                deviceCode                 = netmacobject.IP;                                                                                                                     //向下的发送的MAC地址
                commandobject              = Basic.Framework.Common.ObjectConverter.Copy <QueryHistoryRealDataRequest, QueryHistoryRealDataRequestCommand>(queryHistoryRealData); //得到基础数据
                commandobject.def          = deviceinfoobject;                                                                                                                    //得到分站的对象必须是深度拷贝
                commandobject.SerialNumber = queryHistoryRealData.SerialNumber;
                sendbuff                   = commandobject.ToBytes();                                                                                                             //得到通讯命令的返回Buffer数组
                //if (sendbuff != null)
                //{
                //    sendbuff = Cache.GetPackages(sendbuff, netmacobject, deviceinfoobject, 2, CommandCodes.ExtendCommand, (byte)devicetypeobject.LC2);//通过当前网络模块进行打包
                //}
            }
            return(sendbuff);
        }
        public byte[] HandDeviceControl(MasProtocol masProtocol, DeviceControlRequest deviceControl, ref string deviceCode, ref int commPort)
        {
            DeviceInfo                  deviceinfoobject = null;
            DeviceTypeInfo              devicetypeobject = null;
            NetworkDeviceInfo           netmacobject     = null;
            DeviceControlRequestCommand commandobject    = new DeviceControlRequestCommand();

            byte[] sendbuff = null;
            if (masProtocol == null || deviceControl == null)
            {
                return(sendbuff);
            }
            if (!GetDeviceObject(ref deviceinfoobject, ref devicetypeobject, ref netmacobject, ProtocolType.DeviceControlRequest, deviceControl.DeviceCode))
            {
                return(sendbuff);
            }
            if (netmacobject.NetID > 0)
            {
                commPort      = deviceinfoobject.K3;
                deviceCode    = netmacobject.IP;
                commandobject = Basic.Framework.Common.ObjectConverter.Copy <DeviceControlRequest, DeviceControlRequestCommand>(deviceControl); //得到基础数据
                commandobject.OrderVersion = (byte)devicetypeobject.LC2;                                                                        //设备设备类型
                commandobject.def          = deviceinfoobject;
                sendbuff = commandobject.ToBytes();                                                                                             //得到通讯命令的返回Buffer数组
            }
            return(sendbuff);
        }
        /// <summary>
        /// 获取交换机的基础信息
        /// </summary>
        /// <param name="masProtocol"></param>
        /// <param name="deviceControl"></param>
        /// <param name="deviceCode"></param>
        /// <param name="commPort"></param>
        /// <returns></returns>
        public byte[] HandGetSwitchInfo(MasProtocol masProtocol, GetSwitchboardParamCommRequest getswicthInfo, ref string deviceCode)
        {
            NetworkDeviceInfo netmacobject = null;
            GetSwitchboardParamCommRequestCommand commandobject = new GetSwitchboardParamCommRequestCommand();

            byte[] sendbuff = null;
            if (getswicthInfo == null)
            {
                return(sendbuff);
            }
            netmacobject = Cache.CacheManager.GetItemByKey <NetworkDeviceInfo>(getswicthInfo.DeviceCode, true);//按照IP搜索对象
            if (netmacobject == null)
            {
                LogHelper.Error("获取交换机的基础信息,未找到对应的IP地址【" + getswicthInfo.DeviceCode + "】");
                return(sendbuff);
            }
            lock (Cache.LockWorkNet)//用缓存的连接号和接收时间进行更新
            {
                CacheNetWork curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == netmacobject.IP); });
                if (curnet != null)
                {
                    netmacobject.NetID = curnet.NetID;
                }
            }
            if (netmacobject.NetID > 0)
            {
                deviceCode    = netmacobject.IP;
                commandobject = Basic.Framework.Common.ObjectConverter.Copy <GetSwitchboardParamCommRequest, GetSwitchboardParamCommRequestCommand>(getswicthInfo); //得到基础数据
                sendbuff      = commandobject.ToBytes();                                                                                                            //得到通讯命令的返回Buffer数组
            }
            return(sendbuff);
        }
        public byte[] HandSensorGradingAlarm(MasProtocol masProtocol, SetSensorGradingAlarmRequest sensorGradingAlarm, ref string deviceCode, ref int commPort)
        {
            DeviceInfo        deviceinfoobject = null;
            DeviceTypeInfo    devicetypeobject = null;
            NetworkDeviceInfo netmacobject     = null;
            SetSensorGradingAlarmRequestCommand commandobject = new SetSensorGradingAlarmRequestCommand();

            byte[] sendbuff = null;
            if (sensorGradingAlarm == null)
            {
                return(sendbuff);
            }
            if (!GetDeviceObject(ref deviceinfoobject, ref devicetypeobject, ref netmacobject, ProtocolType.SetSensorGradingAlarmRequest, sensorGradingAlarm.DeviceCode))
            {
                return(sendbuff);
            }
            if (netmacobject.NetID > 0)
            {
                commPort          = deviceinfoobject.K3;
                deviceCode        = netmacobject.IP;                                                                                                                     //向下的发送的MAC地址
                commandobject     = Basic.Framework.Common.ObjectConverter.Copy <SetSensorGradingAlarmRequest, SetSensorGradingAlarmRequestCommand>(sensorGradingAlarm); //得到基础数据
                commandobject.def = deviceinfoobject;                                                                                                                    //得到分站的对象必须是深度拷贝
                sendbuff          = commandobject.ToBytes();                                                                                                             //得到通讯命令的返回Buffer数组
                //if (sendbuff != null)
                //{
                //    sendbuff = Cache.GetPackages(sendbuff, netmacobject, deviceinfoobject, 2, CommandCodes.ExtendCommand, (byte)devicetypeobject.LC2);//通过当前网络模块进行打包
                //}
            }
            return(sendbuff);
        }
        public byte[] HandUpdateCancle(MasProtocol masProtocol, UpdateCancleRequest updateCancleRequest, ref string deviceCode)
        {
            DeviceInfo                 deviceinfoobject = null;
            DeviceTypeInfo             devicetypeobject = null;
            NetworkDeviceInfo          netmacobject     = null;
            UpdateCancleRequestCommand commandobject    = new UpdateCancleRequestCommand();

            byte[] sendbuff = null;
            if (masProtocol == null || updateCancleRequest == null)
            {
                return(sendbuff);
            }
            if (!GetDeviceObject(ref deviceinfoobject, ref devicetypeobject, ref netmacobject, ProtocolType.DeviceControlRequest, updateCancleRequest.DeviceCode))
            {
                return(sendbuff);
            }
            if (netmacobject.NetID > 0)
            {
                deviceCode    = netmacobject.MAC;                                                                                                   //向下的发送的MAC地址
                commandobject = Basic.Framework.Common.ObjectConverter.Copy <UpdateCancleRequest, UpdateCancleRequestCommand>(updateCancleRequest); //得到基础数据

                commandobject.DeviceId    = updateCancleRequest.DeviceId;
                commandobject.HardVersion = updateCancleRequest.HardVersion;
                commandobject.FileVersion = updateCancleRequest.FileVersion;

                commandobject.def = deviceinfoobject;        // Framework.Common.ObjectConverter.DeepCopy<DeviceInfo>(deviceinfoobject);//得到分站的对象必须是深度拷贝;
                sendbuff          = commandobject.ToBytes(); //得到通讯命令的返回Buffer数组
                //if (sendbuff != null)
                //{
                //    sendbuff = Cache.GetPackages(sendbuff, netmacobject, deviceinfoobject, 2, CommandCodes.ExtendCommand, (byte)devicetypeobject.LC2);//通过当前网络模块进行打包
                //}
            }
            return(sendbuff);
        }
Пример #9
0
        /// <summary>
        /// 回发网络模块的状态至核心业务逻辑层
        /// </summary>
        /// <param name="net">网络模块IP</param>
        private void SendNetStateToCenter(NetworkDeviceInfo net)
        {
            ProtocolDataCreatedEventArgs upData = new ProtocolDataCreatedEventArgs();
            NetworkDeviceDataRequest     cData  = new NetworkDeviceDataRequest(); //网络模块自身的实时值数据

            lock (Cache.LockWorkNet)                                              //用缓存的网络设备进行操作
            {
                CacheNetWork curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == net.IP); });
                if (curnet != null)
                {
                    net.NetID = curnet.NetID;
                    net.State = curnet.State;
                }
            }
            upData.DeviceCode  = net.IP;
            upData.DriverCode  = this.DriverCode;
            upData.MasProtocol = new MasProtocol(SystemType.Security, DirectionType.Up, ProtocolType.NetworkDeviceDataRequest);
            cData.DeviceCode   = net.IP;
            cData.RealData     = net.NetID.ToString();
            cData.Address      = net.IP;
            cData.Channel      = net.MAC;
            if (net.State == 4) //直流
            {
                cData.State = ItemState.EquipmentDC;
            }
            else if (net.State == 3) //交流
            {
                cData.State = ItemState.EquipmentAC;;
            }
            upData.MasProtocol.Protocol = cData;
            OnProtocolData(upData);
        }
Пример #10
0
 /// <summary>
 /// 复位委托函数,调用外部接口执行。
 /// </summary>
 /// <param name="netMacObject">网络模块</param>
 /// <param name="resetTwo">表示是否需要连续复位2次</param>
 /// <param name="setType">表示此次复位是什么条件发起的=1 表示强制重启,=2表示无连接号超时,重启;=3表示有连接但是超时无数据</param>
 private void ResetNetWork(NetworkDeviceInfo netMacObject, byte setType, bool resetTwo = false)
 {
     if (OnExcuteDriverCommand != null)//修正一下,如果是第一次进来时,连接两次
     {
         bool result = false;
         DriverCommandEventArgs args         = new DriverCommandEventArgs();
         DeviceResetCommand     resetcommand = new DeviceResetCommand();
         resetcommand.Mac     = netMacObject.MAC;
         resetcommand.Timeout = 2000;
         args.CommandType     = 1;
         args.JsonData        = Basic.Framework.Common.JSONHelper.ToJSONString(resetcommand);
         result = OnExcuteDriverCommand(this, args);
         if (resetTwo && !result)//如果失败需要连续复位,在此执行
         {
             Thread.Sleep(200);
             result = OnExcuteDriverCommand(this, args);
         }
         if (result)
         {
             LogHelper.Info("对" + netMacObject.MAC + "-" + netMacObject.IP + "复位成功-" + setType.ToString());
         }
         else
         {
             LogHelper.Info("对" + netMacObject.MAC + "-" + netMacObject.IP + "复位失败-" + setType.ToString());
         }
     }
 }
Пример #11
0
        private DateTime SetNetWorkNetID(NetworkDeviceInfo net)
        {
            DateTime result = DateTime.Now;

            lock (Cache.LockWorkNet)
            {
                CacheNetWork curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == net.IP); });
                if (curnet != null)
                {
                    net.NetID = curnet.NetID;
                    result    = curnet.DttBridgeReceiveTime;
                }
            }
            return(result);
        }
Пример #12
0
 private void SendSwicthBaseInfo(NetworkDeviceInfo net, byte[] data)
 {
     if (data[0] == 0x7F && data[1] == 0x10)
     {
         //ProtocolDataCreatedEventArgs upData = new ProtocolDataCreatedEventArgs();
         //upData.DeviceCode = net.MAC;
         //upData.DriverCode = this.DriverCode;
         //upData.MasProtocol = new MasProtocol(SystemType.Security, DirectionType.Up, ProtocolType.GetSwitchboardParamCommResponse);
         //GetSwitchboardParamCommResponseCommand.HandleSwitchInfo(data, upData, net);
         //OnProtocolData(upData);
     }
     else if (data[0] == 0x7F && data[1] == 0x03)
     {
         SendBatteryAffirmToCenter(net, data);
     }
 }
Пример #13
0
        /// <summary>
        /// 表示数据包回发处理后至核心业务逻辑层
        /// </summary>
        /// <param name="net"></param>
        /// <param name="data"></param>
        private void SendDevicDataAffirmToCenter(NetworkDeviceInfo net, byte[] data)
        {
            int index = 0, deviceCode;

            byte[]     fzhBuffer          = new byte[1];
            ushort     receiveDeviceCount = 0;//每包的长度
            DeviceInfo def;
            ProtocolDataCreatedEventArgs upData      = new ProtocolDataCreatedEventArgs();
            DeviceDataConrtol            dataConrtol = new DeviceDataConrtol();

            if (data.Length >= 8)
            {
                receiveDeviceCount = CommandUtil.ConvertByteToInt16(data, 6, false);//读长度,后续字节长度为-4
                deviceCode         = data[4];
                def = Cache.CacheManager.QueryFrist <DeviceInfo>(p => p.Fzh == deviceCode && p.DevPropertyID == 0, true);
                if (def != null)
                {
                    if (def.Jckz2 == net.IP)//只处理分站定义相对应的IP。
                    {
                        if (4 + receiveDeviceCount > data.Length)
                        {//数据长度不足
                            LogHelper.Error("回发长度不足:" + net.MAC + "  回发长度:" + data.Length + " 判断长度:" + (4 + receiveDeviceCount).ToString());
                            return;
                        }
                        fzhBuffer = new byte[data.Length - 4];
                        for (index = 4; index < data.Length; index++)
                        {
                            fzhBuffer[index - 4] = data[index];
                        }
                        upData.DriverCode  = this.DriverCode;
                        upData.DeviceCode  = def.Point;
                        upData.MasProtocol = new MasProtocol((SystemType)(def.Sysid), DirectionType.Up, ProtocolType.QueryRealDataResponse);
                        upData.MasProtocol.DeviceNumber = Convert.ToUInt16(def.Fzh);
                        dataConrtol.def = def;
                        dataConrtol.HandleDeviceData(fzhBuffer, upData.MasProtocol);
                        OnProtocolData(upData);
                    }
                }
            }
        }
Пример #14
0
        /// <summary>
        /// 表示交换机电源的确认
        /// </summary>
        /// <param name="net"></param>
        /// <param name="data"></param>
        private void SendBatteryAffirmToCenter(NetworkDeviceInfo net, byte[] data)
        {
            //7F-03-2A-00-00-01-8A-01-8A-01-8A-01-8A-01-8A-01-8A-00-00-00-00-09-41-00-40-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-E8-07

            ProtocolDataCreatedEventArgs upData = new ProtocolDataCreatedEventArgs();
            QueryBatteryRealDataResponse cData  = new QueryBatteryRealDataResponse();//获取交换机电源箱的应答数据;

            //需要反过来找一下,串口服务器对应的交换机IP地址。
            upData.DeviceCode          = net.IP;
            upData.DriverCode          = this.DriverCode;
            upData.MasProtocol         = new MasProtocol(SystemType.Security, DirectionType.Up, ProtocolType.QueryBatteryRealDataResponse);
            cData.DeviceCode           = net.IP;
            cData.DeviceProperty       = ItemDevProperty.Switches;
            cData.BatteryRealDataItems = new List <BatteryRealDataItem>();
            BatteryRealDataItem BatteryItem = new BatteryRealDataItem();

            cData.BatteryDateTime = DateTime.Now;
            Cache.HandleDeviceBattery(data, 5, BatteryItem, true);//解析电源箱的数据
            cData.BatteryRealDataItems.Add(BatteryItem);
            upData.MasProtocol.Protocol = cData;
            OnProtocolData(upData);
        }
        public byte[] HandTimeSynchronization(MasProtocol masProtocol, TimeSynchronizationRequest timeSynchronization, ref string deviceCode, ref int commPort)
        {
            DeviceTypeInfo devicetypeobject = null;

            DeviceInfo        deviceinfoobject = null;
            NetworkDeviceInfo netmacobject     = null;
            TimeSynchronizationRequestCommand commandobject = new TimeSynchronizationRequestCommand();

            byte[] sendbuff = null;
            if (timeSynchronization == null)
            {
                return(sendbuff);
            }
            if (timeSynchronization.DeviceCode.Length < 4)
            {//服务端只发下了分站号,自动生成分站的测点号
                timeSynchronization.DeviceCode = timeSynchronization.DeviceCode.PadLeft(3, '0') + "0000";
            }
            if (!GetDeviceObject(ref deviceinfoobject, ref devicetypeobject, ref netmacobject, ProtocolType.TimeSynchronizationRequest, timeSynchronization.DeviceCode))
            {
                return(sendbuff);
            }
            lock (Cache.LockWorkNet)//用缓存的连接号和接收时间进行更新
            {
                CacheNetWork curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == netmacobject.IP); });
                if (curnet != null)
                {
                    netmacobject.NetID = curnet.NetID;
                }
            }
            if (netmacobject.NetID > 0)
            {
                commPort          = timeSynchronization.CommPort;
                deviceCode        = netmacobject.IP;                                                                                                                  //向下的发送的MAC地址
                commandobject     = Basic.Framework.Common.ObjectConverter.Copy <TimeSynchronizationRequest, TimeSynchronizationRequestCommand>(timeSynchronization); //得到基础数据
                commandobject.def = deviceinfoobject;                                                                                                                 //得到分站的对象必须是深度拷贝
                sendbuff          = commandobject.ToBytes();                                                                                                          //得到通讯命令的返回Buffer数组
            }
            return(sendbuff);
        }
        public byte[] HandInitialize(MasProtocol masProtocol, InitializeRequest Initialize, ref string deviceCode, ref int commPort)
        {
            DeviceInfo               deviceinfoobject = null;
            DeviceTypeInfo           devicetypeobject = null;
            NetworkDeviceInfo        netmacobject     = null;
            InitializeRequestCommand commandobject    = new InitializeRequestCommand();

            byte[] sendbuff = null;
            if (Initialize == null)
            {
                return(sendbuff);
            }
            if (!GetDeviceObject(ref deviceinfoobject, ref devicetypeobject, ref netmacobject, ProtocolType.InitializeRequest, Initialize.DeviceCode))
            {
                return(sendbuff);
            }
            if (netmacobject.NetID > 0)
            {
                commPort      = deviceinfoobject.K3;
                deviceCode    = netmacobject.IP;                                                                                       //向下的发送的MAC地址
                commandobject = Basic.Framework.Common.ObjectConverter.Copy <InitializeRequest, InitializeRequestCommand>(Initialize); //得到基础数据
                commandobject.OrderVersion       = (byte)devicetypeobject.LC2;                                                         //设备设备类型
                commandobject.def                = deviceinfoobject;
                commandobject.DeviceListItem     = Cache.CacheManager.Query <DeviceInfo>(p => p.Fzh == deviceinfoobject.Fzh, true);
                commandobject.DeviceTypeListItem = Cache.CacheManager.GetAll <DeviceTypeInfo>(true);
                if (commandobject.DeviceListItem == null)
                {
                    commandobject.DeviceListItem = new List <DeviceInfo>();
                }
                if (commandobject.DeviceTypeListItem == null)
                {
                    commandobject.DeviceTypeListItem = new List <DeviceTypeInfo>();
                }
                sendbuff = commandobject.ToBytes();//得到通讯命令的返回Buffer数组
            }

            return(sendbuff);
        }
        public byte[] HandCallPersonCommand(MasProtocol masProtocol, CallPersonRequest callPerson, ref string deviceCode)
        {
            DeviceInfo               deviceinfoobject = null;
            NetworkDeviceInfo        netmacobject     = null;
            CallPersonRequestCommand commandobject    = new CallPersonRequestCommand();

            byte[] sendbuff = null;
            if (callPerson == null)
            {
                return(sendbuff);
            }
            netmacobject = Cache.CacheManager.GetItemByKey <NetworkDeviceInfo>(callPerson.DeviceCode, true);//按照MAC搜索对象
            if (netmacobject == null)
            {
                LogHelper.Error("呼叫命令,未找到对应的MAC地址【" + callPerson.DeviceCode + "】");
                return(sendbuff);
            }
            lock (Cache.LockWorkNet)//用缓存的连接号和接收时间进行更新
            {
                CacheNetWork curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == netmacobject.IP); });
                if (curnet != null)
                {
                    netmacobject.NetID = curnet.NetID;
                }
            }
            if (netmacobject.NetID > 0)
            {
                deviceCode    = netmacobject.IP;                                                                                       //向下的发送的MAC地址
                commandobject = Basic.Framework.Common.ObjectConverter.Copy <CallPersonRequest, CallPersonRequestCommand>(callPerson); //得到基础数据
                sendbuff      = commandobject.ToBytes();                                                                               //得到通讯命令的返回Buffer数组
                //if (sendbuff != null)
                //{
                //    sendbuff = Cache.GetPackages(sendbuff, netmacobject, deviceinfoobject, 2, CommandCodes.CallPersonCommand, 0);//通过当前网络模块进行打包
                //}
            }
            return(sendbuff);
        }
        /// <summary>
        /// 外包解包处理
        /// </summary>
        /// <param name="data">收到的网络数据包</param>
        /// <returns></returns>
        public static KJ73NCommand ToCommand(byte[] data, NetworkDeviceInfo net)
        {
            KJ73NCommand command = new Commands.KJ73NCommand();

            try
            {
                if (data.Length > 999 || data.Length < 4)
                {
                    command.IsSuccess    = false;
                    command.errorMessage = "数据包总长度错误:【" + data.Length + "】";
                    return(command);
                }
                if (data[0] == 0x7F && (data[1] == 0x03 || data[1] == 0x10))
                {
                    if (data[1] == 0x03 && data.Length >= 40)
                    {                            //数据回发
                        command.IsSuccess   = true;
                        command.PackageType = 2; //表示包类型---data[4]
                        lock (Cache.LockWorkNet) //用缓存的网络设备进行操作
                        {
                            CacheNetWork curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == net.IP); });
                            if (curnet != null)
                            {
                                curnet.State = 3;
                            }
                        }
                    }
                    else if (data[1] == 0x10 && data.Length >= 7)
                    {                            //控制回发
                        command.IsSuccess   = true;
                        command.PackageType = 2; //表示包类型---data[4]
                        lock (Cache.LockWorkNet) //用缓存的网络设备进行操作
                        {
                            CacheNetWork curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == net.IP); });
                            if (curnet != null)
                            {
                                curnet.State = 3;
                            }
                        }
                    }
                    else
                    {
                        command.IsSuccess    = false;
                        command.errorMessage = string.Format("交换机回发错误:{0}-{1}-{2}-{3}", data[0], data[1], data[2], data.Length);
                        return(command);
                    }
                }
                else
                {
                    if ((data[0] != 0x3E) && (data[1] != 0xE3) && (data[2] != 0x90) && (data[3] != 0x09))
                    {
                        command.IsSuccess    = false;
                        command.errorMessage = string.Format("引导符错误:{0}-{1}-{2}-{3}", data[0], data[1], data[2], data[3]);
                        return(command);
                    }
                    command.IsSuccess   = true;
                    command.PackageType = 1;//表示包类型---data[4]
                    if (data[4] == 252)
                    {
                        command.PackageType = 2; //表示为交换机的状态信息数据包
                    }
                    lock (Cache.LockWorkNet)     //用缓存的网络设备进行操作
                    {
                        CacheNetWork curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == net.IP); });
                        if (curnet != null)
                        {
                            curnet.State = 3;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                command.IsSuccess    = false;
                command.errorMessage = ex.Message;
            }
            return(command);
        }
Пример #19
0
        /// <summary>
        /// 处理网络接收的数据(上行)
        /// </summary>
        /// <param name="data">收到的网络数据包</param>
        /// <param name="net">接收到此数据包的网络模块</param>
        public void HandleNetData(byte[] data, string ip)
        {
            CacheNetWork      curnet;
            bool              tcnet = false;
            NetworkDeviceInfo net   = Cache.CacheManager.QueryFrist <NetworkDeviceInfo>(p => p.IP == ip, true);

            //增加交换机处理
            if (net == null)
            {//再找一下交换机对应的串口服务器IP
                tcnet = true;
                net   = Cache.CacheManager.QueryFrist <NetworkDeviceInfo>(p => p.Bz6 == ip, true);
            }
            if (Cache.ShowYmOut == 1)           //有源码输出才进行相关的处理
            {
                Cache.AddCommData(data, ip, 1); //源码输出
            }
            if (net == null)
            {
                return;
            }
            KJ73NCommand command = KJ73NCommand.ToCommand(data, net);

            if (!command.IsSuccess)
            {
                lock (Cache.LockWorkNet)//用缓存的网络设备进行操作
                {
                    curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == net.IP); });
                    if (curnet != null)
                    {
                        curnet.BBridgeRevMark = false;
                    }
                }
                LogHelper.Info(command.errorMessage);
                return;
            }
            SendNetStateToCenter(net); //发送网络模块的实时数据至中心站;
            lock (Cache.LockWorkNet)   //用缓存的网络设备进行操作
            {
                if (tcnet)
                {
                    curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == net.Bz6); });
                }
                else
                {
                    curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == net.IP); });
                }
                if (curnet != null)
                {
                    curnet.BBridgeRevMark       = true;         //表示数据包接收正常
                    curnet.DttBridgeReceiveTime = DateTime.Now; //用于存储最新接收到数据时的时间
                }
                else
                {//是不正常情况,输出日志,但是在此,也先行进行处理。
                    LogHelper.Error("错误日志:" + net.MAC + "  当数据包上行时,未找到未相应的缓存网络模块链表!!");
                    curnet       = new Driver.CacheNetWork();
                    curnet.State = 3;
                    curnet.MAC   = net.MAC;
                    curnet.IP    = net.IP;
                    curnet.NetID = 0;
                    curnet.DttBridgeReceiveTime = DateTime.Now;//用于存储最新接收到数据时的时间
                    curnet.BBridgeRevMark       = false;
                    Cache.LstWorkNet.Add(curnet);
                }
            }
            switch (command.PackageType)
            {
            case 1:    //上行的数据包
                SendDevicDataAffirmToCenter(net, data);
                LogHelper.Info("收到数据包:" + net.IP + "-" + net.MAC + "  长度:" + data.Length);
                break;

            case 2:    //交换机信息
                SendSwicthBaseInfo(net, data);
                break;
            }
        }
Пример #20
0
        /// <summary>
        /// 网关通讯状态变化通知
        /// </summary>
        /// <param name="stateChangeArgs">通讯状态变化参数</param>
        public void CommunicationStateChangeNotify(CommunicationStateChangeArgs stateChangeArgs)
        {
            bool         flag = false;
            CacheNetWork curnet;
            int          curid = 0;

            if (stateChangeArgs.CommunicationState == CommunicationState.Connect)
            {
                //网络连接成功处理
                flag = true;
                NetworkDeviceInfo net = Cache.CacheManager.QueryFrist <NetworkDeviceInfo>(p => p.IP == stateChangeArgs.UniqueCode, true);
                if (net == null)
                {
                    net = new NetworkDeviceInfo();
                }
                net.NetID = (int)stateChangeArgs.ConntecionId;
                net.State = 3;
                net.IP    = stateChangeArgs.UniqueCode;
                lock (Cache.LockWorkNet)
                {
                    curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == stateChangeArgs.UniqueCode); });
                    flag   = false;
                    if (curnet == null)
                    {
                        flag   = true;
                        curnet = new Driver.CacheNetWork();
                    }
                    curnet.DttBridgeReceiveTime = DateTime.Now;
                    curnet.State          = 3;
                    curnet.IP             = stateChangeArgs.UniqueCode;
                    curnet.NetID          = (int)stateChangeArgs.ConntecionId;
                    curnet.BBridgeRevMark = false;
                    if (flag)
                    {
                        Cache.LstWorkNet.Add(curnet);
                    }
                }
                SendNetStateToCenter(net);
                LogHelper.Info("收到" + curnet.IP + "-请求连接命令!");
            }
            else if (stateChangeArgs.CommunicationState == CommunicationState.Disconnect)
            {
                //网络断开处理
                NetworkDeviceInfo net = Cache.CacheManager.QueryFrist <NetworkDeviceInfo>(p => p.IP == stateChangeArgs.UniqueCode, true);
                if (net == null)
                {
                    net = new NetworkDeviceInfo();
                }
                curid = GetMacConnectionID(stateChangeArgs.UniqueCode);
                if (curid > 0)
                {
                    if (stateChangeArgs.ConntecionId < curid)
                    {
                        LogHelper.Info("断开连接不处理:ConnectIdOld=" + curid + ",ConnectIdNew=" + stateChangeArgs.ConntecionId);
                        return;
                    }
                }
                net.IP    = stateChangeArgs.UniqueCode;
                net.NetID = 0;
                net.State = (short)ItemState.EquipmentInterrupted;

                lock (Cache.LockWorkNet)//更新网络模块缓存
                {
                    curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == stateChangeArgs.UniqueCode); });
                    if (curnet != null)
                    {
                        curnet.DttBridgeReceiveTime = DateTime.Now.AddSeconds(-(ReDoSetTime - 10));
                        curnet.State          = 0;
                        curnet.NetID          = 0;
                        curnet.BBridgeRevMark = false;
                    }
                }
                SendNetStateToCenter(net);
                LogHelper.Info("收到" + curnet.IP + "-断开连接请求!");
            }
        }
        public static void HandleSwitchInfo(byte[] data, ProtocolDataCreatedEventArgs upData, NetworkDeviceInfo net)
        {
            GetSwitchboardParamCommResponse cData = new GetSwitchboardParamCommResponse();//交换机基本信息;

            byte[] buffer = new byte[data.Length - 4];
            ushort startindex = 32760;         //数据开始位置
            int    receivelength = 0;          //下标|接收数据长度
            ushort crcvalue = 0, receivevalue; //crc累加和 回发累加和
            byte   curByte = 0, SignCount = 0;

            cData.DeviceCode    = net.MAC;
            cData.RealDataItems = new List <RealDataItem>();
            for (int i = 4; i < data.Length; i++)
            {
                buffer[i - 4] = data[i];
            }
            if (buffer[0] == 252)
            {
                startindex = 0;
            }
            if (startindex == 32760)
            {
                LogHelper.Error("【HandleSwitchInfo】" + "没有长到分站地址引导符【252】");
                return;
            }
            receivelength = CommandUtil.ConvertByteToInt16(buffer, startindex + 2);
            if (receivelength > buffer.Length)
            {
                LogHelper.Error("【HandleSwitchInfo】" + "回发长度不足【" + receivelength + "】" + "【" + data.Length + "】");
                return;
            }
            receivevalue = CommandUtil.ConvertByteToInt16(buffer, startindex + receivelength - 2);
            crcvalue     = CommandUtil.CRC16_CCITT(buffer, startindex, startindex + receivelength - 2);
            if (crcvalue != receivevalue)
            {
                LogHelper.Error("【HandleSwitchInfo】" + "通讯误码【" + crcvalue + "】" + "【" + receivevalue + "】");
                return;
            }

            #region 协议解析
            //5A A5 3C C3 FC 46 00 12 C1 04 64 FF FF 00 F8 C1 C1 00 00 00 0B 84
            curByte   = buffer[startindex + 5];
            SignCount = (byte)(curByte >> 2);
            cData.BatteryControlState = (byte)(curByte & 0x01);
            cData.BatteryState        = (byte)((curByte >> 1) & 0x01);
            startindex            = (ushort)(startindex + 6);
            cData.BatteryCapacity = buffer[startindex++];
            curByte = buffer[startindex++];
            cData.SerialPortBatteryState = (byte)((curByte >> 6) & 0x01); //(串口服务器-供电电源)
            cData.SerialPortRunState     = (byte)((curByte >> 5) & 0x01); //(串口服务器-运行状态)
            cData.SwitchRunState         = (byte)((curByte >> 1) & 0x01); //(交换机-运行状态)
            cData.SwitchBatteryState     = (byte)(curByte & 0x01);        //(交换机-供电电源)
            cData.Switch1000State        = new byte[3];
            cData.Switch1000State[0]     = (byte)((curByte >> 2) & 0x01); //Bit2位(千兆光口1)
            cData.Switch1000State[1]     = (byte)((curByte >> 3) & 0x01); //Bit2位(千兆光口2)
            cData.Switch1000State[2]     = (byte)((curByte >> 4) & 0x01); //Bit2位(千兆光口3)
            curByte = buffer[startindex++];
            cData.Switch100State = new byte[7];
            for (int i = 0; i < 7; i++)
            {
                cData.Switch100State[i] = (byte)((curByte >> i) & 0x01); //Bit2位(百兆接口1~7)
            }
            startindex++;                                                //预留字节
            if (SignCount > 0)
            {
                HandleSoleCoding(SignCount, buffer, startindex, cData);
            }
            #endregion
            upData.MasProtocol.Protocol = cData;
        }
        public byte[] HandQueryBatteryRealData(MasProtocol masProtocol, QueryBatteryRealDataRequest batteryRealData, ref string deviceCode, ref int commPort)
        {
            List <NetworkDeviceInfo> lstNet = new List <NetworkDeviceInfo>();
            DeviceInfo                    deviceinfoobject = null;
            DeviceTypeInfo                devicetypeobject = null;
            NetworkDeviceInfo             netmacobject     = null;
            BatteryRealDataRequestCommand commandobject    = new BatteryRealDataRequestCommand();

            byte[] sendbuff = null;
            if (batteryRealData == null)
            {
                return(sendbuff);
            }
            if (batteryRealData.DevProperty == ItemDevProperty.Switches)
            {//如果是交换机的操作,仅获取对应的网络地址即可。
                lstNet = Cache.CacheManager.Query <NetworkDeviceInfo>(p => (p.MAC == batteryRealData.DeviceCode) && (p.Upflag == "1"), true);
                if (lstNet.Count > 0)
                {
                    netmacobject = lstNet[0];
                }
                //netmacobject = Cache.CacheManager.GetItemByKey<NetworkDeviceInfo>(batteryRealData.DeviceCode, true);//按照MAC搜索对象
                if (netmacobject == null)
                {
                    LogHelper.Error("查询交换机的电源,未找到对应的IP地址【" + batteryRealData.DeviceCode + "】");
                    return(sendbuff);
                }
                lock (Cache.LockWorkNet)//用缓存的连接号和接收时间进行更新
                {
                    CacheNetWork curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == netmacobject.Bz6); });
                    if (curnet != null)
                    {
                        netmacobject.NetID = curnet.NetID;
                    }
                }
            }
            else
            {
                if (!GetDeviceObject(ref deviceinfoobject, ref devicetypeobject, ref netmacobject, ProtocolType.QueryBatteryRealDataRequest, batteryRealData.DeviceCode))
                {
                    return(sendbuff);
                }
            }
            if (netmacobject.NetID > 0)
            {
                if (batteryRealData.DevProperty == ItemDevProperty.Switches)
                {
                    deviceCode = netmacobject.Bz6;//向下的发送的MAC地址
                    commPort   = 1;
                }
                else
                {
                    commPort = deviceinfoobject.K3;

                    deviceCode = netmacobject.IP;                                                                                                          //向下的发送的MAC地址
                }
                commandobject = Basic.Framework.Common.ObjectConverter.Copy <QueryBatteryRealDataRequest, BatteryRealDataRequestCommand>(batteryRealData); //得到基础数据
                if (devicetypeobject != null)
                {
                    commandobject.OrderVersion = (byte)devicetypeobject.LC2;
                }
                commandobject.def          = deviceinfoobject; // Framework.Common.ObjectConverter.DeepCopy<DeviceInfo>(deviceinfoobject);//得到分站的对象必须是深度拷贝
                commandobject.NetMacObject = netmacobject;
                sendbuff = commandobject.ToBytes();            //得到通讯命令的返回Buffer数组
            }
            return(sendbuff);
        }
        private bool GetDeviceObject(ref DeviceInfo deviceInfoObject, ref DeviceTypeInfo deviceTypeObject, ref NetworkDeviceInfo netMacObject, ProtocolType pType, string deviceCode)
        {
            string ipstr = "";
            List <NetworkDeviceInfo> lstNet = new List <NetworkDeviceInfo>();

            deviceInfoObject = Cache.CacheManager.GetItemByKey <DeviceInfo>(deviceCode, true);//得到对应的分站对象
            if (deviceInfoObject == null)
            {
                LogHelper.Info("未找到分站【" + deviceCode + "】:" + pType.ToString());
                return(false);
            }
            Point  = deviceInfoObject.Point; //用于外部显示分站号
            ipstr  = deviceInfoObject.Jckz2; //获取IP
            lstNet = Cache.CacheManager.Query <NetworkDeviceInfo>(p => (p.IP == ipstr) && (p.Upflag == "0"), true);
            if (lstNet.Count > 0)
            {
                netMacObject = lstNet[0];
            }
            else
            {
                LogHelper.Info("未找到分站【" + deviceCode + "】对应的网络地址【" + deviceInfoObject.Jckz2 + "】:" + pType.ToString());
                return(false);
            }
            lock (Cache.LockWorkNet)//用缓存的连接号进行更新
            {
                CacheNetWork curnet = Cache.LstWorkNet.Find(delegate(CacheNetWork p) { return(p.IP == ipstr); });
                if (curnet != null)
                {
                    netMacObject.NetID = curnet.NetID;
                }
            }
            deviceTypeObject = Cache.CacheManager.GetItemByKey <DeviceTypeInfo>(deviceInfoObject.Devid, true);//按照devid搜索对象
            if (deviceTypeObject == null)
            {
                LogHelper.Info("未找到分站【" + deviceCode + "】对应的设备类型【" + deviceInfoObject.Devid + "】:" + pType.ToString());
                return(false);
            }
            return(true);
        }