Task.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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_task_id := c.GetString("T_task_id")
  284. r, is := Task.Read_Task(T_task_id)
  285. if !is {
  286. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  287. c.ServeJSON()
  288. return
  289. }
  290. // .......
  291. clos := make([]string, 0)
  292. if len(T_name) > 0 {
  293. r.T_name = T_name
  294. clos = append(clos, "T_name")
  295. }
  296. if T_Show_err == nil {
  297. r.T_Show = T_Show
  298. clos = append(clos, "T_Show")
  299. }
  300. if len(T_VerifyTemplate_class) > 0 {
  301. r.T_VerifyTemplate_class = T_VerifyTemplate_class
  302. clos = append(clos, "T_VerifyTemplate_class")
  303. }
  304. if len(T_VerifyTemplate_id) > 0 {
  305. r.T_VerifyTemplate_id = T_VerifyTemplate_id
  306. clos = append(clos, "T_VerifyTemplate_id")
  307. }
  308. if len(T_deadline) > 0 {
  309. r.T_deadline = T_deadline
  310. clos = append(clos, "T_deadline")
  311. }
  312. if len(T_scheme) > 0 {
  313. r.T_scheme = T_scheme
  314. clos = append(clos, "T_scheme")
  315. }
  316. if len(T_collection) > 0 {
  317. r.T_collection = T_collection
  318. clos = append(clos, "T_collection")
  319. }
  320. if len(T_reporting) > 0 {
  321. r.T_reporting = T_reporting
  322. clos = append(clos, "T_reporting")
  323. }
  324. if len(T_delivery) > 0 {
  325. r.T_delivery = T_delivery
  326. clos = append(clos, "T_delivery")
  327. }
  328. if T_collection_state == 4 {
  329. r.T_collection_state = T_collection_state
  330. clos = append(clos, "T_collection_state")
  331. }
  332. if len(T_doc1) > 0 {
  333. r.T_doc1 = T_doc1
  334. clos = append(clos, "T_doc1")
  335. }
  336. // 验证报告内容T_pdf1 ,上传后将 当前任务 实施方案 标志 为 1
  337. if len(T_pdf1) > 0 {
  338. r.T_pdf1 = T_pdf1
  339. clos = append(clos, "T_pdf1")
  340. r.T_scheme_state = 1
  341. clos = append(clos, "T_scheme_state")
  342. }
  343. if len(T_doc2) > 0 {
  344. r.T_doc2 = T_doc2
  345. clos = append(clos, "T_doc2")
  346. }
  347. // 验证报告内容T_pdf2 ,上传后将 当前任务 报告编写 标志 为 1
  348. if len(T_pdf2) > 0 {
  349. r.T_pdf2 = T_pdf2
  350. clos = append(clos, "T_pdf2")
  351. r.T_reporting_state = 1
  352. clos = append(clos, "T_reporting_state")
  353. }
  354. if len(T_doc3) > 0 {
  355. r.T_doc3 = T_doc3
  356. clos = append(clos, "T_doc3")
  357. }
  358. if len(T_pdf3) > 0 {
  359. r.T_pdf3 = T_pdf3
  360. clos = append(clos, "T_pdf3")
  361. }
  362. // .......
  363. // "T_name", "T_Show", "T_VerifyTemplate_id", "T_deadline",
  364. // "T_collection", "T_reporting", "T_delivery",
  365. // "T_collection_state", "T_reporting_state", "T_delivery_state",
  366. // "T_doc1", "T_pdf1", "T_doc2", "T_pdf2", "T_doc3", "T_pdf3"
  367. if !Task.Update_Task(r, clos...) {
  368. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  369. c.ServeJSON()
  370. return
  371. }
  372. // 添加任务操作日志
  373. Task.Add_TaskLogs_T(User_r.T_uuid, T_task_id, "任务管理", "修改", r)
  374. System.Add_UserLogs_T(User_r.T_uuid, "任务管理", "修改", r)
  375. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  376. c.ServeJSON()
  377. return
  378. }
  379. // 删除-
  380. func (c *TaskController) Del() {
  381. // 验证登录 User_is, User_r
  382. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  383. if !User_is {
  384. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  385. c.ServeJSON()
  386. return
  387. }
  388. T_task_id := c.GetString("T_task_id")
  389. if r, is := Task.Read_Task(T_task_id); is {
  390. if !Task.Delete_Task(r) {
  391. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  392. c.ServeJSON()
  393. return
  394. }
  395. // 添加任务操作日志
  396. Task.Add_TaskLogs_T(User_r.T_uuid, T_task_id, "任务管理", "删除", r)
  397. System.Add_UserLogs_T(User_r.T_uuid, "任务管理", "删除", r)
  398. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  399. c.ServeJSON()
  400. return
  401. }
  402. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  403. c.ServeJSON()
  404. return
  405. }
  406. // 列表 -
  407. func (c *TaskController) Logs_List() {
  408. // 验证登录 User_is, User_r
  409. _, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  410. if !User_is {
  411. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  412. c.ServeJSON()
  413. return
  414. }
  415. var r_jsons lib.R_JSONS
  416. page, _ := c.GetInt("page")
  417. if page < 1 {
  418. page = 1
  419. }
  420. page_z, _ := c.GetInt("page_z")
  421. if page_z < 1 {
  422. page_z = conf.Page_size
  423. }
  424. T_task_id := c.GetString("T_task_id")
  425. AdminMap := Account.AdminListToMap(Account.Read_Admin_List_ALL_1())
  426. var cnt int
  427. List, cnt := Task.Read_TaskLogs_List(T_task_id, AdminMap, page, page_z)
  428. page_size := math.Ceil(float64(cnt) / float64(page_z))
  429. r_jsons.List = List
  430. r_jsons.Page = page
  431. r_jsons.Page_size = int(page_size)
  432. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  433. r_jsons.Num = cnt
  434. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  435. c.ServeJSON()
  436. return
  437. }
  438. // 查询图片生成状态
  439. func (c *TaskController) DeviceData_JPGState() {
  440. T_task_id := c.GetString("T_task_id")
  441. T_remark := c.GetString("T_remark")
  442. jpg, is := Device.Redis_DeviceDataJPG_Get(T_task_id + T_remark)
  443. if !is {
  444. c.Data["json"] = lib.JSONS{Code: 202, Msg: "暂无图片正在生成"}
  445. c.ServeJSON()
  446. return
  447. }
  448. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: jpg}
  449. c.ServeJSON()
  450. return
  451. }
  452. func (c *TaskController) DeviceData_JPG() {
  453. StartTime := c.GetString("StartTime")
  454. if len(StartTime) > 0 {
  455. _, ok := lib.TimeStrToTime(StartTime)
  456. if !ok {
  457. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  458. c.ServeJSON()
  459. return
  460. }
  461. }
  462. EndTime := c.GetString("EndTime")
  463. if len(EndTime) > 0 {
  464. _, ok := lib.TimeStrToTime(EndTime)
  465. if !ok {
  466. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
  467. c.ServeJSON()
  468. return
  469. }
  470. }
  471. T_remark := c.GetString("T_remark")
  472. TemperatureMin, _ := c.GetFloat("TemperatureMin") // 最低温度
  473. TemperatureMax, _ := c.GetFloat("TemperatureMax") // 最高温度
  474. if TemperatureMin == 0 {
  475. TemperatureMin = 2
  476. }
  477. if TemperatureMax == 0 {
  478. TemperatureMax = 8
  479. }
  480. T_task_id := c.GetString("T_task_id")
  481. Task_r, is := Task.Read_Task(T_task_id)
  482. if !is {
  483. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
  484. c.ServeJSON()
  485. return
  486. }
  487. if Task_r.T_collection_state == 2 {
  488. c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
  489. c.ServeJSON()
  490. return
  491. }
  492. deviceClassList, _ := Device.Read_DeviceClassList_OrderList(Task_r.T_class, "", T_remark, 0, 9999)
  493. if !is {
  494. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
  495. c.ServeJSON()
  496. return
  497. }
  498. Device.Redis_DeviceDataJPG_Del(T_task_id + T_remark)
  499. // 生成图片
  500. go DeviceDataJPG(StartTime, EndTime, T_task_id, T_remark, deviceClassList, TemperatureMin, TemperatureMax)
  501. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  502. c.ServeJSON()
  503. return
  504. }
  505. // 存档生成图片
  506. func DeviceDataJPG(StartTime, EndTime, T_task_id, T_remark string, deviceList []Device.DeviceClassList, TemperatureMin, TemperatureMax float64) {
  507. Device.Redis_DeviceDataJPG_Set(T_task_id+T_remark, Device.DeviceDataJPG{
  508. State: 1,
  509. Msg: "图片生成中",
  510. Url: "",
  511. })
  512. msg := ""
  513. state := 2
  514. url := ""
  515. if TemperatureMin == 0 {
  516. TemperatureMin = 2
  517. }
  518. if TemperatureMax == 0 {
  519. TemperatureMax = 8
  520. }
  521. var ymin, ymax float64
  522. var xminT, xmaxT time.Time
  523. if len(deviceList) > 0 {
  524. ymin, ymax, xminT, xmaxT = Device.Read_DeviceData_T_Min_Max_Time_Min_Max(deviceList[0].T_sn, StartTime, EndTime)
  525. }
  526. // 创建一个新的绘图
  527. p := plot.New()
  528. // 设置绘图标题和标签
  529. p.Title.Text = "温度折线图"
  530. //p.Legend.ThumbnailWidth = 5 * vg.Inch
  531. p.X.Label.Text = "时间"
  532. p.Y.Label.Text = "温度"
  533. var chData = make(chan int, 10)
  534. var jobGroup sync.WaitGroup
  535. var device = make([]Device.DeviceCount, len(deviceList))
  536. // 创建温度线
  537. for i := 0; i < len(deviceList); i++ {
  538. chData <- 1
  539. jobGroup.Add(1)
  540. go func(index int) {
  541. //go func(index int, wg *sync.WaitGroup, p *plot.Plot) {
  542. defer func() {
  543. <-chData // 完成时chan取出1个
  544. jobGroup.Done() // 完成时将等待组值减1
  545. }()
  546. sn, id := deviceList[index].T_sn, deviceList[index].T_id
  547. ymin_, ymax_, minTime_, maxTime_ := Device.Read_DeviceData_T_Min_Max_Time_Min_Max(sn, StartTime, EndTime)
  548. if ymin > ymin_ {
  549. ymin = ymin_
  550. }
  551. if ymax < ymax_ {
  552. ymax = ymax_
  553. }
  554. if xminT.After(minTime_) && !minTime_.IsZero() {
  555. xminT = minTime_
  556. }
  557. if xmaxT.Before(maxTime_) && !maxTime_.IsZero() {
  558. xmaxT = maxTime_
  559. }
  560. r_maps, r_maps_num := Device.Read_DeviceSensorData_ById_List(sn, StartTime, EndTime, 0, 9999)
  561. device[index] = Device.DeviceCount{
  562. T_id: id,
  563. Num: r_maps_num,
  564. }
  565. if r_maps_num == 0 {
  566. return
  567. }
  568. pts := make(plotter.XYs, len(r_maps))
  569. for j, d := range r_maps {
  570. t, _ := lib.TimeStrToTime(d.T_time)
  571. pts[j].X = float64(t.Unix())
  572. pts[j].Y = float64(d.T_t)
  573. }
  574. line, err := plotter.NewLine(pts)
  575. if err != nil {
  576. return
  577. }
  578. line.Color = randomColor(index)
  579. p.Add(line)
  580. }(i)
  581. }
  582. jobGroup.Wait()
  583. xmin, xmax := float64(xminT.Unix()), float64(xmaxT.Unix())
  584. // 添加最高,最低标准线 用红色虚线标识
  585. p.Add(horizontalLine(xmin, xmax, TemperatureMin))
  586. p.Add(horizontalLine(xmin, xmax, TemperatureMax))
  587. if ymax < 8 {
  588. ymax = 8
  589. }
  590. if ymin > 0 {
  591. ymin = 0
  592. }
  593. p.Y.Min, p.Y.Max = ymin, ymax
  594. p.X.Min, p.X.Max = xmin, xmax
  595. p.Y.Tick.Marker = commaTicks{}
  596. //p.X.Tick.Marker = plot.TimeTicks{Format: "2006-01-02 15:04:05"}
  597. p.X.Tick.Marker = timeTicks{}
  598. p.X.Tick.Label.Rotation = math.Pi / 5
  599. p.X.Tick.Label.YAlign = draw.YCenter
  600. p.X.Tick.Label.XAlign = draw.XRight
  601. filename := "jpg" + time.Now().Format("20060102150405")
  602. // 保存文件
  603. if err := p.Save(10*vg.Inch, 4*vg.Inch, "ofile/"+filename+".jpg"); err != nil {
  604. Device.Redis_DeviceDataJPG_Set(T_task_id+T_remark, Device.DeviceDataJPG{
  605. State: 3,
  606. Msg: "图片生成失败",
  607. Url: url,
  608. })
  609. logs.Error(lib.FuncName(), "生成图片失败", err)
  610. return
  611. }
  612. if !lib.Pload_qiniu("ofile/"+filename+".jpg", "ofile/"+filename+".jpg") {
  613. Device.Redis_DeviceDataJPG_Set(T_task_id+T_remark, Device.DeviceDataJPG{
  614. State: 3,
  615. Msg: "图片上传七牛云失败",
  616. Url: url,
  617. })
  618. logs.Error(lib.FuncName(), "上传七牛云失败")
  619. return
  620. }
  621. //删除目录
  622. os.Remove("ofile/" + filename + ".jpg")
  623. msg = "图片生成成功"
  624. url = "https://bzdcoldverifyoss.baozhida.cn/" + "ofile/" + filename + ".jpg"
  625. Device.Redis_DeviceDataJPG_Set(T_task_id+T_remark, Device.DeviceDataJPG{
  626. State: state,
  627. Msg: msg,
  628. Url: url,
  629. Device: device,
  630. })
  631. return
  632. }
  633. func horizontalLine(xmin, xmax, y float64) *plotter.Line {
  634. pts := make(plotter.XYs, 2)
  635. pts[0].X = xmin
  636. pts[0].Y = y
  637. pts[1].X = xmax
  638. pts[1].Y = y
  639. line, err := plotter.NewLine(pts)
  640. if err != nil {
  641. panic(err)
  642. }
  643. line.LineStyle.Dashes = []vg.Length{vg.Points(8), vg.Points(5), vg.Points(1), vg.Points(5)}
  644. line.Color = color.RGBA{R: 255, A: 255}
  645. return line
  646. }
  647. type timeTicks struct{}
  648. func (timeTicks) Ticks(min, max float64) []plot.Tick {
  649. tks := plot.TimeTicks{}.Ticks(min, max)
  650. for i, t := range tks {
  651. //if t.Label == "" { // Skip minor ticks, they are fine.
  652. // continue
  653. //}
  654. tks[i].Label = time.Unix(int64(t.Value), 0).Format("2006-01-02 15:04:05")
  655. }
  656. return tks
  657. }
  658. type commaTicks struct{}
  659. // Ticks computes the default tick marks, but inserts commas
  660. // into the labels for the major tick marks.
  661. func (commaTicks) Ticks(min, max float64) []plot.Tick {
  662. tks := plot.DefaultTicks{}.Ticks(min, max)
  663. for i, t := range tks {
  664. //if t.Label == "" { // Skip minor ticks, they are fine.
  665. // continue
  666. //}
  667. tks[i].Label = fmt.Sprintf("%.0f", t.Value)
  668. }
  669. return tks
  670. }
  671. // 生成随机颜色的辅助函数
  672. func randomColor(i int) color.RGBA {
  673. var colors []color.RGBA
  674. colors = append(colors,
  675. color.RGBA{R: 52, G: 152, B: 219, A: 255},
  676. color.RGBA{R: 230, G: 126, B: 34, A: 255},
  677. color.RGBA{R: 142, G: 68, B: 173, A: 255},
  678. color.RGBA{R: 211, G: 84, B: 0, A: 255},
  679. color.RGBA{R: 231, G: 76, B: 60, A: 255},
  680. color.RGBA{R: 26, G: 188, B: 156, A: 255},
  681. color.RGBA{R: 243, G: 156, B: 18, A: 255},
  682. color.RGBA{R: 22, G: 160, B: 133, A: 255},
  683. color.RGBA{R: 46, G: 204, B: 113, A: 255},
  684. color.RGBA{R: 39, G: 174, B: 96, A: 255},
  685. color.RGBA{R: 41, G: 128, B: 185, A: 255},
  686. color.RGBA{R: 155, G: 89, B: 182, A: 255},
  687. color.RGBA{R: 192, G: 57, B: 43, A: 255},
  688. color.RGBA{R: 241, G: 196, B: 15, A: 255},
  689. )
  690. return colors[i%len(colors)]
  691. }