static void Main(string[] args) { try { try { string sRecordingName, sMBSLog; System.Collections.Specialized.StringCollection scLines; string sCurrentLine; int iCurrentLine = 0; System.IO.FileInfo fileinfo; string sFileTitle, sFullPathTitle; int iAgeInMinutes = 240; sRecordingName = args[0]; sMBSLog = args[1]; if (args.GetLength(0) >= 3) { iAgeInMinutes = System.Convert.ToInt32(args[2]); } if (!System.IO.File.Exists(sRecordingName)) { Console.WriteLine(sRecordingName + " does not exist. Exiting."); return; } fileinfo = new System.IO.FileInfo(sRecordingName); if (fileinfo.Extension.ToLower() != ".mpg") { Console.WriteLine(sRecordingName + " must have a .mpg extension. Exiting."); return; } sFileTitle = fileinfo.Name.Substring(0,fileinfo.Name.Length - fileinfo.Extension.Length); sFullPathTitle = fileinfo.FullName.Substring(0,fileinfo.FullName.Length - fileinfo.Extension.Length); if (System.IO.File.Exists(sFullPathTitle + ".xml")) { Console.WriteLine(sRecordingName + " already has an XML file associated with it. Creating a recording from the xml file, if it doesn't already exist."); OysterClassLibrary.Oyster oyster = new OysterClassLibrary.Oyster(); OysterClassLibrary.Recording rec = oyster.GetRecordingByName(sFileTitle + ".wmv"); if (rec != null) { Console.WriteLine(sFileTitle + ".wmv" + " already exists as a recording. Exiting."); return; } CarverLab.Utility.CARDVIDEO cv;// = new CarverLab.Utility.CARDVIDEO(); System.Xml.Serialization.XmlSerializer xmlser = new System.Xml.Serialization.XmlSerializer(typeof(CarverLab.Utility.CARDVIDEO)); System.IO.StreamReader xmlfile = System.IO.File.OpenText(sFullPathTitle + ".xml"); cv = (CarverLab.Utility.CARDVIDEO) xmlser.Deserialize(xmlfile); OysterClassLibrary.User usr = oyster.GetUserByCardNumber(cv.CARDID); // make a recording in the db for this user with this name in this room rec = oyster.CreateRecording(sFileTitle + ".wmv",cv.DISPLAYNAME,usr.ID, cv.VIDEOSTORAGESERVERID,cv.STREAMINGENCODERID,null,null); //now change the date to the date of the MPG file rec.CreatedDate = fileinfo.CreationTime; if (rec == null) { Console.WriteLine("could not create a recording. Exiting."); } else Console.WriteLine("recording created."); return; } System.TimeSpan ts = DateTime.Now - fileinfo.CreationTime; if (ts.TotalMinutes < iAgeInMinutes) { Console.WriteLine(sRecordingName + " is only " + ts.TotalMinutes + " minutes old. Not old enough. Exiting."); return; } if (fileinfo.Length == 0) { Console.WriteLine(sRecordingName + " is empty. Exiting."); return; } Console.WriteLine("rbr searching for " + sRecordingName + " in " + sMBSLog); scLines = new System.Collections.Specialized.StringCollection(); string sptnDateTime = @"\[(?<date>[0-9]{1,2}/[0-9]{1,2}/[0-9]{4})\x20(?<time>[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})\]"; string sptnCardId = @"\sm_sCardId\x20=\x20;(?<cardid>[0-9]+=)"; string sptnRoomId = @"\sm_dwRoomId\x20=\x20(?<roomid>[0-9]+),"; // string sptnCardSwipeId = @"\sm_dwCardSwipeId\x20\=\x20(?<cardswipeid>[0-9]+\,"; // string sptnCommand = @"\sm_eCommand\x20\=\x20(?<command>[0-9]+\"; string sptnGF = sptnDateTime + @"\x20CMediaBufferServer::GenerateFilename\(.+" + sFileTitle + @".mpg\)"; string sptnEncoderAddress = sptnDateTime + @"\x20CMediaBufferServer::StartRecording\(STREAM_PROTOCOL\)\x20from\x20" + @"(?<ipaddress>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}):(?<port>[0-9]+)"; // @"(?<ipaddress>\d{1,3}\.\d{1,3}\.\d{1,3}):(?<port>\d+)"; string sptnEncoderRoomAndId = sptnDateTime + @"\x20CStreamingEncoder\[(?<roomid>[0-9]+),(?<encoderid>[0-9]+)\]" + @"::StartRecording:\x20starting\x20encoder\x20at\x20"; //TODO:add encoder address:port for accurate search System.Text.RegularExpressions.Regex regex; System.Text.RegularExpressions.Match match; int iRoomId = 0; // int iCardReaderId; int iEncoderId = 0; string sEncoderAddress = ""; int iEncoderPort = 0; // int iCommand; string sCardNumber = ""; DateTime dtGF = DateTime.MinValue, dtCurrent = DateTime.MaxValue; bool bErroredOut = false; string sptn; bool bDone = false; System.IO.TextReader tr = System.IO.File.OpenText(sMBSLog); SearchingState CurrentState = SearchingState.GF; while (!bDone) { switch (CurrentState) { case SearchingState.GF: // find the file on the MBS::GenerateFilename line sptn = sptnGF; regex = new System.Text.RegularExpressions.Regex(sptn); while (true) { sCurrentLine = tr.ReadLine(); if (sCurrentLine == null) { bErroredOut = true; CurrentState = SearchingState.CleaningUp; break; } scLines.Add(sCurrentLine); iCurrentLine = scLines.Count - 1; match = regex.Match(scLines[iCurrentLine]); if (match.Success) { string sdate = match.Groups["date"].Value; string stime = match.Groups["time"].Value; dtGF = System.DateTime.Parse(sdate + " " + stime); CurrentState = SearchingState.EncoderAddress; break; } } break; case SearchingState.EncoderAddress: // backup to the least previous encoder address line sptn = sptnEncoderAddress; regex = new System.Text.RegularExpressions.Regex(sptn); while (true) { --iCurrentLine; if (iCurrentLine <= 0) { bErroredOut = true; CurrentState = SearchingState.CleaningUp; break; } match = regex.Match(scLines[iCurrentLine]); if (match.Success) { string sdate = match.Groups["date"].Value; string stime = match.Groups["time"].Value; dtCurrent = System.DateTime.Parse(sdate + " " + stime); sEncoderAddress = match.Groups["ipaddress"].Value; iEncoderPort = System.Convert.ToInt32(match.Groups["port"].Value); CurrentState = SearchingState.RoomId; break; } } break; case SearchingState.RoomId: // backup to the least previous room id line sptn = sptnEncoderRoomAndId + sEncoderAddress + ":" + iEncoderPort.ToString(); regex = new System.Text.RegularExpressions.Regex(sptn); while (true) { --iCurrentLine; if (iCurrentLine <= 0) { bErroredOut = true; CurrentState = SearchingState.CleaningUp; break; } match = regex.Match(scLines[iCurrentLine]); if (match.Success) { string sdate = match.Groups["date"].Value; string stime = match.Groups["time"].Value; dtCurrent = System.DateTime.Parse(sdate + " " + stime); iRoomId = System.Convert.ToInt32(match.Groups["roomid"].Value); iEncoderId = System.Convert.ToInt32(match.Groups["encoderid"].Value); CurrentState = SearchingState.CardRoomId; break; } } break; case SearchingState.CardRoomId: // backup to the least previous line with the room id on it sptn = sptnRoomId; regex = new System.Text.RegularExpressions.Regex(sptn); while (true) { --iCurrentLine; if (iCurrentLine <= 0) { bErroredOut = true; CurrentState = SearchingState.CleaningUp; break; } match = regex.Match(scLines[iCurrentLine]); if (match.Success) { if (iRoomId == System.Convert.ToInt32(match.Groups["roomid"].Value)) { CurrentState = SearchingState.CardId; break; } } } break; case SearchingState.CardId: // backup to the least previous card id line sptn = sptnCardId; regex = new System.Text.RegularExpressions.Regex(sptn); while (true) { --iCurrentLine; if (iCurrentLine <= 0) { bErroredOut = true; CurrentState = SearchingState.CleaningUp; break; } match = regex.Match(scLines[iCurrentLine]); if (match.Success) { string scardid = match.Groups["cardid"].Value; sCardNumber = scardid.Substring(scardid.Length - 11,10); CurrentState = SearchingState.MakingRecording; break; } else { bErroredOut = true; CurrentState = SearchingState.CleaningUp; } } break; case SearchingState.MakingRecording: // search for the user with this userid OysterClassLibrary.Oyster oyster = new OysterClassLibrary.Oyster(); OysterClassLibrary.User usr = oyster.GetUserByCardNumber(sCardNumber); OysterClassLibrary.StreamingEncoder se = oyster.GetStreamingEncoderById(iEncoderId); string sDisplayName = dtGF.ToString("MM/dd/yyyy hh:mm tt"); if (usr == null) { bErroredOut = true; CurrentState = SearchingState.CleaningUp; break; } // make a recording in the db for this user with this name in this room OysterClassLibrary.Recording rec = oyster.CreateRecording(sFileTitle + ".wmv",sDisplayName,usr.ID, se.CurrentRoom.VideoStorageServerID,se.ID,null,null); if (rec == null) { bErroredOut = true; CurrentState = SearchingState.CleaningUp; break; } // make the xml file for this recording CarverLab.Utility.CARDVIDEO cv = new CarverLab.Utility.CARDVIDEO(); System.Xml.Serialization.XmlSerializer xmlser = new System.Xml.Serialization.XmlSerializer(typeof(CarverLab.Utility.CARDVIDEO)); System.IO.StreamWriter xmlfile = System.IO.File.CreateText(sFullPathTitle + ".xml"); cv.FILENAME = fileinfo.FullName; cv.CARDID = sCardNumber; cv.DISPLAYNAME = sDisplayName; cv.FILETITLE = sFileTitle; cv.ROOMID = iRoomId; cv.STREAMINGENCODERID = iEncoderId; cv.TIMESTART = dtGF; cv.VIDEOSTORAGESERVERID = se.CurrentRoom.VideoStorageServerID; /* cv.BITRATE = "0"; cv.MPEGFORMAT = "n/a"; cv.SESSIONID = "n/a"; cv.SESSIONNAME = "n/a"; */ xmlser.Serialize(xmlfile,cv); bErroredOut = false; CurrentState = SearchingState.CleaningUp; break; case SearchingState.CleaningUp: if (bErroredOut) { Console.WriteLine("An error occured. Unable to find correct information. Cleaning up for exit."); } else { Console.WriteLine("Work is complete. Cleaning up for exit."); } bDone = true; break; default: throw new ApplicationException("Unknown SearchingState: " + CurrentState); } } } catch (System.IndexOutOfRangeException) { Start.DisplayUsage(); } } catch (System.Exception ex) { Console.WriteLine("Unknown Exception occurred {0}", ex.Message); Console.WriteLine("Here is the Full Message output"); Console.WriteLine("{0}", ex.ToString()); } }
void MakeXMLFile(string[] MPEGPathName, string XMLPath, OysterClassLibrary.User User, OysterClassLibrary.StreamingEncoder StreamingEncoder) { OysterClassLibrary.Oyster oyster = new OysterClassLibrary.Oyster(); OysterClassLibrary.User usr = User; OysterClassLibrary.StreamingEncoder se = StreamingEncoder; OysterClassLibrary.Recording rec; bool bMakeSession = false, bFirstTime = true; string sSessionId = Guid.NewGuid().ToString(), sSessionName = ""; FileInfo fiMPEG; if (chkSingleSession.Checked) { bMakeSession = true; } int i = 0; foreach (string mpegfile in MPEGPathName) { fiMPEG = new FileInfo(mpegfile); if (string.Compare(fiMPEG.Extension,".mpg",true) != 0) { MessageBox.Show("All MPEG files must have a .mpg extension. Stopping."); break; } string sDisplayName = fiMPEG.CreationTime.ToString("MM/dd/yyyy hh:mm tt"); string sRecordingName; if (bFirstTime) { bFirstTime = false; sSessionName = sDisplayName; } if (bMakeSession) { sRecordingName = "Camera " + (char) ('A' + i++); } else sRecordingName = sDisplayName; string sFileTitle = fiMPEG.Name.Substring(0,fiMPEG.Name.Length - 4); rec = oyster.GetRecordingByName(sFileTitle + ".wmv"); if (rec != null) { if (MessageBox.Show(this,sFileTitle + ".wmv is already in the database. Delete it and continue?", "Recording Already Exists...",MessageBoxButtons.YesNo) == DialogResult.Yes) { oyster.DeleteRecordingById(rec.ID); } else { MessageBox.Show("Cannot continue with an already existing recording. Stopping."); break; } } // make a recording in the db for this user with this name in this room if (bMakeSession) { rec = oyster.CreateRecording(sFileTitle + ".wmv",sRecordingName,usr.ID, se.CurrentRoom.VideoStorageServerID,se.ID,sSessionId,sSessionName); } else { rec = oyster.CreateRecording(sFileTitle + ".wmv",sRecordingName,usr.ID, se.CurrentRoom.VideoStorageServerID,se.ID,null,null); } if (rec == null) { MessageBox.Show("Recording " + sFileTitle + " could not be created. Stopping."); break; } // make the xml file for this recording CarverLab.Utility.CARDVIDEO cv = new CarverLab.Utility.CARDVIDEO(); System.Xml.Serialization.XmlSerializer xmlser = new System.Xml.Serialization.XmlSerializer(typeof(CarverLab.Utility.CARDVIDEO)); System.IO.StreamWriter xmlfile = System.IO.File.CreateText( XMLPath + @"\" + sFileTitle + ".xml"); cv.FILENAME = mpegfile; cv.CARDID = usr.CardNumber; cv.DISPLAYNAME = sRecordingName; cv.FILETITLE = sFileTitle; cv.ROOMID = se.CurrentRoom.ID; cv.STREAMINGENCODERID = se.ID; cv.TIMESTART = DateTime.Now; cv.VIDEOSTORAGESERVERID = se.CurrentRoom.VideoStorageServerID; if (bMakeSession) { cv.SESSIONID = sSessionId; cv.SESSIONNAME = sSessionName; } xmlser.Serialize(xmlfile,cv); } MessageBox.Show(this,"Done."); }