Skip to content

Commit 191e6e3

Browse files
committed
addition: A&g reservation save/load
1 parent de76de9 commit 191e6e3

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

src/Models/AgManager.cs

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Stacker.Models.Enums;
1+
using Codeplex.Data;
2+
using Stacker.Models.Enums;
23
using Stacker.Utilities;
34
using System;
45
using System.Collections.Generic;
@@ -33,6 +34,8 @@ public AgManager()
3334
ProgramList = new ValidateableList<AgProgram>(i => i != null);
3435

3536
Reserver = new AgReserver(this);
37+
38+
Load();
3639
}
3740

3841
#region Events
@@ -158,10 +161,49 @@ public void UpdateProgramList()
158161
ProgramList.AddRange(AnalyzeProgramList());
159162
}
160163

164+
public void Load()
165+
{
166+
string src;
167+
168+
if (!File.Exists("ag.dat"))
169+
return;
170+
171+
using (var sr = new StreamReader("ag.dat"))
172+
src = sr.ReadToEnd();
173+
174+
if (string.IsNullOrEmpty(src))
175+
return;
176+
177+
var j = DynamicJson.Parse(src);
178+
179+
foreach(var timeReservation in j.time_reservations)
180+
{
181+
Reserver?.TimeReservationList.Add(new AgTimeReservation(timeReservation.ToString(), this));
182+
}
183+
184+
foreach (var keywordReservation in j.keyword_reservations)
185+
{
186+
Reserver?.KeywordReservationList.Add(new AgKeywordReservation(keywordReservation.ToString(), this));
187+
}
188+
}
189+
190+
public void Save()
191+
{
192+
dynamic j = new DynamicJson();
193+
194+
j.time_reservations = from r in Reserver.TimeReservationList select r.Serialize();
195+
j.keyword_reservations = from r in Reserver.KeywordReservationList select r.Serialize();
196+
197+
using (var sw = new StreamWriter("ag.dat"))
198+
sw.Write(j.ToString());
199+
}
200+
161201
public void Dispose()
162202
{
163203
ReservationRecorder.Dispose();
164204
RealtimeRecorder.Dispose();
205+
206+
Save();
165207
}
166208

167209
#endregion Action methods

src/Models/AgReservation.cs

+55-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Stacker.Models.Enums;
1+
using Codeplex.Data;
2+
using Stacker.Models.Enums;
23
using System;
34

45
namespace Stacker.Models
@@ -11,6 +12,8 @@ public AgReservation(AgManager manager)
1112
}
1213

1314
public AgManager Manager { get; set; }
15+
16+
public abstract object Serialize();
1417
}
1518

1619
/// <summary>
@@ -27,6 +30,17 @@ public AgTimeReservation(string name, bool isRecordVideo, TimeSpan startTime, Ti
2730
EndTime = endTime;
2831
}
2932

33+
public AgTimeReservation(string jsonString, AgManager manager)
34+
: base(manager)
35+
{
36+
var j = DynamicJson.Parse(jsonString);
37+
38+
Name = (string)j.name;
39+
IsRecordVideo = (bool)j.is_record_video;
40+
StartTime = TimeSpan.FromMinutes((int)j.start_time);
41+
EndTime = TimeSpan.FromMinutes((int)j.end_time);
42+
}
43+
3044
/// <summary>
3145
/// 名前を取得または設定します
3246
/// </summary>
@@ -61,6 +75,22 @@ public bool NeedRecording
6175
return nowTimeSpan >= StartTime && nowTimeSpan <= EndTime;
6276
}
6377
}
78+
79+
/// <summary>
80+
/// Jsonにシリアライズします
81+
/// </summary>
82+
/// <returns></returns>
83+
public override object Serialize()
84+
{
85+
var data = new {
86+
name = Name,
87+
is_record_video = IsRecordVideo,
88+
start_time = StartTime.TotalMinutes,
89+
end_time = EndTime.TotalMinutes
90+
};
91+
92+
return data;
93+
}
6494
}
6595

6696
/// <summary>
@@ -75,6 +105,15 @@ public AgKeywordReservation(AgKeywordReservationConditionType conditionType, str
75105
Keyword = keyword;
76106
}
77107

108+
public AgKeywordReservation(string jsonString, AgManager manager)
109+
: base(manager)
110+
{
111+
var j = DynamicJson.Parse(jsonString);
112+
113+
ConditionType = (AgKeywordReservationConditionType)j.condition_type;
114+
Keyword = (string)j.keyword;
115+
}
116+
78117
/// <summary>
79118
/// 条件の種類
80119
/// </summary>
@@ -84,5 +123,20 @@ public AgKeywordReservation(AgKeywordReservationConditionType conditionType, str
84123
/// キーワード
85124
/// </summary>
86125
public string Keyword { get; set; }
126+
127+
/// <summary>
128+
/// Jsonにシリアライズします
129+
/// </summary>
130+
/// <returns></returns>
131+
public override object Serialize()
132+
{
133+
var data = new
134+
{
135+
condition_type = (int)ConditionType,
136+
keyword = Keyword
137+
};
138+
139+
return data;
140+
}
87141
}
88142
}

0 commit comments

Comments
 (0)