Task.go 19 KB

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