Data.go 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215
  1. package controllers
  2. import (
  3. "Cold_Api/Nats/NatsServer"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers/lib"
  6. "Cold_Api/models/Account"
  7. "Cold_Api/models/Device"
  8. "encoding/base64"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "log"
  13. "math"
  14. "net/http"
  15. "os"
  16. "regexp"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "github.com/beego/beego/v2/core/logs"
  21. beego "github.com/beego/beego/v2/server/web"
  22. "github.com/gorilla/websocket"
  23. "github.com/signintech/gopdf"
  24. "github.com/xuri/excelize/v2"
  25. )
  26. type DataController struct {
  27. beego.Controller
  28. Admin_r Account.Admin // 登陆的用户
  29. T_pid int // 公司id
  30. }
  31. func (c *DataController) Prepare() {
  32. GetCookie := c.Ctx.GetCookie("User_tokey")
  33. GetString := c.GetString("User_tokey")
  34. User_tokey := GetCookie
  35. if len(User_tokey) == 0 {
  36. User_tokey = GetString
  37. }
  38. if Account.Admin_r == nil {
  39. return
  40. }
  41. c.Admin_r = *Account.Admin_r
  42. T_pid := c.Admin_r.T_pid
  43. EntryPid, _ := Account.Redis_Tokey_T_pid_Get(User_tokey)
  44. if EntryPid > 0 {
  45. T_pid = EntryPid
  46. }
  47. c.T_pid = T_pid
  48. }
  49. // 获取传感器
  50. func (c *DataController) Device_Sensor_Get() {
  51. T_sn := c.GetString("T_sn")
  52. T_id, _ := c.GetInt("T_id")
  53. //c.Data["Class_List"] = Device.Read_Class_All_1()
  54. DeviceSensor_r, err := Device.Read_DeviceSensor_ByTsn_Tid(T_sn, T_id)
  55. if err != nil {
  56. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取失败!"}
  57. c.ServeJSON()
  58. return
  59. }
  60. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Device.DeviceSensorToDeviceSensor_R(DeviceSensor_r)}
  61. c.ServeJSON()
  62. return
  63. }
  64. // 传感器列表
  65. func (c *DataController) Device_Sensor_List() {
  66. type R_JSONS struct {
  67. //必须的大写开头
  68. DeviceSensor_lite []Device.DeviceSensor_R
  69. Num int
  70. Page int
  71. Page_size int
  72. Pages []lib.Page_T
  73. }
  74. var r_jsons R_JSONS
  75. page, _ := c.GetInt("page")
  76. if page < 1 {
  77. page = 1
  78. }
  79. page_z, _ := c.GetInt("page_z")
  80. if page_z == 0 {
  81. page_z = conf.Page_size
  82. }
  83. T_Class_id, _ := c.GetInt("T_calssid")
  84. T_type, T_type_err := c.GetInt("T_type")
  85. if T_type_err != nil {
  86. T_type = -1
  87. }
  88. T_name := c.GetString("T_name")
  89. // 数据展示页面
  90. T_RealTime, T_RealTime_err := c.GetInt("T_RealTime")
  91. if T_RealTime_err != nil {
  92. T_RealTime = 0
  93. }
  94. // 轨迹展示页面
  95. T_MapShow, T_MapShow_err := c.GetInt("T_MapShow")
  96. if T_MapShow_err != nil {
  97. T_MapShow = 0
  98. }
  99. T_abandon, _ := c.GetInt("T_abandon")
  100. var cnt int64
  101. //Device.Read_DeviceSensorParameter_All_Map("", 0)
  102. bindSN, err := Account.Read_UserDevice_List(c.Admin_r)
  103. if err != nil {
  104. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  105. c.ServeJSON()
  106. return
  107. }
  108. r_jsons.DeviceSensor_lite, cnt = Device.Read_DeviceSensor_List_For_Data(bindSN, c.T_pid, T_name, T_Class_id, T_type, T_RealTime, T_MapShow, T_abandon, page, page_z)
  109. page_size := math.Ceil(float64(cnt) / float64(page_z))
  110. r_jsons.Page = page
  111. r_jsons.Page_size = int(page_size)
  112. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  113. r_jsons.Num = int(cnt)
  114. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  115. c.ServeJSON()
  116. return
  117. }
  118. func (c *DataController) Company_Device_Sensor_List() {
  119. var r_jsons lib.R_JSONS
  120. page, _ := c.GetInt("page")
  121. if page < 1 {
  122. page = 1
  123. }
  124. page_z, _ := c.GetInt("page_z")
  125. if page_z == 0 {
  126. page_z = conf.Page_size
  127. }
  128. T_Class_id, _ := c.GetInt("T_calssid")
  129. T_type, T_type_err := c.GetInt("T_type")
  130. if T_type_err != nil {
  131. T_type = -1
  132. }
  133. T_name := c.GetString("T_name")
  134. // 数据展示页面
  135. T_RealTime, T_RealTime_err := c.GetInt("T_RealTime")
  136. if T_RealTime_err != nil {
  137. T_RealTime = 0
  138. }
  139. // 轨迹展示页面
  140. T_MapShow, T_MapShow_err := c.GetInt("T_MapShow")
  141. if T_MapShow_err != nil {
  142. T_MapShow = 0
  143. }
  144. // 查询公司
  145. Company_r, err := Account.Read_Company_ById(c.T_pid)
  146. if err != nil {
  147. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  148. c.ServeJSON()
  149. return
  150. }
  151. // 查询公司下面所有子公司id
  152. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  153. var cnt int64
  154. Account.Read_Company_All_Maps()
  155. r_jsons.Data, cnt = Device.Read_CompanyDeviceSensor_List_For_Data(T_pids, T_name, T_Class_id, T_type, T_RealTime, T_MapShow, page, page_z)
  156. page_size := math.Ceil(float64(cnt) / float64(page_z))
  157. r_jsons.Page = page
  158. r_jsons.Page_size = int(page_size)
  159. r_jsons.Num = cnt
  160. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  161. c.ServeJSON()
  162. return
  163. }
  164. func (c *DataController) Company_Device_Sensor_List_ByKey() {
  165. var r_jsons lib.R_JSONS
  166. key := c.GetString("key")
  167. T_sn := c.GetString("T_sn")
  168. // 查询公司
  169. Company_r, err := Account.Read_Company_ByKey(key)
  170. if err != nil {
  171. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  172. c.ServeJSON()
  173. return
  174. }
  175. // 查询公司下面所有子公司id
  176. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  177. Account.Read_Company_All_Maps()
  178. r_jsons.Data, r_jsons.Num = Device.Read_CompanyDeviceSensor_List_For_Data_ByKey(T_pids, T_sn)
  179. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  180. c.ServeJSON()
  181. return
  182. }
  183. // Device_Sensor_Data 传感器数据列表
  184. func (c *DataController) Device_Sensor_Data() {
  185. page, _ := c.GetInt("page")
  186. page_z, _ := c.GetInt("page_z")
  187. T_id, _ := c.GetInt("T_id")
  188. SN := c.GetString("T_sn")
  189. Time_start := c.GetString("Time_start")
  190. Time_end := c.GetString("Time_end")
  191. type R_JSONS struct {
  192. //必须的大写开头
  193. DeviceSensor_data []Device.DeviceData_R
  194. Num int
  195. Page int
  196. Page_size int
  197. Pages []lib.Page_T
  198. }
  199. var r_jsons R_JSONS
  200. var cnt int
  201. Device.Read_DeviceSensorParameter_All_Map(SN, T_id)
  202. r_jsons.DeviceSensor_data, cnt = Device.Read_DeviceData_ById_List(SN, T_id, Time_start, Time_end, page, page_z)
  203. var latLngPattern = regexp.MustCompile(`^-?\d{1,3}\.\d+,-?\d{1,3}\.\d+$`)
  204. // 第一次遍历:将所有非法坐标标记为 nil 或 ""
  205. for i := range r_jsons.DeviceSensor_data {
  206. if !latLngPattern.MatchString(r_jsons.DeviceSensor_data[i].T_site) {
  207. r_jsons.DeviceSensor_data[i].T_site = ""
  208. }
  209. }
  210. // 第二次遍历:向前查找最近的有效坐标补全空值
  211. lastValidSite := ""
  212. for i := range r_jsons.DeviceSensor_data {
  213. if r_jsons.DeviceSensor_data[i].T_site != "" {
  214. lastValidSite = r_jsons.DeviceSensor_data[i].T_site
  215. } else if lastValidSite != "" {
  216. r_jsons.DeviceSensor_data[i].T_site = lastValidSite
  217. }
  218. }
  219. page_size := math.Ceil(float64(cnt) / float64(page_z))
  220. r_jsons.Page = page
  221. r_jsons.Page_size = int(page_size)
  222. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  223. r_jsons.Num = cnt
  224. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  225. c.ServeJSON()
  226. return
  227. }
  228. // Device_Sensor_Data_Real 实时轨迹
  229. func (c *DataController) Device_Sensor_Data_Real() {
  230. page, _ := c.GetInt("page")
  231. page_z, _ := c.GetInt("page_z")
  232. T_id, _ := c.GetInt("T_id")
  233. T_sn := c.GetString("T_sn")
  234. TaskList, num := Device.Read_DeviceTask_All(T_sn)
  235. if num == 0 {
  236. c.Data["json"] = lib.JSONS{Code: 202, Msg: "暂时没有轨迹信息"}
  237. c.ServeJSON()
  238. return
  239. }
  240. Time_start := TaskList[0].T_Ut_start
  241. Time_end := TaskList[0].T_Ut_end
  242. type R_JSONS struct {
  243. //必须的大写开头
  244. DeviceSensor_data []Device.DeviceData_R
  245. Num int
  246. Page int
  247. Page_size int
  248. Pages []lib.Page_T
  249. }
  250. var r_jsons R_JSONS
  251. var cnt int
  252. Device.Read_DeviceSensorParameter_All_Map(T_sn, T_id)
  253. r_jsons.DeviceSensor_data, cnt = Device.Read_DeviceData_ById_List(T_sn, T_id, Time_start, Time_end, page, page_z)
  254. page_size := math.Ceil(float64(cnt) / float64(page_z))
  255. r_jsons.Page = page
  256. r_jsons.Page_size = int(page_size)
  257. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  258. r_jsons.Num = cnt
  259. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  260. c.ServeJSON()
  261. return
  262. }
  263. // 设备数据
  264. func (c *DataController) Device_Sensor_Data_More() {
  265. page, _ := c.GetInt("page")
  266. if page < 1 {
  267. page = 1
  268. }
  269. page_z, _ := c.GetInt("page_z")
  270. if page_z < 1 {
  271. page_z = conf.Page_size
  272. }
  273. T_snid := c.GetString("T_snid")
  274. Time_start := c.GetString("Time_start")
  275. Time_end := c.GetString("Time_end")
  276. if len(T_snid) < 10 {
  277. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  278. c.ServeJSON()
  279. return
  280. }
  281. type R_JSONS struct {
  282. //必须的大写开头
  283. Data []Device.DeviceData_R
  284. Num int64
  285. Page int
  286. Page_size int
  287. }
  288. var r_jsons R_JSONS
  289. r_jsons.Data, r_jsons.Num = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, page, page_z)
  290. r_jsons.Page = page
  291. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  292. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  293. c.ServeJSON()
  294. return
  295. }
  296. // GetSecondaryPositioning 获取APP辅助定位
  297. func (c *DataController) GetSecondaryPositioning() {
  298. T_Sn := c.GetString("T_sn")
  299. longitude := c.GetString("longitude")
  300. latitude := c.GetString("latitude")
  301. T_time := c.GetString("T_time")
  302. if len(T_Sn) == 0 || len(longitude) == 0 || len(latitude) == 0 || len(T_time) == 0 {
  303. c.Data["json"] = lib.JSONS{Code: 202, Msg: "缺少必要参数!!!"}
  304. c.ServeJSON()
  305. return
  306. }
  307. err := Device.Update_AssistedPositioning(T_Sn, longitude, latitude, T_time)
  308. if err != nil {
  309. c.Data["json"] = lib.JSONS{Code: 202, Msg: "辅助定位更新失败!!!", Data: err.Error()}
  310. c.ServeJSON()
  311. return
  312. }
  313. sprintln := fmt.Sprintln("T_Sn:", T_Sn, "longitude:", longitude, "latitude:", latitude, "T_time:", T_time)
  314. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: sprintln}
  315. c.ServeJSON()
  316. }
  317. // -------------------------------------
  318. // 导出传感器数据列表
  319. func (c *DataController) Device_Sensor_Data_Excel() {
  320. T_snid := c.GetString("T_snid")
  321. Time_start := c.GetString("Time_start")
  322. Time_end := c.GetString("Time_end")
  323. if len(T_snid) < 10 {
  324. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  325. c.ServeJSON()
  326. return
  327. }
  328. var DeviceSensor_data []Device.DeviceData_R
  329. DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  330. // 按照传感器T_sort排序
  331. //var snids string
  332. //dsList := Device.Read_DeviceSensorList_By_T_snid(T_snid)
  333. //// 创建名称到权重的映射
  334. //orderMap := make(map[int]int)
  335. //for i, v := range dsList {
  336. // snids += fmt.Sprintf("%s,%d|", v.T_sn, v.T_id)
  337. // orderMap[v.T_id] = i
  338. //}
  339. //sort.Slice(DeviceSensor_data, func(i, j int) bool {
  340. // if DeviceSensor_data[i].T_time == DeviceSensor_data[j].T_time {
  341. // // 如果时间相同,则按预设顺序排序
  342. // return orderMap[DeviceSensor_data[i].T_id] < orderMap[DeviceSensor_data[j].T_id]
  343. // }
  344. // return DeviceSensor_data[i].T_time > DeviceSensor_data[j].T_time
  345. //})
  346. f := excelize.NewFile() // 设置单元格的值
  347. // 这里设置表头
  348. f.SetCellValue("Sheet1", "A1", "编号")
  349. f.SetCellValue("Sheet1", "B1", "传感器名称")
  350. f.SetCellValue("Sheet1", "C1", "温度℃")
  351. f.SetCellValue("Sheet1", "D1", "湿度%")
  352. f.SetCellValue("Sheet1", "E1", "温度范围 ℃")
  353. f.SetCellValue("Sheet1", "F1", "湿度范围 %")
  354. f.SetCellValue("Sheet1", "G1", "记录时间")
  355. f.SetCellValue("Sheet1", "H1", "备注")
  356. // 设置列宽
  357. f.SetColWidth("Sheet1", "A", "A", 10)
  358. f.SetColWidth("Sheet1", "B", "B", 15)
  359. f.SetColWidth("Sheet1", "C", "D", 10)
  360. f.SetColWidth("Sheet1", "E", "F", 15)
  361. f.SetColWidth("Sheet1", "G", "G", 22)
  362. f.SetColWidth("Sheet1", "H", "H", 22)
  363. line := 1
  364. headStyleLower, _ := f.NewStyle(
  365. &excelize.Style{
  366. Font: &excelize.Font{Size: 11, Color: "#ff8585", Family: "arial"},
  367. })
  368. headStyleUpper, _ := f.NewStyle(
  369. &excelize.Style{
  370. Font: &excelize.Font{Size: 11, Color: "#ff8585", Family: "arial"},
  371. })
  372. // 循环写入数据
  373. for _, v := range DeviceSensor_data {
  374. line++
  375. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-1)
  376. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name+"["+strconv.Itoa(v.T_id)+"]")
  377. if v.T_ist == 1 {
  378. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_t)
  379. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), strconv.FormatFloat(float64(v.T_tl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_tu), 'f', 2, 64))
  380. if (v.T_t < v.T_tl || v.T_t > v.T_tu) && v.T_free == 0 {
  381. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
  382. }
  383. }
  384. if v.T_ish == 1 {
  385. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_rh)
  386. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), strconv.FormatFloat(float64(v.T_rhl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_rhu), 'f', 2, 64))
  387. if v.T_rh < v.T_rhl || v.T_rh > v.T_rhu && v.T_free == 0 {
  388. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
  389. }
  390. }
  391. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_time)
  392. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_remark)
  393. //if v.T_ist == 1 && (v.T_t < v.T_tl || v.T_t > v.T_tu) {
  394. // f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
  395. //}
  396. //if v.T_ish == 1 && (v.T_rh < v.T_rhl || v.T_rh > v.T_rhu) {
  397. // f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
  398. //}
  399. }
  400. timeStr := time.Now().Format("20060102150405")
  401. // 保存文件
  402. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  403. logs.Error(err)
  404. }
  405. // 上传 OSS
  406. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  407. if !is {
  408. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  409. c.ServeJSON()
  410. return
  411. }
  412. //删除目录
  413. err := os.Remove("ofile/" + timeStr + ".xlsx")
  414. if err != nil {
  415. logs.Error(err)
  416. }
  417. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  418. c.ServeJSON()
  419. return
  420. }
  421. // 列表 - 接口
  422. func (c *DataController) Device_Sensor_Data_Excel_m() {
  423. T_snid := c.GetString("T_snid")
  424. Time_start := c.GetString("Time_start")
  425. Time_end := c.GetString("Time_end")
  426. t_x, _ := c.GetInt("t_x")
  427. if len(T_snid) < 10 {
  428. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  429. c.ServeJSON()
  430. return
  431. }
  432. var DeviceSensor_data []Device.DeviceData_R
  433. DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  434. f := excelize.NewFile() // 设置单元格的值
  435. // 这里设置表头
  436. f.SetCellValue("Sheet1", "A1", "编号")
  437. f.SetCellValue("Sheet1", "B1", "传感器名称")
  438. f.SetCellValue("Sheet1", "C1", "温度℃")
  439. f.SetCellValue("Sheet1", "D1", "湿度%")
  440. f.SetCellValue("Sheet1", "E1", "温度范围 ℃")
  441. f.SetCellValue("Sheet1", "F1", "湿度范围 %")
  442. f.SetCellValue("Sheet1", "G1", "记录时间")
  443. // 设置列宽
  444. f.SetColWidth("Sheet1", "A", "A", 10)
  445. f.SetColWidth("Sheet1", "B", "B", 15)
  446. f.SetColWidth("Sheet1", "C", "D", 10)
  447. f.SetColWidth("Sheet1", "E", "F", 15)
  448. f.SetColWidth("Sheet1", "G", "G", 22)
  449. line := 1
  450. headStyleLower, _ := f.NewStyle(
  451. &excelize.Style{
  452. Font: &excelize.Font{Size: 11, Color: "#a8f7ff", Family: "arial"},
  453. })
  454. headStyleUpper, _ := f.NewStyle(
  455. &excelize.Style{
  456. Font: &excelize.Font{Size: 11, Color: "#ff8585", Family: "arial"},
  457. })
  458. atime, _ := time.ParseDuration("-1m") // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
  459. // 循环写入数据
  460. for v_i, v := range DeviceSensor_data {
  461. line++
  462. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-1)
  463. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name+"["+strconv.Itoa(v.T_id)+"]")
  464. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_t)
  465. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_rh)
  466. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), strconv.FormatFloat(float64(v.T_tl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_tu), 'f', 2, 64))
  467. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), strconv.FormatFloat(float64(v.T_rhl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_rhu), 'f', 2, 64))
  468. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_time)
  469. if v.T_t < v.T_tl || v.T_t > v.T_tu {
  470. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
  471. }
  472. if v.T_rh < v.T_rhl || v.T_rh > v.T_rhu {
  473. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
  474. }
  475. if len(DeviceSensor_data)-1 == v_i {
  476. break
  477. }
  478. cha_v := ((DeviceSensor_data[v_i+1].T_t - v.T_t) / 3)
  479. println("t_x:", t_x)
  480. for i := 0; i < t_x; i++ {
  481. println("// -------", i)
  482. v.T_t = float32(lib.Decimal(float64(v.T_t + cha_v)))
  483. fTime, _ := time.Parse("2006-01-02 15:04:05", v.T_time)
  484. fTime = fTime.Add(atime)
  485. v.T_time = fTime.Format("2006-01-02 15:04:05")
  486. line++
  487. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-1)
  488. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name+"["+strconv.Itoa(v.T_id)+"]")
  489. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_t)
  490. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_rh)
  491. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), strconv.FormatFloat(float64(v.T_tl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_tu), 'f', 2, 64))
  492. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), strconv.FormatFloat(float64(v.T_rhl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_rhu), 'f', 2, 64))
  493. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_time)
  494. if v.T_t < v.T_tl || v.T_t > v.T_tu {
  495. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
  496. }
  497. if v.T_rh < v.T_rhl || v.T_rh > v.T_rhu {
  498. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
  499. }
  500. }
  501. }
  502. timeStr := time.Now().Format("20060102150405")
  503. // 保存文件
  504. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  505. logs.Error(err)
  506. }
  507. // 上传 OSS
  508. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  509. if !is {
  510. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  511. c.ServeJSON()
  512. return
  513. }
  514. //删除目录
  515. //err := os.Remove("ofile/" + timeStr + ".xlsx")
  516. //if err != nil {
  517. // logs.Error(err)
  518. //}
  519. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  520. c.ServeJSON()
  521. return
  522. }
  523. // 小程序 - 打印
  524. func (c *DataController) DeviceSensor_Data_Print() {
  525. SN := c.GetString("T_sn")
  526. Time_start := c.GetString("Time_start")
  527. Time_end := c.GetString("Time_end")
  528. type R_JSONS struct {
  529. //必须的大写开头
  530. T_time string
  531. T1 interface{}
  532. T2 interface{}
  533. T3 interface{}
  534. T4 interface{}
  535. }
  536. r_jsons := make(map[string]R_JSONS)
  537. type JSONS struct {
  538. //必须的大写开头
  539. Code int16
  540. Msg string
  541. DeviceSensorNum int
  542. DeviceSensor Device.DeviceSensor_Applet
  543. AdminNane string
  544. Data interface{} // 泛型
  545. }
  546. GROUP_BY_t_time := Device.Read_DeviceData_List_GROUP_BY_t_time(SN, Time_start, Time_end)
  547. // 时间 MAP
  548. for _, vt := range GROUP_BY_t_time {
  549. stamp, _ := time.Parse("2006-1-02 15:04:05", lib.To_string(vt[0]))
  550. T_time := stamp.Format("2006-01-02 15:04:05")
  551. r_jsons[T_time] = R_JSONS{T_time: T_time, T1: nil, T2: nil, T3: nil, T4: nil}
  552. }
  553. dsList := Device.Read_DeviceSensor_List_T_sn_T_datashow(SN)
  554. if len(dsList) == 0 {
  555. c.Data["json"] = JSONS{Code: 200, Msg: "ok!", DeviceSensorNum: len(dsList), Data: r_jsons}
  556. c.ServeJSON()
  557. return
  558. }
  559. company, _ := Account.Read_Company_ById(c.T_pid)
  560. for i, v := range dsList {
  561. if i >= 4 {
  562. break
  563. }
  564. // 记录传感器
  565. maps, num := Device.Read_DeviceData_ById_List(SN, v.T_id, Time_start, Time_end, 1, 9999)
  566. if num > 0 {
  567. for _, v_map := range maps {
  568. ws, ok := r_jsons[v_map.T_time]
  569. if ok {
  570. switch i {
  571. case 0:
  572. ws.T1 = v_map
  573. case 1:
  574. ws.T2 = v_map
  575. case 2:
  576. ws.T3 = v_map
  577. case 3:
  578. ws.T4 = v_map
  579. }
  580. r_jsons[v_map.T_time] = ws
  581. }
  582. }
  583. }
  584. }
  585. c.Data["json"] = JSONS{Code: 200, Msg: "ok!", DeviceSensorNum: len(dsList), DeviceSensor: Device.DeviceSensorToDeviceSensor_Applet(dsList[0]), AdminNane: company.T_name, Data: r_jsons}
  586. c.ServeJSON()
  587. return
  588. }
  589. // 列表 - 接口
  590. func (c *DataController) Device_Sensor_Data_PDF() {
  591. T_snid := c.GetString("T_snid")
  592. Time_start := c.GetString("Time_start")
  593. Time_end := c.GetString("Time_end")
  594. T_ish, _ := c.GetInt("T_ish") // 展示湿度
  595. if len(T_snid) < 10 {
  596. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_snid Err!"}
  597. c.ServeJSON()
  598. return
  599. }
  600. //T_ist // 温度 1开启 2关闭
  601. //T_ish // 湿度 1开启 2关闭
  602. T_ist := 1
  603. //T_ist_list := make([]int, 0)
  604. //T_ish_list := make([]int, 0)
  605. //T_snid_list := strings.Split(strings.Trim(T_snid, "|"), "|")
  606. //for _, v := range T_snid_list {
  607. // sn_id := strings.Split(v, ",")
  608. // if len(sn_id) == 2 {
  609. // device, _ := Device.Read_Device_ByT_sn(sn_id[0])
  610. // T_ist_list = append(T_ist_list, device.T_ist)
  611. // T_ish_list = append(T_ish_list, device.T_ish)
  612. // }
  613. //}
  614. //istHasOne := false
  615. //istHasTwo := false
  616. //
  617. //for _, v := range T_ist_list {
  618. // if v == 1 {
  619. // istHasOne = true
  620. // }
  621. // if v == 2 {
  622. // istHasTwo = true
  623. // }
  624. //}
  625. //// 全部都为2 则不显示温度
  626. //if !istHasOne && istHasTwo {
  627. // T_ist = 2
  628. //}
  629. //
  630. //ishHasOne := false
  631. //ishHasTwo := false
  632. //T_ish := 1
  633. //for _, v := range T_ish_list {
  634. // if v == 1 {
  635. // ishHasOne = true
  636. // }
  637. // if v == 2 {
  638. // ishHasTwo = true
  639. // }
  640. //}
  641. //// 全部都为2 则不显示湿度
  642. //if !ishHasOne && ishHasTwo {
  643. // T_ish = 2
  644. //}
  645. var DeviceSensor_data []Device.DeviceData_R
  646. DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  647. // 按照传感器T_sort排序
  648. //var snids string
  649. //dsList := Device.Read_DeviceSensorList_By_T_snid(T_snid)
  650. //// 创建名称到权重的映射
  651. //orderMap := make(map[int]int)
  652. //for i, v := range dsList {
  653. // snids += fmt.Sprintf("%s,%d|", v.T_sn, v.T_id)
  654. // orderMap[v.T_id] = i
  655. //}
  656. //sort.Slice(DeviceSensor_data, func(i, j int) bool {
  657. // if DeviceSensor_data[i].T_time == DeviceSensor_data[j].T_time {
  658. // // 如果时间相同,则按预设顺序排序
  659. // return orderMap[DeviceSensor_data[i].T_id] < orderMap[DeviceSensor_data[j].T_id]
  660. // }
  661. // return DeviceSensor_data[i].T_time > DeviceSensor_data[j].T_time
  662. //})
  663. // -------------------获取最高温湿度、最低温湿度、平均温湿度
  664. // 最高温度、最低温度
  665. var maxTemp, minTemp, maxRH, minRH float32
  666. // 最高温度时间、最低温度时间
  667. var maxTempTime, minTempTime, maxRHTime, minRHTime string
  668. // 总温度
  669. var totalTemp, totalRH float32
  670. // 平均温度
  671. var avgTemp, avgRH float32
  672. var TempCount, RHCount int
  673. if len(DeviceSensor_data) > 0 {
  674. // 最高温度及时刻
  675. firstTempFlag := false
  676. //var maxTemp,minTemp float32
  677. //var maxTempTime,minTempTime string
  678. //maxTemp = DeviceSensor_data[0].T_t
  679. //maxTempTime = DeviceSensor_data[0].T_time
  680. //// 最低温度及时刻
  681. //minTemp = DeviceSensor_data[0].T_t
  682. //minTempTime = DeviceSensor_data[0].T_time
  683. // 最高湿度度及时刻
  684. firstRHFlag := false
  685. //maxRH = DeviceSensor_data[0].T_rh
  686. //maxRHTime = DeviceSensor_data[0].T_time
  687. //// 最低湿度及时刻
  688. //minRH = DeviceSensor_data[0].T_rh
  689. //minRHTime = DeviceSensor_data[0].T_time
  690. for _, data := range DeviceSensor_data {
  691. if data.T_ist == 1 {
  692. if firstTempFlag {
  693. if data.T_t > maxTemp {
  694. maxTemp = data.T_t
  695. maxTempTime = data.T_time
  696. }
  697. if data.T_t < minTemp {
  698. minTemp = data.T_t
  699. minTempTime = data.T_time
  700. }
  701. totalTemp += data.T_t
  702. TempCount++
  703. } else {
  704. maxTemp = data.T_t
  705. maxTempTime = data.T_time
  706. // 最低温度及时刻
  707. minTemp = data.T_t
  708. minTempTime = data.T_time
  709. firstTempFlag = true
  710. totalTemp += data.T_t
  711. TempCount = 1
  712. }
  713. }
  714. if data.T_ish == 1 {
  715. if firstRHFlag {
  716. if data.T_rh > maxRH {
  717. maxRH = data.T_rh
  718. maxRHTime = data.T_time
  719. }
  720. if data.T_rh < minRH {
  721. minRH = data.T_rh
  722. minRHTime = data.T_time
  723. }
  724. totalRH += data.T_rh
  725. RHCount++
  726. } else {
  727. maxRH = data.T_rh
  728. maxRHTime = data.T_time
  729. // 最低温度及时刻
  730. minRH = data.T_rh
  731. minRHTime = data.T_time
  732. firstRHFlag = true
  733. totalRH += data.T_rh
  734. RHCount = 1
  735. }
  736. }
  737. }
  738. if TempCount > 0 {
  739. // 平均温度
  740. avgTemp = totalTemp / float32(TempCount)
  741. }
  742. // 平均湿度
  743. if RHCount > 0 {
  744. avgRH = totalRH / float32(RHCount)
  745. }
  746. }
  747. // -------------------获取最高温湿度、最低温湿度、平均温湿度结束
  748. maxTempTime = maxTempTime
  749. minTempTime = minTempTime
  750. maxRHTime = maxRHTime
  751. minRHTime = minRHTime
  752. var err error
  753. pdf := &gopdf.GoPdf{}
  754. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  755. //err = GetFont(pdf, "LiberationSerif-Regular.ttf")
  756. //if err != nil {
  757. // log.Fatalln(err)
  758. //}
  759. //err = pdf.SetFont("Ubuntu-L", "", 14)
  760. //if err != nil {
  761. // log.Fatalln(err)
  762. //}
  763. err = pdf.AddTTFFont("simsun", "static/fonts/MiSans-Medium.ttf")
  764. if err != nil {
  765. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  766. c.ServeJSON()
  767. return
  768. }
  769. err = pdf.SetFont("simsun", "", 24)
  770. if err != nil {
  771. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  772. c.ServeJSON()
  773. return
  774. }
  775. pdf.SetGrayFill(0.5)
  776. pdf.SetMargins(0, 20, 0, 20)
  777. pdf.AddPage()
  778. company, _ := Account.Read_Company_ById(c.T_pid)
  779. textw, _ := pdf.MeasureTextWidth(company.T_name)
  780. pdf.SetX((595 / 2) - (textw / 2))
  781. pdf.SetY(40)
  782. pdf.Text(company.T_name)
  783. // 线
  784. pdf.SetLineWidth(2)
  785. pdf.SetLineType("dashed")
  786. pdf.Line(10, 60, 585, 60)
  787. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  788. if err != nil {
  789. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  790. c.ServeJSON()
  791. return
  792. }
  793. err = pdf.SetFont("wts", "", 12)
  794. if err != nil {
  795. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  796. c.ServeJSON()
  797. return
  798. }
  799. var s_time, e_time string
  800. if len(DeviceSensor_data) > 0 {
  801. s_time = fmt.Sprintf("%s", DeviceSensor_data[0].T_time)
  802. e_time = fmt.Sprintf("%s", DeviceSensor_data[len(DeviceSensor_data)-1].T_time)
  803. }
  804. lib.RectFillColor(pdf, "历史数据["+e_time+" / "+s_time+"]", 14, 22, 80, 550, 40, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  805. pdf.SetFont("wts", "", 10)
  806. pages := pdf.GetNumberOfPages()
  807. pagenums := fmt.Sprintf("第 %d 页", pages)
  808. pagenum, _ := pdf.MeasureTextWidth(pagenums)
  809. pdf.SetX((595 / 2) - (pagenum / 2))
  810. pdf.SetY(810)
  811. pdf.Cell(nil, pagenums)
  812. var y float64 = 120
  813. if T_ist == 1 {
  814. lib.RectFillColor(pdf, fmt.Sprintf("最高温度: %.1f ", maxTemp), 12, 22, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  815. lib.RectFillColor(pdf, fmt.Sprintf("最低温度: %.1f ", minTemp), 12, 205, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  816. lib.RectFillColor(pdf, fmt.Sprintf("平均温度: %.1f ", avgTemp), 12, 388, y, 184, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  817. y += 20
  818. }
  819. if T_ish == 1 {
  820. lib.RectFillColor(pdf, fmt.Sprintf("最高湿度: %.1f ", maxRH), 12, 22, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  821. lib.RectFillColor(pdf, fmt.Sprintf("最低湿度: %.1f ", minRH), 12, 205, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  822. lib.RectFillColor(pdf, fmt.Sprintf("平均湿度: %.1f ", avgRH), 12, 388, y, 184, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  823. y += 20
  824. }
  825. y += 5
  826. lib.RectFillColor(pdf, "序号", 12, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  827. if T_ish == 1 {
  828. lib.RectFillColor(pdf, "传感器名称", 12, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  829. lib.RectFillColor(pdf, "温度℃", 12, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  830. lib.RectFillColor(pdf, "湿度%", 12, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  831. }
  832. if T_ish == 0 {
  833. lib.RectFillColor(pdf, "传感器名称", 12, 52, y, 140, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  834. lib.RectFillColor(pdf, "温度℃", 12, 192, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  835. }
  836. if T_ish == 1 {
  837. lib.RectFillColor(pdf, "温度范围", 12, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  838. lib.RectFillColor(pdf, "湿度范围", 12, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  839. }
  840. if T_ish == 0 {
  841. lib.RectFillColor(pdf, "温度范围", 12, 292, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  842. }
  843. lib.RectFillColor(pdf, "记录时间", 12, 412, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  844. lib.RectFillColor(pdf, "备注", 12, 532, y, 40, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  845. y += 20
  846. err = pdf.SetFont("wts", "", 10)
  847. if err != nil {
  848. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  849. c.ServeJSON()
  850. return
  851. }
  852. for i, v := range DeviceSensor_data {
  853. text := fmt.Sprintf(" %d ", i+1)
  854. var textH float64 = 25 // if text height is 25px.
  855. pdf.SetNewY(y, textH)
  856. y = pdf.GetY()
  857. if y > 790 {
  858. // 翻页
  859. pdf.AddPage()
  860. y = pdf.GetY()
  861. }
  862. // newY = 20
  863. if y < 30 {
  864. lib.RectFillColor(pdf, "序号", 12, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  865. if T_ish == 1 {
  866. lib.RectFillColor(pdf, "传感器名称", 12, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  867. lib.RectFillColor(pdf, "温度℃", 12, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  868. lib.RectFillColor(pdf, "湿度%", 12, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  869. }
  870. if T_ish == 0 {
  871. lib.RectFillColor(pdf, "传感器名称", 12, 52, y, 140, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  872. lib.RectFillColor(pdf, "温度℃", 12, 192, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  873. }
  874. if T_ish == 1 {
  875. lib.RectFillColor(pdf, "温度范围", 12, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  876. lib.RectFillColor(pdf, "湿度范围", 12, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  877. }
  878. if T_ish == 0 {
  879. lib.RectFillColor(pdf, "温度范围", 12, 292, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  880. }
  881. lib.RectFillColor(pdf, "记录时间", 12, 412, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  882. lib.RectFillColor(pdf, "备注", 12, 532, y, 40, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  883. pages = pdf.GetNumberOfPages()
  884. pagenums = fmt.Sprintf("第 %d 页", pages)
  885. pagenum, _ = pdf.MeasureTextWidth(pagenums)
  886. pdf.SetX((595 / 2) - (pagenum / 2))
  887. pdf.SetY(810) // 设置页码位置
  888. pdf.Cell(nil, pagenums)
  889. y += 20
  890. }
  891. //pdf.SetX(x) // must after pdf.SetNewY() called.
  892. //err = pdf.Text(text)
  893. //if err != nil {
  894. // log.Fatalln(err)
  895. //}
  896. T_t := fmt.Sprintf(" %.1f ", v.T_t)
  897. T_rh := fmt.Sprintf(" %.1f ", v.T_rh)
  898. T_Tlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_tl, v.T_tu)
  899. T_Rlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_rhl, v.T_rhu)
  900. T_time := fmt.Sprintf("%s", v.T_time)
  901. //if v.T_ist == 1 {
  902. // lib.RectFillColor(pdf, T_t, 10, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  903. // lib.RectFillColor(pdf, T_Tlu, 10, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  904. //} else {
  905. // lib.RectFillColor(pdf, "", 10, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  906. // lib.RectFillColor(pdf, "", 10, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  907. //}
  908. //if v.T_ish == 1 {
  909. // lib.RectFillColor(pdf, T_rh, 10, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  910. // lib.RectFillColor(pdf, T_Rlu, 10, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  911. //} else {
  912. // lib.RectFillColor(pdf, "", 10, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  913. // lib.RectFillColor(pdf, "", 10, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  914. //}
  915. if T_ish == 1 {
  916. lib.RectFillColor(pdf, T_t, 10, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  917. lib.RectFillColor(pdf, T_Tlu, 10, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  918. lib.RectFillColor(pdf, T_rh, 10, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  919. lib.RectFillColor(pdf, T_Rlu, 10, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  920. lib.RectFillColor(pdf, v.T_name, 10, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  921. }
  922. if T_ish == 0 {
  923. lib.RectFillColor(pdf, v.T_name, 10, 52, y, 140, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  924. lib.RectFillColor(pdf, T_t, 10, 192, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  925. lib.RectFillColor(pdf, T_Tlu, 10, 292, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  926. }
  927. lib.RectFillColor(pdf, text, 10, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  928. lib.RectFillColor(pdf, T_time, 10, 412, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  929. lib.RectFillColor(pdf, v.T_remark, 10, 532, y, 40, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  930. y += 20
  931. }
  932. timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
  933. err = pdf.WritePdf(timeStr)
  934. if err != nil {
  935. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  936. c.ServeJSON()
  937. return
  938. }
  939. // 上传 OSS
  940. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+timeStr, timeStr)
  941. if !is {
  942. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  943. c.ServeJSON()
  944. return
  945. }
  946. //删除目录
  947. err = os.Remove(timeStr)
  948. if err != nil {
  949. logs.Error(err)
  950. }
  951. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  952. c.ServeJSON()
  953. return
  954. }
  955. func (c *DataController) Device_Sensor_Data_ChartShow_PDF() {
  956. T_snid := c.GetString("T_snid")
  957. Time_start := c.GetString("Time_start")
  958. Time_end := c.GetString("Time_end")
  959. T_forwarding_unit := c.GetString("T_forwarding_unit")
  960. T_consignee_unit := c.GetString("T_consignee_unit")
  961. T_remark := c.GetString("T_remark")
  962. T_temp_show, _ := c.GetInt("T_temp_show")
  963. T_Humidity_show, _ := c.GetInt("T_Humidity_show")
  964. T_template := c.GetString("T_template")
  965. if T_temp_show == 0 && T_Humidity_show == 0 {
  966. c.Data["json"] = lib.JSONS{Code: 202, Msg: "温湿度不能同时不显示"}
  967. c.ServeJSON()
  968. return
  969. }
  970. if len(T_snid) < 10 {
  971. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_snid Err!"}
  972. c.ServeJSON()
  973. return
  974. }
  975. // 获取图片信息
  976. base64String := c.GetString("file")
  977. imageBytes, err := base64.StdEncoding.DecodeString(base64String)
  978. if err != nil {
  979. c.Data["json"] = lib.JSONS{Code: 202, Msg: "解析base64失败"}
  980. c.ServeJSON()
  981. return
  982. }
  983. company, _ := Account.Read_Company_ById(c.T_pid)
  984. if T_template == "1" {
  985. url, err := Device_Sensor_Data_ChartShow_PDF1(company.T_name, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark, imageBytes, T_temp_show, T_Humidity_show)
  986. if err != nil {
  987. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  988. c.ServeJSON()
  989. return
  990. }
  991. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  992. c.ServeJSON()
  993. return
  994. }
  995. url, err := Device_Sensor_Data_ChartShow_PDF0(company.T_name, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark, imageBytes, T_temp_show, T_Humidity_show)
  996. if err != nil {
  997. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  998. c.ServeJSON()
  999. return
  1000. }
  1001. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1002. c.ServeJSON()
  1003. return
  1004. }
  1005. // 图表展示 下载PDF 箱/车
  1006. func Device_Sensor_Data_ChartShow_PDF1(companyName, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark string, imageBytes []byte,
  1007. T_temp_show, T_Humidity_show int) (url string, err error) {
  1008. snidNum := strings.Split(strings.Trim(T_snid, "|"), "|")
  1009. if len(snidNum) > 2 {
  1010. err = errors.New("车/箱最多只能选择2个探头!")
  1011. return
  1012. }
  1013. var sn1, sn2 string
  1014. var id1, id2 int
  1015. var r_maps_sn1, r_maps_sn2 []Device.DeviceData_R
  1016. var DeviceSensor_data []Device.DeviceData_R
  1017. var Pdf_data []Device.DeviceData_Pdf
  1018. if len(snidNum) == 1 {
  1019. sn_id := strings.Split(snidNum[0], ",")
  1020. sn1, id1 = sn_id[0], lib.To_int(sn_id[1])
  1021. r_maps_sn1, _ = Device.Read_DeviceData_ById_List_OrderByTimeAes(sn1, id1, Time_start, Time_end, 0, 9999)
  1022. DeviceSensor_data = r_maps_sn1
  1023. Pdf_data = Device.Read_DeviceData_ByIds_ForPDF(sn1, []int{id1}, Time_start, Time_end)
  1024. }
  1025. if len(snidNum) == 2 {
  1026. sn_id1 := strings.Split(snidNum[0], ",")
  1027. sn_id2 := strings.Split(snidNum[1], ",")
  1028. sn1, id1 = sn_id1[0], lib.To_int(sn_id1[1])
  1029. sn2, id2 = sn_id2[0], lib.To_int(sn_id2[1])
  1030. if sn1 != sn2 {
  1031. err = errors.New("请选择相同设备编号的探头导出!")
  1032. return
  1033. }
  1034. r_maps_sn1, _ = Device.Read_DeviceData_ById_List_OrderByTimeAes(sn1, id1, Time_start, Time_end, 0, 9999)
  1035. r_maps_sn2, _ = Device.Read_DeviceData_ById_List_OrderByTimeAes(sn2, id2, Time_start, Time_end, 0, 9999)
  1036. DeviceSensor_data = append(DeviceSensor_data, r_maps_sn1...)
  1037. DeviceSensor_data = append(DeviceSensor_data, r_maps_sn2...)
  1038. Pdf_data = Device.Read_DeviceData_ByIds_ForPDF(sn1, []int{id1, id2}, Time_start, Time_end)
  1039. }
  1040. device, err := Device.Read_Device_ByT_sn(sn1)
  1041. if err != nil {
  1042. err = errors.New("查询主机信息失败!")
  1043. return
  1044. }
  1045. // -------------------获取最高温湿度、最低温湿度、平均温湿度
  1046. // 最高温度、最低温度
  1047. var maxTemp, minTemp float32
  1048. // 最高温度时间、最低温度时间
  1049. var maxTempTime, minTempTime string
  1050. // 总温度
  1051. var totalTemp float32
  1052. // 平均温度
  1053. var avgTemp float32
  1054. if len(DeviceSensor_data) > 0 {
  1055. // 最高温度及时刻
  1056. maxTemp = DeviceSensor_data[0].T_t
  1057. maxTempTime = DeviceSensor_data[0].T_time
  1058. // 最低温度及时刻
  1059. minTemp = DeviceSensor_data[0].T_t
  1060. minTempTime = DeviceSensor_data[0].T_time
  1061. for _, data := range DeviceSensor_data {
  1062. if data.T_t > maxTemp {
  1063. maxTemp = data.T_t
  1064. maxTempTime = data.T_time
  1065. }
  1066. if data.T_t < minTemp {
  1067. minTemp = data.T_t
  1068. minTempTime = data.T_time
  1069. }
  1070. totalTemp += data.T_t
  1071. }
  1072. // 平均温度
  1073. avgTemp = totalTemp / float32(len(DeviceSensor_data))
  1074. // 平均湿度
  1075. //avgHumidity = totalHumidity / float32(len(DeviceSensor_data))
  1076. }
  1077. // -------------------获取最高温湿度、最低温湿度、平均温湿度结束
  1078. pdf := &gopdf.GoPdf{}
  1079. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  1080. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  1081. if err != nil {
  1082. return
  1083. }
  1084. err = pdf.SetFont("wts", "", 20)
  1085. if err != nil {
  1086. return
  1087. }
  1088. pdf.SetGrayFill(0.5)
  1089. pdf.SetMargins(0, 20, 0, 20)
  1090. pdf.AddPage()
  1091. title := companyName + "温湿度记录"
  1092. var y float64 = 40
  1093. textw, _ := pdf.MeasureTextWidth(title)
  1094. pdf.SetX((595 / 2) - (textw / 2))
  1095. pdf.SetY(y)
  1096. pdf.Text(title)
  1097. y += 30
  1098. pdf.SetFont("wts", "", 16)
  1099. pdf.SetXY(10, y)
  1100. pdf.Text("实施设备信息")
  1101. // 线
  1102. y += 10
  1103. pdf.SetLineWidth(0.5)
  1104. pdf.SetStrokeColor(169, 169, 169)
  1105. pdf.Line(10, y, 585, y)
  1106. pdf.SetFont("wts", "", 10)
  1107. y += 20
  1108. pdf.SetXY(10, y)
  1109. pdf.Text(fmt.Sprintf("主机名称:%s", device.T_devName))
  1110. pdf.SetXY(300, y)
  1111. pdf.Text(fmt.Sprintf("主机编号:%s", device.T_sn))
  1112. // 0 仓库 1 车厢
  1113. y += 20
  1114. ds1, _ := Device.Read_DeviceSensor_ByT_sn(sn1, id1)
  1115. pdf.SetXY(10, y)
  1116. pdf.Text(fmt.Sprintf("测点信息T1:%s", ds1.T_name))
  1117. if len(snidNum) > 1 {
  1118. ds2, _ := Device.Read_DeviceSensor_ByT_sn(sn2, id2)
  1119. pdf.SetXY(300, y)
  1120. pdf.Text(fmt.Sprintf("测点信息T2:%s", ds2.T_name))
  1121. }
  1122. y += 35
  1123. pdf.SetFont("wts", "", 16)
  1124. pdf.SetXY(10, y)
  1125. pdf.Text("记录概要信息")
  1126. // 线
  1127. y += 10
  1128. pdf.SetLineWidth(0.5)
  1129. pdf.SetStrokeColor(169, 169, 169)
  1130. pdf.Line(10, y, 585, y)
  1131. y += 20
  1132. var s_time, e_time string
  1133. if len(r_maps_sn1) > 0 {
  1134. s_time = fmt.Sprintf("%s", Pdf_data[0].T_time)
  1135. e_time = fmt.Sprintf("%s", Pdf_data[len(Pdf_data)-1].T_time)
  1136. }
  1137. pdf.SetFont("wts", "", 10)
  1138. pdf.SetXY(10, y)
  1139. pdf.Text(fmt.Sprintf("记录开始时间:%s", s_time))
  1140. pdf.SetXY(240, y)
  1141. pdf.Text(fmt.Sprintf("记录结束时间:%s", e_time))
  1142. sTime, _ := lib.TimeStrToTime(s_time)
  1143. eTime, _ := lib.TimeStrToTime(e_time)
  1144. pdf.SetXY(470, y)
  1145. minutes := int(eTime.Sub(sTime).Minutes())
  1146. hours := minutes / 60
  1147. remainingMinutes := minutes % 60
  1148. pdf.Text(fmt.Sprintf("记录总时间:%dh%dmin", hours, remainingMinutes))
  1149. if T_temp_show == 1 {
  1150. y += 15
  1151. pdf.SetXY(10, y)
  1152. pdf.Text(fmt.Sprintf("最高温度:%s【%.1f℃】", maxTempTime, lib.RoundToDecimal(float64(maxTemp), 1)))
  1153. pdf.SetXY(240, y)
  1154. pdf.Text(fmt.Sprintf("最低温度:%s【%.1f℃】", minTempTime, lib.RoundToDecimal(float64(minTemp), 1)))
  1155. pdf.SetXY(470, y)
  1156. pdf.Text(fmt.Sprintf("平均温度:%.2f℃", lib.RoundToDecimal(float64(avgTemp), 1)))
  1157. }
  1158. y += 15
  1159. pdf.SetXY(10, y)
  1160. var start_temp, end_temp string
  1161. if len(r_maps_sn1) > 0 {
  1162. start_temp = fmt.Sprintf("T1【%.1f℃】", r_maps_sn1[0].T_t)
  1163. end_temp = fmt.Sprintf("T1【%.1f℃】", r_maps_sn1[len(r_maps_sn1)-1].T_t)
  1164. }
  1165. if len(r_maps_sn2) > 0 {
  1166. start_temp += fmt.Sprintf(",T2【%.1f℃】", r_maps_sn2[0].T_t)
  1167. end_temp += fmt.Sprintf(",T2【%.1f℃】", r_maps_sn2[len(r_maps_sn2)-1].T_t)
  1168. }
  1169. pdf.Text(fmt.Sprintf("启运温度:%s", start_temp))
  1170. pdf.SetXY(240, y)
  1171. pdf.Text(fmt.Sprintf("到达温度:%s", end_temp))
  1172. var temp_range string
  1173. if len(DeviceSensor_data) > 0 {
  1174. temp_range = fmt.Sprintf("%.1f ~ %.1f℃", DeviceSensor_data[0].T_tl, DeviceSensor_data[0].T_tu)
  1175. }
  1176. pdf.SetXY(470, y)
  1177. pdf.Text(fmt.Sprintf("温度范围:%s", temp_range))
  1178. y += 15
  1179. pdf.SetXY(10, y)
  1180. T_forwarding_unit_temp := []rune(T_forwarding_unit)
  1181. if len(T_forwarding_unit_temp) > 34 {
  1182. err = errors.New("发货单位长度超过限制(34个字符)")
  1183. return
  1184. }
  1185. if len(T_forwarding_unit_temp) > 17 {
  1186. pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp[0:17])))
  1187. pdf.SetXY(60, y+15)
  1188. pdf.Text(fmt.Sprintf("%s", string(T_forwarding_unit_temp[17:])))
  1189. } else {
  1190. pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp)))
  1191. }
  1192. pdf.SetXY(240, y)
  1193. T_consignee_unit_temp := []rune(T_consignee_unit)
  1194. if len(T_consignee_unit_temp) > 34 {
  1195. err = errors.New("收货单位长度超过限制(34个字符)")
  1196. return
  1197. }
  1198. if len(T_consignee_unit_temp) > 17 {
  1199. pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp[0:17])))
  1200. pdf.SetXY(290, y+15)
  1201. pdf.Text(fmt.Sprintf("%s", string(T_consignee_unit_temp[17:])))
  1202. } else {
  1203. pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp)))
  1204. }
  1205. pdf.SetXY(470, y)
  1206. T_remark_temp := []rune(T_remark)
  1207. if len(T_remark_temp) > 34 {
  1208. err = errors.New("备注长度超过限制(16个字符)")
  1209. return
  1210. }
  1211. if len(T_remark) > 8 {
  1212. pdf.Text(fmt.Sprintf("备注:%s", string(T_remark_temp[0:8])))
  1213. pdf.SetXY(500, y+15)
  1214. pdf.Text(fmt.Sprintf("%s", string(T_remark_temp[8:])))
  1215. } else {
  1216. pdf.Text(fmt.Sprintf("备注: %s", string(T_remark_temp)))
  1217. }
  1218. y += 35
  1219. pdf.SetFont("wts", "", 16)
  1220. pdf.SetXY(10, y)
  1221. pdf.Text("记录曲线信息")
  1222. // 线
  1223. y += 10
  1224. pdf.SetLineWidth(0.5)
  1225. pdf.SetStrokeColor(169, 169, 169)
  1226. pdf.Line(10, y, 585, y)
  1227. y += 1
  1228. // ----------------绘制图片
  1229. //pdf.ImageFrom(img, 10, y, &gopdf.Rect{W: 575, H: 630})
  1230. imgH, err := gopdf.ImageHolderByBytes(imageBytes)
  1231. if T_temp_show == 1 && T_Humidity_show == 1 {
  1232. //pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 630})
  1233. pdf.ImageByHolder(imgH, 32, y, &gopdf.Rect{W: 522, H: 572})
  1234. y += 562
  1235. } else {
  1236. pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 315})
  1237. y += 315
  1238. }
  1239. pdf.SetFont("wts", "", 10)
  1240. pages := pdf.GetNumberOfPages()
  1241. pagenums := fmt.Sprintf("第 %d 页", pages)
  1242. pagenum, _ := pdf.MeasureTextWidth(pagenums)
  1243. pdf.SetX((595 / 2) - (pagenum / 2))
  1244. pdf.SetY(830) // 设置页码位置
  1245. pdf.Cell(nil, pagenums)
  1246. y += 35
  1247. if y > 841.89 {
  1248. // 图片结束直接分页
  1249. pdf.AddPage()
  1250. y = 40
  1251. }
  1252. pdf.SetFont("wts", "", 16)
  1253. pdf.SetXY(10, y)
  1254. pdf.Text("记录数据信息")
  1255. // 线
  1256. y += 10
  1257. pdf.SetLineWidth(0.5)
  1258. pdf.SetStrokeColor(169, 169, 169)
  1259. pdf.Line(10, y, 585, y)
  1260. y += 10
  1261. pdf.SetFont("wts", "", 10)
  1262. if len(snidNum) == 1 {
  1263. var x float64 = 19
  1264. var w float64 = 112
  1265. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1266. x = x + w
  1267. w = 74
  1268. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1269. x = x + w
  1270. w = 110
  1271. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1272. x = x + w
  1273. w = 74
  1274. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1275. x = x + w
  1276. w = 110
  1277. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1278. x = x + w
  1279. w = 74
  1280. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1281. count := 0
  1282. for i, v := range Pdf_data {
  1283. //y = pdf.GetY()
  1284. //if y <= 20 {
  1285. // var x float64 = 19
  1286. // var w float64 = 112
  1287. // fmt.Println(pdf.GetY())
  1288. // lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1289. // x = x + w
  1290. // w = 74
  1291. // lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1292. // x = x + w
  1293. // w = 110
  1294. // lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1295. // x = x + w
  1296. // w = 74
  1297. // lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1298. // x = x + w
  1299. // w = 110
  1300. // lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1301. // x = x + w
  1302. // w = 74
  1303. // lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1304. // y += 20
  1305. //}
  1306. if i%3 == 0 {
  1307. y += 20
  1308. x, w = 19, 112
  1309. var textH float64 = 25 // if text height is 25px.
  1310. pdf.SetNewY(y, textH)
  1311. y = pdf.GetY()
  1312. if y <= 20 {
  1313. var x float64 = 19
  1314. var w float64 = 112
  1315. fmt.Println(pdf.GetY())
  1316. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1317. x = x + w
  1318. w = 74
  1319. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1320. x = x + w
  1321. w = 110
  1322. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1323. x = x + w
  1324. w = 74
  1325. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1326. x = x + w
  1327. w = 110
  1328. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1329. x = x + w
  1330. w = 74
  1331. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1332. y += 20
  1333. }
  1334. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1335. x = x + w
  1336. w = 74
  1337. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1338. }
  1339. if i%3 == 1 {
  1340. x = x + w
  1341. w = 110
  1342. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1343. x = x + w
  1344. w = 74
  1345. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1346. }
  1347. if i%3 == 2 {
  1348. x = x + w
  1349. w = 110
  1350. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1351. x = x + w
  1352. w = 74
  1353. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1354. }
  1355. if pdf.GetY() >= 804 {
  1356. count++
  1357. if count == 3 {
  1358. count = 0
  1359. pages := pdf.GetNumberOfPages()
  1360. pagenums := fmt.Sprintf("第 %d 页", pages)
  1361. pagenum, _ := pdf.MeasureTextWidth(pagenums)
  1362. pdf.SetX((595 / 2) - (pagenum / 2))
  1363. pdf.SetY(830) // 设置页码位置
  1364. pdf.Cell(nil, pagenums)
  1365. }
  1366. }
  1367. }
  1368. pages := pdf.GetNumberOfPages()
  1369. pagenums := fmt.Sprintf("第 %d 页", pages)
  1370. pagenum, _ := pdf.MeasureTextWidth(pagenums)
  1371. pdf.SetX((595 / 2) - (pagenum / 2))
  1372. pdf.SetY(830) // 设置页码位置
  1373. pdf.Cell(nil, pagenums)
  1374. }
  1375. if len(snidNum) == 2 {
  1376. var x float64 = 19
  1377. var w float64 = 112
  1378. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1379. x = x + w
  1380. w = 37
  1381. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1382. x = x + w
  1383. lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1384. x = x + w
  1385. w = 112
  1386. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1387. x = x + w
  1388. w = 37
  1389. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1390. x = x + w
  1391. lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1392. x = x + w
  1393. w = 112
  1394. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1395. x = x + w
  1396. w = 37
  1397. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1398. x = x + w
  1399. lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1400. count := 0
  1401. for i, v := range Pdf_data {
  1402. if i%3 == 0 {
  1403. y += 20
  1404. var textH float64 = 25 // if text height is 25px.
  1405. pdf.SetNewY(y, textH)
  1406. y = pdf.GetY()
  1407. if y <= 20 {
  1408. var x float64 = 19
  1409. var w float64 = 112
  1410. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1411. x = x + w
  1412. w = 37
  1413. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1414. x = x + w
  1415. lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1416. x = x + w
  1417. w = 112
  1418. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1419. x = x + w
  1420. w = 37
  1421. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1422. x = x + w
  1423. lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1424. x = x + w
  1425. w = 112
  1426. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1427. x = x + w
  1428. w = 37
  1429. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1430. x = x + w
  1431. lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1432. y += 20
  1433. }
  1434. x, w = 19, 112
  1435. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1436. x = x + w
  1437. w = 37
  1438. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1439. x = x + w
  1440. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1441. }
  1442. if i%3 == 1 {
  1443. x = x + w
  1444. w = 112
  1445. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1446. x = x + w
  1447. w = 37
  1448. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1449. x = x + w
  1450. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1451. }
  1452. if i%3 == 2 {
  1453. x = x + w
  1454. w = 112
  1455. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1456. x = x + w
  1457. w = 37
  1458. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1459. x = x + w
  1460. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1461. }
  1462. if pdf.GetY() >= 804 {
  1463. count++
  1464. if count == 3 {
  1465. count = 0
  1466. pages := pdf.GetNumberOfPages()
  1467. pagenums := fmt.Sprintf("第 %d 页", pages)
  1468. pagenum, _ := pdf.MeasureTextWidth(pagenums)
  1469. pdf.SetX((595 / 2) - (pagenum / 2))
  1470. pdf.SetY(830) // 设置页码位置
  1471. pdf.Cell(nil, pagenums)
  1472. }
  1473. }
  1474. }
  1475. pages := pdf.GetNumberOfPages()
  1476. pagenums := fmt.Sprintf("第 %d 页", pages)
  1477. pagenum, _ := pdf.MeasureTextWidth(pagenums)
  1478. pdf.SetX((595 / 2) - (pagenum / 2))
  1479. pdf.SetY(830) // 设置页码位置
  1480. pdf.Cell(nil, pagenums)
  1481. }
  1482. timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
  1483. err = pdf.WritePdf(timeStr)
  1484. if err != nil {
  1485. return
  1486. }
  1487. // 上传 OSS
  1488. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+timeStr, timeStr)
  1489. if !is {
  1490. err = errors.New("oss!")
  1491. return
  1492. }
  1493. //删除目录
  1494. err = os.Remove(timeStr)
  1495. if err != nil {
  1496. logs.Error(err)
  1497. }
  1498. return url, nil
  1499. }
  1500. // 图表展示 下载PDF 库房
  1501. func Device_Sensor_Data_ChartShow_PDF0(companyName, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark string, imageBytes []byte,
  1502. T_temp_show, T_Humidity_show int) (url string, err error) {
  1503. sn, ids, isSameSn := lib.IsSNAllSame(T_snid)
  1504. if !isSameSn {
  1505. err = errors.New("请选择相同设备编号的探头导出!")
  1506. return
  1507. }
  1508. device, err := Device.Read_Device_ByT_sn(sn)
  1509. if err != nil {
  1510. err = errors.New("查询主机信息失败!")
  1511. return
  1512. }
  1513. var DeviceSensor_data []Device.DeviceData_R
  1514. Device.Read_DeviceSensorParameter_All_Map(sn, 0)
  1515. // -------------------获取最高温湿度、温蒂温湿度、平均温湿度
  1516. //DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  1517. DeviceSensor_data, _ = Device.Read_DeviceData_ByIds(sn, ids, Time_start, Time_end, 0, 9999)
  1518. // 根据传感器T_sort排序
  1519. //dsList := Device.Read_DeviceSensorList_By_T_snid(T_snid)
  1520. //// 创建名称到权重的映射
  1521. //orderMap := make(map[int]int)
  1522. //for i, v := range dsList {
  1523. // orderMap[v.T_id] = i
  1524. //}
  1525. //sort.Slice(DeviceSensor_data, func(i, j int) bool {
  1526. // if DeviceSensor_data[i].T_time == DeviceSensor_data[j].T_time {
  1527. // // 如果时间相同,则按预设顺序排序
  1528. // return orderMap[DeviceSensor_data[i].T_id] < orderMap[DeviceSensor_data[j].T_id]
  1529. // }
  1530. // return DeviceSensor_data[i].T_time > DeviceSensor_data[j].T_time
  1531. //})
  1532. // 最高温度、最低温度、最高湿度、最低湿度
  1533. var maxTemp, minTemp, maxHumidity, minHumidity float32
  1534. // 最高温度时间、最低温度时间、最高湿度时间、最低湿度时间
  1535. var maxTempTime, minTempTime, maxHumidityTime, minHumidityTime string
  1536. // 总温度 总湿度
  1537. var totalTemp, totalHumidity float32
  1538. // 平均温度 平均湿度
  1539. var avgTemp, avgHumidity float32
  1540. // 温度阈值,湿度阈值
  1541. var tempThreshold, humidityThreshold string
  1542. // 记录开始时间,记录结束时间
  1543. var s_time, e_time string
  1544. var lastTime string
  1545. var isFirst, isSecond = true, false
  1546. var first_column, second_column []Device.DeviceData_R
  1547. if len(DeviceSensor_data) > 0 {
  1548. tempThreshold = fmt.Sprintf("%.1f-%.1f", DeviceSensor_data[0].T_tl, DeviceSensor_data[0].T_tu)
  1549. humidityThreshold = fmt.Sprintf("%.1f-%.1f", DeviceSensor_data[0].T_rhl, DeviceSensor_data[0].T_rhu)
  1550. e_time = DeviceSensor_data[0].T_time
  1551. s_time = DeviceSensor_data[len(DeviceSensor_data)-1].T_time
  1552. // 最高温度及时刻
  1553. maxTemp = DeviceSensor_data[0].T_t
  1554. maxTempTime = DeviceSensor_data[0].T_time
  1555. // 最低温度及时刻
  1556. minTemp = DeviceSensor_data[0].T_t
  1557. minTempTime = DeviceSensor_data[0].T_time
  1558. // 最高湿度及时刻
  1559. maxHumidity = DeviceSensor_data[0].T_rh
  1560. maxHumidityTime = DeviceSensor_data[0].T_time
  1561. // 获取最低湿度及时刻
  1562. minHumidity = DeviceSensor_data[0].T_rh
  1563. minHumidityTime = DeviceSensor_data[0].T_time
  1564. for i := 0; i < len(DeviceSensor_data); i++ {
  1565. data := DeviceSensor_data[i]
  1566. if data.T_t > maxTemp {
  1567. maxTemp = data.T_t
  1568. maxTempTime = data.T_time
  1569. }
  1570. if data.T_t < minTemp {
  1571. minTemp = data.T_t
  1572. minTempTime = data.T_time
  1573. }
  1574. totalTemp += data.T_t
  1575. if data.T_rh > maxHumidity {
  1576. maxHumidity = data.T_rh
  1577. maxHumidityTime = data.T_time
  1578. }
  1579. if data.T_rh < minHumidity {
  1580. minHumidity = data.T_rh
  1581. minHumidityTime = data.T_time
  1582. }
  1583. totalHumidity += data.T_rh
  1584. if len(lastTime) > 0 {
  1585. if lastTime != data.T_time && isFirst == true {
  1586. isFirst = false
  1587. isSecond = true
  1588. } else if lastTime != data.T_time && isSecond == true {
  1589. isFirst = true
  1590. isSecond = false
  1591. }
  1592. }
  1593. if isFirst {
  1594. first_column = append(first_column, data)
  1595. lastTime = data.T_time
  1596. }
  1597. if isSecond {
  1598. second_column = append(second_column, data)
  1599. lastTime = data.T_time
  1600. }
  1601. }
  1602. // 平均温度
  1603. avgTemp = totalTemp / float32(len(DeviceSensor_data))
  1604. // 平均湿度
  1605. avgHumidity = totalHumidity / float32(len(DeviceSensor_data))
  1606. }
  1607. // -------------------获取最高温湿度、温蒂温湿度、平均温湿度结束
  1608. pdf := &gopdf.GoPdf{}
  1609. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  1610. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  1611. if err != nil {
  1612. return
  1613. }
  1614. err = pdf.SetFont("wts", "", 20)
  1615. if err != nil {
  1616. return
  1617. }
  1618. pdf.SetGrayFill(0.5)
  1619. pdf.SetMargins(0, 20, 0, 20)
  1620. pdf.AddPage()
  1621. title := companyName + "温湿度记录"
  1622. var y float64 = 40
  1623. textw, _ := pdf.MeasureTextWidth(title)
  1624. pdf.SetX((595 / 2) - (textw / 2))
  1625. pdf.SetY(y)
  1626. pdf.Text(title)
  1627. y += 30
  1628. pdf.SetFont("wts", "", 16)
  1629. pdf.SetXY(10, y)
  1630. pdf.Text("实施设备信息")
  1631. // 线
  1632. y += 10
  1633. pdf.SetLineWidth(0.5)
  1634. pdf.SetStrokeColor(169, 169, 169)
  1635. pdf.Line(10, y, 585, y)
  1636. pdf.SetFont("wts", "", 10)
  1637. y += 20
  1638. pdf.SetXY(10, y)
  1639. pdf.Text(fmt.Sprintf("主机名称:%s", device.T_devName))
  1640. pdf.SetXY(300, y)
  1641. pdf.Text(fmt.Sprintf("主机编号:%s", device.T_sn))
  1642. y += 35
  1643. pdf.SetFont("wts", "", 16)
  1644. pdf.SetXY(10, y)
  1645. pdf.Text("记录概要信息")
  1646. // 线
  1647. y += 10
  1648. pdf.SetLineWidth(0.5)
  1649. pdf.SetStrokeColor(169, 169, 169)
  1650. pdf.Line(10, y, 585, y)
  1651. y += 20
  1652. pdf.SetFont("wts", "", 10)
  1653. pdf.SetXY(10, y)
  1654. pdf.Text(fmt.Sprintf("记录开始时间:%s", s_time))
  1655. pdf.SetXY(240, y)
  1656. pdf.Text(fmt.Sprintf("记录结束时间:%s", e_time))
  1657. sTime, _ := lib.TimeStrToTime(s_time)
  1658. eTime, _ := lib.TimeStrToTime(e_time)
  1659. pdf.SetXY(470, y)
  1660. minutes := int(eTime.Sub(sTime).Minutes())
  1661. hours := minutes / 60
  1662. remainingMinutes := minutes % 60
  1663. pdf.Text(fmt.Sprintf("记录总时间:%dh%dmin", hours, remainingMinutes))
  1664. // -------------最高温/湿度 最低温/湿度 平均温/湿度
  1665. if T_temp_show == 1 {
  1666. y += 15
  1667. pdf.SetXY(10, y)
  1668. pdf.Text(fmt.Sprintf("最高温度:%.1f℃,%s", lib.RoundToDecimal(float64(maxTemp), 1), maxTempTime))
  1669. pdf.SetXY(240, y)
  1670. pdf.Text(fmt.Sprintf("最低温度:%.1f℃,%s", lib.RoundToDecimal(float64(minTemp), 1), minTempTime))
  1671. pdf.SetXY(470, y)
  1672. pdf.Text(fmt.Sprintf("平均温度:%.1f℃", lib.RoundToDecimal(float64(avgTemp), 1)))
  1673. }
  1674. if T_Humidity_show == 1 {
  1675. y += 15
  1676. pdf.SetXY(10, y)
  1677. pdf.Text(fmt.Sprintf("最高湿度:%.1f%%RH,%s", lib.RoundToDecimal(float64(maxHumidity), 1), maxHumidityTime))
  1678. pdf.SetXY(240, y)
  1679. pdf.Text(fmt.Sprintf("最低湿度:%.1f%%RH,%s", lib.RoundToDecimal(float64(minHumidity), 1), minHumidityTime))
  1680. pdf.SetXY(470, y)
  1681. pdf.Text(fmt.Sprintf("平均湿度:%.1f%%RH", lib.RoundToDecimal(float64(avgHumidity), 1)))
  1682. }
  1683. // -------------温/湿度阈值
  1684. if T_temp_show == 1 {
  1685. y += 15
  1686. pdf.SetXY(10, y)
  1687. pdf.Text(fmt.Sprintf("温度阈值:%s℃", tempThreshold))
  1688. }
  1689. if T_Humidity_show == 1 {
  1690. if T_temp_show != 1 {
  1691. y += 15
  1692. }
  1693. pdf.SetXY(240, y)
  1694. pdf.Text(fmt.Sprintf("湿度阈值:%s%%", humidityThreshold))
  1695. }
  1696. //-------------发货单位,收货单位,备注
  1697. y += 15
  1698. pdf.SetXY(10, y)
  1699. T_forwarding_unit_temp := []rune(T_forwarding_unit)
  1700. if len(T_forwarding_unit_temp) > 34 {
  1701. err = errors.New("发货单位长度超过限制(34个字符)")
  1702. return
  1703. }
  1704. if len(T_forwarding_unit_temp) > 17 {
  1705. pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp[0:17])))
  1706. pdf.SetXY(60, y+15)
  1707. pdf.Text(fmt.Sprintf("%s", string(T_forwarding_unit_temp[17:])))
  1708. } else {
  1709. pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp)))
  1710. }
  1711. pdf.SetXY(240, y)
  1712. T_consignee_unit_temp := []rune(T_consignee_unit)
  1713. if len(T_consignee_unit_temp) > 34 {
  1714. err = errors.New("收货单位长度超过限制(34个字符)")
  1715. return
  1716. }
  1717. if len(T_consignee_unit_temp) > 17 {
  1718. pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp[0:17])))
  1719. pdf.SetXY(290, y+15)
  1720. pdf.Text(fmt.Sprintf("%s", string(T_consignee_unit_temp[17:])))
  1721. } else {
  1722. pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp)))
  1723. }
  1724. pdf.SetXY(470, y)
  1725. T_remark_temp := []rune(T_remark)
  1726. if len(T_remark_temp) > 34 {
  1727. err = errors.New("备注长度超过限制(16个字符)")
  1728. return
  1729. }
  1730. if len(T_remark) > 8 {
  1731. pdf.Text(fmt.Sprintf("备注:%s", string(T_remark_temp[0:8])))
  1732. pdf.SetXY(500, y+15)
  1733. pdf.Text(fmt.Sprintf("%s", string(T_remark_temp[8:])))
  1734. } else {
  1735. pdf.Text(fmt.Sprintf("备注: %s", string(T_remark_temp)))
  1736. }
  1737. y += 35
  1738. pdf.SetFont("wts", "", 16)
  1739. pdf.SetXY(10, y)
  1740. pdf.Text("记录曲线信息")
  1741. // 线
  1742. y += 10
  1743. pdf.SetLineWidth(0.5)
  1744. pdf.SetStrokeColor(169, 169, 169)
  1745. pdf.Line(10, y, 585, y)
  1746. y += 1
  1747. // ----------------绘制图片
  1748. //pdf.ImageFrom(img, 10, y, &gopdf.Rect{W: 575, H: 630})
  1749. imgH, err := gopdf.ImageHolderByBytes(imageBytes)
  1750. if T_temp_show == 1 && T_Humidity_show == 1 {
  1751. //pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 630})
  1752. pdf.ImageByHolder(imgH, 32, y, &gopdf.Rect{W: 522, H: 572})
  1753. y += 562
  1754. } else {
  1755. pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 315})
  1756. y += 315
  1757. }
  1758. y += 35
  1759. pdf.SetFont("wts", "", 10)
  1760. pages := pdf.GetNumberOfPages()
  1761. pagenums := fmt.Sprintf("第 %d 页", pages)
  1762. pagenum, _ := pdf.MeasureTextWidth(pagenums)
  1763. pdf.SetX((595 / 2) - (pagenum / 2))
  1764. pdf.SetY(830) // 设置页码位置
  1765. pdf.Cell(nil, pagenums)
  1766. if y > 841.89 {
  1767. // 图片结束直接分页
  1768. pdf.AddPage()
  1769. y = 40
  1770. }
  1771. pdf.SetFont("wts", "", 16)
  1772. pdf.SetXY(10, y)
  1773. pdf.Text("记录数据信息")
  1774. // 线
  1775. y += 10
  1776. pdf.SetLineWidth(0.5)
  1777. pdf.SetStrokeColor(169, 169, 169)
  1778. pdf.Line(10, y, 585, y)
  1779. y += 10
  1780. pdf.SetFont("wts", "", 10)
  1781. var x float64 = 10
  1782. var w float64 = 112
  1783. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1784. x = x + w
  1785. w = 101
  1786. lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1787. if T_temp_show == 1 && T_Humidity_show == 1 {
  1788. x = x + w
  1789. w = 37
  1790. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1791. x = x + w
  1792. w = 37
  1793. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1794. }
  1795. if T_temp_show == 1 && T_Humidity_show == 0 {
  1796. x = x + w
  1797. w = 74
  1798. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1799. }
  1800. if T_temp_show == 0 && T_Humidity_show == 1 {
  1801. x = x + w
  1802. w = 74
  1803. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1804. }
  1805. x = x + w
  1806. w = 112
  1807. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1808. x = x + w
  1809. w = 101
  1810. lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1811. if T_temp_show == 1 && T_Humidity_show == 1 {
  1812. x = x + w
  1813. w = 37
  1814. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1815. x = x + w
  1816. w = 37
  1817. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1818. }
  1819. if T_temp_show == 1 && T_Humidity_show == 0 {
  1820. x = x + w
  1821. w = 74
  1822. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1823. }
  1824. if T_temp_show == 0 && T_Humidity_show == 1 {
  1825. x = x + w
  1826. w = 74
  1827. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1828. }
  1829. y += 20
  1830. var textH float64 = 25 // if text height is 25px.
  1831. for i, v := range first_column {
  1832. pdf.SetNewY(y, textH)
  1833. y = pdf.GetY()
  1834. if y <= 20 {
  1835. pages := pdf.GetNumberOfPages()
  1836. pagenums := fmt.Sprintf("第 %d 页", pages)
  1837. pagenum, _ := pdf.MeasureTextWidth(pagenums)
  1838. pdf.SetX((595 / 2) - (pagenum / 2))
  1839. pdf.SetY(830) // 设置页码位置
  1840. pdf.Cell(nil, pagenums)
  1841. var x float64 = 10
  1842. var w float64 = 112
  1843. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1844. x = x + w
  1845. w = 101
  1846. lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1847. if T_temp_show == 1 && T_Humidity_show == 1 {
  1848. x = x + w
  1849. w = 37
  1850. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1851. x = x + w
  1852. w = 37
  1853. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1854. }
  1855. if T_temp_show == 1 && T_Humidity_show == 0 {
  1856. x = x + w
  1857. w = 74
  1858. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1859. }
  1860. if T_temp_show == 0 && T_Humidity_show == 1 {
  1861. x = x + w
  1862. w = 74
  1863. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1864. }
  1865. x = x + w
  1866. w = 112
  1867. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1868. x = x + w
  1869. w = 101
  1870. lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1871. if T_temp_show == 1 && T_Humidity_show == 1 {
  1872. x = x + w
  1873. w = 37
  1874. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1875. x = x + w
  1876. w = 37
  1877. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1878. }
  1879. if T_temp_show == 1 && T_Humidity_show == 0 {
  1880. x = x + w
  1881. w = 74
  1882. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1883. }
  1884. if T_temp_show == 0 && T_Humidity_show == 1 {
  1885. x = x + w
  1886. w = 74
  1887. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1888. }
  1889. y += 20
  1890. }
  1891. x, w = 10, 112
  1892. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1893. x = x + w
  1894. w = 101
  1895. lib.RectFillColor(pdf, v.T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1896. // 显示温湿度
  1897. if T_temp_show == 1 && T_Humidity_show == 1 {
  1898. x = x + w
  1899. w = 37
  1900. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1901. x = x + w
  1902. w = 37
  1903. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1904. }
  1905. // 只显示温度
  1906. if T_temp_show == 1 && T_Humidity_show == 0 {
  1907. x = x + w
  1908. w = 74
  1909. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1910. }
  1911. // 只显示湿度
  1912. if T_temp_show == 0 && T_Humidity_show == 1 {
  1913. x = x + w
  1914. w = 74
  1915. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1916. }
  1917. if i < len(second_column) {
  1918. x = x + w
  1919. w = 112
  1920. lib.RectFillColor(pdf, second_column[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1921. x = x + w
  1922. w = 101
  1923. lib.RectFillColor(pdf, second_column[i].T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1924. if T_temp_show == 1 && T_Humidity_show == 1 {
  1925. x = x + w
  1926. w = 37
  1927. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1928. x = x + w
  1929. w = 37
  1930. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1931. }
  1932. if T_temp_show == 1 && T_Humidity_show == 0 {
  1933. x = x + w
  1934. w = 74
  1935. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1936. }
  1937. if T_temp_show == 0 && T_Humidity_show == 1 {
  1938. x = x + w
  1939. w = 74
  1940. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1941. }
  1942. }
  1943. y += 20
  1944. if y >= 820 {
  1945. pages := pdf.GetNumberOfPages()
  1946. pagenums := fmt.Sprintf("第 %d 页", pages)
  1947. pagenum, _ := pdf.MeasureTextWidth(pagenums)
  1948. pdf.SetX((595 / 2) - (pagenum / 2))
  1949. pdf.SetY(830) // 设置页码位置
  1950. pdf.Cell(nil, pagenums)
  1951. }
  1952. }
  1953. if len(second_column) > len(first_column) {
  1954. for i := len(first_column); i < len(second_column); i++ {
  1955. pdf.SetNewY(y, textH)
  1956. y = pdf.GetY()
  1957. if y <= 20 {
  1958. pages := pdf.GetNumberOfPages()
  1959. pagenums := fmt.Sprintf("第 %d 页", pages)
  1960. pagenum, _ := pdf.MeasureTextWidth(pagenums)
  1961. pdf.SetX((595 / 2) - (pagenum / 2))
  1962. pdf.SetY(830) // 设置页码位置
  1963. pdf.Cell(nil, pagenums)
  1964. var x float64 = 10
  1965. var w float64 = 112
  1966. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1967. x = x + w
  1968. w = 101
  1969. lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1970. if T_temp_show == 1 && T_Humidity_show == 1 {
  1971. x = x + w
  1972. w = 37
  1973. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1974. x = x + w
  1975. w = 37
  1976. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1977. }
  1978. if T_temp_show == 1 && T_Humidity_show == 0 {
  1979. x = x + w
  1980. w = 74
  1981. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1982. }
  1983. if T_temp_show == 0 && T_Humidity_show == 1 {
  1984. x = x + w
  1985. w = 74
  1986. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1987. }
  1988. x = x + w
  1989. w = 112
  1990. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1991. x = x + w
  1992. w = 101
  1993. lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1994. if T_temp_show == 1 && T_Humidity_show == 1 {
  1995. x = x + w
  1996. w = 37
  1997. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1998. x = x + w
  1999. w = 37
  2000. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2001. }
  2002. if T_temp_show == 1 && T_Humidity_show == 0 {
  2003. x = x + w
  2004. w = 74
  2005. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2006. }
  2007. if T_temp_show == 0 && T_Humidity_show == 1 {
  2008. x = x + w
  2009. w = 74
  2010. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2011. }
  2012. y += 20
  2013. }
  2014. x, w = 297, 112
  2015. lib.RectFillColor(pdf, second_column[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2016. x = x + w
  2017. w = 101
  2018. lib.RectFillColor(pdf, second_column[i].T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2019. if T_temp_show == 1 && T_Humidity_show == 1 {
  2020. x = x + w
  2021. w = 37
  2022. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2023. x = x + w
  2024. w = 37
  2025. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2026. }
  2027. if T_temp_show == 1 && T_Humidity_show == 0 {
  2028. x = x + w
  2029. w = 74
  2030. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2031. }
  2032. if T_temp_show == 0 && T_Humidity_show == 1 {
  2033. x = x + w
  2034. w = 74
  2035. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2036. }
  2037. y += 20
  2038. if y >= 820 {
  2039. pages := pdf.GetNumberOfPages()
  2040. pagenums := fmt.Sprintf("第 %d 页", pages)
  2041. pagenum, _ := pdf.MeasureTextWidth(pagenums)
  2042. pdf.SetX((595 / 2) - (pagenum / 2))
  2043. pdf.SetY(830) // 设置页码位置
  2044. pdf.Cell(nil, pagenums)
  2045. }
  2046. }
  2047. }
  2048. timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
  2049. err = pdf.WritePdf(timeStr)
  2050. if err != nil {
  2051. return
  2052. }
  2053. // 上传 OSS
  2054. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+timeStr, timeStr)
  2055. if !is {
  2056. err = errors.New("oss!")
  2057. return
  2058. }
  2059. //删除目录
  2060. err = os.Remove(timeStr)
  2061. if err != nil {
  2062. logs.Error(err)
  2063. }
  2064. return url, nil
  2065. }
  2066. // 数据备份
  2067. func (c *DataController) Device_Sensor_Data_BackUp() {
  2068. page, _ := c.GetInt("page")
  2069. if page < 1 {
  2070. page = 1
  2071. }
  2072. page_z, _ := c.GetInt("page_z")
  2073. if page_z < 1 {
  2074. page_z = conf.Page_size
  2075. }
  2076. T_snid := c.GetString("T_snid")
  2077. Time_start := c.GetString("Time_start")
  2078. Time_end := c.GetString("Time_end")
  2079. if len(T_snid) < 10 {
  2080. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_snid Err!"}
  2081. c.ServeJSON()
  2082. return
  2083. }
  2084. if _, is := lib.DateStrToDate(Time_start); !is {
  2085. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2086. c.ServeJSON()
  2087. return
  2088. }
  2089. if _, is := lib.DateStrToDate(Time_end); !is {
  2090. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2091. c.ServeJSON()
  2092. return
  2093. }
  2094. Time_start += " 00:00:00"
  2095. Time_end += " 23:59:59"
  2096. var r_jsons lib.R_JSONS
  2097. r_jsons.Data, r_jsons.Num = Device.Read_DeviceData_BackUp_List(T_snid, Time_start, Time_end, page, page_z)
  2098. r_jsons.Page = page
  2099. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  2100. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2101. c.ServeJSON()
  2102. return
  2103. }
  2104. // 数据备份pdf
  2105. func (c *DataController) Device_Sensor_Data_BackUp_PDF() {
  2106. T_sn := c.GetString("T_sn")
  2107. T_id, _ := c.GetInt("T_id")
  2108. T_date := c.GetString("T_date")
  2109. T_snid := fmt.Sprintf("%s,%d", T_sn, T_id)
  2110. date, is := lib.DateStrToDate(T_date)
  2111. if !is {
  2112. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2113. c.ServeJSON()
  2114. return
  2115. }
  2116. Time_start := T_date + " 00:00:00"
  2117. Time_end := T_date + " 23:59:59"
  2118. var DeviceSensor_data []Device.DeviceData_R
  2119. DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  2120. var err error
  2121. pdf := &gopdf.GoPdf{}
  2122. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  2123. //err = GetFont(pdf, "LiberationSerif-Regular.ttf")
  2124. //if err != nil {
  2125. // log.Fatalln(err)
  2126. //}
  2127. //err = pdf.SetFont("Ubuntu-L", "", 14)
  2128. //if err != nil {
  2129. // log.Fatalln(err)
  2130. //}
  2131. err = pdf.AddTTFFont("simsun", "static/fonts/三极行楷简体-粗.ttf")
  2132. if err != nil {
  2133. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  2134. c.ServeJSON()
  2135. return
  2136. }
  2137. err = pdf.SetFont("simsun", "", 24)
  2138. if err != nil {
  2139. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  2140. c.ServeJSON()
  2141. return
  2142. }
  2143. pdf.SetGrayFill(0.5)
  2144. pdf.SetMargins(0, 20, 0, 20)
  2145. pdf.AddPage()
  2146. //use path
  2147. //pdf.Image("logo.png", 100, 50, &gopdf.Rect{W: 50, H: 50})
  2148. company, _ := Account.Read_Company_ById(c.T_pid)
  2149. textw, _ := pdf.MeasureTextWidth(company.T_name)
  2150. pdf.SetX((595 / 2) - (textw / 2))
  2151. pdf.SetY(40)
  2152. pdf.Text(company.T_name)
  2153. // 线
  2154. pdf.SetLineWidth(2)
  2155. pdf.SetLineType("dashed")
  2156. pdf.Line(10, 60, 585, 60)
  2157. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  2158. if err != nil {
  2159. c.Data["json"] = lib.JSONS{Code: 202, Msg: "ok!", Data: err}
  2160. c.ServeJSON()
  2161. return
  2162. }
  2163. err = pdf.SetFont("wts", "", 12)
  2164. if err != nil {
  2165. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  2166. c.ServeJSON()
  2167. return
  2168. }
  2169. var s_time, e_time string
  2170. if len(DeviceSensor_data) > 0 {
  2171. s_time = fmt.Sprintf("%s", DeviceSensor_data[0].T_time)
  2172. e_time = fmt.Sprintf("%s", DeviceSensor_data[len(DeviceSensor_data)-1].T_time)
  2173. }
  2174. //fmt.Sprintf(" %.1f ", v.T_t)
  2175. lib.RectFillColor(pdf, "历史数据["+e_time+" / "+s_time+"]", 14, 22, 80, 550, 40, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2176. lib.RectFillColor(pdf, "序号", 12, 22, 120, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2177. lib.RectFillColor(pdf, "传感器名称", 12, 52, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2178. lib.RectFillColor(pdf, "温度℃", 12, 152, 120, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2179. lib.RectFillColor(pdf, "湿度%", 12, 212, 120, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2180. lib.RectFillColor(pdf, "温度范围", 12, 272, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2181. lib.RectFillColor(pdf, "湿度范围", 12, 362, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2182. lib.RectFillColor(pdf, "记录时间", 12, 452, 120, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2183. err = pdf.SetFont("wts", "", 10)
  2184. if err != nil {
  2185. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  2186. c.ServeJSON()
  2187. return
  2188. }
  2189. //pages := pdf.GetNumberOfPages()
  2190. //pagenums := fmt.Sprintf("第 %d 页", pages)
  2191. //pagenum, _ := pdf.MeasureTextWidth(pagenums)
  2192. //pdf.SetX((595 / 2) - (pagenum / 2))
  2193. //pdf.SetY(830) // 设置页码位置
  2194. //pdf.Cell(nil, pagenums)
  2195. var y float64 = 140
  2196. err = pdf.SetFont("wts", "", 10)
  2197. if err != nil {
  2198. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  2199. c.ServeJSON()
  2200. return
  2201. }
  2202. for i, v := range DeviceSensor_data {
  2203. text := fmt.Sprintf(" %d ", i+1)
  2204. var textH float64 = 25 // if text height is 25px.
  2205. pdf.SetNewY(y, textH)
  2206. y = pdf.GetY()
  2207. //pdf.SetX(x) // must after pdf.SetNewY() called.
  2208. //err = pdf.Text(text)
  2209. //if err != nil {
  2210. // log.Fatalln(err)
  2211. //}
  2212. pages := pdf.GetNumberOfPages()
  2213. pagenums := fmt.Sprintf("第 %d 页", pages)
  2214. pagenum, _ := pdf.MeasureTextWidth(pagenums)
  2215. pdf.SetX((595 / 2) - (pagenum / 2))
  2216. pdf.SetY(830) // 设置页码位置
  2217. pdf.Cell(nil, pagenums)
  2218. T_t := fmt.Sprintf(" %.1f ", v.T_t)
  2219. T_rh := fmt.Sprintf(" %.1f ", v.T_rh)
  2220. T_Tlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_tl, v.T_tu)
  2221. T_Rlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_rhl, v.T_rhu)
  2222. T_time := fmt.Sprintf("%s", v.T_time)
  2223. //if user_r.Admin_rh == 0 && strings.Contains(v.T_sn, "YD") {
  2224. // T_rh = "-"
  2225. // T_Rlu = "-"
  2226. //}
  2227. lib.RectFillColor(pdf, text, 10, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2228. lib.RectFillColor(pdf, v.T_name, 10, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2229. lib.RectFillColor(pdf, T_t, 10, 152, y, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2230. lib.RectFillColor(pdf, T_rh, 10, 212, y, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2231. lib.RectFillColor(pdf, T_Tlu, 10, 272, y, 90, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2232. lib.RectFillColor(pdf, T_Rlu, 10, 362, y, 90, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2233. lib.RectFillColor(pdf, T_time, 10, 452, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  2234. y += 20
  2235. }
  2236. deviceSensor, _ := Device.Read_DeviceSensor_ByT_sn(T_sn, T_id)
  2237. rand_x := int64(lib.Random(0, 10000))
  2238. filenameStr := fmt.Sprintf("backup/sn/%s_%s(%s).pdf", lib.GetRandstring(8, "0123456789", rand_x), deviceSensor.T_name, date.Format("2006-01-02"))
  2239. err = pdf.WritePdf(filenameStr)
  2240. if err != nil {
  2241. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  2242. c.ServeJSON()
  2243. return
  2244. }
  2245. // 上传 OSS
  2246. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+filenameStr, filenameStr)
  2247. if !is {
  2248. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  2249. c.ServeJSON()
  2250. return
  2251. }
  2252. //删除目录
  2253. err = os.Remove(filenameStr)
  2254. if err != nil {
  2255. logs.Error(lib.FuncName(), err)
  2256. }
  2257. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  2258. c.ServeJSON()
  2259. return
  2260. }
  2261. // 对接电信实时数据
  2262. func (c *DataController) Docking_Real_Data() {
  2263. key := c.GetString("T_key")
  2264. // 查询公司
  2265. Company_r, err := Account.Read_Company_ByKey(key)
  2266. if err != nil {
  2267. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_key Err!"}
  2268. c.ServeJSON()
  2269. return
  2270. }
  2271. // 查询公司下面所有子公司id
  2272. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  2273. type R_JSONS struct {
  2274. //必须的大写开头
  2275. Data []Device.DeviceData_Docking
  2276. }
  2277. var r_jsons R_JSONS
  2278. var tempData []Device.DeviceData_Docking
  2279. tempData = Device.Read_Docking_Real_Data(T_pids)
  2280. for _, v := range tempData {
  2281. t, b := Device.Read_DeviceSensorParameter(v.T_sn, v.T_id)
  2282. if !b {
  2283. continue
  2284. }
  2285. if t.T_free == 1 {
  2286. continue
  2287. }
  2288. r_jsons.Data = append(r_jsons.Data, v)
  2289. }
  2290. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2291. c.ServeJSON()
  2292. return
  2293. }
  2294. // 对接电信历史数据
  2295. func (c *DataController) Docking_Note_Data() {
  2296. key := c.GetString("T_key")
  2297. // 查询公司,验证T_key
  2298. _, err := Account.Read_Company_ByKey(key)
  2299. if err != nil {
  2300. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_key Err!"}
  2301. c.ServeJSON()
  2302. return
  2303. }
  2304. page, _ := c.GetInt("page")
  2305. if page < 1 {
  2306. page = 1
  2307. }
  2308. page_z, _ := c.GetInt("page_z")
  2309. if page_z < 1 {
  2310. page_z = conf.Page_size
  2311. }
  2312. if page_z > 2000 {
  2313. c.Data["json"] = lib.JSONS{Code: 202, Msg: "page_z 超出限制"}
  2314. c.ServeJSON()
  2315. return
  2316. }
  2317. T_sn := c.GetString("T_sn")
  2318. T_id, T_id_Err := c.GetInt("T_id")
  2319. Time_start := c.GetString("Time_start")
  2320. Time_end := c.GetString("Time_end")
  2321. if len(T_sn) == 0 {
  2322. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn 格式错误"}
  2323. c.ServeJSON()
  2324. return
  2325. }
  2326. if T_id_Err != nil {
  2327. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id 格式错误"}
  2328. c.ServeJSON()
  2329. return
  2330. }
  2331. st, is := lib.TimeStrToTime(Time_start)
  2332. if !is {
  2333. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Time_start 格式不正确"}
  2334. c.ServeJSON()
  2335. return
  2336. }
  2337. if st.Add(40 * 24 * time.Hour).Before(time.Now()) {
  2338. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询时间范围超出限制(40天)"}
  2339. c.ServeJSON()
  2340. return
  2341. }
  2342. et, is := lib.TimeStrToTime(Time_end)
  2343. if !is {
  2344. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Time_start 格式不正确"}
  2345. c.ServeJSON()
  2346. return
  2347. }
  2348. if et.Add(40 * 24 * time.Hour).Before(time.Now()) {
  2349. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询时间范围超出限制(40天)"}
  2350. c.ServeJSON()
  2351. return
  2352. }
  2353. type R_JSONS struct {
  2354. //必须的大写开头
  2355. DeviceSensor_data []Device.DeviceData_R
  2356. Num int
  2357. Page int
  2358. Page_size int
  2359. Pages []lib.Page_T
  2360. }
  2361. var r_jsons R_JSONS
  2362. var cnt int
  2363. Device.Read_DeviceSensorParameter_All_Map(T_sn, T_id)
  2364. var tempData []Device.DeviceData_R
  2365. tempData, cnt = Device.Read_DeviceData_ById_List(T_sn, T_id, Time_start, Time_end, page, page_z)
  2366. page_size := math.Ceil(float64(cnt) / float64(page_z))
  2367. r_jsons.Page = page
  2368. r_jsons.Page_size = int(page_size)
  2369. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  2370. r_jsons.Num = cnt
  2371. for _, v := range tempData {
  2372. if v.T_free == 1 {
  2373. continue
  2374. }
  2375. r_jsons.DeviceSensor_data = append(r_jsons.DeviceSensor_data, v)
  2376. }
  2377. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2378. c.ServeJSON()
  2379. return
  2380. }
  2381. // 保温箱实时轨迹
  2382. func (c *DataController) GetNewLocus() {
  2383. // 设置响应头
  2384. c.Ctx.ResponseWriter.Header().Set("Content-Type", "text/event-stream")
  2385. c.Ctx.ResponseWriter.Header().Set("Cache-Control", "no-cache")
  2386. c.Ctx.ResponseWriter.Header().Set("Connection", "keep-alive")
  2387. sn := c.GetString("sn")
  2388. t_id := c.GetString("t_id")
  2389. tid, _ := strconv.Atoi(t_id)
  2390. conn := true
  2391. // 监听客户端断开连接
  2392. notify := c.Ctx.ResponseWriter.CloseNotify()
  2393. type Response struct {
  2394. RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"`
  2395. Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
  2396. Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
  2397. SN string `protobuf:"bytes,3,opt,name=sn,proto3" json:"sn,omitempty"`
  2398. CoolerName string `protobuf:"bytes,4,opt,name=CoolerName,proto3" json:"CoolerName,omitempty"`
  2399. Data []Device.DeviceData_R2 `json:"data"`
  2400. }
  2401. var response Response
  2402. list := make([]Device.DeviceData_, 0)
  2403. device, err := Device.Read_Device_ByT_sn(sn)
  2404. if err != nil {
  2405. logs.Error(lib.FuncName(), "设备不存在: %v", err)
  2406. response.Code = 500
  2407. response.Msg = "设备不存在"
  2408. response.Data = nil
  2409. response.SN = sn
  2410. response.CoolerName = ""
  2411. res, _ := json.Marshal(&response)
  2412. fmt.Fprintf(c.Ctx.ResponseWriter, "data: %s\n\n", string(res))
  2413. c.Ctx.ResponseWriter.Flush()
  2414. return
  2415. }
  2416. for conn {
  2417. select {
  2418. case <-notify:
  2419. fmt.Println("断开连接")
  2420. conn = false
  2421. return
  2422. default:
  2423. err := Device.GetNewLocus(sn, tid, &list)
  2424. if err != nil {
  2425. response.Code = 500
  2426. response.Msg = err.Error()
  2427. response.Data = nil
  2428. response.SN = sn
  2429. response.CoolerName = device.T_devName
  2430. res, _ := json.Marshal(&response)
  2431. fmt.Fprintf(c.Ctx.ResponseWriter, "data: %s\n\n", string(res))
  2432. c.Ctx.ResponseWriter.Flush()
  2433. time.Sleep(20 * time.Second)
  2434. logs.Error(lib.FuncName(), "获取轨迹失败: %v", err)
  2435. } else {
  2436. updatedLists := make([]Device.DeviceData_R2, 0)
  2437. for i := range list {
  2438. updatedLists = append(updatedLists, Device.DeviceData_R2{
  2439. DeviceData_: Device.DeviceData_{
  2440. T_id: list[i].T_id,
  2441. T_rh: list[i].T_rh,
  2442. T_site: list[i].T_site,
  2443. T_sp: list[i].T_sp,
  2444. T_time: list[i].T_time,
  2445. T_t: list[i].T_t,
  2446. },
  2447. Sn: sn,
  2448. T_name: device.T_devName,
  2449. Time: list[i].T_time.Format("2006-01-02 15:04:05"),
  2450. })
  2451. }
  2452. response.Code = 200
  2453. response.Msg = "查询成功"
  2454. response.Data = updatedLists
  2455. response.SN = sn
  2456. response.CoolerName = device.T_devName
  2457. res, _ := json.Marshal(&response)
  2458. fmt.Fprintf(c.Ctx.ResponseWriter, "data: %s\n\n", string(res))
  2459. c.Ctx.ResponseWriter.Flush()
  2460. time.Sleep(10 * time.Second)
  2461. }
  2462. }
  2463. }
  2464. }
  2465. var upgrader = websocket.Upgrader{
  2466. ReadBufferSize: 1024,
  2467. WriteBufferSize: 1024,
  2468. CheckOrigin: func(r *http.Request) bool {
  2469. return true
  2470. },
  2471. }
  2472. type DeviceTrajData struct {
  2473. List []Device.DeviceData_
  2474. Lists []Device.DeviceData_R2
  2475. }
  2476. var deviceTrajDataMap map[string]DeviceTrajData
  2477. //func (c *DataController) GetNewLocus_() {
  2478. // ws, err := upgrader.Upgrade(c.Ctx.ResponseWriter, c.Ctx.Request, nil)
  2479. // if err != nil {
  2480. // log.Println("设置websocket升级失败:", err)
  2481. // return
  2482. // }
  2483. // defer ws.Close()
  2484. // deviceTrajDataMap = make(map[string]DeviceTrajData)
  2485. // type Responses struct {
  2486. // Sn string `json:"sn"`
  2487. // Tid int `json:"tid"`
  2488. // }
  2489. //
  2490. // type Response struct {
  2491. // Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
  2492. // Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
  2493. // Data []Device.DeviceData_R2 `json:"data"`
  2494. // Sn string `json:"sn"`
  2495. // T_name string `json:"t_name"` // 设备名称
  2496. // }
  2497. // _, message, err := ws.ReadMessage()
  2498. // if err != nil {
  2499. // logs.Error(lib.FuncName(), "读取参数失败: %v", err)
  2500. // return
  2501. // }
  2502. // log.Printf("Received: %s", message)
  2503. //
  2504. // var r Responses
  2505. // err = json.Unmarshal(message, &r)
  2506. // if err != nil {
  2507. // logs.Error(lib.FuncName(), "解析参数失败: %v", err)
  2508. // ws.WriteMessage(websocket.TextMessage, []byte("参数解析失败"))
  2509. // return
  2510. // }
  2511. // device, err := Device.Read_Device_ByT_sn(r.Sn)
  2512. // if err != nil {
  2513. // logs.Error(lib.FuncName(), "设备不存在: %v", err)
  2514. // ws.WriteMessage(websocket.TextMessage, []byte("设备不存在"))
  2515. // return
  2516. // }
  2517. // // 根据设备序列号获取对应的数据结构,如果不存在则初始化一个
  2518. // deviceData, ok := deviceTrajDataMap[r.Sn]
  2519. // if !ok {
  2520. // deviceData = DeviceTrajData{
  2521. // List: make([]Device.DeviceData_, 0),
  2522. // Lists: make([]Device.DeviceData_R2, 0),
  2523. // }
  2524. // }
  2525. // for {
  2526. // err = Device.GetNewLocus(r.Sn, r.Tid, &deviceData.List)
  2527. // if err != nil {
  2528. // // 处理获取轨迹失败的情况,构造并发送错误响应给客户端
  2529. // response := Response{
  2530. // Code: 500,
  2531. // Msg: "获取轨迹失败",
  2532. // Data: nil,
  2533. // }
  2534. // data, _ := json.Marshal(response)
  2535. // ws.WriteMessage(websocket.TextMessage, data)
  2536. // continue
  2537. // }
  2538. //
  2539. // updatedLists := make([]Device.DeviceData_R2, 0)
  2540. // for i := range deviceData.List {
  2541. // updatedLists = append(updatedLists, Device.DeviceData_R2{
  2542. // DeviceData_: Device.DeviceData_{
  2543. // T_id: deviceData.List[i].T_id,
  2544. // T_rh: deviceData.List[i].T_rh,
  2545. // T_site: deviceData.List[i].T_site,
  2546. // T_sp: deviceData.List[i].T_sp,
  2547. // T_time: deviceData.List[i].T_time,
  2548. // T_t: deviceData.List[i].T_t,
  2549. // },
  2550. // Sn: r.Sn,
  2551. // T_name: device.T_devName,
  2552. // })
  2553. // }
  2554. // deviceData.Lists = updatedLists
  2555. // deviceTrajDataMap[r.Sn] = deviceData
  2556. //
  2557. // response := Response{
  2558. // Code: 200,
  2559. // Msg: "ok!",
  2560. // Data: deviceData.Lists,
  2561. // Sn: r.Sn,
  2562. // T_name: device.T_devName,
  2563. // }
  2564. // data, _ := json.Marshal(response)
  2565. // ws.WriteMessage(websocket.TextMessage, data)
  2566. // // 检查客户端是否断开连接
  2567. // _, _, err = ws.ReadMessage()
  2568. // if err != nil {
  2569. // if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway, websocket.CloseNoStatusReceived) {
  2570. // log.Println("客户端已断开连接")
  2571. // break
  2572. // }
  2573. // log.Println("读取消息时发生错误:", err)
  2574. // }
  2575. // // 回复消息
  2576. // time.Sleep(10 * time.Second)
  2577. // }
  2578. //}
  2579. func (c *DataController) GetNewLocus_() {
  2580. ws, err := upgrader.Upgrade(c.Ctx.ResponseWriter, c.Ctx.Request, nil)
  2581. if err != nil {
  2582. log.Println("设置websocket升级失败:", err)
  2583. return
  2584. }
  2585. defer ws.Close()
  2586. deviceTrajDataMap = make(map[string]DeviceTrajData)
  2587. type Responses struct {
  2588. Sn string `json:"sn"`
  2589. Tid int `json:"tid"`
  2590. }
  2591. type Response struct {
  2592. Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
  2593. Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
  2594. Data []Device.DeviceData_R2 `json:"data"`
  2595. Sn string `json:"sn"`
  2596. T_name string `json:"t_name"` // 设备名称
  2597. }
  2598. _, message, err := ws.ReadMessage()
  2599. if err != nil {
  2600. logs.Error(lib.FuncName(), "读取参数失败: %v", err)
  2601. return
  2602. }
  2603. log.Printf("Received: %s", message)
  2604. var r Responses
  2605. err = json.Unmarshal(message, &r)
  2606. if err != nil {
  2607. logs.Error(lib.FuncName(), "解析参数失败: %v", err)
  2608. ws.WriteMessage(websocket.TextMessage, []byte("参数解析失败"))
  2609. return
  2610. }
  2611. device, err := Device.Read_Device_ByT_sn(r.Sn)
  2612. if err != nil {
  2613. logs.Error(lib.FuncName(), "设备不存在: %v", err)
  2614. ws.WriteMessage(websocket.TextMessage, []byte("设备不存在"))
  2615. return
  2616. }
  2617. // 根据设备序列号获取对应的数据结构,如果不存在则初始化一个
  2618. deviceData, ok := deviceTrajDataMap[r.Sn]
  2619. if !ok {
  2620. deviceData = DeviceTrajData{
  2621. List: make([]Device.DeviceData_, 0),
  2622. Lists: make([]Device.DeviceData_R2, 0),
  2623. }
  2624. }
  2625. isFirstRequest := true // 新增标识,用于判断是否是首次请求
  2626. for {
  2627. err = Device.GetNewLocus(r.Sn, r.Tid, &deviceData.List)
  2628. if err != nil {
  2629. // 处理获取轨迹失败的情况,构造并发送错误响应给客户端
  2630. response := Response{
  2631. Code: 500,
  2632. Msg: "获取轨迹失败",
  2633. Data: nil,
  2634. }
  2635. data, _ := json.Marshal(response)
  2636. ws.WriteMessage(websocket.TextMessage, data)
  2637. continue
  2638. }
  2639. updatedLists := make([]Device.DeviceData_R2, 0)
  2640. for i := range deviceData.List {
  2641. updatedLists = append(updatedLists, Device.DeviceData_R2{
  2642. DeviceData_: Device.DeviceData_{
  2643. T_id: deviceData.List[i].T_id,
  2644. T_rh: deviceData.List[i].T_rh,
  2645. T_site: deviceData.List[i].T_site,
  2646. T_sp: deviceData.List[i].T_sp,
  2647. T_time: deviceData.List[i].T_time,
  2648. T_t: deviceData.List[i].T_t,
  2649. },
  2650. Sn: r.Sn,
  2651. T_name: device.T_devName,
  2652. })
  2653. }
  2654. deviceData.Lists = updatedLists
  2655. deviceTrajDataMap[r.Sn] = deviceData
  2656. response := Response{
  2657. Code: 200,
  2658. Msg: "ok!",
  2659. Data: deviceData.Lists,
  2660. Sn: r.Sn,
  2661. T_name: device.T_devName,
  2662. }
  2663. data, _ := json.Marshal(response)
  2664. ws.WriteMessage(websocket.TextMessage, data)
  2665. // 检查客户端是否断开连接
  2666. _, _, err = ws.ReadMessage()
  2667. if err != nil {
  2668. if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway, websocket.CloseNoStatusReceived) {
  2669. log.Println("客户端已断开连接")
  2670. break
  2671. }
  2672. log.Println("读取消息时发生错误:", err)
  2673. }
  2674. if isFirstRequest {
  2675. isFirstRequest = false // 首次请求处理完后,将标识设为false
  2676. } else {
  2677. // 非首次请求,按照间隔时间休眠后再进行下一次循环获取数据
  2678. time.Sleep(10 * time.Second)
  2679. }
  2680. }
  2681. }
  2682. // func (c *DataController) GetNewLocus_() {
  2683. // ws, err := upgrader.Upgrade(c.Ctx.ResponseWriter, c.Ctx.Request, nil)
  2684. // if err != nil {
  2685. // log.Println("设置websocket升级失败:", err)
  2686. // return
  2687. // }
  2688. // defer ws.Close()
  2689. //
  2690. // type Responses struct {
  2691. // Sn string `json:"sn"`
  2692. // Tid int `json:"tid"`
  2693. // }
  2694. //
  2695. // type Response struct {
  2696. // Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
  2697. // Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
  2698. // Data []Device.DeviceData_R2 `json:"data"`
  2699. // Sn string `json:"sn"`
  2700. // T_name string `json:"t_name"` // 设备名称
  2701. // }
  2702. //
  2703. // heartbeatInterval := 10 * time.Second
  2704. // timeoutInterval := 20 * time.Second
  2705. // heartbeatTicker := time.NewTicker(heartbeatInterval)
  2706. // defer heartbeatTicker.Stop()
  2707. //
  2708. // for {
  2709. // select {
  2710. // case <-heartbeatTicker.C:
  2711. // // 发送心跳消息
  2712. // if err := ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
  2713. // log.Println("发送心跳消息失败:", err)
  2714. // return
  2715. // }
  2716. // // 设置读取超时时间
  2717. // ws.SetReadDeadline(time.Now().Add(timeoutInterval))
  2718. // default:
  2719. // _, message, err := ws.ReadMessage()
  2720. // if err != nil {
  2721. // if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway, websocket.CloseNoStatusReceived) {
  2722. // log.Println("客户端已断开连接")
  2723. // } else {
  2724. // logs.Error(lib.FuncName(), "读取参数失败: %v", err)
  2725. // }
  2726. // return
  2727. // }
  2728. // log.Printf("Received: %s", message)
  2729. //
  2730. // var r Responses
  2731. // err = json.Unmarshal(message, &r)
  2732. // if err != nil {
  2733. // logs.Error(lib.FuncName(), "解析参数失败: %v", err)
  2734. // ws.WriteMessage(websocket.TextMessage, []byte("参数解析失败"))
  2735. // continue
  2736. // }
  2737. //
  2738. // sn, err := Device.Read_Device_ByT_sn(r.Sn)
  2739. // if err != nil {
  2740. // logs.Error(lib.FuncName(), "设备不存在: %v", err)
  2741. // ws.WriteMessage(websocket.TextMessage, []byte("设备不存在"))
  2742. // continue
  2743. // }
  2744. //
  2745. // var list []Device.DeviceData_
  2746. // var lists []Device.DeviceData_R2
  2747. //
  2748. // err = Device.GetNewLocus(r.Sn, r.Tid, &list)
  2749. // if err != nil {
  2750. // logs.Error(lib.FuncName(), "获取轨迹失败: %v, sn: %s, tid: %d", err, r.Sn, r.Tid)
  2751. // response := Response{
  2752. // Code: 500,
  2753. // Msg: "获取轨迹失败",
  2754. // Data: nil,
  2755. // }
  2756. // data, _ := json.Marshal(response)
  2757. // ws.WriteMessage(websocket.TextMessage, data)
  2758. // continue
  2759. // }
  2760. //
  2761. // for i := range list {
  2762. // lists = append(lists, Device.DeviceData_R2{
  2763. // DeviceData_: Device.DeviceData_{
  2764. // T_id: list[i].T_id,
  2765. // T_rh: list[i].T_rh,
  2766. // T_site: list[i].T_site,
  2767. // T_sp: list[i].T_sp,
  2768. // T_time: list[i].T_time,
  2769. // T_t: list[i].T_t,
  2770. // },
  2771. // Sn: r.Sn,
  2772. // T_name: sn.T_devName,
  2773. // })
  2774. // }
  2775. //
  2776. // response := Response{
  2777. // Code: 200,
  2778. // Msg: "ok!",
  2779. // Data: lists,
  2780. // Sn: r.Sn,
  2781. // T_name: sn.T_devName,
  2782. // }
  2783. // data, _ := json.Marshal(response)
  2784. // ws.WriteMessage(websocket.TextMessage, data)
  2785. //
  2786. // // 设置读取超时时间
  2787. // ws.SetReadDeadline(time.Now().Add(timeoutInterval))
  2788. // }
  2789. // }
  2790. // }
  2791. //func (c *DataController) GetNewLocus_() {
  2792. // ws, err := upgrader.Upgrade(c.Ctx.ResponseWriter, c.Ctx.Request, nil)
  2793. // if err != nil {
  2794. // log.Println("设置websocket升级失败:", err)
  2795. // return
  2796. // }
  2797. // defer ws.Close()
  2798. //
  2799. // type Responses struct {
  2800. // Sn string `json:"sn"`
  2801. // Tid int `json:"tid"`
  2802. // }
  2803. //
  2804. // var r Responses
  2805. // _, message, err := ws.ReadMessage()
  2806. // if err != nil {
  2807. // logs.Error(lib.FuncName(), "读取参数失败: %v", err)
  2808. // return
  2809. // }
  2810. // log.Printf("Received: %s", message)
  2811. //
  2812. // err = json.Unmarshal(message, &r)
  2813. // if err != nil {
  2814. // logs.Error(lib.FuncName(), "解析参数失败: %v", err)
  2815. // ws.WriteMessage(websocket.TextMessage, []byte("参数解析失败"))
  2816. // return
  2817. // }
  2818. //
  2819. // devices, err := Device.Read_Device_ByT_sn(r.Sn)
  2820. // if err != nil {
  2821. // logs.Error(lib.FuncName(), "设备不存在: %v", err)
  2822. // ws.WriteMessage(websocket.TextMessage, []byte("设备不存在"))
  2823. // return
  2824. // }
  2825. //
  2826. // ctx, cancel := context.WithCancel(context.Background())
  2827. // defer cancel()
  2828. //
  2829. // // 启动一个新的goroutine来处理每个设备的轨迹数据
  2830. // go func(sn string, tid int, ctx context.Context) {
  2831. // type Response struct {
  2832. // Code int32 `json:"code,omitempty"`
  2833. // Msg string `json:"msg,omitempty"`
  2834. // Data []Device.DeviceData_R2 `json:"data"`
  2835. // Sn string `json:"sn"`
  2836. // T_name string `json:"t_name"` // 设备名称
  2837. // }
  2838. //
  2839. // for {
  2840. // select {
  2841. // case <-ctx.Done():
  2842. // log.Println("goroutine 被取消")
  2843. // return
  2844. // default:
  2845. // // 检查连接是否仍然打开
  2846. // err := pingWebSocket(ws)
  2847. // if err != nil {
  2848. // log.Println("心跳检测失败:", err)
  2849. // cancel()
  2850. // break
  2851. // }
  2852. //
  2853. // var list []Device.DeviceData_
  2854. // err = Device.GetNewLocus(sn, tid, &list)
  2855. // if err != nil {
  2856. // logs.Error(lib.FuncName(), "获取轨迹失败: %v, sn: %s, tid: %d", err, sn, tid)
  2857. // response := Response{
  2858. // Code: 500,
  2859. // Msg: "获取轨迹失败",
  2860. // Data: nil,
  2861. // }
  2862. // data, marshalErr := json.Marshal(response)
  2863. // if marshalErr == nil {
  2864. // sendMessage(ws, data)
  2865. // }
  2866. // cancel()
  2867. // break
  2868. // }
  2869. //
  2870. // lists := make([]Device.DeviceData_R2, len(list))
  2871. // for i, item := range list {
  2872. // lists[i] = Device.DeviceData_R2{
  2873. // DeviceData_: Device.DeviceData_{
  2874. // T_id: item.T_id,
  2875. // T_rh: item.T_rh,
  2876. // T_site: item.T_site,
  2877. // T_sp: item.T_sp,
  2878. // T_time: item.T_time,
  2879. // T_t: item.T_t,
  2880. // },
  2881. // Sn: sn,
  2882. // T_name: devices.T_devName,
  2883. // }
  2884. // }
  2885. //
  2886. // response := Response{
  2887. // Code: 200,
  2888. // Msg: "ok!",
  2889. // Data: lists,
  2890. // Sn: sn,
  2891. // T_name: devices.T_devName,
  2892. // }
  2893. // data, marshalErr := json.Marshal(response)
  2894. // if marshalErr == nil {
  2895. // sendMessage(ws, data)
  2896. // } else {
  2897. // log.Println("JSON 序列化失败:", marshalErr)
  2898. // }
  2899. //
  2900. // time.Sleep(10 * time.Second)
  2901. // }
  2902. // }
  2903. // }(r.Sn, r.Tid, ctx)
  2904. //
  2905. // // 启动一个goroutine用于监听连接关闭事件
  2906. // go func() {
  2907. // for {
  2908. // _, _, err := ws.ReadMessage()
  2909. // if err != nil {
  2910. // if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway, websocket.CloseNoStatusReceived) {
  2911. // log.Println("客户端已断开连接")
  2912. // } else {
  2913. // log.Println("读取消息时发生错误:", err)
  2914. // }
  2915. // cancel()
  2916. // return
  2917. // }
  2918. // }
  2919. // }()
  2920. //}
  2921. //
  2922. //// pingWebSocket 发送一个ping帧以确认连接是否仍然活跃
  2923. //func pingWebSocket(ws *websocket.Conn) error {
  2924. // err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second*5))
  2925. // if err != nil {
  2926. // return fmt.Errorf("发送心跳失败: %v", err)
  2927. // }
  2928. // return nil
  2929. //}
  2930. //
  2931. //// sendMessage 封装了发送消息的过程,包括错误处理
  2932. //func sendMessage(ws *websocket.Conn, data []byte) {
  2933. // err := ws.WriteMessage(websocket.TextMessage, data)
  2934. // if err != nil {
  2935. // log.Println("发送消息失败:", err)
  2936. // }
  2937. //}