// A packed function to call before a timer is built. // 这里的switchbuilder有很大的问题,类的特性表现的比较差。应该改为Timer中统一的接口来处理。 private static Timer TimerBuildSwitcher(string path) { TimerConfigure tc = new TimerConfigure(path); // The timer configure to build from Timer T = null; // The timer to build switch (tc.timerOption) { case TimerOption.Normal: T = new Timer(path); break; case TimerOption.Cycle: T = new CycleTimer(path); break; case TimerOption.Timing: T = new TimingTimer(path); break; case TimerOption.CycleCount: T = new CycleCountTimer(path); break; case TimerOption.Interval: T = new IntervalCycleTimer(path); break; default: Console.WriteLine("Timer type not chosen, it's set to default now."); T = new Timer(path); // Build a default timer. break; } return(T); }
// Dump this Timer to a configure file (json) public virtual void dumpConfig(string path = "./TimerConfig.json") { Console.WriteLine("dumping"); this.timerConfigure = new TimerConfigure(this.startTime, this.endTime, this.currentTime, System.DateTime.Now, this.diffTimeSpan, this.originTimeSpan, this.expire, this.alarm, this.endSig, this.pause, this.timerOption); this.timerConfigure.dump(path); /* * string json = JsonConvert.SerializeObject(this.timerConfigure); * using (StreamWriter sw = new StreamWriter(path)) * { * sw.Write(json); * Console.WriteLine("Dump done"); * } */ }
// Read the json file from the path and build from it. public virtual void load(string path) { TimerConfigure tc = new TimerConfigure(); string json; using (StreamReader sr = new StreamReader(path)) { json = sr.ReadToEnd(); Console.WriteLine("Read configure done."); tc = JsonConvert.DeserializeObject <TimerConfigure>(json); this.startTime = tc.startTime; this.endTime = tc.endTime; this.originTimeSpan = tc.originTimeSpan; this.expire = tc.expire; this.endSig = false; this.alarm = tc.alarm; this.pause = true; this.timerOption = tc.timerOption; this.diffTimeSpan = tc.diffTimeSpan; this.pauseTime = tc.pauseTime; } }
// Constructor: read cofigure from json file and build public Timer(string path = "./TimerConfig.json") { string json; using (StreamReader sr = new StreamReader(path)) { json = sr.ReadToEnd(); Console.WriteLine("Read configure done\n Now building the timer..."); this.timerConfigure = JsonConvert.DeserializeObject <TimerConfigure>(json); this.startTime = this.timerConfigure.startTime; this.endTime = this.timerConfigure.endTime; this.originTimeSpan = this.timerConfigure.originTimeSpan; this.expire = this.timerConfigure.expire; this.endSig = false; this.alarm = this.timerConfigure.alarm; this.pause = true; this.timerOption = this.timerConfigure.timerOption; this.diffTimeSpan = this.timerConfigure.diffTimeSpan; this.currentTime = System.DateTime.Now; this.pauseTime = this.timerConfigure.pauseTime; } }