12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594 |
- package controllers
- import (
- "ColdVerify_local/Nats"
- "ColdVerify_local/Nats/NatsServer"
- "ColdVerify_local/conf"
- "ColdVerify_local/lib"
- "ColdVerify_local/logs"
- "ColdVerify_local/models/Certificate"
- "ColdVerify_local/models/System"
- "ColdVerify_local/models/Task"
- "errors"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- beego "github.com/beego/beego/v2/server/web"
- "github.com/signintech/gopdf"
- "github.com/vmihailenco/msgpack/v5"
- "github.com/xuri/excelize/v2"
- "gonum.org/v1/plot"
- "gonum.org/v1/plot/plotter"
- "gonum.org/v1/plot/vg"
- "gonum.org/v1/plot/vg/draw"
- "math"
- "os"
- "sort"
- "strconv"
- "strings"
- "sync"
- "time"
- )
- type TaskDataController struct {
- beego.Controller
- }
- // 列表 -
- func (c *TaskDataController) TaskData_List() {
- var r_jsons lib.R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- T_sn := c.GetString("T_sn")
- T_id := c.GetString("T_id")
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- dcList, err := NatsServer.Device_Class_List(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取设备列表失败!"}
- c.ServeJSON()
- return
- }
- // 保存布局编号和校准证书对应关系
- var deviceCertificateMap = make(map[string]string) // t_id, T_Certificate_sn
- for _, v := range dcList {
- deviceCertificateMap[v.T_id] = v.T_Certificate_sn
- }
- var cnt int64
- List, cnt := Task.Read_TaskData_ById_List(Task_r.T_task_id, T_sn, T_id, Time_start, Time_end, page, page_z)
- for i := 0; i < len(List); i++ {
- List[i].T_Certificate_sn = deviceCertificateMap[List[i].T_id]
- }
- page_size := math.Ceil(float64(cnt) / float64(page_z))
- r_jsons.List = List
- r_jsons.Page = page
- r_jsons.Page_size = int(page_size)
- r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
- r_jsons.Num = int(cnt)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *TaskDataController) TaskData_Temperature_Pdf() {
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- T_snid := c.GetString("T_snid")
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- user, err := NatsServer.Read_User(Task_r.T_uuid)
- var T_snid_list []string
- if len(T_snid) > 0 {
- T_snid_list = strings.Split(strings.Trim(T_snid, "|"), "|")
- }
- dataMap := make(map[string]string)
- timeMap := make(map[string]bool)
- //classList := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
- //for _, i2 := range classList {
- // T_snid_list = append(T_snid_list, fmt.Sprintf("%s,%s", i2.T_sn, i2.T_id))
- //}
- for _, v := range T_snid_list {
- sn_id := strings.Split(v, ",")
- if len(sn_id) == 2 {
- List, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, sn_id[0], sn_id[1], Time_start, Time_end, 0, 9999)
- for _, data := range List {
- k := fmt.Sprintf("%s,%s", data.T_time, data.T_id)
- dataMap[k] = fmt.Sprintf("%.1f", data.T_t)
- if _, ok := timeMap[data.T_time]; !ok {
- timeMap[data.T_time] = true
- }
- }
- }
- }
- var timeList []string
- for k, _ := range timeMap {
- timeList = append(timeList, k)
- }
- sort.Slice(timeList, func(i, j int) bool {
- return timeList[i] < timeList[j]
- })
- pdf := &gopdf.GoPdf{}
- pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
- err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
- if err != nil {
- return
- }
- err = pdf.SetFont("wts", "", 15)
- if err != nil {
- return
- }
- pdf.SetGrayFill(0.5)
- pdf.SetMargins(0, 20, 0, 20)
- pdf.AddPage()
- imgH, _ := gopdf.ImageHolderByPath("./ofile/logo.jpg")
- err = pdf.ImageByHolder(imgH, 10, 10, &gopdf.Rect{W: 93, H: 32})
- name := user.T_name
- var y float64 = 40
- textw, _ := pdf.MeasureTextWidth(name)
- pdf.SetX((595 / 2) - (textw / 2))
- pdf.SetY(y)
- pdf.Text(name)
- y += 20
- title := Task_r.T_name + "温度验证数据"
- textw, _ = pdf.MeasureTextWidth(title)
- pdf.SetX((595 / 2) - (textw / 2))
- pdf.SetY(y)
- pdf.Text(title)
- if len(timeList) > 0 {
- y += 20
- timeStr := fmt.Sprintf("%s - %s", timeList[0], timeList[len(timeList)-1])
- textw, _ = pdf.MeasureTextWidth(timeStr)
- pdf.SetX((595 / 2) - (textw / 2))
- pdf.SetY(y)
- pdf.Text(timeStr)
- }
- err = pdf.SetFont("wts", "", 10)
- if err != nil {
- return
- }
- y += 20
- var x float64 = 10
- var w float64 = 120
- chunks := chunkBy(T_snid_list, 15)
- for _, list := range chunks {
- x = 10
- w = 120
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- w = 30
- for _, v2 := range list {
- sn_id2 := strings.Split(v2, ",")
- id2 := sn_id2[1]
- lib.RectFillColor(pdf, id2, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- }
- y += 20
- for _, t := range timeList {
- x = 10
- var textH float64 = 20 // if text height is 25px.
- pdf.SetNewY(y, textH)
- y = pdf.GetY()
- if y == 20 {
- w = 120
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- w = 30
- for _, v2 := range list {
- sn_id2 := strings.Split(v2, ",")
- id2 := sn_id2[1]
- lib.RectFillColor(pdf, id2, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- }
- y += 20
- x = 10
- }
- w = 120
- lib.RectFillColor(pdf, t, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- w = 30
- for _, v := range list {
- sn_id := strings.Split(v, ",")
- id := sn_id[1]
- t_t := dataMap[fmt.Sprintf("%s,%s", t, id)]
- lib.RectFillColor(pdf, t_t, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- }
- y += 20
- }
- y += 20
- }
- filename := time.Now().Format("20060102150405") + ".pdf"
- timeStr := "ofile/温度" + filename
- err = pdf.WritePdf(timeStr)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- defer func() {
- //删除目录
- os.Remove(timeStr)
- }()
- c.Ctx.Output.Download(timeStr)
- }
- func (c *TaskDataController) TaskData_Humidity_Pdf() {
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- T_snid := c.GetString("T_snid")
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- user, err := NatsServer.Read_User(Task_r.T_uuid)
- var T_snid_list []string
- if len(T_snid) > 0 {
- T_snid_list = strings.Split(strings.Trim(T_snid, "|"), "|")
- }
- dataMap := make(map[string]string)
- timeMap := make(map[string]bool)
- //classList := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
- //for _, i2 := range classList {
- // T_snid_list = append(T_snid_list, fmt.Sprintf("%s,%s", i2.T_sn, i2.T_id))
- //}
- for _, v := range T_snid_list {
- sn_id := strings.Split(v, ",")
- if len(sn_id) == 2 {
- List, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, sn_id[0], sn_id[1], Time_start, Time_end, 0, 9999)
- for _, data := range List {
- k := fmt.Sprintf("%s,%s", data.T_time, data.T_id)
- dataMap[k] = fmt.Sprintf("%.1f", data.T_rh)
- if _, ok := timeMap[data.T_time]; !ok {
- timeMap[data.T_time] = true
- }
- }
- }
- }
- var timeList []string
- for k, _ := range timeMap {
- timeList = append(timeList, k)
- }
- sort.Slice(timeList, func(i, j int) bool {
- return timeList[i] < timeList[j]
- })
- pdf := &gopdf.GoPdf{}
- pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
- err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
- if err != nil {
- return
- }
- err = pdf.SetFont("wts", "", 15)
- if err != nil {
- return
- }
- pdf.SetGrayFill(0.5)
- pdf.SetMargins(0, 20, 0, 20)
- pdf.AddPage()
- imgH, _ := gopdf.ImageHolderByPath("./ofile/logo.jpg")
- err = pdf.ImageByHolder(imgH, 10, 10, &gopdf.Rect{W: 93, H: 32})
- name := user.T_name
- var y float64 = 40
- textw, _ := pdf.MeasureTextWidth(name)
- pdf.SetX((595 / 2) - (textw / 2))
- pdf.SetY(y)
- pdf.Text(name)
- y += 20
- title := Task_r.T_name + "湿度验证数据"
- textw, _ = pdf.MeasureTextWidth(title)
- pdf.SetX((595 / 2) - (textw / 2))
- pdf.SetY(y)
- pdf.Text(title)
- if len(timeList) > 0 {
- y += 20
- timeStr := fmt.Sprintf("%s - %s", timeList[0], timeList[len(timeList)-1])
- textw, _ = pdf.MeasureTextWidth(timeStr)
- pdf.SetX((595 / 2) - (textw / 2))
- pdf.SetY(y)
- pdf.Text(timeStr)
- }
- err = pdf.SetFont("wts", "", 10)
- if err != nil {
- return
- }
- y += 20
- var x float64 = 10
- var w float64 = 120
- chunks := chunkBy(T_snid_list, 15)
- for _, list := range chunks {
- x = 10
- w = 120
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- w = 30
- for _, v2 := range list {
- sn_id2 := strings.Split(v2, ",")
- id2 := sn_id2[1]
- lib.RectFillColor(pdf, id2, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- }
- y += 20
- for _, t := range timeList {
- x = 10
- var textH float64 = 20 // if text height is 25px.
- pdf.SetNewY(y, textH)
- y = pdf.GetY()
- if y == 20 {
- w = 120
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- w = 30
- for _, v2 := range list {
- sn_id2 := strings.Split(v2, ",")
- id2 := sn_id2[1]
- lib.RectFillColor(pdf, id2, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- }
- y += 20
- x = 10
- }
- w = 120
- lib.RectFillColor(pdf, t, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- w = 30
- for _, v := range list {
- sn_id := strings.Split(v, ",")
- id := sn_id[1]
- t_t := dataMap[fmt.Sprintf("%s,%s", t, id)]
- lib.RectFillColor(pdf, t_t, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x += w
- }
- y += 20
- }
- y += 20
- }
- filename := time.Now().Format("20060102150405") + ".pdf"
- timeStr := "ofile/湿度" + filename
- err = pdf.WritePdf(timeStr)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- defer func() {
- //删除目录
- os.Remove(timeStr)
- }()
- c.Ctx.Output.Download(timeStr)
- }
- func chunkBy[T any](list []T, size int) [][]T {
- var chunks [][]T
- for size < len(list) {
- list, chunks = list[size:], append(chunks, list[0:size:size])
- }
- return append(chunks, list)
- }
- // 列表 -
- func (c *TaskDataController) TaskDataClass_List() {
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: List}
- c.ServeJSON()
- return
- }
- func (c *TaskDataController) TaskDataClass_Edit() {
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- T_task_id := c.GetString("T_task_id")
- T_sn := c.GetString("T_sn")
- T_id := c.GetString("T_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- sn, err := Task.Read_TaskData_ByT_id(Task_r.T_task_id, T_id)
- if err != nil && !errors.Is(err, orm.ErrNoRows) {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- if len(sn) > 0 && sn != T_sn {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", T_id, sn)}
- c.ServeJSON()
- return
- }
- err = NatsServer.Edit_DeviceClassList(T_task_id, T_sn, T_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- err = Task.Update_TaskData_ByT_sn(Task_r.T_task_id, T_sn, T_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(T_uuid, "本地版-任务分裂", "修改分类数据"+Task_r.T_name, Task_r.T_task_id+"|"+T_sn+"|"+T_id)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- func (c *TaskDataController) TaskDataClass_Del() {
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- T_task_id := c.GetString("T_task_id")
- T_sn := c.GetString("T_sn")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- err = Task.Delete_TaskData_ByT_sn(Task_r.T_task_id, T_sn)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(T_uuid, "本地版-任务分类", "删除分类数据"+Task_r.T_name, Task_r.T_task_id+"|"+T_sn)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 添加-
- func (c *TaskDataController) TaskData_AddS() {
- // 获取登录用户的uuid
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- //T_sn|T_id|T_t|T_rh|T_time?
- T_Data := c.GetString("T_Data")
- if len(T_Data) < 5 {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "err T_Data!"}
- c.ServeJSON()
- return
- }
- T_Data_list := strings.Split(T_Data, "?")
- println(len(T_Data_list), "len(T_Data_list)")
- var T_Data_list_x = 0
- snMaps := make(map[string]string)
- for _, v := range T_Data_list {
- // 132|132|23.9|72.1|2023-04-30 07:03:00
- if len(v) < 5 {
- println(v, "len(v) < 5")
- continue
- }
- v_list := strings.Split(v, "|")
- id, sn := strings.Trim(v_list[1], " "), strings.Trim(v_list[0], " ")
- sn1, ok := snMaps[id]
- if !ok {
- snMaps[id] = sn
- } else if sn1 != sn {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", id, sn1)}
- c.ServeJSON()
- return
- }
- }
- for T_id, T_sn := range snMaps {
- sn, err := Task.Read_TaskData_ByT_id(Task_r.T_task_id, T_id)
- if err != nil && !errors.Is(err, orm.ErrNoRows) {
- continue
- }
- if len(sn) > 0 && sn != T_sn {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", T_id, sn)}
- c.ServeJSON()
- return
- }
- id, err := Task.Read_TaskData_ByT_sn(Task_r.T_task_id, T_sn)
- if err != nil && !errors.Is(err, orm.ErrNoRows) {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- if len(id) > 0 && id != T_id {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("sn[%s]已被编号[%s]关联,请修改后重试!", T_sn, id)}
- c.ServeJSON()
- return
- }
- // 同步设备到线上
- err = NatsServer.Add_DeviceClassList(T_task_id, T_sn, T_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- }
- for _, v := range T_Data_list {
- // 132|132|23.9|72.1|2023-04-30 07:03:00
- if len(v) < 5 {
- println(v, "len(v) < 5")
- continue
- }
- v_list := strings.Split(v, "|")
- t, _ := lib.ReplaceSeconds(v_list[4])
- is := Task.Add_TaskData(Task_r.T_task_id, v_list[0], v_list[1], v_list[2], v_list[3], t)
- if is {
- T_Data_list_x += 1
- }
- }
- System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "添加数据"+Task_r.T_name, Task_r.T_task_id+"结果:"+string(len(T_Data_list))+"/"+string(T_Data_list_x)+"|=> "+T_Data)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: strconv.Itoa(T_Data_list_x)}
- c.ServeJSON()
- return
- }
- func (c *TaskDataController) TaskData_AddS_Excel() {
- // 获取登录用户的uuid
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- // 获取上传的文件
- file, _, err := c.GetFile("file")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "文件上传失败!"}
- c.ServeJSON()
- return
- }
- // 解析 Excel 文件
- xlFile, err := excelize.OpenReader(file)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "文件解析失败!"}
- c.ServeJSON()
- return
- }
- rows, err := xlFile.GetRows(xlFile.GetSheetName(0))
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取工作表失败!"}
- c.ServeJSON()
- return
- }
- var T_Data_list_x int
- snMaps := make(map[string]string)
- var valueStrings []string
- for k, val := range rows {
- // 跳过第一行
- if k == 0 {
- continue
- }
- if len(val) < 5 {
- println(val, "len(v) < 5")
- continue
- }
- fmt.Println(val)
- id, sn := val[1], val[0]
- sn1, ok := snMaps[id]
- if !ok {
- snMaps[id] = sn
- } else if sn1 != sn {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", id, sn1)}
- c.ServeJSON()
- return
- }
- t, _ := lib.ReplaceSeconds(val[4])
- valueStrings = append(valueStrings, fmt.Sprintf("('%s','%s',%v,%v,'%s')", val[0], val[1], val[2], val[3], t))
- //T_Data_list_x += 1
- }
- for T_id, T_sn := range snMaps {
- sn, err := Task.Read_TaskData_ByT_id(Task_r.T_task_id, T_id)
- if err != nil && !errors.Is(err, orm.ErrNoRows) {
- continue
- }
- if len(sn) > 0 && sn != T_sn {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", T_id, sn)}
- c.ServeJSON()
- return
- }
- id, err := Task.Read_TaskData_ByT_sn(Task_r.T_task_id, T_sn)
- if err != nil && !errors.Is(err, orm.ErrNoRows) {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- if len(id) > 0 && id != T_id {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("sn[%s]已被编号[%s]关联,请修改后重试!", T_sn, id)}
- c.ServeJSON()
- return
- }
- }
- // 删除重复数据
- err = Task.DeleteDeduplicate(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "导入失败"}
- c.ServeJSON()
- return
- }
- // 创建唯一缩影
- err = Task.Create_Unique_Index(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "导入失败"}
- c.ServeJSON()
- return
- }
- pageSize := 10000 // 每页的条数
- totalPages := len(valueStrings) / pageSize // 总页数
- // 遍历每一页
- for page := 1; page <= totalPages+1; page++ {
- // 计算当前页的起始索引和结束索引
- startIndex := (page - 1) * pageSize
- if startIndex > len(valueStrings) {
- startIndex = len(valueStrings)
- }
- endIndex := page * pageSize
- // 边界判断,如果结束索引超过列表长度,则将结束索引设置为列表最后一个元素的索引加一
- if endIndex > len(valueStrings) {
- endIndex = len(valueStrings)
- }
- fmt.Printf("startIndex %d endIndex %d\n", startIndex, endIndex)
- // 获取当前页的数据
- currentList := valueStrings[startIndex:endIndex]
- err = Task.Adds_TaskData(T_task_id, currentList)
- if err == nil {
- T_Data_list_x += len(currentList)
- }
- }
- //err = Task.Adds_TaskData(T_task_id, valueStrings)
- // 删除唯一索引
- Task.Delete_Unique_Index(T_task_id)
- System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "添加数据"+Task_r.T_name, Task_r.T_task_id+"结果:"+strconv.Itoa(T_Data_list_x))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: strconv.Itoa(T_Data_list_x)}
- c.ServeJSON()
- return
- }
- // 添加-
- func (c *TaskDataController) TaskData_Add() {
- // 获取登录用户的uuid
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- T_sn := c.GetString("T_sn")
- T_id := c.GetString("T_id")
- T_t, _ := c.GetFloat("T_t")
- T_rh, _ := c.GetFloat("T_rh")
- T_time := c.GetString("T_time")
- if len(T_sn) == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn 不能为空!"}
- c.ServeJSON()
- return
- }
- if len(T_id) == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id 不能为空!"}
- c.ServeJSON()
- return
- }
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- sn, err := Task.Read_TaskData_ByT_id(Task_r.T_task_id, T_id)
- if err != nil && !errors.Is(err, orm.ErrNoRows) {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
- c.ServeJSON()
- return
- }
- if len(sn) > 0 && sn != T_sn {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("编号[%s]已被sn[%s]关联,请修改后重试!", T_id, sn)}
- c.ServeJSON()
- return
- }
- id, err := Task.Read_TaskData_ByT_sn(Task_r.T_task_id, T_sn)
- if err != nil && !errors.Is(err, orm.ErrNoRows) {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
- c.ServeJSON()
- return
- }
- if len(id) > 0 && id != T_id {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("sn[%s]已被编号[%s]关联,请修改后重试!", T_sn, id)}
- c.ServeJSON()
- return
- }
- // 同步设备到线上
- if T_t == 0 && T_rh == 0 {
- err = NatsServer.Add_DeviceClassList(T_task_id, T_sn, T_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- }
- is := Task.Add_TaskData(Task_r.T_task_id, T_sn, T_id, fmt.Sprintf("%.2f", T_t), fmt.Sprintf("%.2f", T_rh), T_time)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "添加数据"+Task_r.T_name, Task_r.T_task_id+"|"+T_sn+"|"+T_id+"|"+fmt.Sprintf("%.2f", T_t)+"|"+fmt.Sprintf("%.2f", T_rh)+T_time)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 修改-
- func (c *TaskDataController) TaskData_Up() {
- // 获取登录用户的uuid
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- Id, err := c.GetInt("Id")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "Id 错误!"}
- c.ServeJSON()
- return
- }
- T_t, err := c.GetFloat("T_t")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_t 错误!"}
- c.ServeJSON()
- return
- }
- T_rh, err := c.GetFloat("T_rh")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_rh 错误!"}
- c.ServeJSON()
- return
- }
- T_time := c.GetString("T_time")
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- is := Task.Up_TaskData(Task_r.T_task_id, strconv.Itoa(Id), fmt.Sprintf("%.2f", T_t), fmt.Sprintf("%.2f", T_rh), T_time)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "修改数据"+Task_r.T_name, Task_r.T_task_id+"|"+strconv.Itoa(Id)+"|"+fmt.Sprintf("%.2f", T_t)+"|"+fmt.Sprintf("%.2f", T_rh)+T_time)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 删除-
- func (c *TaskDataController) TaskData_Del() {
- // 获取登录用户的uuid
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- Id, err := c.GetInt("Id")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "Id 错误!"}
- c.ServeJSON()
- return
- }
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- is := Task.Del_TaskData(Task_r.T_task_id, strconv.Itoa(Id))
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "删除"+Task_r.T_name, Task_r.T_task_id+"|"+strconv.Itoa(Id))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 删除-
- func (c *TaskDataController) TaskData_Del_t_id() {
- // 获取登录用户的uuid
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- Id := c.GetString("Id")
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- is := Task.Del_TaskData_t_id(Task_r.T_task_id, Id)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(T_uuid, "本地版-任务数据", "删除"+Task_r.T_name, Task_r.T_task_id+"|"+Id)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 列表 - 接口
- func (c *TaskDataController) Export_Data_Excel() {
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- T_sn_str := c.GetString("T_sn_list") //865901058809339,865901058815849,865901058818991,865901058810568
- //T_id, err := c.GetInt("T_id")
- //if err != nil {
- // T_id = -1
- //
- //}
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- T_sn_list := strings.Split(T_sn_str, ",")
- DeviceSensor_data_list := []Task.TaskData_{}
- for _, v := range T_sn_list {
- DeviceSensor_data, _ := Task.Read_TaskData_ById_List(Task_r.T_task_id, v, "", Time_start, Time_end, 1, 9999)
- DeviceSensor_data_list = append(DeviceSensor_data_list, DeviceSensor_data...)
- }
- f := excelize.NewFile() // 设置单元格的值
- // 这里设置表头
- f.SetCellValue("Sheet1", "A1", "编号")
- f.SetCellValue("Sheet1", "B1", "SN")
- f.SetCellValue("Sheet1", "C1", "温度℃")
- f.SetCellValue("Sheet1", "D1", "湿度%")
- f.SetCellValue("Sheet1", "E1", "记录时间")
- // 设置列宽
- f.SetColWidth("Sheet1", "A", "A", 7)
- f.SetColWidth("Sheet1", "B", "B", 20)
- f.SetColWidth("Sheet1", "C", "C", 10)
- f.SetColWidth("Sheet1", "D", "D", 10)
- f.SetColWidth("Sheet1", "E", "E", 22)
- line := 1
- // 循环写入数据
- for _, v := range DeviceSensor_data_list {
- line++
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.T_id)
- f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_sn)
- f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), strconv.FormatFloat(float64(v.T_t), 'f', 1, 64))
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), strconv.FormatFloat(float64(v.T_rh), 'f', 1, 64))
- f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_time)
- }
- fmt.Println("DeviceSensor_data:", len(DeviceSensor_data_list))
- lib.Create_Dir("./ofile")
- timeStr := time.Now().Format("20060102150405")
- // 保存文件
- if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
- fmt.Println(err)
- }
- if !lib.Pload_qiniu("ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx") {
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
- c.ServeJSON()
- return
- }
- //删除目录
- err = os.Remove("ofile/" + timeStr + ".xlsx")
- if err != nil {
- fmt.Println(err)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: "https://bzdcoldverifyoss.baozhida.cn/" + "ofile/" + timeStr + ".xlsx"}
- c.ServeJSON()
- return
- }
- func (c *TaskDataController) Check() {
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- type R_JSONS struct {
- T_sn string // SN
- T_id string // 编号
- T_unm int // 数据量
- T_time_interval int64 // 时间间隔
- Time_start string // 开始
- Time_end string // 结束
- Result int // 200 OK, 201 可以补 , 202 不能补
- Result_str string // 提示内容
- Result_Time_start string // 2022-8-05 21:01
- Result_Time_defect int64 // 缺失时间间隔
- }
- var r_jsons []R_JSONS
- List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
- for _, v := range List {
- var r_json R_JSONS
- r_json.T_sn = v.T_sn
- r_json.T_id = v.T_id
- r_json.Result = 200
- DeviceSensor_data := Task.Read_TaskData_ById_List_(Task_r.T_task_id, v.T_sn)
- r_json.T_unm = len(DeviceSensor_data)
- if r_json.T_unm > 2 {
- r_json.T_id = DeviceSensor_data[0].T_id
- r_json.Time_end = DeviceSensor_data[len(DeviceSensor_data)-1].T_time
- r_json.Time_start = DeviceSensor_data[0].T_time
- formatTime_a, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[0].T_time)
- formatTime_b, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[1].T_time)
- formatTime_x := formatTime_b.Unix() - formatTime_a.Unix()
- r_json.T_time_interval = formatTime_x
- println("formatTime_x:", v.T_id, v.T_sn, formatTime_x)
- if formatTime_x > 60*30 || formatTime_x < 60 {
- r_json.Result = 202
- r_json.Result_str = "数据异常(数据的第一个时间与第二个时间相差 " + strconv.FormatInt(formatTime_x, 10) + "秒),必须大于60秒,小于30分钟"
- r_jsons = append(r_jsons, r_json)
- continue
- }
- for data_i := 2; data_i < len(DeviceSensor_data); data_i++ {
- formatTime_a = formatTime_b
- formatTime_b, _ = time.Parse("2006-1-2 15:04", DeviceSensor_data[data_i].T_time)
- formatTime_ := formatTime_b.Unix() - formatTime_a.Unix()
- println("formatTime_x-:", formatTime_)
- if formatTime_ < formatTime_x {
- r_json.Result = 202
- r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,间隔小于时间间隔,不能自动补充"
- break
- }
- if formatTime_x != formatTime_ {
- if formatTime_ > 60*30 {
- r_json.Result = 202
- r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,相差时间大于30分钟,不能自动补充"
- break
- }
- r_json.Result = 201
- r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒"
- r_json.Result_Time_start = DeviceSensor_data[data_i-1].T_time
- r_json.Result_Time_defect = formatTime_
- println(r_json.Result_str)
- break
- }
- }
- } else {
- r_json.Result = 202
- r_json.Result_str = "数据量太少 必须大于 2条以上!"
- }
- r_jsons = append(r_jsons, r_json)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *TaskDataController) Check_Asyn() {
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- type R_JSONS struct {
- T_sn string // SN
- T_id string // 编号
- T_unm int // 数据量
- T_time_interval int64 // 时间间隔
- Time_start string // 开始
- Time_end string // 结束
- Result int // 200 OK, 201 可以补 , 202 不能补
- Result_str string // 提示内容
- Result_Time_start string // 2022-8-05 21:01
- Result_Time_defect int64 // 缺失时间间隔
- }
- List := Task.Read_TaskData_ById_ClassList(Task_r.T_task_id)
- r_jsons := make([]R_JSONS, len(List))
- var limitMaxNum = 10
- var chData = make(chan int, limitMaxNum)
- var jobGroup sync.WaitGroup
- var tasknum = len(List)
- for i := 0; i < tasknum; i++ {
- chData <- 1
- jobGroup.Add(1)
- go func(index int, wg *sync.WaitGroup, res *[]R_JSONS) {
- defer wg.Done()
- var r_json R_JSONS
- r_json.T_sn = List[index].T_sn
- r_json.T_id = List[index].T_id
- r_json.Result = 200
- DeviceSensor_data := Task.Read_TaskData_ById_List_(Task_r.T_task_id, List[index].T_sn)
- r_json.T_unm = len(DeviceSensor_data)
- if r_json.T_unm > 2 {
- r_json.T_id = DeviceSensor_data[0].T_id
- r_json.Time_end = DeviceSensor_data[len(DeviceSensor_data)-1].T_time
- r_json.Time_start = DeviceSensor_data[0].T_time
- formatTime_a, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[0].T_time)
- formatTime_b, _ := time.Parse("2006-1-2 15:04", DeviceSensor_data[1].T_time)
- formatTime_x := formatTime_b.Unix() - formatTime_a.Unix()
- r_json.T_time_interval = formatTime_x
- println("formatTime_x:", List[index].T_id, List[index].T_sn, formatTime_x)
- if formatTime_x > 60*30 || formatTime_x < 60 {
- r_json.Result = 202
- r_json.Result_str = "数据异常(数据的第一个时间与第二个时间相差 " + strconv.FormatInt(formatTime_x, 10) + "秒),必须大于60秒,小于30分钟"
- *res = append(*res, r_json)
- <-chData
- return
- }
- for data_i := 2; data_i < len(DeviceSensor_data); data_i++ {
- formatTime_a = formatTime_b
- formatTime_b, _ = time.Parse("2006-1-2 15:04", DeviceSensor_data[data_i].T_time)
- formatTime_ := formatTime_b.Unix() - formatTime_a.Unix()
- if formatTime_ < formatTime_x {
- println("formatTime_x-:", formatTime_)
- r_json.Result = 202
- r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,间隔小于时间间隔,不能自动补充"
- break
- }
- if formatTime_x != formatTime_ {
- if formatTime_ > 60*30 {
- println("formatTime_x-:", formatTime_)
- r_json.Result = 202
- r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒,相差时间大于30分钟,不能自动补充"
- break
- }
- r_json.Result = 202
- r_json.Result_str = "开始时间:" + DeviceSensor_data[data_i-1].T_time + " 离下一条时间间隔:" + strconv.FormatInt(formatTime_, 10) + "秒"
- r_json.Result_Time_start = DeviceSensor_data[data_i-1].T_time
- r_json.Result_Time_defect = formatTime_
- println(r_json.Result_str)
- break
- }
- }
- } else {
- r_json.Result = 202
- r_json.Result_str = "数据量太少 必须大于 2条以上!"
- }
- *res = append(*res, r_json)
- <-chData
- }(i, &jobGroup, &r_jsons)
- }
- //使用Wait等待所有任务执行完毕
- jobGroup.Wait()
- jsonsMap := map[string]R_JSONS{}
- resp := make([]R_JSONS, 0)
- for _, json := range r_jsons {
- jsonsMap[json.T_id] = json
- }
- for _, v := range List {
- resp = append(resp, jsonsMap[v.T_id])
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: resp}
- c.ServeJSON()
- return
- }
- // 打包数据本地数据
- func (c *TaskDataController) TaskData_Import_TaskData() {
- // 获取登录用户的uuid
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- T_task_id := c.GetString("T_task_id")
- _, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if err = lib.Create_Dir(conf.Sql_Temp_Dir); err != nil {
- logs.Println("创建sql临时文件失败")
- }
- sql_file := fmt.Sprintf("%sZ_TaskData_%s.sql", conf.Sql_Temp_Dir, T_task_id)
- org, err := Task.Dump_TaskData(T_task_id, conf.MysqlServer2_Username, conf.MysqlServer2_Password, conf.MysqlServer2_UrlPort, conf.MysqlServer2_Database, sql_file)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "z_task_data_" + T_task_id + "线上数据导出失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(T_uuid, "本地版-任务数据-打包本地数据", "导出线上数据Z_TaskData_"+T_task_id, org)
- i := 0
- flag := false
- Task.CREATE_TaskData(conf.Local_AliasName, T_task_id)
- for i < 10 {
- Task.Truncate_TaskData(conf.Local_AliasName, T_task_id)
- org, err = Task.Insert_TaskData(conf.MysqlServer_Username, conf.MysqlServer_Password, conf.MysqlServer_UrlPort, conf.MysqlServer_Database, sql_file)
- if err != nil {
- logs.Println("任务数据-打包本地数据", "线下导入数据Z_TaskData_"+T_task_id, err.Error())
- } else {
- if Task.Check_TaskData_Num(T_task_id) {
- System.Add_UserLogs_T(T_uuid, "本地版-任务数据-打包本地数据", "线下导入数据Z_TaskData_"+T_task_id, org)
- flag = true
- break
- }
- }
- i++
- }
- // 重试10次后仍然没有成功导入数据
- if !flag {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "z_task_data_" + T_task_id + "线下导入数据失败!"}
- c.ServeJSON()
- return
- }
- //删除导出的sql文件
- _ = os.Remove(sql_file)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- }
- // 更新线上数据
- func (c *TaskDataController) TaskData_Up_TaskData() {
- // 获取登录用户的uuid
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if err = lib.Create_Dir(conf.Sql_Temp_Dir); err != nil {
- logs.Println("创建sql临时文件失败")
- }
- sql_file := fmt.Sprintf("%sZ_TaskData_%s.sql", conf.Sql_Temp_Dir, T_task_id)
- org, err := Task.Dump_TaskData(T_task_id, conf.MysqlServer_Username, conf.MysqlServer_Password, conf.MysqlServer_UrlPort, conf.MysqlServer_Database, sql_file)
- if err != nil {
- logs.Println(T_uuid, "任务数据-更新线上数据", "导出线下数据Z_TaskData_"+T_task_id, err.Error())
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "z_task_data_" + T_task_id + "线下数据导出失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(T_uuid, "本地版-任务数据-更新线上数据", "线上导入数据Z_TaskData_"+T_task_id, org)
- i := 0
- flag := false
- for i < 10 {
- Task.Truncate_TaskData(conf.Server_AliasName, T_task_id)
- org, err = Task.Insert_TaskData(conf.MysqlServer2_Username, conf.MysqlServer2_Password, conf.MysqlServer2_UrlPort, conf.MysqlServer2_Database, sql_file)
- if err != nil {
- logs.Println(T_uuid, "任务数据-更新线上数据", "线上导入数据Z_TaskData_"+T_task_id, err.Error())
- } else {
- if Task.Check_TaskData_Num(T_task_id) {
- System.Add_UserLogs_T(T_uuid, "本地版-任务数据-更新线上数据", "线上导入数据Z_TaskData_"+T_task_id, org)
- flag = true
- break
- }
- }
- i++
- }
- // 重试10次后仍然没有成功导入数据
- if !flag {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "z_task_data_" + T_task_id + "线上导入数据失败!"}
- c.ServeJSON()
- return
- }
- //删除导出的sql文件
- _ = os.Remove(sql_file)
- // 线上数据更新后 将当前任务 交付审核 标志 为 1
- Task_r.T_delivery_state = 1
- err = NatsServer.Update_Task(Task_r)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- // 向线上用户日志表写入数据
- System.Add_UserLogs_T(T_uuid, "本地版-任务", "修改", Task_r)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- }
- // 更新线上数据后台执行
- func (c *TaskDataController) TaskData_Up_TaskData_Back() {
- // 获取登录用户的uuid
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_delivery_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 采集中
- Task_r.T_delivery_state = 2
- err = NatsServer.Update_Task(Task_r)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "提交失败!"}
- c.ServeJSON()
- return
- }
- data := Nats.Up_TaskData_Back{
- T_uuid: T_uuid,
- Task: Task_r,
- }
- b, _ := msgpack.Marshal(&data)
- _ = lib.Nats.Publish("ColdVerify_Local_Up_TaskData", b)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // CopyFromPosition 数据拷贝
- func (c *TaskDataController) CopyFromPosition() {
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- //if !b_ {
- // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- // c.ServeJSON()
- // return
- //}
- StartTime := c.GetString("StartTime")
- startTime, ok := lib.TimeStrToTime(StartTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- EndTime := c.GetString("EndTime")
- endTime, ok := lib.TimeStrToTime(EndTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- CopyTime := c.GetString("CopyTime")
- copyTime, ok := lib.TimeStrToTime(CopyTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- CopyEndTime := copyTime.Add(endTime.Sub(startTime)).Format("2006-01-02 15:04:05")
- // 时间间隔 分钟
- T_saveT, _ := c.GetInt("T_saveT")
- SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- for _, v := range SN_List {
- sn_id := strings.Split(v, ",")
- if len(sn_id) != 2 {
- continue
- }
- sn, id_str := sn_id[0], sn_id[1]
- List, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999)
- Task.DeleteTaskDataByTimeRange(Task_r.T_task_id, sn, id_str, CopyTime, CopyEndTime)
- ct := copyTime
- go func(TaskDataList []Task.TaskData_, task_id string, T_saveT int) {
- for _, taskData := range TaskDataList {
- taskData.T_time = ct.Format("2006-01-02 15:04:05")
- Task.InsertTaskData(task_id, taskData)
- ct = ct.Add(time.Second * time.Duration(T_saveT))
- }
- }(List, Task_r.T_task_id, T_saveT)
- }
- System.Add_UserLogs_T(T_uuid, "数据复制", fmt.Sprintf("数据复制(%s)[%s|%s],复制时间:%s,,时间间隔:%d", T_task_id, StartTime, EndTime, CopyTime, T_saveT), SN_List)
- c.Data["json"] = lib.JSONS{200, "数据复制已提交后台处理!", nil}
- c.ServeJSON()
- return
- }
- // RepairSensorData 数据补漏
- func (c *TaskDataController) RepairSensorData() {
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- //if !b_ {
- // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- // c.ServeJSON()
- // return
- //}
- StartTime := c.GetString("StartTime")
- _, ok := lib.TimeStrToTime(StartTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- EndTime := c.GetString("EndTime")
- _, ok = lib.TimeStrToTime(EndTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- // 时间间隔 秒
- saveTime, _ := c.GetInt("T_saveT")
- SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- num := 0
- for _, v := range SN_List {
- sn_id := strings.Split(v, ",")
- if len(sn_id) != 2 {
- continue
- }
- sn, id_str := sn_id[0], sn_id[1]
- list, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999)
- for i := 0; i < len(list)-1; i++ {
- current := list[i].T_time
- next := list[i+1].T_time
- c, _ := time.Parse("2006-01-02 15:04:05", current)
- n, _ := time.Parse("2006-01-02 15:04:05", next)
- interval := n.Unix() - c.Unix()
- //logs.Debug("时间间隔:", interval, "保存时间:", saveTime)
- fmt.Println("当前:", current, "下一个:", next)
- if int(interval) > saveTime {
- ttInterval := list[i+1].T_t - list[i].T_t
- ttt := list[i].T_t // 温度临时变量
- trhInterval := list[i+1].T_rh - list[i].T_rh
- trht := list[i].T_rh //湿度临时变量
- count := (int(interval) - saveTime) / saveTime
- if int(interval)%saveTime != 0 {
- count++
- }
- num += count
- for k := 0; k < count; k++ {
- t := c.Add(time.Second * time.Duration(saveTime)).Format("2006-01-02 15:04:05") //时间临时变量
- ttt += ttInterval / float32(count)
- trht += trhInterval / float32(count)
- Task.InsertTaskData(Task_r.T_task_id, Task.TaskData_{
- T_sn: list[i].T_sn,
- T_id: list[i].T_id,
- T_t: ttt,
- T_rh: trht,
- T_time: t,
- })
- c = c.Add(time.Second * time.Duration(saveTime))
- }
- }
- }
- }
- System.Add_UserLogs_T(T_uuid, "数据补漏", fmt.Sprintf("数据补漏(%s)[%s|%s],时间间隔:%d", T_task_id, StartTime, EndTime, saveTime), SN_List)
- c.Data["json"] = lib.JSONS{200, fmt.Sprintf("补漏完成!共补漏%d条数据", num), nil}
- c.ServeJSON()
- return
- }
- // DataSensorDataTrend 数据趋势
- func (c *TaskDataController) DataSensorDataTrend() {
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- //if !b_ {
- // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- // c.ServeJSON()
- // return
- //}
- StartTime := c.GetString("StartTime")
- _, ok := lib.TimeStrToTime(StartTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- EndTime := c.GetString("EndTime")
- _, ok = lib.TimeStrToTime(EndTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- // 时间间隔 秒
- saveTime, _ := c.GetInt("T_saveT")
- SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- for _, v := range SN_List {
- sn_id := strings.Split(v, ",")
- if len(sn_id) != 2 {
- continue
- }
- sn, id_str := sn_id[0], sn_id[1]
- list, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999)
- if len(list) == 0 {
- continue
- }
- Task.DeleteTaskDataByTimeRange(Task_r.T_task_id, sn, id_str, StartTime, EndTime)
- first := list[0]
- last := list[len(list)-1]
- current, _ := time.Parse("2006-01-02 15:04:05", first.T_time)
- next, _ := time.Parse("2006-01-02 15:04:05", last.T_time)
- interval := next.Sub(current).Seconds() / float64(saveTime)
- ttInterval := (last.T_t - first.T_t) / float32(interval)
- trhInterval := (last.T_rh - first.T_rh) / float32(interval)
- tt := first.T_t
- ttrh := first.T_rh
- for current.Unix() < next.Unix() {
- tt += ttInterval
- ttrh += trhInterval
- ttime := current.Format("2006-01-02 15:04:05")
- Task.InsertTaskData(Task_r.T_task_id, Task.TaskData_{
- //加保存时间
- T_sn: first.T_sn,
- T_id: id_str,
- T_t: tt,
- T_rh: ttrh,
- T_time: ttime,
- })
- current = current.Add(time.Second * time.Duration(saveTime))
- }
- }
- System.Add_UserLogs_T(T_uuid, "数据趋势", fmt.Sprintf("数据趋势(%s)[%s|%s],时间间隔:%d", T_task_id, StartTime, EndTime, saveTime), SN_List)
- c.Data["json"] = lib.JSONS{200, "设置趋势操作成功", nil}
- c.ServeJSON()
- return
- }
- // UpdateRand 数据随机
- func (c *TaskDataController) UpdateRand() {
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- //if !b_ {
- // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- // c.ServeJSON()
- // return
- //}
- StartTime := c.GetString("StartTime")
- _, ok := lib.TimeStrToTime(StartTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- EndTime := c.GetString("EndTime")
- _, ok = lib.TimeStrToTime(EndTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- TemperatureMin, _ := c.GetInt("TemperatureMin") // 温度
- TemperatureMax, _ := c.GetInt("TemperatureMax")
- HumidityMax, _ := c.GetInt("HumidityMax") // 湿度
- HumidityMin, _ := c.GetInt("HumidityMin")
- SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- for _, v := range SN_List {
- sn_id := strings.Split(v, ",")
- if len(sn_id) != 2 {
- continue
- }
- sn, id := sn_id[0], sn_id[1]
- Task.UpdateTaskDataTemperatureAndHumidityRandom(Task_r.T_task_id, sn, id, StartTime, EndTime, TemperatureMax, TemperatureMin, HumidityMax, HumidityMin)
- }
- System.Add_UserLogs_T(T_uuid, "数据随机", fmt.Sprintf("数据随机(%s)[%s|%s],温度[%d-%d],湿度[%d-%d]", T_task_id, StartTime, EndTime, TemperatureMin, TemperatureMax, HumidityMax, HumidityMin), SN_List)
- //反馈成功
- c.Data["json"] = lib.JSONS{200, "调整随机偏移值成功!", nil}
- c.ServeJSON()
- return
- }
- // UpdateFix 更新固定值
- func (c *TaskDataController) UpdateFix() {
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- //if !b_ {
- // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- // c.ServeJSON()
- // return
- //}
- StartTime := c.GetString("StartTime")
- _, ok := lib.TimeStrToTime(StartTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- EndTime := c.GetString("EndTime")
- _, ok = lib.TimeStrToTime(EndTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- FixTemperature := c.GetString("FixTemperature")
- FixHumidity := c.GetString("FixHumidity")
- //2.得到数据进行统一设置访问修改
- humidity, _ := strconv.ParseFloat(FixHumidity, 64)
- temperature, _ := strconv.ParseFloat(FixTemperature, 64)
- SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- //3.循环更新数据
- for _, v := range SN_List {
- sn_id := strings.Split(v, ",")
- if len(sn_id) != 2 {
- continue
- }
- sn, id := sn_id[0], sn_id[1]
- Task.UpdateTaskDataTemperatureAndHumidity(Task_r.T_task_id, sn, id, StartTime, EndTime, temperature, humidity)
- }
- System.Add_UserLogs_T(T_uuid, "更新固定值", fmt.Sprintf("更新固定值(%s)[%s|%s],温度[%f],湿度[%f]", T_task_id, StartTime, EndTime, temperature, humidity), SN_List)
- //4.反馈成功
- c.Data["json"] = lib.JSONS{200, "调整固定偏移值成功!", nil}
- c.ServeJSON()
- }
- // DataSensorDataSmooth 数据平滑
- func (c *TaskDataController) DataSensorDataSmooth() {
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- //if !b_ {
- // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- // c.ServeJSON()
- // return
- //}
- StartTime := c.GetString("StartTime")
- _, ok := lib.TimeStrToTime(StartTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- EndTime := c.GetString("EndTime")
- _, ok = lib.TimeStrToTime(EndTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- tRange, _ := c.GetFloat("tRange")
- hRange, _ := c.GetFloat("hRange")
- SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- var count int
- for _, v := range SN_List {
- sn_id := strings.Split(v, ",")
- if len(sn_id) != 2 {
- continue
- }
- sn := sn_id[0]
- id_str := sn_id[1]
- list, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999)
- for i := 1; i < len(list); i++ {
- n := list[i-1]
- old := list[i]
- newO := list[i]
- //变化差
- var tInterval = old.T_t - n.T_t
- var hInterval = old.T_rh - n.T_rh
- fmt.Println("温度:", n.T_t, "温度next:", old.T_t, "差值:", n.T_t-old.T_t)
- if tRange != 0 {
- if tInterval > float32(tRange) {
- newO.T_t = n.T_t + float32(tRange)
- } else if tInterval < -float32(tRange) {
- newO.T_t = n.T_t - float32(tRange)
- }
- }
- if hRange != 0 {
- if hInterval > float32(hRange) {
- newO.T_rh = n.T_rh + float32(hRange)
- } else if hInterval < -float32(hRange) {
- newO.T_rh = n.T_rh - float32(hRange)
- }
- }
- if old.T_rh != newO.T_rh || old.T_t != newO.T_t {
- fmt.Println("原始数据:", old, "新数据:", newO)
- list[i] = newO
- Task.UpdateTaskData(Task_r.T_task_id, sn, id_str, old, newO)
- count++
- }
- }
- }
- System.Add_UserLogs_T(T_uuid, "数据平滑", fmt.Sprintf("数据平滑(%s)[%s|%s],温度[%f],湿度[%f]", T_task_id, StartTime, EndTime, tRange, hRange), SN_List)
- c.Data["json"] = lib.JSONS{200, "操作成功处理" + fmt.Sprint(count) + "条数据", nil}
- c.ServeJSON()
- }
- // CopyFromPosition 数据拷贝平均值
- func (c *TaskDataController) CopyFromPositionAverageSN() {
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- //if !b_ {
- // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- // c.ServeJSON()
- // return
- //}
- StartTime := c.GetString("StartTime")
- startTime, ok := lib.TimeStrToTime(StartTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- EndTime := c.GetString("EndTime")
- endTime, ok := lib.TimeStrToTime(EndTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- CopyTime := c.GetString("CopyTime")
- CopySN := c.GetString("CopySN")
- CopyID := c.GetString("CopyID")
- copyTime, ok := lib.TimeStrToTime(CopyTime)
- // 时间间隔 s
- T_saveT, _ := c.GetInt("T_saveT")
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- CopyEndTime := copyTime.Add(endTime.Sub(startTime)).Format("2006-01-02 15:04:05")
- SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
- if len(SN_List) != 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "SN_List Err!"}
- c.ServeJSON()
- return
- }
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- sn_id1 := strings.Split(SN_List[0], ",")
- if len(sn_id1) != 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "SN_List Err!"}
- c.ServeJSON()
- return
- }
- sn1, id_str1 := sn_id1[0], sn_id1[1]
- sn_id2 := strings.Split(SN_List[1], ",")
- if len(sn_id2) != 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "SN_List Err!"}
- c.ServeJSON()
- return
- }
- sn2, id_str2 := sn_id2[0], sn_id2[1]
- List1, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn1, id_str1, StartTime, EndTime, 0, 9999)
- List2, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn2, id_str2, StartTime, EndTime, 0, 9999)
- num := len(List1)
- if len(List2) < len(List1) {
- num = len(List2)
- }
- var list []Task.TaskData_
- ct := copyTime
- for i := 0; i < num; i++ {
- if List1[i].T_time != List2[i].T_time {
- c.Data["json"] = lib.JSONS{202, fmt.Sprintf("%s【%s】、%s【%s】时间不一致", List1[i].T_id, List1[i].T_time, List2[i].T_id, List2[i].T_time), nil}
- c.ServeJSON()
- return
- //ct = ct.Add(time.Second * time.Duration(T_saveT))
- //continue
- }
- T_t := (List1[i].T_t + List2[i].T_t) / 2
- T_rh := (List1[i].T_rh + List2[i].T_rh) / 2
- list = append(list, Task.TaskData_{
- T_sn: CopySN,
- T_id: CopyID,
- T_t: T_t,
- T_rh: T_rh,
- T_time: ct.Format("2006-01-02 15:04:05"),
- })
- ct = ct.Add(time.Second * time.Duration(T_saveT))
- }
- Task.DeleteTaskDataByTimeRange(Task_r.T_task_id, CopySN, CopyID, CopyTime, CopyEndTime)
- go func(TaskDataList []Task.TaskData_, task_id string) {
- for _, taskData := range TaskDataList {
- Task.InsertTaskData(task_id, taskData)
- }
- }(list, Task_r.T_task_id)
- System.Add_UserLogs_T(T_uuid, "数据复制平均值", fmt.Sprintf("数据复制(%s)[%s|%s],复制时间:%s,,时间间隔:%d", T_task_id, StartTime, EndTime, CopyTime, T_saveT), SN_List)
- c.Data["json"] = lib.JSONS{200, "数据复制已提交后台处理!", nil}
- c.ServeJSON()
- return
- }
- // CopyFromPosition 数据拷贝
- func (c *TaskDataController) CopyFromPositionSN() {
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- //if !b_ {
- // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- // c.ServeJSON()
- // return
- //}
- StartTime := c.GetString("StartTime")
- startTime, ok := lib.TimeStrToTime(StartTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- EndTime := c.GetString("EndTime")
- endTime, ok := lib.TimeStrToTime(EndTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- CopyTime := c.GetString("CopyTime")
- copyTime, ok := lib.TimeStrToTime(CopyTime)
- CopySN := c.GetString("CopySN")
- CopyID := c.GetString("CopyID")
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- CopyEndTime := copyTime.Add(endTime.Sub(startTime)).Format("2006-01-02 15:04:05")
- // 时间间隔 分钟
- T_saveT, _ := c.GetInt("T_saveT")
- SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
- if len(SN_List) != 1 {
- c.Data["json"] = lib.JSONS{202, "SN_List Err!", nil}
- c.ServeJSON()
- return
- }
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- sn_id := strings.Split(SN_List[0], ",")
- if len(sn_id) != 2 {
- c.Data["json"] = lib.JSONS{202, "SN_List Err!", nil}
- c.ServeJSON()
- return
- }
- sn, id_str := sn_id[0], sn_id[1]
- List, _ := Task.Read_TaskData_ById_List_AES(Task_r.T_task_id, sn, id_str, StartTime, EndTime, 0, 9999)
- Task.DeleteTaskDataByTimeRange(Task_r.T_task_id, CopySN, CopyID, CopyTime, CopyEndTime)
- ct := copyTime
- go func(TaskDataList []Task.TaskData_, task_id string, T_saveT int) {
- for _, taskData := range TaskDataList {
- taskData.T_time = ct.Format("2006-01-02 15:04:05")
- taskData.T_sn = CopySN
- taskData.T_id = CopyID
- Task.InsertTaskData(task_id, taskData)
- ct = ct.Add(time.Second * time.Duration(T_saveT))
- }
- }(List, Task_r.T_task_id, T_saveT)
- System.Add_UserLogs_T(T_uuid, "数据复制", fmt.Sprintf("数据复制(%s)[%s|%s],复制时间:%s,,时间间隔:%d", T_task_id, StartTime, EndTime, CopyTime, T_saveT), SN_List)
- c.Data["json"] = lib.JSONS{200, "数据复制已提交后台处理!", nil}
- c.ServeJSON()
- return
- }
- // 数据偏移到区间内
- func (c *TaskDataController) DataSkewingInterval() {
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- //if !b_ {
- // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- // c.ServeJSON()
- // return
- //}
- StartTime := c.GetString("StartTime")
- _, ok := lib.TimeStrToTime(StartTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- EndTime := c.GetString("EndTime")
- _, ok = lib.TimeStrToTime(EndTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- TemperatureMin, _ := c.GetFloat("TemperatureMin") // 温度
- TemperatureMax, _ := c.GetFloat("TemperatureMax")
- Tmin, Tmax := TemperatureMin, TemperatureMax
- SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- errMsg := []string{}
- for _, v := range SN_List {
- sn_id := strings.Split(v, ",")
- if len(sn_id) != 2 {
- continue
- }
- sn, id := sn_id[0], sn_id[1]
- min, max := Task.Read_TaskData_T_Min_Max(Task_r.T_task_id, sn, id, StartTime, EndTime)
- if min < Tmin && max > Tmax {
- errMsg = append(errMsg, fmt.Sprintf("【%s】最低温度[%.2f]低于给定的最低温度[%.2f],并且最高温度[%.2f]高于给定的最高温度[%.2f],无法偏移!", id, min, Tmin, max, Tmax))
- continue
- }
- if min > Tmin && max < Tmax {
- continue
- //errMsg = append(errMsg, fmt.Sprintf("【%s】最低温度[%.2f]高于给定最低温度[%.2f],并且最高温度[%.2f]低于给定最高温度[%.2f],无须偏移!", id, min, Tmin, max, Tmax))
- }
- // 判断是否能向上偏移
- if min < Tmin && max < Tmax && (Tmin-min+max) > Tmax {
- errMsg = append(errMsg, fmt.Sprintf("【%s】偏移后最高温度[%.2f]将高于给定的最高温度[%.2f],无法偏移!", id, Tmin-min+max, Tmax))
- continue
- }
- if max > Tmax && min > Tmin && (min-(max-Tmax)) < Tmin {
- errMsg = append(errMsg, fmt.Sprintf("【%s】偏移后最低温度[%.2f]将低于给定的最低温度[%.2f],无法偏移!", id, min-(max-Tmax), Tmin))
- continue
- }
- // 向上偏移
- if min < Tmin && max < Tmax && (Tmin-min+max) <= Tmax {
- Task.UpdateTaskDataTemperatureAndHumidity(Task_r.T_task_id, sn, id, StartTime, EndTime, Tmin-min, 0)
- continue
- }
- // 向下偏移
- if max > Tmax && min > Tmin && (min-(max-Tmax)) >= Tmin {
- Task.UpdateTaskDataTemperatureAndHumidity(Task_r.T_task_id, sn, id, StartTime, EndTime, -(max - Tmax), 0)
- continue
- }
- }
- System.Add_UserLogs_T(T_uuid, "数据偏移到给定范围", fmt.Sprintf("数据偏移到给定范围(%s)[%s|%s],温度[%f-%f]", T_task_id, StartTime, EndTime, TemperatureMin, TemperatureMax), SN_List)
- if len(errMsg) > 0 {
- c.Data["json"] = lib.JSONS{202, "", errMsg}
- c.ServeJSON()
- return
- }
- //反馈成功
- c.Data["json"] = lib.JSONS{200, "调整区间偏移值成功!", nil}
- c.ServeJSON()
- return
- }
- // 等比缩放
- func (c *TaskDataController) DataGeometricScale() {
- T_uuid, _ := lib.GetAdminT_Uuid(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- //if !b_ {
- // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- // c.ServeJSON()
- // return
- //}
- StartTime := c.GetString("StartTime")
- _, ok := lib.TimeStrToTime(StartTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- EndTime := c.GetString("EndTime")
- _, ok = lib.TimeStrToTime(EndTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- Temperature, _ := c.GetFloat("Temperature") // 温度比列
- Humidity, _ := c.GetFloat("Humidity") // 适度比列
- SN_List := strings.Split(strings.Trim(c.GetString("SN_List"), "|"), "|")
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- errMsg := []string{}
- for _, v := range SN_List {
- sn_id := strings.Split(v, ",")
- if len(sn_id) != 2 {
- continue
- }
- sn, id := sn_id[0], sn_id[1]
- Task.UpdateTaskDataTemperatureAndHumidityByGeometric(Task_r.T_task_id, sn, id, StartTime, EndTime, Temperature, Humidity)
- }
- System.Add_UserLogs_T(T_uuid, "数据等比缩放", fmt.Sprintf("数据等比缩放(%s)[%s|%s],温度[%f-%f]", T_task_id, StartTime, EndTime, Temperature, Humidity), SN_List)
- if len(errMsg) > 0 {
- c.Data["json"] = lib.JSONS{202, "", errMsg}
- c.ServeJSON()
- return
- }
- //反馈成功
- c.Data["json"] = lib.JSONS{200, "数据等比缩放成功!", nil}
- c.ServeJSON()
- return
- }
- // 存档列表
- func (c *TaskDataController) TaskDataCopy_List() {
- var r_jsons lib.R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- var cnt int64
- List, cnt := Task.Read_TaskCopy_List(Task_r.T_task_id, page, page_z)
- page_size := math.Ceil(float64(cnt) / float64(page_z))
- r_jsons.List = List
- r_jsons.Page = page
- r_jsons.Page_size = int(page_size)
- r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
- r_jsons.Num = int(cnt)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 添加存档
- func (c *TaskDataController) TaskDataCopy_Add() {
- T_task_id := c.GetString("T_task_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- var_ := Task.TaskCopy{
- T_task_id: T_task_id,
- T_time: time.Now().Format("2006-01-02 15:04:05"),
- T_State: 1,
- }
- T_copy_id, is := Task.Add_TaskCopy(var_)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
- c.ServeJSON()
- return
- }
- // 创建数据表
- Task.CREATE_TaskDataCopy(conf.Local_AliasName, T_copy_id)
- // 将数据表存档
- Task.TaskData_Arhiving(T_task_id, T_copy_id)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_copy_id}
- c.ServeJSON()
- return
- }
- // 删除存档
- func (c *TaskDataController) TaskDataCopy_Del() {
- T_copy_id := c.GetString("T_copy_id")
- r, err := Task.Read_TaskCopy(T_copy_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_copy_id 错误!"}
- c.ServeJSON()
- return
- }
- is := Task.Delete_TaskCopy(r)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_copy_id}
- c.ServeJSON()
- return
- }
- // 存档恢复
- func (c *TaskDataController) TaskDataCopy_Recover() {
- T_task_id := c.GetString("T_task_id")
- T_copy_id := c.GetString("T_copy_id")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- _, err = Task.Read_TaskCopy(T_copy_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_copy_id 错误!"}
- c.ServeJSON()
- return
- }
- is := Task.TaskData_Recover(T_task_id, T_copy_id)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_copy_id}
- c.ServeJSON()
- return
- }
- // 查询图片生成状态
- func (c *TaskDataController) TaskData_JPGState() {
- T_task_id := c.GetString("T_task_id")
- jpg, is := Task.Redis_TaskDataJPG_Get(T_task_id)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "暂无图片正在生成"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: jpg}
- c.ServeJSON()
- return
- }
- func (c *TaskDataController) TaskData_JPG() {
- StartTime := c.GetString("StartTime")
- if len(StartTime) > 0 {
- _, ok := lib.TimeStrToTime(StartTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- }
- EndTime := c.GetString("EndTime")
- if len(EndTime) > 0 {
- _, ok := lib.TimeStrToTime(EndTime)
- if !ok {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
- c.ServeJSON()
- return
- }
- }
- TemperatureMin, _ := c.GetFloat("TemperatureMin") // 最低温度
- TemperatureMax, _ := c.GetFloat("TemperatureMax") // 最高温度
- T_task_id := c.GetString("T_task_id")
- SN_List := c.GetString("SN_List")
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- Task.Redis_TaskDataJPG_Del(T_task_id)
- // 生成图片
- go TaskDataJPG(StartTime, EndTime, T_task_id, SN_List, TemperatureMin, TemperatureMax)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 存档生成图片
- func TaskDataJPG(StartTime, EndTime, T_task_id, snList string, TemperatureMin, TemperatureMax float64) {
- Task.Redis_TaskDataJPG_Set(T_task_id, Task.TaskDataJPG{
- State: 1,
- Msg: "图片生成中",
- Url: "",
- })
- msg := ""
- state := 2
- url := ""
- SN_List := strings.Split(strings.Trim(snList, "|"), "|")
- if len(snList) == 0 {
- List := Task.Read_TaskData_ById_ClassList(T_task_id)
- for _, v := range List {
- SN_List = append(SN_List, fmt.Sprintf("%s,%s", v.T_sn, v.T_id))
- }
- }
- id_List := []string{}
- sn_List := []string{}
- if len(snList) > 0 {
- for _, v := range SN_List {
- sn_id := strings.Split(v, ",")
- if len(sn_id) != 2 {
- return
- }
- id_List = append(id_List, fmt.Sprintf("'%s'", sn_id[1]))
- sn_List = append(sn_List, fmt.Sprintf("'%s'", sn_id[0]))
- }
- }
- ymin, ymax, minTime, maxTime := Task.Read_TaskData_T_Min_Max_Time_Min_Max(T_task_id, sn_List, id_List, StartTime, EndTime)
- xmin, xmax := float64(minTime.Unix()), float64(maxTime.Unix())
- // 创建一个新的绘图
- p := plot.New()
- // 设置绘图标题和标签
- p.Title.Text = "温度折线图"
- //p.Legend.ThumbnailWidth = 5 * vg.Inch
- p.X.Label.Text = "时间"
- p.Y.Label.Text = "温度"
- // 添加最高,最低标准线 用红色虚线标识
- p.Add(lib.HorizontalLine(xmin, xmax, TemperatureMin))
- p.Add(lib.HorizontalLine(xmin, xmax, TemperatureMax))
- var chData = make(chan int, 10)
- var jobGroup sync.WaitGroup
- // 创建温度线
- for i := 0; i < len(SN_List); i++ {
- chData <- 1
- jobGroup.Add(1)
- go func(index int) {
- //go func(index int, wg *sync.WaitGroup, p *plot.Plot) {
- defer func() {
- <-chData // 完成时chan取出1个
- jobGroup.Done() // 完成时将等待组值减1
- }()
- sn_id := strings.Split(SN_List[index], ",")
- if len(sn_id) != 2 {
- return
- }
- sn, id := sn_id[0], sn_id[1]
- list, _ := Task.Read_TaskData_ById_List_AES(T_task_id, sn, id, StartTime, EndTime, 0, 9999)
- if len(list) == 0 {
- return
- }
- pts := make(plotter.XYs, len(list))
- for j, d := range list {
- t, _ := lib.TimeStrToTime(d.T_time)
- pts[j].X = float64(t.Unix())
- pts[j].Y = float64(d.T_t)
- }
- line, err := plotter.NewLine(pts)
- if err != nil {
- return
- }
- line.Color = lib.RandomColor(index)
- p.Add(line)
- }(i)
- }
- jobGroup.Wait()
- if ymax < 8 {
- ymax = 8
- }
- if ymin > 0 {
- ymin = 0
- }
- p.Y.Min, p.Y.Max = ymin, ymax
- p.X.Min, p.X.Max = xmin, xmax
- p.Y.Tick.Marker = lib.CommaTicks{}
- //p.X.Tick.Marker = plot.TimeTicks{Format: "2006-01-02 15:04:05"}
- p.X.Tick.Marker = lib.TimeTicks{}
- p.X.Tick.Label.Rotation = math.Pi / 5
- p.X.Tick.Label.YAlign = draw.YCenter
- p.X.Tick.Label.XAlign = draw.XRight
- filename := "jpg" + time.Now().Format("20060102150405")
- // 保存文件
- if err := p.Save(10*vg.Inch, 4*vg.Inch, "ofile/"+filename+".jpg"); err != nil {
- state = 3
- msg = "图片生成失败"
- logs.Error(lib.FuncName(), "生成图片失败", err)
- }
- if !lib.Pload_qiniu("ofile/"+filename+".jpg", "ofile/"+filename+".jpg") {
- state = 3
- msg = "图片上传七牛云失败"
- logs.Error(lib.FuncName(), "上传七牛云失败")
- }
- //删除目录
- os.Remove("ofile/" + filename + ".jpg")
- if len(msg) == 0 {
- msg = "图片生成成功"
- url = "https://bzdcoldverifyoss.baozhida.cn/" + "ofile/" + filename + ".jpg"
- }
- Task.Redis_TaskDataJPG_Set(T_task_id, Task.TaskDataJPG{
- State: state,
- Msg: msg,
- Url: url,
- })
- }
- func (c *TaskDataController) Certificate_List() {
- var r_jsons lib.R_JSONS
- T_task_id := c.GetString("T_task_id")
- T_sn := c.GetString("T_sn")
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- Task_r, err := NatsServer.Read_Task(T_task_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
- c.ServeJSON()
- return
- }
- if Task_r.T_collection_state == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据采集中,请稍后!"}
- c.ServeJSON()
- return
- }
- List, cnt := Certificate.Read_Certificate_List(T_task_id, T_sn, page, page_z)
- r_jsons.List = List
- r_jsons.Page = page
- r_jsons.Page_size = page_z
- r_jsons.Num = int(cnt)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
|