using System; using System.Collections.Generic; using System.Linq; using System.Text; using Snap7; using Model; using DBFactory; using System.Data; using Snap7Client; namespace Snap7Server { public static class CSnap7Server { public static object obj = new object(); private static S7Server.TSrvCallback TheEventCallBack; // <== Static var containig the callback private static S7Server.TSrvCallback TheReadCallBack; private static Dictionary server_dic = new Dictionary();//ip与s7server private static Dictionary callback_para = new Dictionary();//plc名与ip private static Dictionary db1_dic = new Dictionary();//ip与db1 private static Dictionary db2_dic = new Dictionary();//ip与db2 private static Dictionary db_length = new Dictionary();//ip与每个设备的db长度 public static Dictionary> conveyorRouteList = new Dictionary>();//Task 的所有输送机 public static Dictionary> taskDeviceList = new Dictionary>();//Task 的所有设备 public static DBOperator dbo = new DBOperator(); static StringBuilder sql = new StringBuilder(); public static void InitSnap7Server() { //根据数据库初始化字典, sql.Remove(0, sql.Length); sql.Append("select distinct F_RemoteIP from T_Base_Device where (F_RemoteIP is not null) "); DataView dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView; for (int i=0; i db2_length ? db1_length : db2_length; byte[] db1_temp = new byte[len]; byte[] db2_temp = new byte[len]; for (int i=0; i Server_dic { get => server_dic; set => server_dic = value; } public static Dictionary Db1_dic { get => db1_dic; set => db1_dic = value; } public static Dictionary Db2_dic { get => db2_dic; set => db2_dic = value; } public static Dictionary Db_length { get => db_length; set => db_length = value; } public static Dictionary Callback_para { get => callback_para; set => callback_para = value; } public static Dictionary> ConveyorRouteList { get => conveyorRouteList; set => conveyorRouteList = value; } //根据设备号,获取DB1 //根据设备号,获取DB2 //根据设备号,返回设备信息 public static Model.MDevice GetDeviceInfo(int DeviceIndex) { return Model.CGetInfo.GetDeviceInfo(DeviceIndex); } //根据设备号,自动改变状态, public static bool AutoChangeAllState(int DeviceIndex) { foreach(var ip in Db2_dic.Keys) { foreach(var device in CGetInfo.DeviceInfo.Values) { if(device.RemoteIP == ip) { //if } } } return true; } //根据设备号,清零 DB2 public static void ClearDeviceDB2(int DeviceIndex) { askDevinfo = GetDeviceInfo(DeviceIndex); //for (int i=askDevinfo.Dbw2Address; i 0) { devKind = Convert.ToInt32(dv[0]["F_DeviceKindIndex"].ToString()); return Convert.ToInt32(dv[0]["F_DeviceIndex"].ToString()); } return 0; } public static void initTaskRoute(string task) { if (ConveyorRouteList.ContainsKey(task) == true) return; sql.Remove(0, sql.Length); sql.Append("select FCONTROLTASKTYPE from T_Manage_Task, T_Monitor_task where fid = F_ManageTaskIndex and F_MonitorIndex = '" + task + "'"); DataView dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView; string nextDeviceName = ""; if (dv.Count > 0) { int type = Convert.ToInt32(dv[0]["FCONTROLTASKTYPE"].ToString()); if (type == 1) { nextDeviceName = "F_InputNextDevice"; } else { nextDeviceName = "F_OutputNextDevice"; } } sql.Remove(0, sql.Length); sql.Append("select F_NumParam1,F_NumParam4 from T_Monitor_Task where F_MonitorIndex = '" + task + "'"); dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView; int startDev = 0; int endDev = 0; if (dv.Count > 0) { startDev = Convert.ToInt32(dv[0]["F_NumParam1"].ToString()); endDev = Convert.ToInt32(dv[0]["F_NumParam4"].ToString()); List> devs = RecursionDevices(startDev, endDev, nextDeviceName); if (devs.Count > 0) { if (conveyorRouteList.ContainsKey(task.ToString()) == false) { conveyorRouteList.Add(task.ToString(), devs[0]); } } } return ; } public static List> RecursionDevices(int startdev, int enddev, string IONextDevice) { string sql = ""; DataView dv; string nextdev = ""; string[] spl; char[] cc = new char[1] { ';' }; List> routedevs = new List>(); sql = "SELECT " + IONextDevice + " FROM T_Base_Device where F_DeviceIndex=" + startdev; dv = dbo.ExceSQL(sql).Tables[0].DefaultView; if (dv.Count > 0) { nextdev = dv[0][IONextDevice].ToString(); if (nextdev.Length > 0) { spl = nextdev.Split(cc); for (int i = spl.GetLowerBound(0); i <= spl.GetUpperBound(0); i++) { if (routedevs.Count != 0 && routedevs[0].Contains(enddev) == true) return routedevs; List> rs = RecursionSingle(Convert.ToInt32(spl[i]), enddev, IONextDevice); if (rs.Count > 0) { //返回多条路经设备集合的集合 List rss = new List(); for (int ii = 0; ii < rs.Count; ii++) { rss = rs[ii]; if (rss.Contains(Convert.ToInt32(spl[i])) == false) { rss.Add(Convert.ToInt32(spl[i])); } if (rss.Contains(startdev) == false) { rss.Add(startdev); } if (routedevs.Contains(rss) == false) { routedevs.Add(rss); } } //Predicate> FindValue = delegate(List obj) { return obj.Contains(Convert.ToInt32(spl[i])); }; } } return routedevs; } } return routedevs; } static List> RecursionSingle(int startdev, int enddev, string IONextDevice) { char[] cc = new char[1] { ';' }; List routedevs = new List(); List> mutiRoute = new List>(); #region 递归获得多条路径详细设备集合 if (startdev == enddev)//找到了终点设备 { if (routedevs.Contains(enddev) == false) { routedevs.Add(enddev); } if (routedevs.Contains(startdev) == false) { routedevs.Add(startdev); } mutiRoute.Add(routedevs); return mutiRoute; } else//接着找 { List> mutireturndevs = RecursionDevices(startdev, enddev, IONextDevice); if (mutireturndevs.Count > 0) { for (int k = 0; k < mutireturndevs.Count; k++) { if (mutireturndevs[k].Contains(startdev) == false) { mutireturndevs[k].Add(startdev); } mutiRoute.Add(mutireturndevs[k]); } } return mutiRoute; } #endregion } } }