Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

350 řádky
12KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Threading;
  6. using OpenQA.Selenium;
  7. using OpenQA.Selenium.Chrome;
  8. using OpenQA.Selenium.Support.UI;
  9. using SeleniumExtras.WaitHelpers;
  10. using CSVDownloader.File;
  11. using CSVDownloader.Store;
  12. using CSVDownloader.Store.CreditCSVData;
  13. using CSVDownloader.Store.QRCSVData;
  14. using CSVDownloader.Web;
  15. using MySql.Data.MySqlClient;
  16. namespace CSVDownloader {
  17. class Program {
  18. static void Main(string[] args) {
  19. var controller = new Controller();
  20. String ret = controller.Start();
  21. if (ret.Length != 0) {
  22. // エラー検知
  23. Console.WriteLine(ret);
  24. Console.WriteLine("エラー発生 詳細はログを参照");
  25. Environment.Exit(1);
  26. }
  27. }
  28. }
  29. class Controller {
  30. private log4net.ILog logger_ = log4net.LogManager.GetLogger("");
  31. private List<WebController> web_controller_list_ = new List<WebController>();
  32. private HistoryDAO history_dao_;
  33. private ParkingCreditCardAgenciesSpotIDDAO parking_credit_card_agencies_spotID_dao_;
  34. private DaitoCreitDataStore daito_credit_store_;
  35. private ZeusCreditDataStore zeus_credit_store_;
  36. private ItecCreditDataStore itec_credit_store_;
  37. private HelloTechnoCreditDataStore hello_techno_credit_store_;
  38. private DaitoQRDataStore daito_qr_store_;
  39. private IConfigReader config_;
  40. private ChromeDriver driver_;
  41. private MySqlConnection conn_;
  42. private MySqlTransaction transaction_;
  43. private Dictionary<Code.CreditAgent, CreditCSVDataStore> credit_datastore_map_ = new Dictionary<Code.CreditAgent, CreditCSVDataStore>();
  44. private Dictionary<Code.CreditAgent, QRCSVDataStore> qr_datastore_map_ = new Dictionary<Code.CreditAgent, QRCSVDataStore>();
  45. public String Start() {
  46. Console.WriteLine("★★★★★★自動CSVダウンロード 起動★★★★★");
  47. logger_.Info("★★★★★★自動CSVダウンロード 起動★★★★★");
  48. String returnable = "";
  49. // 各種ストアを用意
  50. history_dao_ = new HistoryDAO();
  51. config_ = new ConfigReader("config/config.ini");
  52. var mysql_param = new MySqlConnectionParameter() {
  53. host = "192.168.1.17",
  54. port = 3306,
  55. database = "yp",
  56. user = "sosuke",
  57. password = ".1Satellite"
  58. };
  59. try {
  60. conn_ = MySQL.GetConnection(mysql_param);
  61. parking_credit_card_agencies_spotID_dao_ = new ParkingCreditCardAgenciesSpotIDDAO(conn_);
  62. daito_credit_store_ = new DaitoCreitDataStore(conn_);
  63. zeus_credit_store_ = new ZeusCreditDataStore(conn_);
  64. itec_credit_store_ = new ItecCreditDataStore(conn_);
  65. hello_techno_credit_store_ = new HelloTechnoCreditDataStore(conn_);
  66. daito_qr_store_ = new DaitoQRDataStore(conn_);
  67. } catch (Exception e) {
  68. logger_.Error("DB接続失敗");
  69. logger_.Error(e.Message);
  70. return "DBへのコネクト失敗";
  71. }
  72. // サイトごとにコントローラーを用意する。
  73. MakeConttollers();
  74. // 各コントローラーを起動する。
  75. foreach (var web_controller in web_controller_list_) {
  76. var ret = HandleWebController(web_controller);
  77. if (ret == Code.ResultCode.NG) {
  78. logger_.Error($"処理失敗 {web_controller.GetCreditAgent().ToString()}");
  79. returnable = "処理失敗";
  80. }
  81. }
  82. // ブラウザの終了
  83. if (driver_ != null) {
  84. driver_.Quit();
  85. }
  86. // 終了
  87. logger_.Info("処理成功");
  88. return returnable;
  89. }
  90. private void MakeConttollers() {
  91. driver_ = DriverFactory.GetDriver();
  92. // CREVAS
  93. {
  94. var controller = new DaitoController(driver_);
  95. var dic = parking_credit_card_agencies_spotID_dao_.GetDictionary(controller.GetCreditAgent());
  96. controller.SetParkingDic(dic);
  97. web_controller_list_.Add(controller);
  98. credit_datastore_map_.Add(controller.GetCreditAgent(), daito_credit_store_);
  99. qr_datastore_map_.Add(controller.GetCreditAgent(), daito_qr_store_);
  100. }
  101. // Zeus
  102. {
  103. var controller = new ZeusController(driver_);
  104. var dic = parking_credit_card_agencies_spotID_dao_.GetDictionary(controller.GetCreditAgent());
  105. controller.SetParkingDic(dic);
  106. web_controller_list_.Add(controller);
  107. credit_datastore_map_.Add(controller.GetCreditAgent(), zeus_credit_store_);
  108. }
  109. // Itec
  110. {
  111. var controller = new ItecController(driver_);
  112. var dic = parking_credit_card_agencies_spotID_dao_.GetDictionary(controller.GetCreditAgent());
  113. controller.SetParkingDic(dic);
  114. web_controller_list_.Add(controller);
  115. credit_datastore_map_.Add(controller.GetCreditAgent(), itec_credit_store_);
  116. }
  117. // HelloTechno
  118. {
  119. var controller = new HelloTechnoController(driver_);
  120. var dic = parking_credit_card_agencies_spotID_dao_.GetDictionary(controller.GetCreditAgent());
  121. controller.SetParkingDic(dic);
  122. web_controller_list_.Add(controller);
  123. credit_datastore_map_.Add(controller.GetCreditAgent(), hello_techno_credit_store_);
  124. }
  125. }
  126. private Code.ResultCode HandleWebController(WebController web_controller) {
  127. Code.ResultCode result_code = Code.ResultCode.OK;
  128. try {
  129. // 各ダウンロードディレクトリをクリーンする。
  130. CreanDirectries();
  131. // カード会社を特定する。
  132. Code.CreditAgent agent = web_controller.GetCreditAgent();
  133. logger_.Info($"ダウンロード開始 対象:{agent}");
  134. // 履歴を参照する。
  135. var history = history_dao_.GetHistory(agent);
  136. // 履歴から取得範囲を特定する。
  137. (var from, var to) = GetFromTo(agent, history);
  138. logger_.Info($"取得範囲 {from.ToString("yyyy/MM/dd")} ~ {to.ToString("yyyy/MM/dd")}");
  139. // ログイン情報を取得
  140. var login_info = GetLoginInfo(agent);
  141. // ログインを開始
  142. result_code = web_controller.Login(login_info);
  143. if (result_code != Code.ResultCode.OK) {
  144. throw new Exception("ログイン失敗");
  145. }
  146. logger_.Info("ログイン成功");
  147. // ダウンロード開始
  148. result_code = web_controller.Download(from, to);
  149. if (result_code != Code.ResultCode.OK) {
  150. throw new Exception("ダウンロード失敗");
  151. }
  152. logger_.Info("ダウンロード成功");
  153. // ログアウト
  154. result_code = web_controller.Logout();
  155. if (result_code != Code.ResultCode.OK) {
  156. throw new Exception("ログアウト失敗");
  157. }
  158. logger_.Info("ログアウト成功");
  159. // データ保存
  160. int delete_count_credit = 0;
  161. int delete_count_qr = 0;
  162. transaction_ = conn_.BeginTransaction();
  163. // クレジット
  164. var credit_info_list = web_controller.GetCreditCSVDataList();
  165. if (0 < credit_info_list.Count) {
  166. var store = credit_datastore_map_[agent];
  167. delete_count_credit = store.Delete(from, to);
  168. foreach (var credit_info in credit_info_list) {
  169. store.Save(credit_info);
  170. }
  171. }
  172. // QR
  173. var qr_info_list = web_controller.GetQRCSVDataList();
  174. if (0 < qr_info_list.Count) {
  175. var store = qr_datastore_map_[agent];
  176. delete_count_qr = store.Delete(from, to);
  177. foreach (var qr_info in qr_info_list) {
  178. store.Save(qr_info);
  179. }
  180. }
  181. // コミット処理
  182. transaction_.Commit();
  183. logger_.Info($"削除件数 CREDIT:{delete_count_credit}");
  184. logger_.Info($"削除件数 QR: {delete_count_qr}");
  185. logger_.Info($"登録件数 CREDIT:{credit_info_list.Count}");
  186. logger_.Info($"登録件数 QR :{qr_info_list.Count}");
  187. web_controller.Archive(GetArchiveFilename(agent, DateTime.Now));
  188. // 履歴の登録
  189. history_dao_.Save(agent, DateTime.Now);
  190. logger_.Info($"ダウンロード終了 対象:{agent}");
  191. } catch (Exception e) {
  192. // ロールバック処理
  193. logger_.Error(e.Message);
  194. if (transaction_ != null) {
  195. transaction_.Rollback();
  196. }
  197. return Code.ResultCode.NG;
  198. }
  199. transaction_ = null;
  200. return Code.ResultCode.OK;
  201. }
  202. /// <summary>
  203. /// データ取得範囲を特定する。<br/>
  204. /// </summary>
  205. /// <param name="history"></param>
  206. /// <returns></returns>
  207. private (DateTime, DateTime) GetFromTo(Code.CreditAgent agent, List<History> history) {
  208. DateTime from, to;
  209. if (agent == Code.CreditAgent.Zeus) {
  210. // Zeusの場合は、データが修正されるケースがあるため
  211. // 期間を制御する
  212. // 前回取得日から1か月遡る。
  213. // var prev_date = DateTime.Now.AddMonths(-1);
  214. // from = new DateTime(prev_date.Year, prev_date.Month, 1);
  215. // to = from.AddMonths(1).AddDays(-1);
  216. var now_date = DateTime.Now.Date;
  217. from = now_date.AddMonths(-1);
  218. to = now_date;
  219. if (history.Count != 0) {
  220. var prev_date = history[0].date;
  221. from = prev_date.AddMonths(-1);
  222. }
  223. } else {
  224. var now_date = DateTime.Now.Date;
  225. from = now_date.AddDays(-20);
  226. to = now_date;
  227. if (history.Count != 0) {
  228. var prev_date = history[0].date;
  229. if (from < prev_date) {
  230. from = prev_date.Date;
  231. }
  232. }
  233. }
  234. return (from, to);
  235. }
  236. /// <summary>
  237. /// コンフィグファイルよりログイン情報を取得する。
  238. /// </summary>
  239. /// <param name="agent"></param>
  240. /// <returns></returns>
  241. private LoginInfo GetLoginInfo(Code.CreditAgent agent) {
  242. var login_info = new LoginInfo();
  243. String sesction = $"LOGIN_INFO_{agent.ToString()}";
  244. login_info.user_name = config_.Read(sesction, "user");
  245. if (login_info.user_name.Length == 0) {
  246. throw new Exception($"ログイン情報不足 host {agent.ToString()}");
  247. }
  248. login_info.password = config_.Read(sesction, "password");
  249. if (login_info.password.Length == 0) {
  250. throw new Exception($"ログイン情報不足 password {agent.ToString()}");
  251. }
  252. return login_info;
  253. }
  254. private String GetArchiveFilename(Code.CreditAgent agent, DateTime now) {
  255. return $"{agent.ToString()}_{now.ToString("yyyyMMddHHmmss")}.zip";
  256. }
  257. private void CreanDirectries() {
  258. var dir_download = new DirectoryInfo(DriverFactory.GetDownloadDir());
  259. foreach (var file in dir_download.GetFiles()) {
  260. file.Delete();
  261. }
  262. var tmp_download = new DirectoryInfo(DriverFactory.GetTmpDownloadDir());
  263. foreach (var file in tmp_download.GetFiles()) {
  264. file.Delete();
  265. }
  266. }
  267. }
  268. }