/* TODO add support for other databases.*/ public Database() { var setting = SettingObj.Read(); if (!setting.DataBase.Equals("mysql", StringComparison.InvariantCultureIgnoreCase)) { throw new InvalidOperationException("DB setting is wrone!"); } this.serverAddr = setting.DataBase_Addr; this.uid = setting.DataBase_User; this.pwd = setting.DataBase_Pw; this.port = setting.DataBase_Port ?? 3306; baseConnection = new MySqlConnection($"Server={serverAddr};Uid={uid};Pwd={pwd};Database={DATABASENAME};Port={port};SslMode=Preferred;"); if (ValidDatabase()) { if (!ValidTable()) { throw new EntryPointNotFoundException("Invalid DataBase has been used on this session."); } else { baseConnection.Open(); } } else if (!CreateDatabase()) { throw new EntryPointNotFoundException("Invalid DataBase has been used on this session."); } else { baseConnection.Open(); } }
public static SettingObj Read() { if (!System.IO.File.Exists(path)) { SettingObj tmp = new SettingObj(); tmp.Save(); return(tmp); } System.Runtime.Serialization.Json.DataContractJsonSerializer sr = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(SettingObj)); using (System.IO.FileStream sw = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { return((SettingObj)sr.ReadObject(sw)); } }
private static void OKBeep(SettingObj mySetting) { if (!mySetting.AllowBeep) { return; } // Beep in morse says OK Console.Beep(550, 200); System.Threading.Tasks.Task.Delay(20); Console.Beep(550, 200); System.Threading.Tasks.Task.Delay(20); Console.Beep(550, 200); System.Threading.Tasks.Task.Delay(50); Console.Beep(550, 200); System.Threading.Tasks.Task.Delay(20); Console.Beep(550, 100); System.Threading.Tasks.Task.Delay(10); Console.Beep(550, 200); System.Threading.Tasks.Task.Delay(20); }
private static void AddToDatabase(RmtFile file, SettingObj setting, DateTime startTime) { try { if (!setting.DataBase.Equals("mysql", StringComparison.InvariantCultureIgnoreCase)) { throw new InvalidOperationException("Database is wrone or not in use."); } using (var database = new Database(setting.DataBase_Addr, setting.DataBase_User, setting.DataBase_Pw)) { database.AddDataItem(file.FullFilePath.Substring(file.FullFilePath.IndexOf('\\') + 1), new System.IO.FileInfo(file.FullFilePath).Name, MirakurunWarpper.MirakurunService.GetUNIXTimeStamp(startTime), file.IsFatherUpdate ? file.OldFatherName : null); } } catch (Exception ex) { ex.Message.ErrorLognConsole(); Upload(file); } }
public IEnumerable <RmtFile> LoadData(bool loadAll = false) { var mCon = new MySqlConnection($"Server={serverAddr};Uid={uid};Pwd={pwd};Port={port};Database={DATABASENAME};SslMode=Preferred;"); var cmd = new MySqlCommand($"SELECT `id`, `time`, `name`, `path`, `isuploaded`, `showonuploader`, `uploadid`, `upprogress`, `initialfoldername` " + $"FROM `{TABLE_RECORDED}` " + (loadAll ? "" : "WHERE `showonuploader` = '1' AND `isuploaded` = '0'") + $"ORDER BY `time` ASC LIMIT 1000", mCon); MySqlDataReader result = null; try { mCon.Open(); result = cmd.ExecuteReader(); } catch (MySqlException e) { "Database Error:{0}".ErrorLognConsole(e.Message); yield break; } catch (Exception e) { "Unknow error occured when reading database : {0}".ErrorLognConsole(e.Message); yield break; } string root = SettingObj.Read().StorageFolder; while (result.Read()) { var tmp = result["initialfoldername"]; var item = new RmtFile(System.IO.Path.Combine(root, (string)result["path"]), !(tmp == DBNull.Value), (tmp == DBNull.Value ? string.Empty:tmp.ToString()), true); item.ID = (string)result["id"]; yield return(item); } cmd.Dispose(); mCon.Close(); }
private static bool MoveFile(StreamFile para, SettingObj mySetting, EPGAccess epgAccess = null) { if (!System.IO.File.Exists(para.FilePath)) { return(false); // Ops! file is not there. } string fileName = System.IO.Path.GetFileName(para.FilePath); string[] FolderList = System.IO.Directory.GetDirectories(mySetting.StorageFolder); //Get local preset base folder's child folers. string Targetfolder = mySetting.StorageFolder; RmtFile mFile = new RmtFile(); // Object for Upload process if (!string.IsNullOrWhiteSpace(mySetting.GetFolderTag(para.Genre))) { try //try get full path of genre folder. { Targetfolder = FolderList.First(x => Strings.InStr(x, mySetting.GetFolderTag(para.Genre)) > 0); } catch (Exception e)// In case the preset genre folder is not there. { Targetfolder = System.IO.Path.Combine(mySetting.StorageFolder, mySetting.GetFolderTag(para.Genre)); Console.WriteLine(e.Message); } } if (!System.IO.Directory.Exists(Targetfolder)) //if genre folder is not there make one. { System.IO.Directory.CreateDirectory(Targetfolder); } if (para.Genre.HasFlag(ProgramGenre.Anime) || para.Genre.HasFlag(ProgramGenre.Drama) || para.Genre.HasFlag(ProgramGenre.Variety)) { // those programs has genre of anime drama or variety could be in series. if that is the case make a folder to hold them. string programName = MasterHelper.FindTitle(para.Title); //find title. if (!programName.Equals(para.Title)) // if the title find by program doesn't match it's full name means it's in a series. { FolderList = System.IO.Directory.GetDirectories(Targetfolder); try // from here on is basicly copy plast the folder exist thing. { Targetfolder = FolderList.First(x => { // find a folder matchs series name var str = x.Substring(x.LastIndexOf(@"\") + 1); str = str.Substring(str.IndexOf("]") + 1); return(Strings.InStr(x, programName) > 0 || Strings.InStr(x, Strings.StrConv(programName, VbStrConv.Wide)) > 0 || Strings.InStr(programName, str) > 0 || Strings.InStr(Strings.StrConv(programName, VbStrConv.Wide), str) > 0); }); } catch (Exception e) { //no match so far so make a one. Targetfolder = System.IO.Path.Combine(Targetfolder, MasterHelper.GetTimeSpan(para.StartTime, para.StartTime) + programName); Console.WriteLine("Try find folder : " + e.Message); } mFile.FullFilePath = System.IO.Path.Combine(Targetfolder, fileName); //this will make sure the date period at the head of folder name is correct. if (MasterHelper.RenameDirUpToDate(ref Targetfolder, para.EndTime)) {//this is for upload process . notify that the folder name has been changed. mFile.OldFatherName = System.IO.Path.GetFileName(System.IO.Path.GetDirectoryName(mFile.FullFilePath)); mFile.IsFatherUpdate = true; mFile.FullFilePath = System.IO.Path.Combine(Targetfolder, fileName); } Console.WriteLine("Target folder is : " + Targetfolder); if (!System.IO.Directory.Exists(Targetfolder)) { System.IO.Directory.CreateDirectory(Targetfolder); "Create folder: {0}".InfoLognConsole(Targetfolder); } } //Move file to where it belongs. FileMovier(para.FilePath, System.IO.Path.Combine(Targetfolder, fileName)); para.FilePath = System.IO.Path.Combine(Targetfolder, fileName); if (epgAccess == null) {// if the epgAccess is null means this process is called by tvrock //no need to get extra information from server. //XML file is standerd meta data format for tvrock for now. para.ToXml(System.IO.Path.Combine(Targetfolder, fileName)); } else {// this process is called by epgstation or else. //information is from server, it will be stored in a //*.meta file, it could also include a station logo and a thumbnail of video. var tmp = para.EPGStation.WtiteFile(System.IO.Path.Combine(Targetfolder, fileName)); if (tmp) { epgAccess.DeleteRecordByID(para.EPGStation.Meta.id); } } //comit upload AddToDatabase(mFile, mySetting, para.StartTime); OKBeep(mySetting); // beep } else { mFile.FullFilePath = System.IO.Path.Combine(Targetfolder, fileName); FileMovier(para.FilePath, System.IO.Path.Combine(Targetfolder, fileName)); para.FilePath = System.IO.Path.Combine(Targetfolder, fileName); if (epgAccess == null) { para.ToXml(System.IO.Path.Combine(Targetfolder, fileName)); } else { var tmp = para.EPGStation.WtiteFile(System.IO.Path.Combine(Targetfolder, fileName)); if (tmp) { epgAccess.DeleteRecordByID(para.EPGStation.Meta.id); } } //Do upload same here.. AddToDatabase(mFile, mySetting, para.StartTime); OKBeep(mySetting); } return(true); }
public static bool SortFile(string[] margs) { "App started.".InfoLognConsole(); SettingObj mySetting = null; bool a = false; while (!a) { Console.WriteLine("Reading settings."); try { mySetting = SettingObj.Read(); //mySetting = ((dynamic)Activator.CreateInstance(SettingObj)).Read(); a = true; } catch (Exception ex) { Console.WriteLine("Error : {0}", ex.Message); "Fail to read settings [{0}]".InfoLognConsole(ex.Message); Console.WriteLine("Sleep 10 sec..."); System.Threading.Tasks.Task.Delay(10000); } } EPGAccess mAccess = null; StreamFile mpars = null; if (margs.Any(x => x.Equals("-main", StringComparison.OrdinalIgnoreCase))) { var wpfwindow = new MasterView.MasterViewControl(); ElementHost.EnableModelessKeyboardInterop(wpfwindow); if (wpfwindow.ShowDialog() == true) { return(true); } } if (margs.Any(x => x.Equals("-mirakurun", StringComparison.OrdinalIgnoreCase))) { var wpfwindow = new MirakurunWarpper.MirakurunViewer(); ElementHost.EnableModelessKeyboardInterop(wpfwindow); if (wpfwindow.ShowDialog() == true) { return(true); } } if (margs.Any(x => x.Equals("-setup", StringComparison.OrdinalIgnoreCase))) { var wpfwindow = new MasterView.Setting(); ElementHost.EnableModelessKeyboardInterop(wpfwindow); if (wpfwindow.ShowDialog() == true) { return(true); } } if (margs.Any(x => x.Equals("-rcdbview", StringComparison.OrdinalIgnoreCase))) { var wpfwindow = new MasterView.RecordedListView(); ElementHost.EnableModelessKeyboardInterop(wpfwindow); if (wpfwindow.ShowDialog() == true) { return(true); } } if (margs.Any(x => x.Equals("-cloud", StringComparison.OrdinalIgnoreCase))) { var wpfwindow = new MasterView.CloudViewer(); ElementHost.EnableModelessKeyboardInterop(wpfwindow); if (wpfwindow.ShowDialog() == true) { return(true); } } if (margs.Any(x => x.Equals("-upload", StringComparison.OrdinalIgnoreCase))) { var wpfwindow = new MasterView.Uploader(); ElementHost.EnableModelessKeyboardInterop(wpfwindow); if (wpfwindow.ShowDialog() == true) { return(true); } } if (margs.Any(x => x.Equals("-epgstation", StringComparison.OrdinalIgnoreCase))) { Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=EPGstation=-=-=-=-=-=-=-=-=-=-=-"); // Start process the parameters, group them to their own entries. List <string> clPara = new List <string>(); if (margs.Count() <= 1) { return(false);/* TODO Change to default(_) if this is not a reference type */ } System.Text.StringBuilder sbb = new System.Text.StringBuilder(); foreach (var p in margs) { if (p.StartsWith("-")) { var tmp = sbb.ToString(); if (tmp.Length > 1 && tmp.StartsWith("-")) { clPara.Add(tmp); } sbb.Clear(); // Clear the stringbuilder every time it hits the char "-" sbb.Append(p); } else { sbb.AppendFormat(" {0}", p); } } "App start with parameter\"{0}\"".InfoLognConsole(sbb.ToString()); clPara.Add(sbb.ToString()); // Lack of the last run in the loop, so add the last parameter manuly. int id = -1; bool t_check = false; try {//first see para -id t_check = int.TryParse(clPara.First(x => x.StartsWith("-id")).Substring(4), out id); } catch { t_check = false; } if (!t_check) {//if -id is not working, try get enviroment variable. maybe not working. "parameter -id not found or not a number.".ErrorLognConsole(); var e_id = Environment.GetEnvironmentVariable("RECORDEDID"); if (!e_id.IsNullOrEmptyOrWhiltSpace() && !int.TryParse(e_id, out id)) { "Failed to find \"RECORDEDID\" from Enviorment variable".ErrorLognConsole(); "App catch error. exiting...".InfoLognConsole(); return(false); } "Environment Variable \"RECORDEDID={0}\"".InfoLognConsole(id.ToString()); } Console.WriteLine("Preparing for access Epgstation server."); mAccess = new EPGAccess(mySetting); mpars = mAccess.GetStreamFileObj(id); if (mpars == null) { "Remote file is missing or \"ID:{0}\" does not exsits.".ErrorLognConsole(id.ToString()); "App catch error. exiting...".InfoLognConsole(); return(false); } if (mpars.ChannelName.IsNullOrEmptyOrWhiltSpace()) { string channel = ""; try { channel = clPara.First(x => x.StartsWith("-cn")).Substring(4); } catch { channel = "UNKNOW"; } finally { mpars.ChannelName = channel; } } } else { mpars = GetPara(margs); } if (mpars == null) { return(false); } System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (var p in margs) { sb.Append(p + " "); } PrintInfomation(mpars); if (!MoveFile(mpars, mySetting, mAccess)) { "Local file is missing : \"File:{0}\" does not exsit.".ErrorLognConsole(System.IO.Path.GetFileName(mpars.FilePath)); "App catch error. exiting...".InfoLognConsole(); return(false); } ; "App has completed job. exiting...".InfoLognConsole(); return(true); }
private void Settings_Load(object sender, EventArgs e) { mySetting = SettingObj.Read(); SettingToForm(); }