Exemplo n.º 1
0
        /// <summary>
        /// Process a stream, reading and parsing its content
        /// </summary>
        /// <param name="InLines"></param>
        /// <param name="FileName"></param>
        public static void ProcessStreamRead(StreamReader sRd, String FileName, Type TargetType, ref DateTime LatestBatchID)
        {
            //Now, parse our stream
            IList OutList = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(TargetType));

            //Read our header
            String FirstLine = sRd.ReadLine().TrimStart('#').TrimEnd(',');

            String[] HeaderLine;
            Double   ProperDate;
            DateTime BatchId = DateTime.Now;

            if (Double.TryParse(FirstLine, out ProperDate))
            {
                BatchId    = new DateTime(1970, 1, 1).AddSeconds(ProperDate);
                HeaderLine = sRd.ReadLine().TrimStart('#').Split(',');
            }
            else
            {
                HeaderLine = FirstLine.Split(',');
            }

            if (BatchId < LatestBatchID)
            {
                MM_Notification.WriteLine(ConsoleColor.Red, "Aborting {0} Batch {1} because it's older than {1}", FileName, BatchId, LatestBatchID);
                return;
            }
            else
            {
                LatestBatchID = BatchId;
            }

            PropertyInfo[] HeaderInfo = MM_Serialization.GetHeaderInfo(TargetType, HeaderLine);
            for (int a = 0; a < HeaderLine.Length; a++)
            {
                if (HeaderInfo[a] == null)
                {
                    MM_Notification.WriteLine(ConsoleColor.Yellow, "Unknown variable {0} in {1}", HeaderLine[a], FileName);
                }
            }

            //Confirm all of our headers are present
            foreach (PropertyInfo pI in TargetType.GetProperties())
            {
                if (Array.FindIndex <String>(HeaderLine, T => T.Equals(pI.Name, StringComparison.CurrentCultureIgnoreCase)) == -1)
                {
                    MM_Notification.WriteLine(ConsoleColor.Yellow, "Missing variable {0} ({1}) in {2} / {3}", pI.Name, pI.PropertyType.Name, TargetType.Name, FileName);
                }
            }

            //Now, read in all of our lines of data, using reflection to store our data
            String InLine;

            while ((InLine = sRd.ReadLine()) != null)
            {
                if (InLine.StartsWith("#"))
                {
                    DateTime EndTime = new DateTime(1970, 1, 1).AddSeconds(Convert.ToDouble(InLine.TrimStart('#').TrimEnd(',')));
                    if (EndTime != BatchId)
                    {
                        MM_Notification.WriteLine(ConsoleColor.Red, "Mismatch date on {0}: Start {1}, End {2}", FileName, BatchId, EndTime);
                    }
                }
                else
                {
                    Object OutObj = MM_Serialization.Deserialize(HeaderInfo, InLine.Split(','), Activator.CreateInstance(TargetType));
                    //if (OutObj is MacomberMapCommunications.Messages.EMS.MM_BreakerSwitch_Data)
                    //{
                    //    MacomberMapCommunications.Messages.EMS.MM_BreakerSwitch_Data bs = (MacomberMapCommunications.Messages.EMS.MM_BreakerSwitch_Data)OutObj;
                    //    if (bs.TEID_CB == 118964)
                    //        MM_Notification.WriteLine(ConsoleColor.Magenta, "   TEID=" + bs.TEID_CB.ToString() + "    status=" + (bs.Open_CB ? "Open" : "Closed"));
                    //}
                    OutList.Add(OutObj);
                }
            }

            //Once our data are done, use the interprocess communication to update our data
            if (TargetType == typeof(MM_EMS_Command))
            {
                MM_Server.EMSCommands.Clear();
                foreach (Object obj in OutList)
                {
                    MM_Server.EMSCommands.Add((MM_EMS_Command)obj);
                }
            }
            else
            {
                MM_EMS_Data_Updater.ProcessUpdate(TargetType, (Array)OutList.GetType().GetMethod("ToArray").Invoke(OutList, null));
            }
            OutList.Clear();
            OutList = null;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Process an incoming client
        /// </summary>
        /// <param name="state"></param>
        private static void ProcessTCPMessage(object state)
        {
            //Now, notify that we have a connection, let's create our stream reader around it, and retrieve our header
            TcpClient Conn      = (TcpClient)state;
            String    FileName  = "";
            DateTime  StartTime = DateTime.Now;

            //If our source IP address changes, notify the console
            IPAddress Address = ((IPEndPoint)Conn.Client.RemoteEndPoint).Address;

            if (Address.ToString() != MM_Server.LastTEDEAddress.ToString())
            {
                MM_Notification.WriteLine(ConsoleColor.Yellow, "TEDE: IP Source Address change detected: {0}, last {1}", Address, MM_Server.LastTEDEAddress);
            }
            MM_Server.LastTEDEAddress = Address;

            using (NetworkStream nRd = Conn.GetStream())
                using (StreamReader sRd = new StreamReader(nRd))
                    try
                    {
                        //Define our key variables
                        List <String>  OutList     = new List <string>();
                        bool           AtBeginning = true;
                        PropertyInfo[] HeaderInfo  = null;
                        DateTime       BatchId     = default(DateTime);
                        Type           TargetType  = null;

                        //Now, read in all of our lines of data, using reflection to store our data
                        String InLine;
                        while ((InLine = sRd.ReadLine()) != null)
                        {
                            if (InLine.StartsWith("#"))
                            {
                                String[] splStr = InLine.TrimStart('#').TrimEnd(',').Split(',');
                                MM_Server.LastTEDEUpdate = DateTime.Now;

                                //If we're at the beginning, process accordingly
                                if (AtBeginning)
                                {
                                    StartTime = DateTime.Now;
                                    //Pull in, and parse our first line.
                                    BatchId  = new DateTime(1970, 1, 1).AddSeconds(Convert.ToDouble(splStr[0]));
                                    FileName = splStr[1];
                                    if (!FileName.EndsWith(".csv", StringComparison.CurrentCultureIgnoreCase))
                                    {
                                        FileName += ".csv";
                                    }

                                    if (InputTypes.TryGetValue(FileName, out TargetType))
                                    {
                                        //Now that we have our first, line, build our outgoing list
                                        OutList.Clear();

                                        //Read our header
                                        String[] HeaderLine = sRd.ReadLine().TrimStart('#').Split(',');

                                        HeaderInfo = MM_Serialization.GetHeaderInfo(TargetType, HeaderLine);
                                        for (int a = 0; a < HeaderLine.Length; a++)
                                        {
                                            if (HeaderInfo[a] == null)
                                            {
                                                MM_Notification.WriteLine(ConsoleColor.Yellow, "Unknown variable {0} in {1}", HeaderLine[a], FileName);
                                            }
                                        }

                                        //Confirm all of our headers are present
                                        foreach (PropertyInfo pI in TargetType.GetProperties())
                                        {
                                            if (Array.FindIndex <String>(HeaderLine, T => T.Equals(pI.Name, StringComparison.CurrentCultureIgnoreCase)) == -1)
                                            {
                                                MM_Notification.WriteLine(ConsoleColor.Yellow, "Missing variable {0} ({1}) in {2} / {3}", pI.Name, pI.PropertyType.Name, TargetType.Name, FileName);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        //Console.WriteLine("Rereading file " + FileName);
                                        sRd.ReadLine();
                                    }

                                    AtBeginning = false;
                                }
                                else
                                {
                                    DateTime EndTime = new DateTime(1970, 1, 1).AddSeconds(Convert.ToDouble(splStr[0]));
                                    if (EndTime != BatchId)
                                    {
                                        MM_Notification.WriteLine(ConsoleColor.Red, "Mismatch date on {0}: Start {1}, End {2}", FileName, BatchId, EndTime);
                                    }
                                    if (OutList != null)
                                    {
                                        ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessData), new object[] { OutList.ToArray(), TargetType, HeaderInfo });
                                    }
                                    else
                                    {
                                        //MM_Notification.WriteLine(ConsoleColor.Yellow, "Ignore processing data from {0}.", FileName);
                                    }
                                    AtBeginning = true;
                                }
                            }
                            else if (OutList != null)
                            {
                                OutList.Add(InLine);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MM_Notification.WriteLine(ConsoleColor.Red, "Error reading {0} from {1}: {2}", FileName, Conn.Client.RemoteEndPoint, ex);
                    }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Handle the drag/drop event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbl_DragDrop(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent(DataFormats.FileDrop) || ((string[])e.Data.GetData(DataFormats.FileDrop)).Length != 1)
            {
                return;
            }
            string FileName = "";

            try
            {
                FileName = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
                if (sender == lblDropMode)
                {
                    ProcessFile <int, String>(Modes, FileName, "ID_MODE(", ")", "= ", "", lblCountModes, "modes");
                }
                else if (sender == lblDropUserTypeFile)
                {
                    ProcessFile <int, String>(UserTypeNames, FileName, "USER(", ")", "= ", "", lblCountUserType, "usertypes");
                }
                else if (sender == lblDropUserName)
                {
                    ProcessFile <int, String>(UserNames, FileName, "USER(", ")", "= ", "", lblCountUserNames, "usernames");
                }
                else if (sender == lblDropArea)
                {
                    ProcessFile <int, String>(Areas, FileName, "ID_AREA(", ")", "= ", "", lblCountAreas, "areas");
                }
                else if (sender == lblDropPermission)
                {
                    ProcessFile <int[], bool>(Permissions, FileName, "_AREA_MODE(", ")", "= ", "", lblCountPermissions, "permissions");
                }
                else if (sender == lblDropOldOperatorships)
                {
                    ProcessFile(OldPermissions, FileName, lblCountAreas, "operatorships");
                }
                else if (sender == lblDropUserTypeToModeLinkages)
                {
                    String     InLine;
                    List <int> MappedTypes = null;
                    UserTypeToModeLinkages.Clear();
                    using (StreamReader sRd = new StreamReader(FileName))
                        while ((InLine = sRd.ReadLine()) != null)
                        {
                            if (InLine.StartsWith("USRTYP,"))
                            {
                                int StartStr = InLine.IndexOf("ID='") + 4;
                                UserTypeToModeLinkages.Add(InLine.Substring(StartStr, InLine.IndexOf("'", StartStr + 1) - StartStr), MappedTypes = new List <int>());
                            }
                            else if (InLine.StartsWith("UMODE,"))
                            {
                                MappedTypes.Add(int.Parse(InLine.Substring(InLine.LastIndexOf('=') + 1)));
                            }
                        }

                    foreach (List <int> MappedType in UserTypeToModeLinkages.Values)
                    {
                        MappedType.TrimExcess();
                    }
                    lblCountUserTypeToModeLinkages.Text = UserTypeToModeLinkages.Count.ToString() + " user->mode";
                }

                //Now, if we have enough data, produce our list
                if (Permissions.Count > 0 && Areas.Count > 0 && UserNames.Count > 0 && UserTypeNames.Count > 0 && Modes.Count > 0 && UserTypeToModeLinkages.Count > 0)
                {
                    NewPermissions.Clear();

                    //Now, perform our merging
                    List <String> MissingUsers = new List <String>();
                    List <int>    MissingAreas = new List <int>();
                    Dictionary <String, String> UserNameToUserType = new Dictionary <string, string>();

                    List <int> UserTypeToModeLinkage;
                    foreach (KeyValuePair <int, string> User in UserNames)
                    {
                        if (!UserTypeToModeLinkages.TryGetValue(UserTypeNames[User.Key], out UserTypeToModeLinkage))
                        {
                            MissingUsers.Add(UserTypeNames[User.Key]);
                        }
                        else
                        {
                            UserNameToUserType.Add(User.Value, UserTypeNames[User.Key]);
                            List <String> OutAreas = new List <string>();
                            String        FoundArea;
                            foreach (KeyValuePair <int[], bool> kvp in Permissions)
                            {
                                if (kvp.Value && UserTypeToModeLinkage.Contains(kvp.Key[1]))
                                {
                                    if (!Areas.TryGetValue(kvp.Key[0], out FoundArea))
                                    {
                                        MissingAreas.Add(kvp.Key[0]);
                                    }
                                    else if (!OutAreas.Contains(FoundArea))
                                    {
                                        OutAreas.Add(FoundArea);
                                    }
                                }
                            }
                            OutAreas.Sort(StringComparer.CurrentCultureIgnoreCase);
                            if (OutAreas.Count > 0)
                            {
                                NewPermissions.Add(User.Value, OutAreas.ToArray());
                            }
                        }
                    }


                    //Now, clear our ListView
                    lvOutputs.Items.Clear();
                    if (OldPermissions.Count > 0)
                    {
                        String[] FoundPermissions;
                        foreach (KeyValuePair <String, String[]> kvp in NewPermissions)
                        {
                            if (!OldPermissions.TryGetValue(kvp.Key, out FoundPermissions))
                            {
                                ListViewItem lvI = lvOutputs.Items.Add(kvp.Key);
                                lvI.SubItems.Add(UserNameToUserType[kvp.Key]);
                                lvI.SubItems.Add("Yes");
                                lvI.SubItems.Add(String.Join(",", kvp.Value));
                                lvI.SubItems.Add("");
                            }
                            else
                            {
                                List <String> Additions = new List <string>();
                                List <String> Removals  = new List <string>();
                                foreach (String str in FoundPermissions)
                                {
                                    if (Array.IndexOf(kvp.Value, str) == -1)
                                    {
                                        Removals.Add(str);
                                    }
                                }
                                foreach (String str in kvp.Value)
                                {
                                    if (Array.IndexOf(FoundPermissions, str) == -1)
                                    {
                                        Additions.Add(str);
                                    }
                                }
                                if (Additions.Count + Removals.Count > 0)
                                {
                                    ListViewItem lvI = lvOutputs.Items.Add(kvp.Key);
                                    lvI.SubItems.Add(UserNameToUserType[kvp.Key]);
                                    lvI.SubItems.Add("");
                                    lvI.SubItems.Add(String.Join(",", Additions.ToArray()));
                                    lvI.SubItems.Add(String.Join(",", Removals.ToArray()));
                                }
                            }
                        }

                        //Now, pull in our old ones that are missing
                        foreach (KeyValuePair <String, String[]> kvp in OldPermissions)
                        {
                            if (!NewPermissions.ContainsKey(kvp.Key))
                            {
                                ListViewItem lvI = lvOutputs.Items.Add(kvp.Key);
                                lvI.SubItems.Add("Old");
                                lvI.SubItems.Add(String.Join(",", kvp.Value.ToArray()));
                                lvI.SubItems.Add("");
                            }
                        }
                    }
                    else
                    {
                        foreach (KeyValuePair <String, String[]> kvp in NewPermissions)
                        {
                            ListViewItem lvI = lvOutputs.Items.Add(kvp.Key);
                            lvI.SubItems.Add(UserNameToUserType[kvp.Key]);

                            lvI.SubItems.Add("Yes");
                            lvI.SubItems.Add(String.Join(",", kvp.Value));
                            lvI.SubItems.Add("");
                        }
                    }

                    lvOutputs.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                    lvOutputs.Sort();
                    if (MissingAreas.Count > 0)
                    {
                        MessageBox.Show("Missing " + MissingAreas.Count.ToString() + " areas: " + String.Join(", ", MissingAreas.ToArray()));
                    }


                    if (MissingUsers.Count > 0)
                    {
                        MessageBox.Show("Missing " + MissingUsers.Count.ToString() + " users: " + String.Join(", ", MissingUsers.ToArray()));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading file " + FileName + ":" + ex.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }