Data.go 99 KB

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