Task.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. package controllers
  2. import (
  3. "ColdVerify_server/Nats/NatsServer"
  4. "ColdVerify_server/conf"
  5. "ColdVerify_server/lib"
  6. "ColdVerify_server/logs"
  7. "ColdVerify_server/models/Account"
  8. "ColdVerify_server/models/Device"
  9. "ColdVerify_server/models/System"
  10. "ColdVerify_server/models/Task"
  11. "fmt"
  12. beego "github.com/beego/beego/v2/server/web"
  13. "gonum.org/v1/plot"
  14. "gonum.org/v1/plot/plotter"
  15. "gonum.org/v1/plot/vg"
  16. "gonum.org/v1/plot/vg/draw"
  17. "image/color"
  18. "math"
  19. "os"
  20. "sync"
  21. "time"
  22. )
  23. type TaskController struct {
  24. beego.Controller
  25. }
  26. // 列表 -
  27. func (c *TaskController) List() {
  28. // 验证登录 User_is, User_r
  29. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  30. if !User_is {
  31. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  32. c.ServeJSON()
  33. return
  34. }
  35. var r_jsons lib.R_JSONS
  36. page, _ := c.GetInt("page")
  37. if page < 1 {
  38. page = 1
  39. }
  40. page_z, _ := c.GetInt("page_z")
  41. if page_z < 1 {
  42. page_z = conf.Page_size
  43. }
  44. T_name := c.GetString("T_name")
  45. T_uuid := c.GetString("T_uuid")
  46. UserMap := Account.UserListToMap(Account.Read_User_List_ALL_1())
  47. AdminMap := Account.AdminListToMap(Account.Read_Admin_List_ALL_1())
  48. var T_admin string
  49. if User_r.T_power > 2 {
  50. T_admin = User_r.T_uuid
  51. }
  52. var cnt int
  53. List, cnt := Task.Read_Task_List(T_uuid, T_admin, T_name, UserMap, AdminMap, page, page_z)
  54. page_size := math.Ceil(float64(cnt) / float64(page_z))
  55. r_jsons.List = List
  56. r_jsons.Page = page
  57. r_jsons.Page_size = int(page_size)
  58. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  59. r_jsons.Num = cnt
  60. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  61. c.ServeJSON()
  62. return
  63. }
  64. // 列表 -
  65. func (c *TaskController) UserTaskList() {
  66. // 验证登录 User_is, User_r
  67. User_r, User_is := Account.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  68. if !User_is {
  69. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  70. c.ServeJSON()
  71. return
  72. }
  73. var r_jsons lib.R_JSONS
  74. page, _ := c.GetInt("page")
  75. if page < 1 {
  76. page = 1
  77. }
  78. page_z, _ := c.GetInt("page_z")
  79. if page_z < 1 {
  80. page_z = conf.Page_size
  81. }
  82. T_name := c.GetString("T_name")
  83. UserMap := Account.UserListToMap(Account.Read_User_List_ALL_1())
  84. AdminMap := Account.AdminListToMap(Account.Read_Admin_List_ALL_1())
  85. var cnt int
  86. List, cnt := Task.Read_UserTask_List(User_r.T_uuid, T_name, UserMap, AdminMap, page, page_z)
  87. page_size := math.Ceil(float64(cnt) / float64(page_z))
  88. r_jsons.List = List
  89. r_jsons.Page = page
  90. r_jsons.Page_size = int(page_size)
  91. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  92. r_jsons.Num = cnt
  93. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  94. c.ServeJSON()
  95. return
  96. }
  97. // 获取-
  98. func (c *TaskController) Get() {
  99. // 验证登录 User_is, User_r
  100. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  101. if !User_is {
  102. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  103. c.ServeJSON()
  104. return
  105. }
  106. T_task_id := c.GetString("T_task_id")
  107. r, is := Task.Read_Task(T_task_id)
  108. if !is {
  109. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  110. c.ServeJSON()
  111. return
  112. }
  113. // 添加浏览量
  114. _ = Task.Add_Task_Visit(r)
  115. r.T_Visit += 1
  116. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Task.TaskToTask_R(r)}
  117. c.ServeJSON()
  118. return
  119. }
  120. // 添加-
  121. func (c *TaskController) Add() {
  122. // 验证登录 User_is, User_r
  123. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  124. if !User_is {
  125. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  126. c.ServeJSON()
  127. return
  128. }
  129. dc := Device.DeviceClass{
  130. T_uuid: User_r.T_uuid,
  131. T_State: 1,
  132. }
  133. T_class_id, is := Device.Add_DeviceClass(dc)
  134. if !is {
  135. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加分类失败!"}
  136. c.ServeJSON()
  137. return
  138. }
  139. System.Add_UserLogs_T(User_r.T_uuid, "分类管理", "添加", dc)
  140. T_name := c.GetString("T_name")
  141. T_uuid := c.GetString("T_uuid") // 用户uuid
  142. T_VerifyTemplate_class := c.GetString("T_VerifyTemplate_class")
  143. T_VerifyTemplate_id := c.GetString("T_VerifyTemplate_id")
  144. T_deadline := c.GetString("T_deadline")
  145. T_scheme := c.GetString("T_scheme")
  146. T_collection := c.GetString("T_collection")
  147. T_reporting := c.GetString("T_reporting")
  148. T_delivery := c.GetString("T_delivery")
  149. var_ := Task.Task{
  150. T_class: int(T_class_id),
  151. T_uuid: T_uuid,
  152. T_name: T_name,
  153. T_VerifyTemplate_class: T_VerifyTemplate_class,
  154. T_VerifyTemplate_id: T_VerifyTemplate_id,
  155. T_deadline: T_deadline,
  156. T_scheme: T_scheme,
  157. T_collection: T_collection,
  158. T_reporting: T_reporting,
  159. T_delivery: T_delivery,
  160. T_Show: 1,
  161. T_State: 1,
  162. }
  163. T_task_id, is := Task.Add_Task(var_)
  164. if !is {
  165. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  166. c.ServeJSON()
  167. return
  168. }
  169. NatsServer.Create_Local_Table(T_task_id)
  170. // 添加任务操作日志
  171. Task.Add_TaskLogs_T(User_r.T_uuid, T_task_id, "任务管理", "添加", var_)
  172. System.Add_UserLogs_T(User_r.T_uuid, "任务管理", "添加", var_)
  173. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_task_id}
  174. c.ServeJSON()
  175. return
  176. }
  177. // 添加-
  178. func (c *TaskController) AddData_Tool() {
  179. T_uuid := "3e84dda9-9eec-42b9-9350-0894262fc8a1" // 用户uuid
  180. T_name := c.GetString("T_name")
  181. T_task_id := c.GetString("T_task_id")
  182. r, _ := Task.Read_Task(T_task_id)
  183. if r.T_collection_state == 2 {
  184. c.Data["json"] = lib.JSONS{Code: 200, Msg: "数据采集中..."}
  185. c.ServeJSON()
  186. return
  187. }
  188. if r.Id > 0 {
  189. // 同步1.0数据
  190. NatsServer.Sync1_TaskData(T_task_id)
  191. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_task_id}
  192. c.ServeJSON()
  193. return
  194. }
  195. dc := Device.DeviceClass{
  196. T_uuid: T_uuid,
  197. T_State: 1,
  198. }
  199. T_class_id, is := Device.Add_DeviceClass(dc)
  200. if !is {
  201. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加分类失败!"}
  202. c.ServeJSON()
  203. return
  204. }
  205. var_ := Task.Task{
  206. T_task_id: T_task_id,
  207. T_class: int(T_class_id),
  208. T_uuid: T_uuid,
  209. T_name: T_name,
  210. T_Show: 1,
  211. T_State: 1,
  212. T_collection_state: 2,
  213. }
  214. _, is = Task.Add_Task_Tool(var_)
  215. if !is {
  216. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  217. c.ServeJSON()
  218. return
  219. }
  220. // 创建本地表
  221. NatsServer.Create_Local_Table(T_task_id)
  222. // 同步1.0数据
  223. NatsServer.Sync1_TaskData(T_task_id)
  224. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_task_id}
  225. c.ServeJSON()
  226. return
  227. }
  228. // 修改采集状态-
  229. func (c *TaskController) UpCollectionState() {
  230. // 验证登录 User_is, User_r
  231. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  232. if !User_is {
  233. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  234. c.ServeJSON()
  235. return
  236. }
  237. T_collection_state, _ := c.GetInt("T_collection_state")
  238. T_task_id := c.GetString("T_task_id")
  239. r, is := Task.Read_Task(T_task_id)
  240. if !is {
  241. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  242. c.ServeJSON()
  243. return
  244. }
  245. r.T_collection_state = T_collection_state
  246. if !Task.Update_Task(r, "T_collection_state") {
  247. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  248. c.ServeJSON()
  249. return
  250. }
  251. // 添加任务操作日志
  252. Task.Add_TaskLogs_T(User_r.T_uuid, T_task_id, "任务管理", "修改采集状态", r)
  253. System.Add_UserLogs_T(User_r.T_uuid, "任务管理", "修改采集状态", r)
  254. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  255. c.ServeJSON()
  256. return
  257. }
  258. // 修改-
  259. func (c *TaskController) Up() {
  260. // 验证登录 User_is, User_r
  261. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  262. if !User_is {
  263. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  264. c.ServeJSON()
  265. return
  266. }
  267. T_name := c.GetString("T_name")
  268. T_Show, T_Show_err := c.GetInt("T_Show")
  269. T_VerifyTemplate_class := c.GetString("T_VerifyTemplate_class")
  270. T_VerifyTemplate_id := c.GetString("T_VerifyTemplate_id")
  271. T_deadline := c.GetString("T_deadline")
  272. T_scheme := c.GetString("T_scheme")
  273. T_collection := c.GetString("T_collection")
  274. T_collection_state, _ := c.GetInt("T_collection_state")
  275. T_reporting := c.GetString("T_reporting")
  276. T_delivery := c.GetString("T_delivery")
  277. T_doc1 := c.GetString("T_doc1")
  278. T_pdf1 := c.GetString("T_pdf1")
  279. T_doc2 := c.GetString("T_doc2")
  280. T_pdf2 := c.GetString("T_pdf2")
  281. T_doc3 := c.GetString("T_doc3")
  282. T_pdf3 := c.GetString("T_pdf3")
  283. T_VerifyDeviceDataStartTime := c.GetString("T_VerifyDeviceDataStartTime") // 验证设备数据开始时间
  284. T_VerifyDeviceDataEndTime := c.GetString("T_VerifyDeviceDataEndTime") // 验证设备数据开始时间
  285. T_BindDeviceDataStartTime := c.GetString("T_BindDeviceDataStartTime") // 绑定设备数据开始时间
  286. T_BindDeviceDataEndTime := c.GetString("T_BindDeviceDataEndTime") // 绑定设备数据结束时间
  287. T_task_id := c.GetString("T_task_id")
  288. r, is := Task.Read_Task(T_task_id)
  289. if !is {
  290. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  291. c.ServeJSON()
  292. return
  293. }
  294. // .......
  295. clos := make([]string, 0)
  296. if len(T_name) > 0 {
  297. r.T_name = T_name
  298. clos = append(clos, "T_name")
  299. }
  300. if T_Show_err == nil {
  301. r.T_Show = T_Show
  302. clos = append(clos, "T_Show")
  303. }
  304. if len(T_VerifyTemplate_class) > 0 {
  305. r.T_VerifyTemplate_class = T_VerifyTemplate_class
  306. clos = append(clos, "T_VerifyTemplate_class")
  307. }
  308. if len(T_VerifyTemplate_id) > 0 {
  309. r.T_VerifyTemplate_id = T_VerifyTemplate_id
  310. clos = append(clos, "T_VerifyTemplate_id")
  311. }
  312. if len(T_deadline) > 0 {
  313. r.T_deadline = T_deadline
  314. clos = append(clos, "T_deadline")
  315. }
  316. if len(T_scheme) > 0 {
  317. r.T_scheme = T_scheme
  318. clos = append(clos, "T_scheme")
  319. }
  320. if len(T_collection) > 0 {
  321. r.T_collection = T_collection
  322. clos = append(clos, "T_collection")
  323. }
  324. if len(T_reporting) > 0 {
  325. r.T_reporting = T_reporting
  326. clos = append(clos, "T_reporting")
  327. }
  328. if len(T_delivery) > 0 {
  329. r.T_delivery = T_delivery
  330. clos = append(clos, "T_delivery")
  331. }
  332. if T_collection_state == 4 {
  333. r.T_collection_state = T_collection_state
  334. clos = append(clos, "T_collection_state")
  335. }
  336. if len(T_doc1) > 0 {
  337. r.T_doc1 = T_doc1
  338. clos = append(clos, "T_doc1")
  339. }
  340. // 验证报告内容T_pdf1 ,上传后将 当前任务 实施方案 标志 为 1
  341. if len(T_pdf1) > 0 {
  342. r.T_pdf1 = T_pdf1
  343. clos = append(clos, "T_pdf1")
  344. r.T_scheme_state = 1
  345. clos = append(clos, "T_scheme_state")
  346. }
  347. if len(T_doc2) > 0 {
  348. r.T_doc2 = T_doc2
  349. clos = append(clos, "T_doc2")
  350. }
  351. // 验证报告内容T_pdf2 ,上传后将 当前任务 报告编写 标志 为 1
  352. if len(T_pdf2) > 0 {
  353. r.T_pdf2 = T_pdf2
  354. clos = append(clos, "T_pdf2")
  355. r.T_reporting_state = 1
  356. clos = append(clos, "T_reporting_state")
  357. }
  358. if len(T_doc3) > 0 {
  359. r.T_doc3 = T_doc3
  360. clos = append(clos, "T_doc3")
  361. }
  362. if len(T_pdf3) > 0 {
  363. r.T_pdf3 = T_pdf3
  364. clos = append(clos, "T_pdf3")
  365. }
  366. if len(T_VerifyDeviceDataStartTime) > 0 {
  367. r.T_VerifyDeviceDataStartTime = T_VerifyDeviceDataStartTime
  368. clos = append(clos, "T_VerifyDeviceDataStartTime")
  369. }
  370. if len(T_VerifyDeviceDataEndTime) > 0 {
  371. r.T_VerifyDeviceDataEndTime = T_VerifyDeviceDataEndTime
  372. clos = append(clos, "T_VerifyDeviceDataEndTime")
  373. }
  374. if len(T_BindDeviceDataStartTime) > 0 {
  375. r.T_BindDeviceDataStartTime = T_BindDeviceDataStartTime
  376. clos = append(clos, "T_BindDeviceDataStartTime")
  377. }
  378. if len(T_BindDeviceDataEndTime) > 0 {
  379. r.T_BindDeviceDataEndTime = T_BindDeviceDataEndTime
  380. clos = append(clos, "T_BindDeviceDataEndTime")
  381. }
  382. // .......
  383. // "T_name", "T_Show", "T_VerifyTemplate_id", "T_deadline",
  384. // "T_collection", "T_reporting", "T_delivery",
  385. // "T_collection_state", "T_reporting_state", "T_delivery_state",
  386. // "T_doc1", "T_pdf1", "T_doc2", "T_pdf2", "T_doc3", "T_pdf3"
  387. if !Task.Update_Task(r, clos...) {
  388. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  389. c.ServeJSON()
  390. return
  391. }
  392. // 添加任务操作日志
  393. Task.Add_TaskLogs_T(User_r.T_uuid, T_task_id, "任务管理", "修改", r)
  394. System.Add_UserLogs_T(User_r.T_uuid, "任务管理", "修改", r)
  395. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  396. c.ServeJSON()
  397. return
  398. }
  399. // 删除-
  400. func (c *TaskController) Del() {
  401. // 验证登录 User_is, User_r
  402. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  403. if !User_is {
  404. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  405. c.ServeJSON()
  406. return
  407. }
  408. T_task_id := c.GetString("T_task_id")
  409. if r, is := Task.Read_Task(T_task_id); is {
  410. if !Task.Delete_Task(r) {
  411. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  412. c.ServeJSON()
  413. return
  414. }
  415. // 添加任务操作日志
  416. Task.Add_TaskLogs_T(User_r.T_uuid, T_task_id, "任务管理", "删除", r)
  417. System.Add_UserLogs_T(User_r.T_uuid, "任务管理", "删除", r)
  418. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  419. c.ServeJSON()
  420. return
  421. }
  422. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  423. c.ServeJSON()
  424. return
  425. }
  426. // 列表 -
  427. func (c *TaskController) Logs_List() {
  428. // 验证登录 User_is, User_r
  429. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  430. if !User_is {
  431. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  432. c.ServeJSON()
  433. return
  434. }
  435. var r_jsons lib.R_JSONS
  436. page, _ := c.GetInt("page")
  437. if page < 1 {
  438. page = 1
  439. }
  440. page_z, _ := c.GetInt("page_z")
  441. if page_z < 1 {
  442. page_z = conf.Page_size
  443. }
  444. T_task_id := c.GetString("T_task_id")
  445. AdminMap := Account.AdminListToMap(Account.Read_Admin_List_ALL_1())
  446. var cnt int
  447. List, cnt := Task.Read_TaskLogs_List(T_task_id, AdminMap, page, page_z)
  448. page_size := math.Ceil(float64(cnt) / float64(page_z))
  449. r_jsons.List = List
  450. r_jsons.Page = page
  451. r_jsons.Page_size = int(page_size)
  452. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  453. r_jsons.Num = cnt
  454. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  455. c.ServeJSON()
  456. return
  457. }
  458. // 查询图片生成状态
  459. func (c *TaskController) DeviceData_JPGState() {
  460. T_task_id := c.GetString("T_task_id")
  461. T_remark := c.GetString("T_remark")
  462. jpg, is := Device.Redis_DeviceDataJPG_Get(T_task_id + T_remark)
  463. if !is {
  464. c.Data["json"] = lib.JSONS{Code: 202, Msg: "暂无图片正在生成"}
  465. c.ServeJSON()
  466. return
  467. }
  468. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: jpg}
  469. c.ServeJSON()
  470. return
  471. }
  472. func (c *TaskController) DeviceData_JPG() {
  473. StartTime := c.GetString("StartTime")
  474. if len(StartTime) > 0 {
  475. _, ok := lib.TimeStrToTime(StartTime)
  476. if !ok {
  477. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  478. c.ServeJSON()
  479. return
  480. }
  481. }
  482. EndTime := c.GetString("EndTime")
  483. if len(EndTime) > 0 {
  484. _, ok := lib.TimeStrToTime(EndTime)
  485. if !ok {
  486. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  487. c.ServeJSON()
  488. return
  489. }
  490. }
  491. T_remark := c.GetString("T_remark")
  492. TemperatureMin, _ := c.GetFloat("TemperatureMin") // 最低温度
  493. TemperatureMax, _ := c.GetFloat("TemperatureMax") // 最高温度
  494. if TemperatureMin == 0 {
  495. TemperatureMin = 2
  496. }
  497. if TemperatureMax == 0 {
  498. TemperatureMax = 8
  499. }
  500. T_task_id := c.GetString("T_task_id")
  501. Task_r, is := Task.Read_Task(T_task_id)
  502. if !is {
  503. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  504. c.ServeJSON()
  505. return
  506. }
  507. if Task_r.T_collection_state == 2 {
  508. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  509. c.ServeJSON()
  510. return
  511. }
  512. deviceClassList, _ := Device.Read_DeviceClassList_OrderList(Task_r.T_class, "", "", T_remark, 0, 9999)
  513. if !is {
  514. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  515. c.ServeJSON()
  516. return
  517. }
  518. Device.Redis_DeviceDataJPG_Del(T_task_id + T_remark)
  519. // 生成图片
  520. go DeviceDataJPG(StartTime, EndTime, T_task_id, T_remark, deviceClassList, TemperatureMin, TemperatureMax)
  521. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  522. c.ServeJSON()
  523. return
  524. }
  525. // 存档生成图片
  526. func DeviceDataJPG(StartTime, EndTime, T_task_id, T_remark string, deviceList []Device.DeviceClassList, TemperatureMin, TemperatureMax float64) {
  527. Device.Redis_DeviceDataJPG_Set(T_task_id+T_remark, Device.DeviceDataJPG{
  528. State: 1,
  529. Msg: "图片生成中",
  530. Url: "",
  531. })
  532. msg := ""
  533. state := 2
  534. url := ""
  535. if TemperatureMin == 0 {
  536. TemperatureMin = 2
  537. }
  538. if TemperatureMax == 0 {
  539. TemperatureMax = 8
  540. }
  541. var ymin, ymax float64
  542. var xminT, xmaxT time.Time
  543. if len(deviceList) > 0 {
  544. ymin, ymax, xminT, xmaxT = Device.Read_DeviceData_T_Min_Max_Time_Min_Max(deviceList[0].T_sn, StartTime, EndTime)
  545. }
  546. // 创建一个新的绘图
  547. p := plot.New()
  548. // 设置绘图标题和标签
  549. p.Title.Text = "温度折线图"
  550. //p.Legend.ThumbnailWidth = 5 * vg.Inch
  551. p.X.Label.Text = "时间"
  552. p.Y.Label.Text = "温度"
  553. var chData = make(chan int, 10)
  554. var jobGroup sync.WaitGroup
  555. var device = make([]Device.DeviceCount, len(deviceList))
  556. // 创建温度线
  557. for i := 0; i < len(deviceList); i++ {
  558. chData <- 1
  559. jobGroup.Add(1)
  560. go func(index int) {
  561. //go func(index int, wg *sync.WaitGroup, p *plot.Plot) {
  562. defer func() {
  563. <-chData // 完成时chan取出1个
  564. jobGroup.Done() // 完成时将等待组值减1
  565. }()
  566. sn, id := deviceList[index].T_sn, deviceList[index].T_id
  567. ymin_, ymax_, minTime_, maxTime_ := Device.Read_DeviceData_T_Min_Max_Time_Min_Max(sn, StartTime, EndTime)
  568. if ymin > ymin_ {
  569. ymin = ymin_
  570. }
  571. if ymax < ymax_ {
  572. ymax = ymax_
  573. }
  574. if xminT.After(minTime_) && !minTime_.IsZero() {
  575. xminT = minTime_
  576. }
  577. if xmaxT.Before(maxTime_) && !maxTime_.IsZero() {
  578. xmaxT = maxTime_
  579. }
  580. r_maps, r_maps_num := Device.Read_DeviceSensorData_ById_List(sn, StartTime, EndTime, 0, 9999)
  581. device[index] = Device.DeviceCount{
  582. T_id: id,
  583. Num: r_maps_num,
  584. }
  585. if r_maps_num == 0 {
  586. return
  587. }
  588. pts := make(plotter.XYs, len(r_maps))
  589. for j, d := range r_maps {
  590. t, _ := lib.TimeStrToTime(d.T_time)
  591. pts[j].X = float64(t.Unix())
  592. pts[j].Y = float64(d.T_t)
  593. }
  594. line, err := plotter.NewLine(pts)
  595. if err != nil {
  596. return
  597. }
  598. line.Color = randomColor(index)
  599. p.Add(line)
  600. }(i)
  601. }
  602. jobGroup.Wait()
  603. xmin, xmax := float64(xminT.Unix()), float64(xmaxT.Unix())
  604. // 添加最高,最低标准线 用红色虚线标识
  605. p.Add(horizontalLine(xmin, xmax, TemperatureMin))
  606. p.Add(horizontalLine(xmin, xmax, TemperatureMax))
  607. if ymax < 8 {
  608. ymax = 8
  609. }
  610. if ymin > 0 {
  611. ymin = 0
  612. }
  613. p.Y.Min, p.Y.Max = ymin, ymax
  614. p.X.Min, p.X.Max = xmin, xmax
  615. p.Y.Tick.Marker = commaTicks{}
  616. //p.X.Tick.Marker = plot.TimeTicks{Format: "2006-01-02 15:04:05"}
  617. p.X.Tick.Marker = timeTicks{}
  618. p.X.Tick.Label.Rotation = math.Pi / 5
  619. p.X.Tick.Label.YAlign = draw.YCenter
  620. p.X.Tick.Label.XAlign = draw.XRight
  621. filename := "jpg" + time.Now().Format("20060102150405")
  622. // 保存文件
  623. if err := p.Save(10*vg.Inch, 4*vg.Inch, "ofile/"+filename+".jpg"); err != nil {
  624. Device.Redis_DeviceDataJPG_Set(T_task_id+T_remark, Device.DeviceDataJPG{
  625. State: 3,
  626. Msg: "图片生成失败",
  627. Url: url,
  628. })
  629. logs.Error(lib.FuncName(), "生成图片失败", err)
  630. return
  631. }
  632. if !lib.Pload_qiniu("ofile/"+filename+".jpg", "ofile/"+filename+".jpg") {
  633. Device.Redis_DeviceDataJPG_Set(T_task_id+T_remark, Device.DeviceDataJPG{
  634. State: 3,
  635. Msg: "图片上传七牛云失败",
  636. Url: url,
  637. })
  638. logs.Error(lib.FuncName(), "上传七牛云失败")
  639. return
  640. }
  641. //删除目录
  642. os.Remove("ofile/" + filename + ".jpg")
  643. msg = "图片生成成功"
  644. url = "https://bzdcoldverifyoss.baozhida.cn/" + "ofile/" + filename + ".jpg"
  645. Device.Redis_DeviceDataJPG_Set(T_task_id+T_remark, Device.DeviceDataJPG{
  646. State: state,
  647. Msg: msg,
  648. Url: url,
  649. Device: device,
  650. })
  651. return
  652. }
  653. func horizontalLine(xmin, xmax, y float64) *plotter.Line {
  654. pts := make(plotter.XYs, 2)
  655. pts[0].X = xmin
  656. pts[0].Y = y
  657. pts[1].X = xmax
  658. pts[1].Y = y
  659. line, err := plotter.NewLine(pts)
  660. if err != nil {
  661. panic(err)
  662. }
  663. line.LineStyle.Dashes = []vg.Length{vg.Points(8), vg.Points(5), vg.Points(1), vg.Points(5)}
  664. line.Color = color.RGBA{R: 255, A: 255}
  665. return line
  666. }
  667. type timeTicks struct{}
  668. func (timeTicks) Ticks(min, max float64) []plot.Tick {
  669. tks := plot.TimeTicks{}.Ticks(min, max)
  670. for i, t := range tks {
  671. //if t.Label == "" { // Skip minor ticks, they are fine.
  672. // continue
  673. //}
  674. tks[i].Label = time.Unix(int64(t.Value), 0).Format("2006-01-02 15:04:05")
  675. }
  676. return tks
  677. }
  678. type commaTicks struct{}
  679. // Ticks computes the default tick marks, but inserts commas
  680. // into the labels for the major tick marks.
  681. func (commaTicks) Ticks(min, max float64) []plot.Tick {
  682. tks := plot.DefaultTicks{}.Ticks(min, max)
  683. for i, t := range tks {
  684. //if t.Label == "" { // Skip minor ticks, they are fine.
  685. // continue
  686. //}
  687. tks[i].Label = fmt.Sprintf("%.0f", t.Value)
  688. }
  689. return tks
  690. }
  691. // 生成随机颜色的辅助函数
  692. func randomColor(i int) color.RGBA {
  693. var colors []color.RGBA
  694. colors = append(colors,
  695. color.RGBA{R: 52, G: 152, B: 219, A: 255},
  696. color.RGBA{R: 230, G: 126, B: 34, A: 255},
  697. color.RGBA{R: 142, G: 68, B: 173, A: 255},
  698. color.RGBA{R: 211, G: 84, B: 0, A: 255},
  699. color.RGBA{R: 231, G: 76, B: 60, A: 255},
  700. color.RGBA{R: 26, G: 188, B: 156, A: 255},
  701. color.RGBA{R: 243, G: 156, B: 18, A: 255},
  702. color.RGBA{R: 22, G: 160, B: 133, A: 255},
  703. color.RGBA{R: 46, G: 204, B: 113, A: 255},
  704. color.RGBA{R: 39, G: 174, B: 96, A: 255},
  705. color.RGBA{R: 41, G: 128, B: 185, A: 255},
  706. color.RGBA{R: 155, G: 89, B: 182, A: 255},
  707. color.RGBA{R: 192, G: 57, B: 43, A: 255},
  708. color.RGBA{R: 241, G: 196, B: 15, A: 255},
  709. )
  710. return colors[i%len(colors)]
  711. }