TaskManager.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. using COSXML;
  7. using COSXML.Auth;
  8. using COSXML.Transfer;
  9. using LitJson;
  10. using Unity.RenderStreaming;
  11. using UnityEngine;
  12. using UnityEngine.Networking;
  13. using UnityEngine.UI;
  14. public class TaskManager : MonoBehaviour
  15. {
  16. public clientTCP ctcp;
  17. public GameObject Hand;
  18. public SignalingManager sm;
  19. private void Awake()
  20. {
  21. //SignalingSettings signaling = new WebSocketSignalingSettings("ws://192.168.31.239");
  22. //sm.SetSignalingSettings(signaling);
  23. // sm.gameObject.SetActive(true);
  24. StartCoroutine("Gettest");
  25. StartCoroutine("Gettest2");
  26. }
  27. IEnumerator Gettest()
  28. {
  29. //while (true)
  30. //{
  31. UnityWebRequest www = UnityWebRequest.Get("https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/3.txt");
  32. //www.timeout = 30;
  33. yield return www.SendWebRequest();
  34. //异常处理,很多博文用了error!=null这是错de
  35. if (www.isHttpError || www.isNetworkError)
  36. {
  37. Debug.Log("err: " + www.error);
  38. throw new Exception("www download had an error" + www.error);
  39. }
  40. else
  41. {
  42. }
  43. if (www.isDone)
  44. {
  45. string STR = www.downloadHandler.text;
  46. print("接受:" + STR);
  47. SignalingSettings signaling = new WebSocketSignalingSettings(STR);
  48. sm.SetSignalingSettings(signaling);
  49. sm.gameObject.SetActive(true);
  50. }
  51. }
  52. IEnumerator Gettest2()
  53. {
  54. //while (true)
  55. //{
  56. UnityWebRequest www = UnityWebRequest.Get("https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/44.txt");
  57. //www.timeout = 30;
  58. yield return www.SendWebRequest();
  59. //异常处理,很多博文用了error!=null这是错de
  60. if (www.isHttpError || www.isNetworkError)
  61. {
  62. Debug.Log("err: " + www.error);
  63. throw new Exception("www download had an error" + www.error);
  64. }
  65. else
  66. {
  67. }
  68. if (www.isDone)
  69. {
  70. string STR = www.downloadHandler.text;
  71. print("接受:" + STR);
  72. ctcp.ClickConnect(STR);//"192.168.1.109"
  73. }
  74. }
  75. public static SJ sj;
  76. public GameObject head;
  77. bool isa;
  78. bool isp;
  79. public int nowindex;
  80. public Text text;
  81. public Text ltext;
  82. public PlayAnManager pm;
  83. public List<TaskItem> tasklist;
  84. public List<GameObject> yidonglist;
  85. bool isGrip;
  86. bool lisGrip;
  87. // Start is called before the first frame update
  88. void Start()
  89. {
  90. tasklist[0].starttask(this);
  91. }
  92. public static async void creatcsvAsync()
  93. {
  94. // 指定文件路径和文件名
  95. string filePath = Application.persistentDataPath + "/myCSVFile.csv";
  96. if(File.Exists(filePath))
  97. {
  98. File.Delete(filePath);
  99. }
  100. //开始时间、结束时间、训练次数、每次完成时长,完成组数,计划组数
  101. // 创建CSV数据
  102. string csvData = "开始时间,结束时间,训练次数,每次完成时长,完成组数,计划组数\n" +
  103. sj.starttime + "," + sj.endtime + "," + sj.xlct + "," + sj.mcsc + "," + sj.wczs + "," + sj.jhzs + "\n";
  104. // 写入文件
  105. File.WriteAllText(filePath, csvData);
  106. Debug.Log("CSV file generated at " + filePath);
  107. //初始化 CosXmlConfig
  108. string region = "ap-shanghai"; //设置一个默认的存储桶地域
  109. CosXmlConfig config = new CosXmlConfig.Builder()
  110. .IsHttps(true) //设置默认 HTTPS 请求
  111. .SetRegion(region) //设置一个默认的存储桶地域
  112. .SetDebugLog(true) //显示日志
  113. .Build(); //创建 CosXmlConfig 对象
  114. string secretId = "AKIDWwBqLepvukNlwviGaryAD2e2KXaO0Rgm";// Environment.GetEnvironmentVariable("AKIDWwBqLepvukNlwviGaryAD2e2KXaO0Rgm"); //用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
  115. string secretKey = "IbuDSep08HQaimntyz0JZwFVioo5TzY6";// Environment.GetEnvironmentVariable("IbuDSep08HQaimntyz0JZwFVioo5TzY6"); //用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
  116. long durationSecond = 600; //每次请求签名有效时长,单位为秒
  117. QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(
  118. secretId, secretKey, durationSecond);
  119. CosXml cosXml = new CosXmlServer(config, cosCredentialProvider);
  120. // 初始化 TransferConfig
  121. TransferConfig transferConfig = new TransferConfig();
  122. // 初始化 TransferManager
  123. TransferManager transferManager = new TransferManager(cosXml, transferConfig);
  124. string bucket = "wx-model-1317557471"; //存储桶,格式:BucketName-APPID
  125. string cosPath = "ff.csv"; //对象在存储桶中的位置标识符,即称对象键
  126. string srcPath = filePath;//本地文件绝对路径
  127. // 上传对象
  128. COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath);
  129. uploadTask.SetSrcPath(srcPath);
  130. uploadTask.progressCallback = delegate (long completed, long total)
  131. {
  132. Debug.Log( string.Format("progress = {0:##.##}%", completed * 100.0 / total));
  133. };
  134. try
  135. {
  136. COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = await
  137. transferManager.UploadAsync(uploadTask);
  138. Console.WriteLine(result.GetResultInfo());
  139. string eTag = result.eTag;
  140. }
  141. catch (Exception e)
  142. {
  143. Debug.Log("CosException: " + e);
  144. }
  145. }
  146. // Update is called once per frame
  147. void Update()
  148. {
  149. if (Input.GetKeyDown(KeyCode.O))
  150. {
  151. yidong();
  152. }
  153. if (text.text.Contains("Open"))
  154. {
  155. isGrip = true;
  156. }
  157. else if (isGrip && (text.text.Contains("Grab") || text.text.Contains("Pinch")))
  158. {
  159. isGrip = false;
  160. yidong();
  161. }
  162. if (ltext.text.Contains("Open"))
  163. {
  164. lisGrip = true;
  165. }
  166. else if (lisGrip && (ltext.text.Contains("Grab") || ltext.text.Contains("Pinch")))
  167. {
  168. lisGrip = false;
  169. yidong();
  170. }
  171. }
  172. public GameObject cj;
  173. public void yidong()
  174. {
  175. if (yidonglist.Count > nowindex)
  176. {
  177. Vector3 v3 = yidonglist[nowindex].transform.position - OpenXRCamera.Instance.head.transform.localPosition;
  178. head.transform.position = new Vector3(v3.x, OpenXRCamera.Instance.head.transform.position.y, v3.z);
  179. }
  180. if(nowindex== yidonglist.Count-1)
  181. {
  182. sj = new SJ();
  183. sj.starttime = DateTime.Now.ToString();
  184. creatcsvAsync();
  185. cj.SetActive(false) ;
  186. }
  187. nowindex++;
  188. isa = false;
  189. isp = false;
  190. // Debug.Log("yidong===");
  191. }
  192. public class SJ
  193. {
  194. public string starttime;
  195. public string endtime;
  196. public int xlct;
  197. public int mcsc;
  198. public int wczs;
  199. public int jhzs = 2;
  200. }
  201. }