index.html 162 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>电力监测系统</title>
  8. <style>
  9. * {
  10. box-sizing: border-box;
  11. font-family: 'Inter', system-ui, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  12. }
  13. body {
  14. background: #eef2f8;
  15. margin: 0;
  16. padding: 24px 20px;
  17. min-height: 100vh;
  18. }
  19. .dashboard { max-width: 1600px; width: 100%; margin: 0 auto; }
  20. .ws-status-bar {
  21. background: white; border-radius: 60px; padding: 8px 20px; margin-bottom: 20px;
  22. display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 12px;
  23. box-shadow: 0 2px 8px rgba(0,0,0,0.05); border: 1px solid #e2edf7;
  24. }
  25. .ws-indicator { display: flex; align-items: center; gap: 8px; }
  26. .ws-led { width: 12px; height: 12px; border-radius: 50%; background-color: #aaa; transition: background-color 0.3s; }
  27. .ws-led.connected { background-color: #2ecc71; box-shadow: 0 0 6px #2ecc71; }
  28. .ws-led.disconnected { background-color: #e74c3c; }
  29. .ws-led.connecting { background-color: #f39c12; animation: pulse 1s infinite; }
  30. @keyframes pulse { 0% { opacity: 0.5; } 50% { opacity: 1; } 100% { opacity: 0.5; } }
  31. .ws-url { font-family: monospace; font-size: 0.8rem; color: #4a627a; }
  32. .reconnect-btn { background: #eef2f8; border: none; border-radius: 40px; padding: 4px 16px; font-size: 0.7rem; cursor: pointer; color: #1f6392; }
  33. .reconnect-btn:hover { background: #dfe7f0; }
  34. .tabs { display: flex; gap: 12px; margin-bottom: 24px; border-bottom: 2px solid #dfe8f0; padding-bottom: 8px; flex-wrap: wrap; }
  35. .tab-btn { background: none; border: none; padding: 10px 24px; font-size: 1rem; font-weight: 600; color: #6c8eae; cursor: pointer; border-radius: 40px; transition: all 0.2s; }
  36. .tab-btn.active { background: #1f6392; color: white; box-shadow: 0 4px 12px rgba(31,99,146,0.3); }
  37. .tab-btn:hover:not(.active) { background: #e2eaf1; color: #2c5a7a; }
  38. .tab-key-btn { background: none; border: none; padding: 10px 24px; font-size: 1rem; font-weight: 600; color: #6c8eae; cursor: pointer; border-radius: 10px;transition: all 0.2s; }
  39. .tab-key-btn.active { background: #a85b1b; color: white; box-shadow: 0 4px 12px rgba(241, 241, 240, 0.3); }
  40. .tab-key-btn:hover:not(.active) { background: #799143; color: #5a7e20; }
  41. .page { display: none; }
  42. .page.active-page { display: block; }
  43. .header { text-align: center; margin-bottom: 28px; }
  44. .header h1 { font-size: 1.7rem; font-weight: 600; background: linear-gradient(135deg, #1e2a3a, #0f2b3d); background-clip: text; -webkit-background-clip: text; color: transparent; margin: 0 0 6px 0; }
  45. .header p { color: #4a627a; font-size: 0.9rem; }
  46. .summary-panel {
  47. background: white; border-radius: 28px; padding: 16px 24px; margin-bottom: 20px;
  48. box-shadow: 0 4px 12px rgba(0,0,0,0.05); border: 1px solid #e2edf7;
  49. }
  50. .summary-title { text-align: center; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 1px; color: #7c9ab8; margin-bottom: 16px; }
  51. .summary-grid { display: flex; flex-wrap: wrap; justify-content: center; gap: 24px; row-gap: 16px; }
  52. .summary-item { text-align: center; min-width: 120px; }
  53. .summary-label { font-size: 0.7rem; color: #7c9ab8; margin-bottom: 6px; }
  54. .summary-value { font-size: 1.4rem; font-weight: 700; color: #1e2f3c; }
  55. .summary-unit { font-size: 0.7rem; font-weight: 400; color: #7c9ab8; margin-left: 3px; }
  56. .table-wrapper {
  57. background: white; border-radius: 28px; box-shadow: 0 12px 30px rgba(0,0,0,0.08);
  58. overflow-x: auto; border: 1px solid #e2edf7; margin-bottom: 24px;
  59. }
  60. .power-table { width: 100%; border-collapse: separate; border-spacing: 0; min-width: 1300px; font-size: 0.85rem; }
  61. .power-table th { padding: 14px 10px; font-weight: 600; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.4px; color: #2c5a7a; text-align: center; background: #f9fbfe; border-bottom: 2px solid #dfe8f0; }
  62. .power-table td { padding: 12px 10px; text-align: center; border-bottom: 1px solid #edf2f7; color: #1e2f3c; font-weight: 500; }
  63. .numeric-value { font-weight: 600; }
  64. .unit-sub { font-size: 0.65rem; font-weight: 400; color: #7c9ab8; margin-left: 2px; }
  65. .energy-cell { background: #f4faf7; font-weight: 700; color: #1a6e55; }
  66. .power-bg { background: #fef9e6; }
  67. .reactive-bg { background: #f0f0fe; }
  68. .pf-cell { background: #f5f0fc; font-weight: 700; color: #5e3a8c; }
  69. .channel-badge { display: inline-flex; align-items: center; gap: 6px; }
  70. .ch-icon { background: #eef2fa; border-radius: 30px; padding: 3px 8px; font-size: 0.65rem; font-weight: 600; color: #1f6392; }
  71. .ph-icon { color: #070707; font-weight: 700; background: #85ddb1; display: inline-block; padding: 3px 12px; border-radius: 10px; font-size: 0.75rem;}
  72. .action-btn { background: #eef2f8; border: none; padding: 5px 14px; border-radius: 40px; font-size: 0.7rem; font-weight: 600; cursor: pointer; color: #2c6282; min-width: 64px; }
  73. .action-btn:hover { background: #dfe7f0; }
  74. .delay-badge { background: #eef2fa; border-radius: 20px; padding: 3px 8px; font-size: 0.65rem; font-weight: 600; display: inline-block; min-width: 70px; text-align: center; }
  75. .delay-idle { background: #e8f0fe; color: #4f7ea0; }
  76. .channel-off td:not(.energy-cell) { opacity: 0.7; background-color: #fefcf5; }
  77. .status-on { color: #2c8f6e; font-weight: 700; background: #e0f2e9; display: inline-block; padding: 3px 12px; border-radius: 30px; font-size: 0.75rem; }
  78. .status-off { color: #b33; font-weight: 700; background: #ffe6e5; display: inline-block; padding: 3px 12px; border-radius: 30px; font-size: 0.75rem; }
  79. .action-bar { display: flex; justify-content: flex-end; gap: 12px; margin-bottom: 16px; padding: 8px 0; flex-wrap: wrap; }
  80. .action-bar .btn-config, .action-bar .btn-threshold, .action-bar .btn-reset,
  81. .action-bar .btn-on, .action-bar .btn-off, .action-bar .btn-alarm {
  82. background: #ffffffcc; backdrop-filter: blur(4px); border: 1px solid #cbdde9;
  83. border-radius: 40px; padding: 6px 18px; font-size: 0.75rem; font-weight: 600;
  84. cursor: pointer; position: relative;
  85. }
  86. .threshold-alarm { background-color: #ffe0e0 !important; color: #c00 !important; font-weight: 700; border-radius: 8px; }
  87. .threshold-low-alarm { background-color: #d4de94 !important; color: #c00 !important; font-weight: 700; border-radius: 8px; }
  88. .switch-btn { background: #eef2f8; border: none; padding: 5px 14px; border-radius: 40px; font-size: 0.7rem; font-weight: 600; cursor: pointer; color: #2c6282; min-width: 64px; }
  89. .switch-btn:hover { background: #dfe7f0; }
  90. .action-bar .btn-config, .action-bar .btn-threshold, .action-bar .btn-reset, .action-bar .btn-alarm { color: #1f6392; }
  91. .action-bar .btn-on { color: #2c8f6e; border-color: #2c8f6e; }
  92. .action-bar .btn-off { color: #b33; border-color: #b33; }
  93. .action-bar .btn-alarm { background: #fff3e0; border-color: #e6b800; color: #b8860b; }
  94. .action-bar .btn-alarm:hover { background: #ffe0b3; }
  95. .action-bar .btn-config:hover, .action-bar .btn-threshold:hover, .action-bar .btn-reset:hover,
  96. .action-bar .btn-on:hover, .action-bar .btn-off:hover { background: white; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
  97. .alarm-badge { position: absolute; top: -8px; right: -8px; background-color: #e74c3c; color: white; border-radius: 20px; padding: 2px 8px; font-size: 0.65rem; font-weight: 600; min-width: 22px; text-align: center; box-shadow: 0 1px 3px rgba(0,0,0,0.2); }
  98. .env-cards { display: flex; flex-wrap: wrap; gap: 20px; margin-bottom: 24px; justify-content: space-between; }
  99. .env-card { background: white; border-radius: 24px; padding: 16px 20px; flex: 1; min-width: 140px; text-align: center; box-shadow: 0 4px 12px rgba(0,0,0,0.05); border: 1px solid #e2edf7; transition: transform 0.2s; }
  100. .env-card:hover { transform: translateY(-3px); box-shadow: 0 8px 20px rgba(0,0,0,0.1); }
  101. .env-card .env-label { font-size: 0.8rem; text-transform: uppercase; color: #358ade; letter-spacing: 1px; margin-bottom: 8px; }
  102. .env-card .env-value { font-size: 1.8rem; font-weight: 700; color: #1e2f3c; }
  103. .env-card .env-unit { font-size: 0.8rem; font-weight: 400; color: #7c9ab8; margin-left: 4px; }
  104. .env-card.alarm-warning .env-value { color: #e67e22; }
  105. .env-card.alarm-danger .env-value { color: #e74c3c; }
  106. .live-footer { margin-top: 24px; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 12px; background: rgba(255,255,245,0.7); backdrop-filter: blur(4px); padding: 8px 20px; border-radius: 60px; }
  107. .footer-status { display: flex; align-items: center; gap: 12px; font-size: 0.75rem; color: #4f6f8f; }
  108. .channel-jump { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 20px; justify-content: center; }
  109. .jump-chip { background: #eef2f8; border-radius: 40px; padding: 4px 12px; font-size: 0.7rem; font-weight: 500; cursor: pointer; }
  110. .alarm-table { width: 100%; border-collapse: collapse; font-size: 0.8rem; }
  111. .alarm-table th, .alarm-table td { padding: 10px 8px; text-align: left; border-bottom: 1px solid #e2edf7; }
  112. .alarm-table th { background: #f8fafd; font-weight: 600; color: #2c5a7a; }
  113. .modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.5); backdrop-filter: blur(4px); display: flex; align-items: center; justify-content: center; z-index: 2000; visibility: hidden; opacity: 0; transition: visibility 0.2s, opacity 0.2s; }
  114. .modal-overlay.active { visibility: visible; opacity: 1; }
  115. .modal-container { background: white; border-radius: 32px; width: 90%; max-width: 950px; max-height: 85vh; display: flex; flex-direction: column; overflow: hidden; }
  116. .modal-header { padding: 18px 24px; border-bottom: 1px solid #e9edf2; display: flex; justify-content: space-between; align-items: center; background: #fafcff; }
  117. .modal-header h3 { margin: 0; font-size: 1.3rem; font-weight: 700; color: #1e2a3a; }
  118. .modal-close { background: none; border: none; font-size: 1.8rem; cursor: pointer; color: #8ba3bc; }
  119. .modal-body { padding: 20px 24px; overflow-y: auto; flex: 1; }
  120. .modal-footer { padding: 16px 24px; border-top: 1px solid #e9edf2; display: flex; justify-content: flex-end; gap: 12px; background: #fafcff; }
  121. .modal-card-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.5); backdrop-filter: blur(4px); display: flex; align-items: center; justify-content: center; z-index: 2000; visibility: hidden; opacity: 0; transition: visibility 0.2s, opacity 0.2s; }
  122. .modal-card-overlay.active { visibility: visible; opacity: 1; }
  123. .modal-card-container { background: white; border-radius: 32px; width: 90%; max-width: 400px; max-height: 65vh; display: flex; flex-direction: column; overflow: hidden; }
  124. .modal-card-header { padding: 18px 24px; border-bottom: 1px solid #e9edf2; display: flex; justify-content: space-between; align-items: center; background: #fafcff; }
  125. .modal-card-header h3 { margin: 0; font-size: 1.3rem; font-weight: 300; color: #1e2a3a; }
  126. .modal-card-close { background: none; border: none; font-size: 1.8rem; cursor: pointer; color: #8ba3bc; }
  127. .modal-card-body { padding: 20px 24px; overflow-y: auto; flex: 1; }
  128. .modal-card-footer { padding: 16px 24px; border-top: 1px solid #e9edf2; display: flex; justify-content: flex-end; gap: 12px; background: #fafcff; }
  129. .btn-cancel-modal { background: #eef2f8; border: none; border-radius: 40px; padding: 8px 22px; font-weight: 600; cursor: pointer; color: #2c6282; }
  130. .btn-confirm { background: #1f6392; color: white; border: none; border-radius: 40px; padding: 8px 22px; font-weight: 600; cursor: pointer; }
  131. .config-card { background: white; border-radius: 28px; box-shadow: 0 12px 30px rgba(0,0,0,0.08); padding: 28px 32px; max-width: 650px; margin: 0 auto; }
  132. .config-group { margin-bottom: 20px; }
  133. .config-group label { display: block; font-weight: 600; color: #2c5a7a; margin-bottom: 8px; font-size: 0.85rem; }
  134. .config-group input, .config-group select { width: 100%; padding: 12px 16px; border: 1px solid #cbdde9; border-radius: 28px; font-size: 0.9rem; background: #f9fbfe; }
  135. .save-config-btn { background: #1f6392; color: white; border: none; border-radius: 40px; padding: 12px 28px; font-size: 0.9rem; font-weight: 600; cursor: pointer; width: 100%; margin-top: 16px; }
  136. .save-config-btn:hover { background: #0e4b70; }
  137. .config-status { margin-top: 20px; padding: 12px; background: #e8f0fe; border-radius: 20px; font-size: 0.8rem; color: #2c5a7a; text-align: center; }
  138. .sub-hint { font-size: 0.7rem; color: #7c9ab8; margin-top: 4px; }
  139. .info-row { display: flex; justify-content: space-between; padding: 7px 0; border-bottom: 1px solid #e2edf7; }
  140. .info-label { font-weight: 600; color: #2c5a7a; }
  141. .info-value { font-weight: 500; color: #111111;}
  142. .datetime-set-group { display: flex; gap: 12px; margin-top: 8px; flex-wrap: wrap; }
  143. .datetime-set-group input { flex: 1; padding: 10px 14px; border: 1px solid #cbdde9; border-radius: 28px; }
  144. .btn-small { background: #eef2f8; border: none; border-radius: 40px; padding: 6px 18px; font-size: 0.75rem; font-weight: 600; cursor: pointer; color: #2c6282; }
  145. .btn-small:hover { background: #dfe7f0; }
  146. .btn-reboot { background: #eef2f8; border: none; border-radius: 40px; padding: 6px 18px; font-size: 0.75rem; font-weight: 700; cursor: pointer; color: #2c6282; }
  147. .btn-reboot:hover { background: #dfe7f0; }
  148. .config-row { background: #f9fbfe; border-radius: 20px; padding: 14px 18px; border: 1px solid #eef2f8; margin-bottom: 16px; }
  149. .config-row-title { font-weight: 700; font-size: 0.9rem; color: #2c5a7a; margin-bottom: 12px; }
  150. .config-inputs { display: flex; flex-wrap: wrap; gap: 16px; align-items: flex-end; }
  151. .config-item { display: flex; flex-direction: column; gap: 4px; min-width: 100px; }
  152. .config-item label { font-size: 0.75rem; font-weight: 600; color: #5f7e9c; }
  153. .config-item input { background: white; border: 1px solid #cbdde9; border-radius: 28px; padding: 8px 12px; width: 120px; font-size: 0.8rem; }
  154. .config-batch-row { background: #e7f0fd; border-radius: 20px; padding: 14px 18px; border: 1px solid #7386a3; margin-bottom: 16px; }
  155. .config-batch-item { display: flex; flex-direction: column; gap: 4px; min-width: 100px; }
  156. .config-batch-item label { font-size: 0.85rem; font-weight: 600; color: #114579; }
  157. .config-batch-item input { background: white; border: 1px solid #677883; border-radius: 28px; padding: 8px 12px; width: 120px; font-size: 0.8rem; }
  158. .save-channel-config-btn { background: #1f6392; color: white; border: none; border-radius: 32px; padding: 6px 20px; font-size: 0.7rem; font-weight: 600; cursor: pointer; margin-left: auto; }
  159. .channel-select-list { display: flex; flex-direction: column; gap: 12px; margin-bottom: 20px; }
  160. .channel-checkbox { display: flex; align-items: center; gap: 12px; padding: 8px 12px; background: #f9fbfe; border-radius: 40px; }
  161. .channel-checkbox input { width: 18px; height: 18px; cursor: pointer; }
  162. .channel-checkbox label { font-weight: 600; color: #1e2f3c; cursor: pointer; flex: 1; }
  163. .select-all-row { display: flex; gap: 16px; margin-bottom: 16px; padding-bottom: 12px; border-bottom: 1px solid #e2edf7; }
  164. .select-all-btn { background: #eef2f8; border: none; border-radius: 30px; padding: 6px 16px; font-size: 0.7rem; font-weight: 600; cursor: pointer; color: #2c6282; }
  165. .logout-btn { background: #eef2f8; border: 1px solid #cbdde9; border-radius: 40px; padding: 6px 18px; font-size: 0.7rem; font-weight: 600; cursor: pointer; color: #2c6282; margin-left: 12px; }
  166. .login-container { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: linear-gradient(135deg, #1e2f3c 0%, #0f2b3d 100%); display: flex; align-items: center; justify-content: center; z-index: 3000; transition: all 0.3s; }
  167. .login-card { background: white; border-radius: 48px; padding: 40px 36px; width: 90%; max-width: 420px; box-shadow: 0 25px 50px rgba(0,0,0,0.3); text-align: center; }
  168. .login-card h2 { font-size: 1.8rem; font-weight: 700; margin-bottom: 8px; background: linear-gradient(135deg, #1e2a3a, #1f6392); background-clip: text; -webkit-background-clip: text; color: transparent; }
  169. .login-input-group { margin-bottom: 24px; text-align: left; }
  170. .login-input-group label { display: block; font-weight: 600; color: #2c5a7a; margin-bottom: 8px; font-size: 0.8rem; }
  171. .login-input-group input { width: 100%; padding: 14px 18px; border: 1px solid #cbdde9; border-radius: 40px; font-size: 0.9rem; background: #f9fbfe; }
  172. .login-btn { background: #1f6392; color: white; border: none; border-radius: 40px; padding: 14px 28px; font-size: 1rem; font-weight: 700; cursor: pointer; width: 100%; transition: background 0.2s; }
  173. .login-btn:disabled { background: #9ab3c7; cursor: not-allowed; }
  174. .login-error { color: #e74c3c; font-size: 0.8rem; margin-top: 16px; min-height: 40px; }
  175. .hidden { display: none; }
  176. .add-user-btn { background: #1f6392; color: white; border: none; border-radius: 40px; padding: 8px 20px; margin-bottom: 16px; cursor: pointer; float: right; }
  177. .table-wrapper { background: white; border-radius: 28px; box-shadow: 0 12px 30px rgba(0,0,0,0.08); overflow-x: auto; border: 1px solid #e2edf7; margin-bottom: 24px; }
  178. .btn-icon { background: #eef2f8; border: none; padding: 4px 12px; border-radius: 30px; font-size: 0.7rem; margin: 0 4px; cursor: pointer; }
  179. .btn-icon.danger { color: #e74c3c; }
  180. .btn-icon.success { color: #2c8f6e; }
  181. .user-table { width: 100%; border-collapse: collapse; background: white; border-radius: 24px; overflow: hidden; }
  182. .user-table th, .user-table td { padding: 12px 16px; text-align: left; border-bottom: 1px solid #e2edf7; }
  183. .user-table th { background: #f8fafd; color: #2c5a7a; }
  184. </style>
  185. </head>
  186. <body>
  187. <!-- 登录页面 -->
  188. <div id="loginContainer" class="login-container">
  189. <div class="login-card">
  190. <h2>⚡ 电力监测系统</h2>
  191. <p>请输入凭证进行身份验证</p>
  192. <div class="login-input-group"><label>👤 用户名</label><input type="text" id="loginUsername" placeholder="admin" autocomplete="off"></div>
  193. <div class="login-input-group"><label>🔒 密码</label><input type="password" id="loginPassword" placeholder="******"></div>
  194. <button class="login-btn" id="loginBtn">登 录</button>
  195. <div id="loginErrorMsg" class="login-error"></div>
  196. </div>
  197. </div>
  198. <div id="dashboardApp" class="dashboard" style="display: none;">
  199. <div class="ws-status-bar">
  200. <div class="ws-indicator">
  201. <div class="ws-led" id="wsLed"></div>
  202. <span id="wsStatusText">未连接</span>
  203. </div>
  204. <div>
  205. <button class="logout-btn" id="logoutBtn">🚪 登出</button>
  206. <button class="reconnect-btn" id="reconnectBtn">🔌 重新连接</button>
  207. </div>
  208. </div>
  209. <div class="tabs">
  210. <button class="tab-btn active" data-page="monitor">📊 电力监测</button>
  211. <button class="tab-btn" data-page="network">🌐 网络配置</button>
  212. <button class="tab-btn" data-page="rs485">🔌 RS485配置</button>
  213. <button class="tab-btn" data-page="snmp">📡 SNMP配置</button>
  214. <button class="tab-btn" data-page="device">📟 设备信息</button>
  215. <button class="tab-btn" data-page="user">👥 用户管理</button>
  216. </div>
  217. <!-- 电力监测页面 -->
  218. <div id="monitorPage" class="page active-page">
  219. <div class="summary-panel">
  220. <div class="summary-title">📊 系统总览</div>
  221. <div class="summary-grid">
  222. <div class="summary-item"><div class="summary-label" id="All-V">总电压 (V)</div><div class="summary-value" id="totalVoltage">0.0<span class="summary-unit">V</span></div></div>
  223. <div class="summary-item"><div class="summary-label" id="All-I">总电流 (A)</div><div class="summary-value" id="totalCurrent">0.0<span class="summary-unit">A</span></div></div>
  224. <div class="summary-item"><div class="summary-label">总有功功率 (kW)</div><div class="summary-value" id="totalActivePower">0<span class="summary-unit">W</span></div></div>
  225. <div class="summary-item"><div class="summary-label">总无功功率 (kvar)</div><div class="summary-value" id="totalReactivePower">0<span class="summary-unit">var</span></div></div>
  226. <div class="summary-item"><div class="summary-label">总视在功率 (kVA)</div><div class="summary-value" id="totalApparentPower">0<span class="summary-unit">VA</span></div></div>
  227. <div class="summary-item"><div class="summary-label">总功率因数</div><div class="summary-value" id="totalPowerFactor">0.00<span class="summary-unit"></span></div></div>
  228. <div class="summary-item"><div class="summary-label">总耗电量(kWh)</div><div class="summary-value" id="totalConsumer">0.00<span class="summary-unit">kWh</span></div></div>
  229. </div>
  230. </div>
  231. <div class="action-bar">
  232. <button class="btn-config" id="openDelayConfigBtn">⚙️ 延时配置</button>
  233. <button class="btn-threshold" id="openThresholdConfigBtn">⚠️ 阈值配置</button>
  234. <button class="btn-reset" id="openResetEnergyModalBtn">↻ 重置耗电量(可选通道)</button>
  235. <button class="btn-on" id="allOnBtn">🔛 全部开启</button>
  236. <button class="btn-off" id="allOffBtn">⛔ 全部关闭</button>
  237. <button class="btn-on" id="cleanSoundLight">✏️ 清除声光报警</button>
  238. <button class="btn-alarm" id="openAlarmModalBtn">🔔 报警记录<span id="alarmCountBadge" class="alarm-badge" style="display:none;">0</span></button>
  239. </div>
  240. <div class="channel-jump" id="channelJumpContainer"></div>
  241. <div class="table-wrapper">
  242. <table class="power-table" id="powerTable">
  243. <thead>
  244. <tr><th>通道</th><th>电压 (V)</th><th>电流 (A)</th><th>有功 (kW)</th><th>无功 (kvar)</th><th>视在 (kVA)</th><th>功率因数</th><th>频率 (Hz)</th><th>累计耗电 (kWh)</th><th>开关延时</th><th>开关状态</th><th>操作</th></tr>
  245. </thead>
  246. <tbody id="tableBody"><tr><td colspan="12">等待WebSocket数据...</td></tr></tbody>
  247. </table>
  248. </div>
  249. <div class="env-cards">
  250. <div class="env-card" id="tempCard"><div class="env-label">🌡️ 温度</div><div class="env-label" id="tempStatus">--</div><div class="env-value" id="tempValue">--<span class="env-unit">°C</span></div></div>
  251. <div class="env-card" id="humiCard"><div class="env-label">💧 湿度</div><div class="env-label" id="humiStatus">--</div><div class="env-value" id="humiValue">--<span class="env-unit">%RH</span></div></div>
  252. <div class="env-card" id="smokeCard"><div class="env-label">🔥 烟雾</div><div class="env-label" id="smokeStatus">--</div><div class="env-value" id="smokeValue">--<span class="env-unit">ppm</span></div></div>
  253. <div class="env-card" id="co2Card"><div class="env-label">🌫️ CO₂浓度</div><div class="env-label" id="co2Status">--</div><div class="env-value" id="co2Value">--<span class="env-unit">ppm</span></div></div>
  254. </div>
  255. </div>
  256. <!-- 网络配置页面 -->
  257. <div id="networkPage" class="page">
  258. <div class="header"><h1>🌐 网络配置</h1><p>配置设备网络参数 (IP/掩码/网关/DNS)</p></div>
  259. <div class="config-card">
  260. <div class="config-group"><label>📡 IP获取模式</label><select id="ipMode"><option value="auto">自动 (DHCP)</option><option value="manual">手动 (静态IP)</option></select></div>
  261. <div id="staticIpGroup">
  262. <div class="config-group"><label id="ip_lable">📌 IP地址</label><input type="text" id="ipAddress" placeholder="192.168.1.100"></div>
  263. <div class="config-group"><label id="mask_lable">🔗 子网掩码</label><input type="text" id="subnetMask" placeholder="255.255.255.0"></div>
  264. <div class="config-group"><label id="gate_lable">🚪 默认网关</label><input type="text" id="gateway" placeholder="192.168.1.1"></div>
  265. </div>
  266. <div class="config-group"><label>🌍 首选DNS服务器</label><input type="text" id="dns1" placeholder="114.114.114.114"></div>
  267. <div class="config-group"><label>🌍 备用DNS服务器</label><input type="text" id="dns2" placeholder="8.8.8.8"></div>
  268. <button class="save-config-btn" id="saveNetConfigBtn">💾 保存网络配置</button>
  269. <div class="config-status" id="netStatusMsg">当前配置: 未加载</div>
  270. </div>
  271. </div>
  272. <!-- RS485配置页面 -->
  273. <div id="rs485Page" class="page">
  274. <div class="header"><h1>🔌 RS485 通讯配置</h1><p>配置串口通讯参数 (主从机模式/地址/波特率)</p></div>
  275. <div class="config-card">
  276. <div class="config-group"><label>🎛️ 主从机模式</label><select id="rs485Mode"><option value="master">主机 (Master)</option><option value="slave">从机 (Slave)</option></select></div>
  277. <div class="config-group"><label>📍 设备地址 (1-247)</label><input type="number" id="rs485Addr" min="1" max="247" value="1" step="1"></div>
  278. <div class="config-group"><label>⚡ 波特率 (bps)</label>
  279. <select id="rs485Baudrate">
  280. <option value="9600">9600</option>
  281. <option value="19200">19200</option>
  282. <option value="38400">38400</option>
  283. <option value="57600">57600</option>
  284. <option value="115200">115200</option>
  285. </select>
  286. </div>
  287. <button class="save-config-btn" id="saveRs485ConfigBtn">💾 保存485配置</button>
  288. <div class="config-status" id="rs485StatusMsg">当前配置: 未加载</div>
  289. </div>
  290. </div>
  291. <!-- SNMP配置页面 -->
  292. <div id="snmpPage" class="page">
  293. <div class="header"><h1>📡 SNMP 配置</h1><p>配置简单网络管理协议参数 (社团名称/TRAP目标)</p></div>
  294. <div class="config-card">
  295. <div class="config-group"><label>🔐 只读社团名称 (Read-Only)</label><input type="text" id="snmpReadCommunity" placeholder="public" value="public"></div>
  296. <div class="config-group"><label>✏️ 读写社团名称 (Read-Write)</label><input type="text" id="snmpWriteCommunity" placeholder="private" value="private"></div>
  297. <div class="config-group"><label>🔌 TRAP状态</label><select id="snmpTrapStatus"><option value="open">开启 (Open)</option><option value="close">关闭 (Close)</option></select></div>
  298. <div class="config-group"><label id="trapAddr">📡 TRAP目标IP地址</label><input type="text" id="snmpTrapIp" placeholder="192.168.1.100"></div>
  299. <div class="sub-hint">TRAP将用于发送告警信息(如阈值超限、开关状态变化等)</div>
  300. <button class="save-config-btn" id="saveSnmpConfigBtn">💾 保存SNMP配置</button>
  301. <div class="config-status" id="snmpStatusMsg">当前配置: 未加载</div>
  302. </div>
  303. </div>
  304. <!-- 设备信息页面 -->
  305. <div id="devicePage" class="page">
  306. <div class="header"><h1>📟 设备信息</h1><p>查看和设置系统时间、固件版本等信息</p></div>
  307. <div class="config-card">
  308. <div class="info-row"><span class="info-label">设备型号:</span><span class="info-value">SmartPDU</span></div>
  309. <div class="info-row"><span class="info-label">通道数:</span><span class="info-value" id="deviceChannel"></span></div>
  310. <div class="info-row"><span class="info-label">供电类型:</span><span class="info-value" id="powerType"></span></div>
  311. <div class="info-row"><span class="info-label">控制板数:</span><span class="info-value" id="controlBoard"></span></div>
  312. <div class="info-row"><span class="info-label">序列号:</span><span class="info-value" id="serivalNumber">SN20241206001</span></div>
  313. <div class="info-row"><span class="info-label">软件版本:</span><span class="info-value" id="softVersion"></span></div>
  314. <div class="info-row"><span class="info-label">时区:</span><span class="info-value" id="zone"></span></div>
  315. <div class="info-row"><span class="info-label">当前系统时间:</span><span class="info-value" id="systemTimeDisplay">--:--:--</span></div>
  316. <div class="config-group" style="margin-top: 20px;">
  317. <label>⏰ 设置系统时间</label>
  318. <div class="config-card">
  319. <div class="config-group"><label>⚡ 时区</label>
  320. <select id="ntpZone">
  321. <option value="0">0</option>
  322. <option value="1">+1</option><option value="2">+2</option><option value="3">+3</option><option value="4">+4</option><option value="5">+5</option><option value="6">+6</option><option value="7">+7</option><option value="8">+8</option><option value="9">+9</option><option value="10">+10</option><option value="11">+11</option><option value="12">+12</option>
  323. <option value="-1">-1</option><option value="-2">-2</option><option value="-3">-3</option><option value="-4">-4</option><option value="-5">-5</option><option value="-6">-6</option><option value="-7">-7</option><option value="-8">-8</option><option value="-9">-9</option><option value="-10">-10</option><option value="-11">-11</option><option value="-12">-12</option>
  324. </select>
  325. </div>
  326. <div class="datetime-set-group">
  327. <input type="datetime-local" step="1" id="newSystemTime">
  328. <button class="btn-small" id="setSystemTimeBtn">同步时间</button>
  329. <button class="btn-reboot" id="setSystemRebootBtn">重启设备</button>
  330. </div>
  331. </div>
  332. </div>
  333. </div>
  334. </div>
  335. <!-- 用户管理界面 (新增) -->
  336. <div id="userPage" class="page">
  337. <div class="header"><h1>👥 用户管理</h1></div>
  338. <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
  339. <h3 style="margin:0;">用户列表</h3>
  340. <button class="add-user-btn" id="addUserBtn">➕ 新增用户</button>
  341. </div>
  342. <div class="table-wrapper" style="overflow-x: auto;">
  343. <table class="user-table" id="userTable">
  344. <thead><tr><th style="text-align:center;">用户名</th><th style="text-align:center;">角色</th><th style="text-align:center;">创建时间</th><th style="text-align:center;">操作</th></tr></thead>
  345. <tbody id="userTableBody"><tr><td colspan="3" style="text-align:center;">暂无用户数据</td></tr></tbody>
  346. </table>
  347. </div>
  348. </div>
  349. </div>
  350. <!-- 模态框 -->
  351. <div id="alarmModal" class="modal-overlay">
  352. <div class="modal-container">
  353. <div class="modal-header"><h3>🔔 报警记录</h3><button class="modal-close" id="closeAlarmModalBtn">&times;</button></div>
  354. <div class="modal-body"><div id="alarmListContainer"><div style="text-align:center;color:#888;padding:20px;">暂无报警记录</div></div></div>
  355. <div class="modal-footer"><button class="btn-cancel-modal" id="clearAlarmBtn">清空记录</button><button class="btn-cancel-modal" id="cancelAlarmModalBtn">关闭</button></div>
  356. </div>
  357. </div>
  358. <div id="delayModal" class="modal-overlay">
  359. <div class="modal-container">
  360. <div class="modal-header"><h3>⏱️ 通道延时设置</h3><button class="modal-close" id="closeDelayModalBtn">&times;</button></div>
  361. <div class="modal-body"><div id="delayGrid"></div></div>
  362. <div class="modal-footer"><button class="btn-cancel-modal" id="cancelDelayModalBtn">关闭</button></div>
  363. </div>
  364. </div>
  365. <div id="thresholdModal" class="modal-overlay">
  366. <div class="modal-container">
  367. <div class="modal-header"><h3>⚠️ 阈值配置</h3><button class="modal-close" id="closeThresholdModalBtn">&times;</button></div>
  368. <div class="modal-body"><div id="pathchGrid"></div><div id="thresholdGrid"></div></div>
  369. <div class="modal-footer"><button class="btn-cancel-modal" id="cancelThresholdModalBtn">关闭</button></div>
  370. </div>
  371. </div>
  372. <div id="resetEnergyModal" class="modal-overlay">
  373. <div class="modal-container">
  374. <div class="modal-header"><h3>↻ 选择要重置耗电量的通道</h3><button class="modal-close" id="closeResetModalBtn">&times;</button></div>
  375. <div class="modal-body">
  376. <div class="select-all-row"><button class="select-all-btn" id="selectAllChannels">全选</button><button class="select-all-btn" id="deselectAllChannels">全不选</button></div>
  377. <div class="channel-select-list" id="channelSelectList"></div>
  378. </div>
  379. <div class="modal-footer"><button class="btn-cancel-modal" id="cancelResetModalBtn">取消</button><button class="btn-confirm" id="confirmResetEnergyBtn">确认重置</button></div>
  380. </div>
  381. </div>
  382. <!-- Temp 模态框 -->
  383. <div id="tempCardModal" class="modal-card-overlay">
  384. <div class="modal-card-container">
  385. <div class="modal-card-header"><h3>🌡️ 温度配置</h3><button class="modal-card-close" id="closeTempModalBtn">&times;</button></div>
  386. <div class="modal-card-body">
  387. <div class="config-group"><label>🔺最大值</label><input type="text" id="tempMax" placeholder="25.5"></div>
  388. <div class="config-group"><label>🔻最小值</label><input type="text" id="tempMin" placeholder="12.6"></div>
  389. </div>
  390. <div class="modal-card-footer"><button class="btn-confirm" id="confirmTempThrBtn">提交</button></div>
  391. </div>
  392. </div>
  393. <div id="humiCardModal" class="modal-card-overlay">
  394. <div class="modal-card-container">
  395. <div class="modal-card-header"><h3>💧 湿度配置</h3><button class="modal-card-close" id="closeHumiModalBtn">&times;</button></div>
  396. <div class="modal-card-body">
  397. <div class="config-group"><label>🔺最大值</label><input type="text" id="humiMax" placeholder="80.5"></div>
  398. <div class="config-group"><label>🔻最小值</label><input type="text" id="humiMin" placeholder="50.6"></div>
  399. </div>
  400. <div class="modal-card-footer"><button class="btn-confirm" id="confirmHumiThrBtn">提交</button></div>
  401. </div>
  402. </div>
  403. <div id="smokeCardModal" class="modal-card-overlay">
  404. <div class="modal-card-container">
  405. <div class="modal-card-header"><h3>🔥 烟雾配置</h3><button class="modal-card-close" id="closeSmokeModalBtn">&times;</button></div>
  406. <div class="modal-card-body">
  407. <div class="config-group"><label>🔺最大值</label><input type="text" id="smokeMax" placeholder="25.5"></div>
  408. <div class="config-group"><label>🔻最小值</label><input type="text" id="smokeMin" placeholder="12.6"></div>
  409. </div>
  410. <div class="modal-card-footer"><button class="btn-confirm" id="confirmSmokeThrBtn">提交</button></div>
  411. </div>
  412. </div>
  413. <div id="co2CardModal" class="modal-card-overlay">
  414. <div class="modal-card-container">
  415. <div class="modal-card-header"><h3>🌫️ CO₂配置</h3><button class="modal-card-close" id="closeCo2ModalBtn">&times;</button></div>
  416. <div class="modal-card-body">
  417. <div class="config-group"><label>🔺最大值</label><input type="text" id="Co2Max" placeholder="25.5"></div>
  418. <div class="config-group"><label>🔻最小值</label><input type="text" id="Co2Min" placeholder="12.6"></div>
  419. </div>
  420. <div class="modal-card-footer"><button class="btn-confirm" id="confirmCo2ThrBtn">提交</button></div>
  421. </div>
  422. </div>
  423. <!-- 用户编辑模态框 -->
  424. <div id="userModal" class="modal-card-overlay">
  425. <div class="modal-card-container">
  426. <div class="modal-card-header"><h3 id="userModalTitle">编辑用户</h3><button class="modal-card-close" id="closeUserModalBtn">&times;</button></div>
  427. <div class="modal-body">
  428. <div class="config-group"><label>用户名</label><input type="text" id="userName" placeholder="请输入用户名" autocomplete="off"></div>
  429. <div class="config-group"><label>旧密码</label><input type="password" id="userOldPassword" placeholder="留空则不修改密码(编辑时)"></div>
  430. <div class="config-group"><label>新密码</label><input type="password" id="userNewPassword" placeholder="留空则不修改密码(编辑时)"></div>
  431. <div class="config-group"><label>角色</label><select id="userRole"><option value="superadmin">超级管理员</option><option value="admin">管理员</option><option value="user">普通用户</option></select></div>
  432. <div id="loginUpdateErrorMsg" class="login-error"></div>
  433. </div>
  434. <div class="modal-card-footer"><button class="btn-cancel-modal" id="cancelUserModalBtn">取消</button><button class="btn-confirm" id="confirmUserBtn">确认</button></div>
  435. </div>
  436. </div>
  437. <!-- 用户新增 -->
  438. <div id="userAddModal" class="modal-card-overlay">
  439. <div class="modal-card-container">
  440. <div class="modal-card-header"><h3 id="userAddModalTitle">新增用户</h3><button class="modal-card-close" id="closeUserAddModalBtn">&times;</button></div>
  441. <div class="modal-body">
  442. <div class="config-group"><label>用户名</label><input type="text" id="userAddName" placeholder="请输入用户名" autocomplete="off"></div>
  443. <div class="config-group"><label>密码</label><input type="password" id="userAddPassword" placeholder="留空则不修改密码(编辑时)"></div>
  444. <div class="config-group"><label>角色</label><select id="userAddRole"><option value="superadmin">超级管理员</option><option value="admin">管理员</option><option value="user">普通用户</option></select></div>
  445. <div id="loginAddErrorMsg" class="login-error"></div>
  446. </div>
  447. <div class="modal-card-footer"><button class="btn-cancel-modal" id="cancelUserAddModalBtn">取消</button><button class="btn-confirm" id="confirmUserAddBtn">确认</button></div>
  448. </div>
  449. </div>
  450. <script>
  451. const PKT_TYPE = function() {
  452. var len = 0;
  453. return {
  454. NEW_PKT_TYPE_DEVICE_INFO: len++,
  455. NEW_PKT_TYPE_ELE_INFO: len++,
  456. NEW_PKT_TYPE_SENSOR: len++,
  457. NEW_PKT_TYPE_NETWORK: len++,
  458. NEW_PKT_TYPE_RS485: len++,
  459. NEW_PKT_TYPE_SNMP: len++,
  460. NEW_PKT_TYPE_WANNING: len++,
  461. NEW_PKT_TYPE_NTP :len++,
  462. MEW_PKT_TYPE_LOGIN :len++,
  463. NEW_PKT_TYPE_USER :len++,
  464. }
  465. }();
  466. const SUB_POWER_TYPE = function() {
  467. var len = 0;
  468. return {
  469. NEW_SUB_PKT_TYPE_ELE_DATA_L1: len++,
  470. NEW_SUB_PKT_TYPE_ELE_DATA_L2: len++,
  471. NEW_SUB_PKT_TYPE_ELE_DATA_L3: len++,
  472. NEW_SUB_PKT_TYPE_ELE_DATA_INFO: len++,
  473. NEW_SUB_PKT_TYPE_ELE_DATA_ALL: len++,
  474. NEW_SUB_PKT_TYPE_ELE_DATA_THR: len++,
  475. NEW_SUB_PKT_TYPE_ELE_DELAY: len++,
  476. NEW_SUB_PKT_TYPE_ELE_CLEAR_CONSUMER: len++,
  477. NEW_SUB_PKT_TYPE_ELE_CTRL: len++,
  478. NEW_SUB_PKT_TYPE_ELE_ALL_CTRL: len++,
  479. NEW_SUB_PKT_TYPE_ELE_BATCH: len++,
  480. }
  481. }();
  482. const SUB_SENSOR_TYPE = function() {
  483. var len = 0;
  484. return {
  485. NEW_SUB_PKT_TYPE_HUMI: len++,
  486. NEW_SUB_PKT_TYPE_TEMP: len++,
  487. NEW_SUB_PKT_TYPE_SMOKE: len++,
  488. NEW_SUB_PKT_TYPE_CO2: len++,
  489. }
  490. }();
  491. const SUB_WANNING_TYPE = function(){
  492. var len = 0;
  493. return{
  494. NEW_SUB_PKT_TYPE_WANNING_CLEAN:len++,
  495. NEW_SUB_PKT_TYPE_WANNING_SOUND_LIGHT_CLEAN:len++,
  496. }
  497. }();
  498. const SUB_USER_TYPE = function(){
  499. var len = 0;
  500. return{
  501. NEW_SUB_PKT_TYPE_USER_INFO:len++,
  502. NEW_SUB_PKT_TYPE_USER_ADD:len++,
  503. NEW_SUB_PKT_TYPE_USER_UPDATE:len++,
  504. NEW_SUB_PKT_TYPE_USER_DELETE:len++,
  505. }
  506. }();
  507. function array_set(array, str, offset) {
  508. const encoder = new TextEncoder();
  509. const buffer = encoder.encode(str);
  510. array.set(buffer, offset);
  511. }
  512. function array_to_string(data) {
  513. const decoder = new TextDecoder('utf-8');
  514. const decoded = decoder.decode(data);
  515. const cleaned = decoded.replace(/\0/g, '');
  516. return cleaned;
  517. }
  518. function array_cat(a1, a2) {
  519. var tlen = a1.byteLength + a2.byteLength;
  520. var buffer = new ArrayBuffer(tlen);
  521. var vw = new Uint8Array(buffer);
  522. var vw1 = new Uint8Array(a1);
  523. for (var i = 0; i < a1.byteLength; i++) {
  524. vw[i] = vw1[i];
  525. }
  526. var vw2 = new Uint8Array(a2);
  527. for (var i = 0; i < a2.byteLength; i++) {
  528. vw[i + a1.byteLength] = vw2[i];
  529. }
  530. return buffer;
  531. }
  532. function bin_to_hdr(bin) {
  533. var tmp, length = 0;
  534. tmp = new DataView(bin, length); this.magic = tmp.getUint16(0, true); length += 2;
  535. tmp = new DataView(bin, length); this.addr = tmp.getUint8(0, true); length += 1;
  536. tmp = new DataView(bin, length); this.type = tmp.getUint8(0, true); length += 1;
  537. tmp = new DataView(bin, length); this.subtype = tmp.getUint8(0, true); length += 1;
  538. tmp = new DataView(bin, length); this.nack = tmp.getUint8(0, true); length += 1;
  539. tmp = new DataView(bin, length); this.flag = tmp.getUint32(0, true); length += 4;
  540. tmp = new DataView(bin, length); this.dlen = tmp.getUint32(0, true); length += 4;
  541. this.len = length;
  542. }
  543. function mk_hdr_bin(addr, type, subtype, nack, flag, dlen) {
  544. var tmp, length = 0;
  545. var t = MAGIC;
  546. var buffer = new ArrayBuffer(14);
  547. tmp = new DataView(buffer, length); tmp.setUint16(0, t, true); length += 2; //MAGIC
  548. tmp = new DataView(buffer, length); tmp.setUint8(0, addr, true); length += 1; //addr
  549. tmp = new DataView(buffer, length); tmp.setUint8(0, type, true); length += 1; //type
  550. tmp = new DataView(buffer, length); tmp.setUint8(0, subtype, true); length += 1; //subtype
  551. tmp = new DataView(buffer, length); tmp.setUint8(0, nack, true); length += 1; //nack
  552. tmp = new DataView(buffer, length); tmp.setUint32(0, flag, true); length += 4; //flag
  553. tmp = new DataView(buffer, length); tmp.setUint32(0, dlen, true); length += 4; //dlen
  554. return buffer;
  555. }
  556. function bin_to_device_info(bin) {
  557. var tmp, bin2, length = 0;
  558. tmp = new DataView(bin, length); this.chs = tmp.getUint8(0, true); length += 1;
  559. tmp = new DataView(bin, length); this.power_type = tmp.getInt8(0, true); length += 1;
  560. tmp = new DataView(bin, length); this.zone = tmp.getUint8(0, true); length += 1;
  561. tmp = new DataView(bin, length); this.board_cnt = tmp.getUint8(0, true); length += 1;
  562. this.soft_version = array_to_string(bin.slice(length, 32)); length += 32;
  563. this.hard_version = array_to_string(bin.slice(length, length + 32)); length += 32;
  564. this.date = array_to_string(bin.slice(length, length + 16)); length += 16;
  565. this.time = array_to_string(bin.slice(length, length + 16)); length += 16;
  566. this.len = length;
  567. }
  568. function bin_to_power_th(bin) {
  569. var tmp, length = 2;
  570. tmp = new DataView(bin, length); this.v_upper = tmp.getFloat32(0, true); length += 4;
  571. tmp = new DataView(bin, length); this.v_lower = tmp.getFloat32(0, true); length += 4;
  572. tmp = new DataView(bin, length); this.c_upper = tmp.getFloat32(0, true); length += 4;
  573. tmp = new DataView(bin, length); this.p_upper = tmp.getFloat32(0, true); length += 4;
  574. tmp = new DataView(bin, length); this.w_upper = tmp.getFloat32(0, true); length += 4;
  575. this.len = length;
  576. }
  577. function bin_to_sensor(bin){
  578. var tmp, length = 0;
  579. this.name = array_to_string(bin.slice(length, 32)); length += 32;
  580. tmp = new DataView(bin, length); this.status = tmp.getUint8(0, true); length += 1;
  581. tmp = new DataView(bin, length); this.v_max_wrn = tmp.getUint8(0, true); length += 1;
  582. tmp = new DataView(bin, length); this.v_min_wrn = tmp.getUint8(0, true); length += 1;
  583. tmp = new DataView(bin, length); this.value = tmp.getFloat32(0, true); length += 4;
  584. tmp = new DataView(bin, length); this.v_max = tmp.getFloat32(0, true); length += 4;
  585. tmp = new DataView(bin, length); this.v_min = tmp.getFloat32(0, true); length += 4;
  586. this.len = length;
  587. }
  588. function bin_to_network(bin)
  589. {
  590. var tmp, length = 0;
  591. tmp = new DataView(bin, length); this.mode = tmp.getUint8(0, true); length += 1;
  592. this.ipaddr = array_to_string(bin.slice(length, length+48)); length += 48;
  593. this.mask = array_to_string(bin.slice(length, length+48)); length += 48;
  594. this.gateway = array_to_string(bin.slice(length, length+48)); length += 48;
  595. this.dns1 = array_to_string(bin.slice(length, length+48)); length += 48;
  596. this.dns2 = array_to_string(bin.slice(length, length+48)); length += 48;
  597. this.len = length;
  598. }
  599. function bin_to_rs485(bin)
  600. {
  601. var tmp, length = 0;
  602. tmp = new DataView(bin, length); this.mode = tmp.getUint8(0, true); length += 1;
  603. tmp = new DataView(bin, length); this.addr = tmp.getUint8(0, true); length += 1;
  604. tmp = new DataView(bin, length); this.baud = tmp.getUint32(0, true); length += 4;
  605. this.len = length;
  606. }
  607. function bin_to_snmp(bin)
  608. {
  609. var tmp, length = 0;
  610. tmp = new DataView(bin, length); this.mode = tmp.getUint8(0, true); length += 1;
  611. tmp = new DataView(bin, length); this.version = tmp.getUint8(0, true); length += 1;
  612. this.publics = array_to_string(bin.slice(length, length+10)); length += 10;
  613. this.privates = array_to_string(bin.slice(length, length+10)); length += 10;
  614. tmp = new DataView(bin, length); this.trapmode = tmp.getUint8(0, true); length += 1;
  615. this.nms_ip = array_to_string(bin.slice(length, length+32)); length += 32;
  616. this.len = length;
  617. }
  618. function bin_to_login(bin)
  619. {
  620. var tmp, length = 0;
  621. tmp = new DataView(bin, length); this.flag = tmp.getUint8(0, true); length += 1;
  622. this.len = length;
  623. }
  624. function bin_to_user(bin)
  625. {
  626. var tmp, length = 0;
  627. tmp = new DataView(bin, length); this.flag = tmp.getUint8(0, true); length += 1;
  628. this.len = length;
  629. }
  630. function bin_to_user_cnt(bin)
  631. {
  632. var tmp,length = 0;
  633. tmp = new DataView(bin, length); this.cnt = tmp.getUint32(0, true); length += 4;
  634. this.len = length;
  635. }
  636. function bin_to_users(bin)
  637. {
  638. var tmp, length = 0;
  639. tmp = new DataView(bin, length); this.role = tmp.getUint32(0, true); length += 4;
  640. this.user = array_to_string(bin.slice(length, length+32)); length += 32;
  641. this.date = array_to_string(bin.slice(length, length+16)); length += 16;
  642. this.time = array_to_string(bin.slice(length, length+16)); length += 16;
  643. this.len = length;
  644. }
  645. function bin_to_wanning_count(bin)
  646. {
  647. var tmp, length = 0;
  648. tmp = new DataView(bin, length); this.count = tmp.getUint16(0, true); length += 2;
  649. this.len = length;
  650. }
  651. function bin_to_wanning(bin)
  652. {
  653. var tmp, length = 0;
  654. tmp = new DataView(bin, length); this.type = tmp.getUint8(0, true); length += 1;
  655. this.date = array_to_string(bin.slice(length, length+32)); length += 32;
  656. this.context = array_to_string(bin.slice(length, length+64)); length += 64;
  657. this.len = length;
  658. }
  659. function bin_to_power(bin) {
  660. var tmp, length = 0;
  661. tmp = new DataView(bin, length); this.voltage = tmp.getFloat32(0, true); length += 4;
  662. tmp = new DataView(bin, length); this.current = tmp.getFloat32(0, true); length += 4;
  663. tmp = new DataView(bin, length); this.power = tmp.getFloat32(0, true); length += 4;
  664. tmp = new DataView(bin, length); this.reactive_power = tmp.getFloat32(0, true); length += 4;
  665. tmp = new DataView(bin, length); this.app_power = tmp.getFloat32(0, true); length += 4;
  666. tmp = new DataView(bin, length); this.freq = tmp.getFloat32(0, true); length += 4;
  667. tmp = new DataView(bin, length); this.factor = tmp.getFloat32(0, true); length += 4;
  668. tmp = new DataView(bin, length); this.consump = tmp.getFloat32(0, true); length += 4;
  669. this.len = length;
  670. }
  671. function bin_to_power_info(bin) {
  672. var tmp, length = 0;
  673. this.name = array_to_string(bin.slice(length, 16)); length += 16;
  674. tmp = new DataView(bin, length); this.addr = tmp.getUint8(0, true); length += 1;
  675. tmp = new DataView(bin, length); this.sch = tmp.getUint8(0, true); length += 1;
  676. tmp = new DataView(bin, length); this.phse = tmp.getUint8(0, true); length += 1;
  677. tmp = new DataView(bin, length); this.phse1 = tmp.getUint8(0, true); length += 1;
  678. tmp = new DataView(bin, length); this.open = tmp.getUint8(0, true); length += 1;
  679. tmp = new DataView(bin, length); this.close = tmp.getUint8(0, true); length += 1;
  680. tmp = new DataView(bin, length); this.status = tmp.getUint8(0, true); length += 1;
  681. tmp = new DataView(bin, length); let val = tmp.getUint32(0, true); length += 4;
  682. this.l1_v_upper = (val & 0x1); this.l1_v_lower = (val & 0x2); this.l1_c_upper = (val & 0x4); this.l1_p_upper = (val & 0x8); this.l1_w_upper = (val & 0x10);
  683. this.l2_v_upper = (val & (1 << 8)); this.l2_v_lower = (val & (1 << 9)); this.l2_c_upper = (val & (1 << 10)); this.l2_p_upper = (val & (1 << 11)); this.l2_w_upper = (val & (1 << 12));
  684. this.l3_v_upper = (val & (1 << 16)); this.l3_v_lower = (val & (1 << 17)); this.l3_c_upper = (val & (1 << 18)); this.l3_p_upper = (val & (1 << 19)); this.l3_w_upper = (val & (1 << 20));
  685. this.ph_loss1 = (val & (1 << 24)); this.ph_loss2 = (val & (1 << 25)); this.ph_loss3 = (val & (1 << 26));
  686. this.len = length;
  687. }
  688. function ctrl_to_bin(data)
  689. {
  690. var bin, tmp, length = 0;
  691. bin = new ArrayBuffer(data.len);
  692. tmp = new DataView(bin, length); tmp.setUint8(0, data.sch, true); length +=1;
  693. tmp = new DataView(bin, length); tmp.setUint8(0, data.status, true); length +=1;
  694. return bin;
  695. }
  696. function reboot_to_bin(data)
  697. {
  698. var bin, tmp, length = 0;
  699. bin = new ArrayBuffer(data.len);
  700. tmp = new DataView(bin, length); tmp.setUint8(0, data.flag, true); length +=1;
  701. return bin;
  702. }
  703. function delay_to_bin(data)
  704. {
  705. var bin, tmp, length = 0;
  706. bin = new ArrayBuffer(data.len);
  707. tmp = new DataView(bin, length); tmp.setUint8(0, data.channel, true); length +=1;
  708. tmp = new DataView(bin, length); tmp.setUint8(0, data.open, true); length +=1;
  709. tmp = new DataView(bin, length); tmp.setUint8(0, data.close, true); length +=1;
  710. return bin;
  711. }
  712. function thr_to_bin(data)
  713. {
  714. var bin, tmp, length = 0;
  715. bin = new ArrayBuffer(data.len);
  716. tmp = new DataView(bin, length); tmp.setUint8(0, data.channel, true); length +=1;
  717. tmp = new DataView(bin, length); tmp.setUint16(0, data.en, true); length +=2;
  718. tmp = new DataView(bin, length); tmp.setFloat32(0, data.vmax, true); length +=4;
  719. tmp = new DataView(bin, length); tmp.setFloat32(0, data.vmin, true); length +=4;
  720. tmp = new DataView(bin, length); tmp.setFloat32(0, data.cmax, true); length +=4;
  721. tmp = new DataView(bin, length); tmp.setFloat32(0, data.pmax, true); length +=4;
  722. tmp = new DataView(bin, length); tmp.setFloat32(0, data.wmax, true); length +=4;
  723. return bin;
  724. }
  725. function clear_consumer_bin(data)
  726. {
  727. var bin, tmp, length = 0;
  728. bin = new ArrayBuffer(data.len);
  729. tmp = new DataView(bin, length); tmp.setUint8(0, data.channel, true); length +=1;
  730. return bin;
  731. }
  732. function all_contol_to_bin(data)
  733. {
  734. var bin, tmp, length = 0;
  735. bin = new ArrayBuffer(data.len);
  736. tmp = new DataView(bin, length); tmp.setUint8(0, data.status, true); length +=1;
  737. return bin;
  738. }
  739. function sensor_thr_to_bin(data)
  740. {
  741. var bin, tmp, length = 0;
  742. bin = new ArrayBuffer(data.len);
  743. tmp = new DataView(bin, length); tmp.setFloat32(0, data.vmax, true); length +=4;
  744. tmp = new DataView(bin, length); tmp.setFloat32(0, data.vmin, true); length +=4;
  745. return bin;
  746. }
  747. function network_to_bin(data)
  748. {
  749. var bin, tmp, length = 0;
  750. bin = new ArrayBuffer(data.len);
  751. tmp = new DataView(bin, length); tmp.setUint8(0, data.ipMode,true); length += 1;
  752. array_set(new Uint8Array(bin), data.ipAddress, length); length += 48;
  753. array_set(new Uint8Array(bin), data.subnetMask, length); length += 48;
  754. array_set(new Uint8Array(bin), data.gateway, length); length += 48;
  755. array_set(new Uint8Array(bin), data.dns1, length); length += 48;
  756. array_set(new Uint8Array(bin), data.dns2, length); length += 48;
  757. return bin;
  758. }
  759. function rs485_to_bin(data)
  760. {
  761. var bin, tmp, length = 0;
  762. bin = new ArrayBuffer(data.len);
  763. tmp = new DataView(bin, length); tmp.setUint8(0, data.rs485Mode,true); length += 1;
  764. tmp = new DataView(bin, length); tmp.setUint8(0, data.rs485Addr,true); length += 1;
  765. tmp = new DataView(bin, length); tmp.setUint32(0, data.rs485Baudrate,true); length += 4;
  766. return bin;
  767. }
  768. function snmp_to_bin(data)
  769. {
  770. var bin, tmp, length = 0;
  771. bin = new ArrayBuffer(data.len);
  772. tmp = new DataView(bin, length); tmp.setUint8(0, data.mode,true); length += 1;
  773. tmp = new DataView(bin, length); tmp.setUint8(0, data.version,true); length += 1;
  774. array_set(new Uint8Array(bin), data.snmpReadCommunity, length); length += 10;
  775. array_set(new Uint8Array(bin), data.snmpWriteCommunity, length); length += 10;
  776. tmp = new DataView(bin, length); tmp.setUint8(0, data.snmpTrapStatus,true); length += 1;
  777. array_set(new Uint8Array(bin), data.snmpTrapIp, length); length += 32;
  778. return bin;
  779. }
  780. function ntp_to_bin(data)
  781. {
  782. var bin, tmp, length = 0;
  783. bin = new ArrayBuffer(data.len);
  784. tmp = new DataView(bin, length); tmp.setInt8(0, data.ntpZone,true); length += 1;
  785. array_set(new Uint8Array(bin), data.ntpDateTime, length); length += 32;
  786. return bin;
  787. }
  788. function wanning_to_bin(data)
  789. {
  790. var bin, tmp, length = 0;
  791. bin = new ArrayBuffer(data.len);
  792. tmp = new DataView(bin, length); tmp.setUint8(0, data.value,true); length += 1;
  793. return bin;
  794. }
  795. function user_add_to_bin(data)
  796. {
  797. var bin, tmp, length = 0;
  798. bin = new ArrayBuffer(data.len);
  799. tmp = new DataView(bin, length); tmp.setUint8(0, data.role,true); length += 1;
  800. array_set(new Uint8Array(bin), data.user, length); length += 32;
  801. array_set(new Uint8Array(bin), data.password, length); length += 32;
  802. return bin;
  803. }
  804. function user_delete_to_bin(data)
  805. {
  806. var bin, tmp, length = 0;
  807. bin = new ArrayBuffer(data.len);
  808. array_set(new Uint8Array(bin), data.user, length); length += 32;
  809. return bin;
  810. }
  811. function user_update_to_bin(data)
  812. {
  813. var bin, tmp, length = 0;
  814. bin = new ArrayBuffer(data.len);
  815. tmp = new DataView(bin, length); tmp.setUint8(0, data.role,true); length += 1;
  816. array_set(new Uint8Array(bin), data.user, length); length += 32;
  817. array_set(new Uint8Array(bin), data.old_password, length); length += 32;
  818. array_set(new Uint8Array(bin), data.new_password, length); length += 32;
  819. return bin;
  820. }
  821. function pkt_to_bin(addr, type, subtype, nack, flag, js) {
  822. var tmp;
  823. var buffer = new mk_hdr_bin(addr, type, subtype, nack, flag, js.len);
  824. switch(type)
  825. {
  826. case PKT_TYPE.NEW_PKT_TYPE_DEVICE_INFO:
  827. {
  828. tmp = new reboot_to_bin(js);
  829. }
  830. break;
  831. case PKT_TYPE.NEW_PKT_TYPE_ELE_INFO:
  832. {
  833. switch(subtype)
  834. {
  835. case SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_CTRL:
  836. {
  837. tmp = new ctrl_to_bin(js);
  838. }
  839. break;
  840. case SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DELAY:
  841. {
  842. tmp = new delay_to_bin(js);
  843. }
  844. break;
  845. case SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_THR:
  846. case SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_BATCH:
  847. {
  848. tmp = new thr_to_bin(js);
  849. }
  850. break;
  851. case SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_CLEAR_CONSUMER:
  852. {
  853. tmp = new clear_consumer_bin(js);
  854. }
  855. break;
  856. case SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_ALL_CTRL:
  857. {
  858. tmp = new all_contol_to_bin(js);
  859. }
  860. break;
  861. default:
  862. break;
  863. }
  864. }
  865. break;
  866. case PKT_TYPE.NEW_PKT_TYPE_SENSOR:
  867. {
  868. tmp = new sensor_thr_to_bin(js);
  869. }
  870. break;
  871. case PKT_TYPE.NEW_PKT_TYPE_NETWORK:
  872. {
  873. tmp = new network_to_bin(js);
  874. }
  875. break;
  876. case PKT_TYPE.NEW_PKT_TYPE_RS485:
  877. {
  878. tmp = new rs485_to_bin(js);
  879. }
  880. break;
  881. case PKT_TYPE.NEW_PKT_TYPE_SNMP:
  882. {
  883. tmp = new snmp_to_bin(js);
  884. }
  885. break;
  886. case PKT_TYPE.NEW_PKT_TYPE_NTP:
  887. {
  888. tmp = new ntp_to_bin(js);
  889. }
  890. break;
  891. case PKT_TYPE.NEW_PKT_TYPE_WANNING:
  892. {
  893. switch(subtype)
  894. {
  895. case SUB_WANNING_TYPE.NEW_SUB_PKT_TYPE_WANNING_CLEAN:
  896. {
  897. tmp = new wanning_to_bin(js);
  898. }
  899. break;
  900. case SUB_WANNING_TYPE.NEW_SUB_PKT_TYPE_WANNING_SOUND_LIGHT_CLEAN:
  901. {
  902. tmp = new wanning_to_bin(js);
  903. }
  904. break;
  905. default:
  906. break;
  907. }
  908. }
  909. break;
  910. case PKT_TYPE.NEW_PKT_TYPE_USER:
  911. {
  912. switch(subtype)
  913. {
  914. case SUB_USER_TYPE.NEW_SUB_PKT_TYPE_USER_ADD:
  915. {
  916. tmp = new user_add_to_bin(js);
  917. }
  918. break;
  919. case SUB_USER_TYPE.NEW_SUB_PKT_TYPE_USER_UPDATE:
  920. {
  921. tmp = new user_update_to_bin(js);
  922. }
  923. break;
  924. case SUB_USER_TYPE.NEW_SUB_PKT_TYPE_USER_DELETE:
  925. {
  926. tmp = new user_delete_to_bin(js);
  927. }
  928. break;
  929. }
  930. }
  931. break;
  932. default:
  933. break;
  934. }
  935. return array_cat(buffer, tmp);
  936. }
  937. function single_ctrl(channel,status)
  938. {
  939. data = {sch:0,status:0,len:2};
  940. data.sch = channel;
  941. data.status = status;
  942. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_CTRL, 0, 0, data);
  943. ws.send(a);
  944. }
  945. function channel_set_delay(channel,open,close)
  946. {
  947. data = [];
  948. data.channel = channel;
  949. data.open = open;
  950. data.close = close;
  951. data.len = 3;
  952. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DELAY, 0, 0, data);
  953. ws.send(a);
  954. }
  955. function channel_set_thr(channel,vmax,vmin,cmax,pmax,wmax)
  956. {
  957. data = [];
  958. data.channel = channel;
  959. data.en=0x1F;
  960. data.vmax = vmax;
  961. data.vmin = vmin;
  962. data.cmax = cmax;
  963. data.pmax = pmax;
  964. data.wmax = wmax;
  965. data.len = 23;
  966. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_THR, 0, 0, data);
  967. ws.send(a);
  968. }
  969. function batch_set_thr(channel,vmax,vmin,cmax,pmax,wmax)
  970. {
  971. data = [];
  972. data.channel = 0;
  973. data.en=0x1F;
  974. data.vmax = vmax;
  975. data.vmin = vmin;
  976. data.cmax = cmax;
  977. data.pmax = pmax;
  978. data.wmax = wmax;
  979. data.len = 23;
  980. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_BATCH, 0, 0, data);
  981. ws.send(a);
  982. }
  983. function channel_clear_consumer(channel)
  984. {
  985. data = [];
  986. data.channel = channel;
  987. data.len = 1;
  988. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_CLEAR_CONSUMER, 0, 0, data);
  989. ws.send(a);
  990. }
  991. function all_contrl(status)
  992. {
  993. data = [];
  994. data.status = status;
  995. data.len = 1;
  996. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_ALL_CTRL, 0, 0, data);
  997. ws.send(a);
  998. }
  999. function set_sensor_thr(vmax,vmin,type)
  1000. {
  1001. data = [];
  1002. data.vmax = vmax;
  1003. data.vmin = vmin;
  1004. data.len = 8;
  1005. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_SENSOR,type, 0, 0, data);
  1006. console.log(data.vmax,data.vmin);
  1007. ws.send(a);
  1008. }
  1009. function clean_alarm()
  1010. {
  1011. data = [];
  1012. data.value = 0;
  1013. data.len = 1;
  1014. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_WANNING,SUB_WANNING_TYPE.NEW_SUB_PKT_TYPE_WANNING_CLEAN, 0, 0, data);
  1015. ws.send(a);
  1016. }
  1017. function clean_sound_light()
  1018. {
  1019. data = [];
  1020. data.value = 0;
  1021. data.len = 1;
  1022. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_WANNING,SUB_WANNING_TYPE.NEW_SUB_PKT_TYPE_WANNING_SOUND_LIGHT_CLEAN, 0, 0, data);
  1023. ws.send(a);
  1024. showToast("清除声光报警", 1000);
  1025. }
  1026. var temp = [];
  1027. function bin_parse(bin) {
  1028. var tmp, h = new bin_to_hdr(bin);
  1029. if (h.nack == 1) {}
  1030. var data = bin.slice(h.len);
  1031. if (data.byteLength > 0 && h.dlen == data.byteLength) {
  1032. switch (h.type) {
  1033. case PKT_TYPE.NEW_PKT_TYPE_DEVICE_INFO:
  1034. {
  1035. h.device_info = new bin_to_device_info(data.slice(length));
  1036. }
  1037. break;
  1038. case PKT_TYPE.NEW_PKT_TYPE_ELE_INFO:
  1039. {
  1040. var length = 0;
  1041. switch(h.subtype)
  1042. {
  1043. case SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_ALL:
  1044. {
  1045. h.power_all = new bin_to_power(data.slice(length));
  1046. length += h.power_all.len;
  1047. for(var i = 0; i < 3;i++)
  1048. {
  1049. temp[i] = new bin_to_power(data.slice(length));
  1050. length += temp[i].len;
  1051. }
  1052. h.power_all_l = temp;
  1053. }
  1054. break;
  1055. case SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_INFO:
  1056. {
  1057. for(let i = 0;i < device_info.chs;i++)
  1058. {
  1059. temp[i] = new bin_to_power_info(data.slice(length));
  1060. length += temp[i].len;
  1061. }
  1062. h.power_info = temp;
  1063. }
  1064. break;
  1065. case SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_L1:
  1066. {
  1067. for(var i = 0; i < device_info.chs;i++)
  1068. {
  1069. temp[i] = new bin_to_power(data.slice(length));
  1070. length += temp[i].len;
  1071. }
  1072. h.power_data = temp;
  1073. }
  1074. break;
  1075. case SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_L2:
  1076. {
  1077. for(var i = 0; i < device_info.chs;i++)
  1078. {
  1079. temp[i] = new bin_to_power(data.slice(length));
  1080. length += temp[i].len;
  1081. }
  1082. h.power_data1 = temp;
  1083. }
  1084. break;
  1085. case SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_L3:
  1086. {
  1087. for(var i = 0; i < device_info.chs;i++)
  1088. {
  1089. temp[i] = new bin_to_power(data.slice(length));
  1090. length += temp[i].len;
  1091. }
  1092. h.power_data2 = temp;
  1093. }
  1094. break;
  1095. case SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_THR:
  1096. {
  1097. for(var i = 0; i < device_info.chs;i++)
  1098. {
  1099. temp[i] = new bin_to_power_th(data.slice(length));
  1100. length += temp[i].len;
  1101. }
  1102. h.power_thr = temp;
  1103. }
  1104. break;
  1105. default:
  1106. break;
  1107. }
  1108. }
  1109. break;
  1110. case PKT_TYPE.NEW_PKT_TYPE_SENSOR:
  1111. {
  1112. var length = 0;
  1113. for(let i =0;i < 4; i++)
  1114. {
  1115. temp[i] = new bin_to_sensor(data.slice(length));
  1116. length += temp[i].len;
  1117. }
  1118. h.sensor_info = temp;
  1119. }
  1120. break;
  1121. case PKT_TYPE.NEW_PKT_TYPE_NETWORK:
  1122. {
  1123. h.network = new bin_to_network(data.slice(length));
  1124. }
  1125. break;
  1126. case PKT_TYPE.NEW_PKT_TYPE_RS485:
  1127. {
  1128. h.rs485 = new bin_to_rs485(data.slice(length));
  1129. }
  1130. break;
  1131. case PKT_TYPE.NEW_PKT_TYPE_SNMP:
  1132. {
  1133. h.snmp = new bin_to_snmp(data.slice(length));
  1134. }
  1135. break;
  1136. case PKT_TYPE.NEW_PKT_TYPE_WANNING:
  1137. {
  1138. var length = 0;
  1139. h.wanni_count = new bin_to_wanning_count(data.slice(length));
  1140. length += h.wanni_count.len;
  1141. for(let i = 0; i < h.wanni_count.count;i++)
  1142. {
  1143. temp[i] = new bin_to_wanning(data.slice(length));
  1144. length += temp[i].len;
  1145. }
  1146. h.wanning = temp;
  1147. }
  1148. break;
  1149. case PKT_TYPE.NEW_PKT_TYPE_USER:
  1150. {
  1151. switch(h.subtype)
  1152. {
  1153. case SUB_USER_TYPE.NEW_SUB_PKT_TYPE_USER_INFO:
  1154. {
  1155. var length = 0;
  1156. var tmp = [];
  1157. h.user_count = new bin_to_user_cnt(data.slice(length));
  1158. length += h.user_count.len;
  1159. for(let i = 0; i < h.user_count.cnt;i++)
  1160. {
  1161. tmp[i] = new bin_to_users(data.slice(length));
  1162. length += tmp[i].len;
  1163. }
  1164. h.users = tmp;
  1165. }
  1166. break;
  1167. case SUB_USER_TYPE.NEW_SUB_PKT_TYPE_USER_ADD:
  1168. {
  1169. h.user_add = new bin_to_user(data.slice(length));
  1170. }
  1171. break;
  1172. case SUB_USER_TYPE.NEW_SUB_PKT_TYPE_USER_DELETE:
  1173. {
  1174. h.user_delete = new bin_to_user(data.slice(length));
  1175. }
  1176. break;
  1177. case SUB_USER_TYPE.NEW_SUB_PKT_TYPE_USER_UPDATE:
  1178. {
  1179. h.user_update = new bin_to_user(data.slice(length));
  1180. }
  1181. break;
  1182. }
  1183. }
  1184. break;
  1185. case PKT_TYPE.MEW_PKT_TYPE_LOGIN:
  1186. {
  1187. h.login = new bin_to_login(data.slice(length));
  1188. }
  1189. break;
  1190. default:
  1191. console.log("wrong hdr type: " + h.type);
  1192. break;
  1193. }
  1194. }
  1195. return h;
  1196. };
  1197. function openTempModal()
  1198. {
  1199. let vmax = tempValue.v_max;
  1200. let vmin = tempValue.v_min;
  1201. document.getElementById('tempMax').value=vmax.toFixed(2);
  1202. document.getElementById('tempMin').value=vmin.toFixed(2);
  1203. document.getElementById('tempCardModal').classList.add('active');
  1204. }
  1205. function openHumiModal()
  1206. {
  1207. let vmax = humiValue.v_max;
  1208. let vmin = humiValue.v_min;
  1209. document.getElementById('humiMax').value=vmax.toFixed(2);
  1210. document.getElementById('humiMin').value=vmin.toFixed(2);
  1211. document.getElementById('humiCardModal').classList.add('active');
  1212. }
  1213. function openSmokeModal()
  1214. {
  1215. let vmax = smokeValue.v_max;
  1216. let vmin = smokeValue.v_min;
  1217. document.getElementById('smokeMax').value=vmax.toFixed(2);
  1218. document.getElementById('smokeMin').value=vmin.toFixed(2);
  1219. document.getElementById('smokeCardModal').classList.add('active');
  1220. }
  1221. function openCo2Modal()
  1222. {
  1223. let vmax = co2Value.v_max;
  1224. let vmin = co2Value.v_min;
  1225. document.getElementById('Co2Max').value=vmax.toFixed(2);
  1226. document.getElementById('Co2Min').value=vmin.toFixed(2);
  1227. document.getElementById('co2CardModal').classList.add('active');
  1228. }
  1229. function openDelayModal() {
  1230. const grid = document.getElementById('delayGrid');
  1231. if (!grid) return;
  1232. grid.innerHTML = '';
  1233. for (let i = 0; i < device_info.chs; i++) {
  1234. const delays = channelDelays[i];
  1235. const rowDiv = document.createElement('div');
  1236. rowDiv.className = 'config-row';
  1237. rowDiv.innerHTML = '<div class="config-row-title">CH' + (i+1) + ' · ' + delays.name + '</div>' +
  1238. '<div class="config-inputs">' +
  1239. '<div class="config-item"><label>⏫ 开延时 (秒)</label><input type="number" step="0.1" min="0.3" max="10" id="onDelay_' + i + '" value="' + delays.onSec + '"></div>' +
  1240. '<div class="config-item"><label>⏬ 关延时 (秒)</label><input type="number" step="0.1" min="0.3" max="10" id="offDelay_' + i + '" value="' + delays.offSec + '"></div>' +
  1241. '<button class="save-channel-config-btn" data-channel="' + i + '">💾 保存本通道</button>' +
  1242. '</div>';
  1243. const saveBtn = rowDiv.querySelector('.save-channel-config-btn');
  1244. saveBtn.addEventListener('click', (function(idx) {
  1245. return function() {
  1246. const onVal = parseFloat(document.getElementById('onDelay_' + idx).value);
  1247. const offVal = parseFloat(document.getElementById('offDelay_' + idx).value);
  1248. channel_set_delay(idx,onVal,offVal);
  1249. showToast('通道 ' + (idx+1) + ' 延时已保存: 开' + onVal.toFixed(1) + 's / 关' + offVal.toFixed(1) + 's', 1200);
  1250. };
  1251. })(i));
  1252. grid.appendChild(rowDiv);
  1253. }
  1254. document.getElementById('delayModal').classList.add('active');
  1255. }
  1256. function openThresholdModal() {
  1257. const grid = document.getElementById('thresholdGrid');
  1258. const patch_grid = document.getElementById('pathchGrid');
  1259. if (!grid || !patch_grid) return;
  1260. grid.innerHTML = '';
  1261. patch_grid.innerHTML = '';
  1262. if(device_info.chs > 0)
  1263. {
  1264. const rowDiv = document.createElement('div');
  1265. rowDiv.className = 'config-batch-row';
  1266. rowDiv.innerHTML = '<div class="config-row-title">批量设置'+'</div>' +
  1267. '<div class="config-inputs">' +
  1268. '<div class="config-batch-item"><label>🔻 电压最小值 (V)</label><input type="number" step="1" min="0" max="300" id="vMin_batch' + '" value="' + 0.00 + '"></div>' +
  1269. '<div class="config-batch-item"><label>🔺 电压最大值 (V)</label><input type="number" step="1" min="0" max="300" id="vMax_batch' + '" value="' + 0.00 + '"></div>' +
  1270. '<div class="config-batch-item"><label>⚡ 电流最大值 (A)</label><input type="number" step="5" min="0" max="200" id="iMax_batch' + '" value="' + 0.00 + '"></div>' +
  1271. '<div class="config-batch-item"><label>🔋 功率最大值 (W)</label><input type="number" step="500" min="0" max="50000" id="pMax_batch' + '" value="' + 0.00 + '"></div>' +
  1272. '<div class="config-batch-item"><label>📊 耗电量最大值 (kWh)</label><input type="number" step="50" min="0" max="10000" id="eMax_batch' + '" value="' + 0.00 + '"></div>' +
  1273. '<button class="save-channel-config-btn" data-channel="'+ '">💾 保存</button>' +
  1274. '</div>';
  1275. const saveBtn = rowDiv.querySelector('.save-channel-config-btn');
  1276. saveBtn.addEventListener('click',function() {
  1277. const vMin = parseFloat(document.getElementById('vMin_batch').value);
  1278. const vMax = parseFloat(document.getElementById('vMax_batch').value);
  1279. const iMax = parseFloat(document.getElementById('iMax_batch').value);
  1280. const pMax = parseFloat(document.getElementById('pMax_batch').value);
  1281. const eMax = parseFloat(document.getElementById('eMax_batch').value);
  1282. if (!isNaN(vMin) && !isNaN(vMax) && !isNaN(iMax) && !isNaN(pMax) && !isNaN(eMax)) {
  1283. batch_set_thr(0,vMax,vMin,iMax,pMax,eMax);
  1284. showToast('批量阈值' + '已配置', 1000);
  1285. closeModal('thresholdModal');
  1286. } else {
  1287. showToast('请输入有效数值', 1000);
  1288. }
  1289. });
  1290. patch_grid.appendChild(rowDiv);
  1291. }
  1292. for (let i = 0; i < device_info.chs; i++) {
  1293. const thr = channelThresholds[i];
  1294. const rowDiv = document.createElement('div');
  1295. rowDiv.className = 'config-row';
  1296. rowDiv.innerHTML = '<div class="config-row-title">CH' + (i+1) + ' · '+ (thr.name) + '</div>' +
  1297. '<div class="config-inputs">' +
  1298. '<div class="config-item"><label>🔻 电压最小值 (V)</label><input type="number" step="1" min="0" max="300" id="vMin_' + i + '" value="' + thr.vMin.toFixed(2) + '"></div>' +
  1299. '<div class="config-item"><label>🔺 电压最大值 (V)</label><input type="number" step="1" min="0" max="300" id="vMax_' + i + '" value="' + thr.vMax.toFixed(2) + '"></div>' +
  1300. '<div class="config-item"><label>⚡ 电流最大值 (A)</label><input type="number" step="5" min="0" max="200" id="iMax_' + i + '" value="' + thr.iMax.toFixed(2) + '"></div>' +
  1301. '<div class="config-item"><label>🔋 功率最大值 (W)</label><input type="number" step="500" min="0" max="50000" id="pMax_' + i + '" value="' + thr.pMax.toFixed(2) + '"></div>' +
  1302. '<div class="config-item"><label>📊 耗电量最大值 (kWh)</label><input type="number" step="50" min="0" max="10000" id="eMax_' + i + '" value="' + thr.eMax.toFixed(2) + '"></div>' +
  1303. '<button class="save-channel-config-btn" data-channel="' + i + '">💾 保存本通道</button>' +
  1304. '</div>';
  1305. const saveBtn = rowDiv.querySelector('.save-channel-config-btn');
  1306. saveBtn.addEventListener('click', (function(idx) {
  1307. return function() {
  1308. const vMin = parseFloat(document.getElementById('vMin_' + idx).value);
  1309. const vMax = parseFloat(document.getElementById('vMax_' + idx).value);
  1310. const iMax = parseFloat(document.getElementById('iMax_' + idx).value);
  1311. const pMax = parseFloat(document.getElementById('pMax_' + idx).value);
  1312. const eMax = parseFloat(document.getElementById('eMax_' + idx).value);
  1313. if (!isNaN(vMin) && !isNaN(vMax) && !isNaN(iMax) && !isNaN(pMax) && !isNaN(eMax)) {
  1314. channel_set_thr(idx,vMax,vMin,iMax,pMax,eMax)
  1315. showToast('通道 ' + (idx+1) + ' 阈值已保存', 1000);
  1316. } else {
  1317. showToast('请输入有效数值', 1000);
  1318. }
  1319. };
  1320. })(i));
  1321. grid.appendChild(rowDiv);
  1322. }
  1323. document.getElementById('thresholdModal').classList.add('active');
  1324. }
  1325. function updateResetModalUI() {
  1326. const container = document.getElementById('channelSelectList');
  1327. if (!container) return;
  1328. container.innerHTML = '';
  1329. for (let i = 0; i < device_info.chs; i++) {
  1330. const div = document.createElement('div');
  1331. div.className = 'channel-checkbox';
  1332. div.innerHTML = '<input type="checkbox" id="resetChk_' + i + '" ' + (currentSelectedChannels[i] ? 'checked' : '') + '>' +
  1333. '<label for="resetChk_' + i + '">CH' + (i+1) + ' · '+(channelTelemetry[i].name) + ' (当前耗电: ' + ((channelTelemetry[i]?.energyKwh || 0).toFixed(2)) + ' kWh)</label>';
  1334. const chk = div.querySelector('input');
  1335. chk.addEventListener('change', (function(idx) {
  1336. return function(e) { currentSelectedChannels[idx] = e.target.checked; };
  1337. })(i));
  1338. container.appendChild(div);
  1339. }
  1340. }
  1341. function openResetEnergyModal() {
  1342. for (let i = 0; i < device_info.chs; i++) currentSelectedChannels[i] = true;
  1343. updateResetModalUI();
  1344. document.getElementById('resetEnergyModal').classList.add('active');
  1345. }
  1346. function confirmResetEnergy() {
  1347. const toReset = [];
  1348. for (let i = 0; i < device_info.chs; i++) {
  1349. if (currentSelectedChannels[i]) toReset.push(i);
  1350. }
  1351. if (toReset.length === 0) {
  1352. showToast("未选择任何通道", 1000);
  1353. return;
  1354. }
  1355. for (let idx of toReset) {
  1356. channel_clear_consumer(idx);
  1357. }
  1358. //renderTable();
  1359. const names = toReset.map(i => 'CH' + (i + 1)).join(', ');
  1360. showToast('已重置 ' + names + ' 的耗电量', 1500);
  1361. closeModal('resetEnergyModal');
  1362. }
  1363. function confirmTempThr() {
  1364. valmax = document.getElementById('tempMax').value;
  1365. valmin = document.getElementById('tempMin').value;
  1366. set_sensor_thr(valmax,valmin,SUB_SENSOR_TYPE.NEW_SUB_PKT_TYPE_TEMP);
  1367. showToast('设置温度阈值', 1500);
  1368. closeModal('tempCardModal');
  1369. }
  1370. function confirmHumiThr() {
  1371. valmax = document.getElementById('humiMax').value;
  1372. valmin = document.getElementById('humiMin').value;
  1373. set_sensor_thr(valmax,valmin,SUB_SENSOR_TYPE.NEW_SUB_PKT_TYPE_HUMI);
  1374. showToast('设置湿度阈值', 1500);
  1375. closeModal('humiCardModal');
  1376. }
  1377. function confirmSmokeThr() {
  1378. valmax = document.getElementById('smokeMax').value;
  1379. valmin = document.getElementById('smokeMin').value;
  1380. set_sensor_thr(valmax,valmin,SUB_SENSOR_TYPE.NEW_SUB_PKT_TYPE_SMOKE);
  1381. showToast('设置烟雾阈值', 1500);
  1382. closeModal('smokeCardModal');
  1383. }
  1384. function confirmCo2Thr() {
  1385. valmax = document.getElementById('Co2Max').value;
  1386. valmin = document.getElementById('Co2Min').value;
  1387. set_sensor_thr(valmax,valmin,SUB_SENSOR_TYPE.NEW_SUB_PKT_TYPE_CO2);
  1388. showToast('设置Co2阈值', 1500);
  1389. closeModal('co2CardModal');
  1390. }
  1391. function confirmNetwork()
  1392. {
  1393. ip_data=[];
  1394. ip_data.ipMode = (document.getElementById('ipMode').value == "auto") ? 0 : 1;
  1395. ip_data.ipAddress = document.getElementById('ipAddress').value;
  1396. ip_data.subnetMask = document.getElementById('subnetMask').value;
  1397. ip_data.gateway = document.getElementById('gateway').value;
  1398. ip_data.dns1 = document.getElementById('dns1').value;
  1399. ip_data.dns2 = document.getElementById('dns2').value;
  1400. ip_data.len = 241;
  1401. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_NETWORK,0, 0, 0, ip_data);
  1402. ws.send(a);
  1403. }
  1404. function addAlarm(data,i) {
  1405. // const now = new Date();
  1406. // const timeStr = now.toLocaleString();
  1407. const alarmId = 'AL' + String(i).padStart(4, '0');
  1408. type = (data.type == 0) ? "电能告警" : (data.type == 1) ? "传感器告警" : (data.type == 2) ? "网络告警" : "其他告警";
  1409. alarmList.unshift({
  1410. id: alarmId,
  1411. time: data.date,
  1412. type: type,
  1413. message: data.context
  1414. });
  1415. if (alarmList.length > 10) alarmList.pop();
  1416. // updateAlarmBadge();
  1417. // updateAlarmUI();
  1418. // showToast("新告警: " + type, 2000);
  1419. }
  1420. function confirmNtp()
  1421. {
  1422. ntp_data = [];
  1423. ntp_data.ntpZone = document.getElementById('ntpZone').value;
  1424. datetimeStr = document.getElementById('newSystemTime').value;
  1425. ntp_data.len = 33;
  1426. if (datetimeStr) {
  1427. const newDate = new Date(datetimeStr);
  1428. if (!isNaN(newDate.getTime()))
  1429. {
  1430. ntp_data.ntpDateTime = newDate.toLocaleString();
  1431. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_NTP,0, 0, 0, ntp_data);
  1432. ws.send(a);
  1433. showToast(newDate.toLocaleString(), 1000);
  1434. }
  1435. else
  1436. showToast("无效的时间格式", 1000);
  1437. } else showToast("请选择日期时间", 1000);
  1438. }
  1439. function rebootSystem()
  1440. {
  1441. data = [];
  1442. data.flag = 1;
  1443. data.len = 1;
  1444. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_DEVICE_INFO,0, 0, 0, data);
  1445. ws.send(a);
  1446. showToast("重启设备", 1000);
  1447. }
  1448. function selectAllChannels() {
  1449. for (let i = 0; i < device_info.chs; i++) currentSelectedChannels[i] = true;
  1450. updateResetModalUI();
  1451. }
  1452. function deselectAllChannels() {
  1453. for (let i = 0; i < device_info.chs; i++) currentSelectedChannels[i] = false;
  1454. updateResetModalUI();
  1455. }
  1456. function updateSummary() {
  1457. let totalVoltage = 0.00, totalCurrent = 0.00, totalActivePower = 0.00, totalReactivePower = 0.00, totalApparentPower = 0.00, totalWeightedPf = 0.000,totalPf=0.00,totalConsumer= 0.00;
  1458. if(device_info.chs > 0)
  1459. {
  1460. totalVoltage = all_channel.voltage;
  1461. totalCurrent = all_channel.current;
  1462. totalActivePower = all_channel.power;
  1463. totalReactivePower = all_channel.reactive_power;
  1464. totalApparentPower = all_channel.app_power;
  1465. totalPf = all_channel.factor;
  1466. totalConsumer = all_channel.consump;
  1467. }
  1468. document.getElementById('totalVoltage').innerHTML = totalVoltage.toFixed(2) + '<span class="summary-unit">V</span>';
  1469. document.getElementById('totalCurrent').innerHTML = totalCurrent.toFixed(2) + '<span class="summary-unit">A</span>';
  1470. document.getElementById('totalActivePower').innerHTML = totalActivePower.toLocaleString() + '<span class="summary-unit">kW</span>';
  1471. document.getElementById('totalReactivePower').innerHTML = totalReactivePower.toLocaleString() + '<span class="summary-unit">kvar</span>';
  1472. document.getElementById('totalApparentPower').innerHTML = totalApparentPower.toLocaleString() + '<span class="summary-unit">kVA</span>';
  1473. document.getElementById('totalPowerFactor').innerHTML = totalPf.toFixed(3);
  1474. document.getElementById('totalConsumer').innerHTML = totalConsumer.toFixed(2) + '<span class="summary-unit">kWh</span>';
  1475. }
  1476. function updateWsStatus(connected, message) {
  1477. const led = document.getElementById('wsLed');
  1478. const statusText = document.getElementById('wsStatusText');
  1479. if (connected) {
  1480. led.className = 'ws-led connected';
  1481. statusText.innerHTML = '已连接';
  1482. } else {
  1483. led.className = 'ws-led disconnected';
  1484. statusText.innerHTML = message || '未连接';
  1485. }
  1486. }
  1487. function onWsMessage(event) {
  1488. try {
  1489. if (event.data instanceof ArrayBuffer) {
  1490. var obj = bin_parse(event.data);
  1491. if (obj.device_info != undefined) {
  1492. if(device_info.chs != obj.device_info.chs)
  1493. {
  1494. device_info.chs = obj.device_info.chs;
  1495. currentSelectedChannels = null;
  1496. currentSelectedChannels = new Array(device_info.chs).fill(true);
  1497. for (let i = 0; i < device_info.chs; i++) {
  1498. channelThresholds[i] = {name:"smartPDU",vMin: 198, vMax: 242, iMax: 100, pMax: 15000, eMax: 500 };
  1499. channelDelays[i] = {name:"smartPDU", onSec: 1.8, offSec: 1.5 };
  1500. channelTelemetry[i] = {name:"smartPDU", voltage: 220, current: 30, activePower: 6600, reactivePower: 2500, apparentPower: 7000, powerFactor: 0.85, frequency: 50.00, energyKwh: 100 };
  1501. channelTelemetry1[i] = {name:"smartPDU", voltage: 220, current: 30, activePower: 6600, reactivePower: 2500, apparentPower: 7000, powerFactor: 0.85, frequency: 50.00, energyKwh: 100 };
  1502. channelTelemetry2[i] = {name:"smartPDU", voltage: 220, current: 30, activePower: 6600, reactivePower: 2500, apparentPower: 7000, powerFactor: 0.85, frequency: 50.00, energyKwh: 100 };
  1503. channelSwitchStates[i] = true;
  1504. channel_info[i] ={name:"SmartPDU",addr:0,sch:0,phse:0,phse1:1,open:1,close:1,status:1,
  1505. l1_v_upper:0,l1_v_lower:0,l1_c_upper:0,l1_p_upper:0,l1_w_upper:0,
  1506. l2_v_upper:0,l2_v_lower:0,l2_c_upper:0,l2_p_upper:0,l2_w_upper:0,
  1507. l3_v_upper:0,l3_v_lower:0,l3_c_upper:0,l3_p_upper:0,l3_w_upper:0
  1508. };
  1509. }
  1510. all_channel.voltage = 0;
  1511. all_channel.current = 0;
  1512. all_channel.power = 0;
  1513. all_channel.reactive_power = 0;
  1514. all_channel.app_power = 0;
  1515. all_channel.factor = 0;
  1516. renderTable();
  1517. }
  1518. device_info.chs = obj.device_info.chs;
  1519. device_info.power_type = obj.device_info.power_type;
  1520. device_info.zone = obj.device_info.zone;
  1521. device_info.board_cnt = obj.device_info.board_cnt;
  1522. device_info.soft_version = obj.device_info.soft_version;
  1523. device_info.hard_version = obj.device_info.hard_version;
  1524. device_info.date = obj.device_info.date;
  1525. device_info.time = obj.device_info.time;
  1526. updateDeviceStatusMsg();
  1527. updateTimeDisplay();
  1528. console.log('WebSocket收到: ', device_info.chs,device_info.date,device_info.time);
  1529. }
  1530. if (obj.power_data != undefined) {
  1531. for (let i = 0; i < device_info.chs; i++) {
  1532. channelTelemetry[i].voltage = obj.power_data[i].voltage;
  1533. channelTelemetry[i].current = obj.power_data[i].current;
  1534. channelTelemetry[i].activePower = obj.power_data[i].power;
  1535. channelTelemetry[i].reactivePower = obj.power_data[i].reactive_power;
  1536. channelTelemetry[i].apparentPower = obj.power_data[i].app_power;
  1537. channelTelemetry[i].powerFactor = obj.power_data[i].factor;
  1538. channelTelemetry[i].frequency = obj.power_data[i].freq;
  1539. channelTelemetry[i].energyKwh = obj.power_data[i].consump;
  1540. obj.power_data[i] = null;
  1541. }
  1542. //updateTableData();
  1543. }
  1544. if (obj.power_data1 != undefined) {
  1545. for (let i = 0; i < device_info.chs; i++) {
  1546. channelTelemetry1[i].voltage = obj.power_data1[i].voltage;
  1547. channelTelemetry1[i].current = obj.power_data1[i].current;
  1548. channelTelemetry1[i].activePower = obj.power_data1[i].power;
  1549. channelTelemetry1[i].reactivePower = obj.power_data1[i].reactive_power;
  1550. channelTelemetry1[i].apparentPower = obj.power_data1[i].app_power;
  1551. channelTelemetry1[i].powerFactor = obj.power_data1[i].factor;
  1552. channelTelemetry1[i].frequency = obj.power_data1[i].freq;
  1553. channelTelemetry1[i].energyKwh = obj.power_data1[i].consump;
  1554. obj.power_data1[i] = null;
  1555. }
  1556. //updateTableData();
  1557. }
  1558. if (obj.power_data2 != undefined) {
  1559. for (let i = 0; i < device_info.chs; i++) {
  1560. channelTelemetry2[i].voltage = obj.power_data2[i].voltage;
  1561. channelTelemetry2[i].current = obj.power_data2[i].current;
  1562. channelTelemetry2[i].activePower = obj.power_data2[i].power;
  1563. channelTelemetry2[i].reactivePower = obj.power_data2[i].reactive_power;
  1564. channelTelemetry2[i].apparentPower = obj.power_data2[i].app_power;
  1565. channelTelemetry2[i].powerFactor = obj.power_data2[i].factor;
  1566. channelTelemetry2[i].frequency = obj.power_data2[i].freq;
  1567. channelTelemetry2[i].energyKwh = obj.power_data2[i].consump;
  1568. obj.power_data2[i] = null;
  1569. }
  1570. //updateTableData();
  1571. }
  1572. if (obj.power_all != undefined) {
  1573. all_channel = obj.power_all;
  1574. obj.power_all = null;
  1575. updateSummary();
  1576. }
  1577. if (obj.power_all_l != undefined) {
  1578. all_l_channel = obj.power_all_l;
  1579. obj.power_all_l = null;
  1580. }
  1581. if (obj.power_info != undefined) {
  1582. channel_info = obj.power_info;
  1583. for(let i =0;i < device_info.chs;i++)
  1584. {
  1585. channelDelays[i].onSec =channel_info[i].open;
  1586. channelDelays[i].offSec = channel_info[i].close;
  1587. channelDelays[i].name = channel_info[i].name;
  1588. channelThresholds[i].name = channel_info[i].name;
  1589. channelTelemetry[i].name = channel_info[i].name;
  1590. }
  1591. obj.power_info = null;
  1592. updateTableData();
  1593. }
  1594. if (obj.power_thr != undefined) {
  1595. for (let i = 0; i < device_info.chs; i++) {
  1596. channelThresholds[i].vMax = obj.power_thr[i].v_upper;
  1597. channelThresholds[i].vMin = obj.power_thr[i].v_lower;
  1598. channelThresholds[i].iMax = obj.power_thr[i].c_upper;
  1599. channelThresholds[i].pMax = obj.power_thr[i].p_upper;
  1600. channelThresholds[i].eMax = obj.power_thr[i].w_upper;
  1601. obj.power_thr[i] = null;
  1602. }
  1603. }
  1604. if(obj.sensor_info != undefined)
  1605. {
  1606. humiValue = obj.sensor_info[0];
  1607. tempValue = obj.sensor_info[1];
  1608. smokeValue = obj.sensor_info[2];
  1609. co2Value = obj.sensor_info[3];
  1610. updateEnvDisplay();
  1611. }
  1612. if(obj.network != undefined)
  1613. {
  1614. netConfig.ipMode = (obj.network.mode == 0) ? "auto" : "manual";
  1615. netConfig.ipAddress = obj.network.ipaddr;
  1616. netConfig.subnetMask = obj.network.mask;
  1617. netConfig.gateway = obj.network.gateway;
  1618. netConfig.dns1 = obj.network.dns1;
  1619. netConfig.dns2 = obj.network.dns2;
  1620. loadNetConfig();
  1621. }
  1622. if(obj.rs485 != undefined)
  1623. {
  1624. rs485Config.mode = obj.rs485.mode == 0? "master" : "slave";
  1625. rs485Config.address = obj.rs485.addr;
  1626. rs485Config.baudrate = obj.rs485.baud;
  1627. loadRs485Config();
  1628. }
  1629. if(obj.snmp != undefined)
  1630. {
  1631. snmpConfig.status = (obj.snmp.mode == 0)? "close" :"open";
  1632. snmpConfig.readCommunity = obj.snmp.publics;
  1633. snmpConfig.writeCommunity = obj.snmp.privates;
  1634. snmpConfig.trapStatus = (obj.snmp.trapmode == 0)? "close" :"open";;
  1635. snmpConfig.trapIp =obj.snmp.nms_ip;
  1636. loadSnmpConfig();
  1637. }
  1638. if(obj.wanni_count != undefined)
  1639. {
  1640. //clearAlarm();
  1641. alarmList = [];
  1642. for(let i = 0; i < obj.wanni_count.count;i++)
  1643. {
  1644. addAlarm(obj.wanning[i],i);
  1645. }
  1646. updateAlarmBadge();
  1647. updateAlarmUI();
  1648. }
  1649. if(obj.user_count != undefined )
  1650. {
  1651. user_count = obj.user_count;
  1652. users = obj.users;
  1653. renderUserTable();
  1654. }
  1655. if(obj.user_add != undefined )
  1656. {
  1657. const errorDiv = document.getElementById('loginAddErrorMsg');
  1658. if(obj.user_add.flag == 1)
  1659. {
  1660. errorDiv.innerText = '用户已存在';
  1661. }else if(obj.user_add.flag == 2)
  1662. {
  1663. errorDiv.innerText = '用户超过10';
  1664. }else
  1665. {
  1666. errorDiv.innerTex='';
  1667. closeModal('userAddModal');
  1668. ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_USER,0, 0, 0));
  1669. }
  1670. }
  1671. if(obj.user_update != undefined)
  1672. {
  1673. const errorDiv = document.getElementById('loginUpdateErrorMsg');
  1674. if(obj.user_update.flag == 1)
  1675. {
  1676. errorDiv.innerText = '密码错误';
  1677. }else
  1678. {
  1679. errorDiv.innerTex='';
  1680. closeModal('userModal');
  1681. ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_USER,0, 0, 0));
  1682. }
  1683. }
  1684. if(obj.user_delete != undefined)
  1685. {
  1686. ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_USER,0, 0, 0));
  1687. }
  1688. }
  1689. } catch (e) {
  1690. console.error('解析WebSocket消息失败:', e);
  1691. }
  1692. }
  1693. function sendSwitchCommand(channel, state) {
  1694. if (ws && ws.readyState === WebSocket.OPEN) {
  1695. showToast("发送指令: 通道" + (channel + 1) + " " + (state ? "开启" : "关闭"), 1000);
  1696. } else {
  1697. showToast("WebSocket未连接,无法发送指令", 1500);
  1698. }
  1699. }
  1700. function sendAllSwitchCommand(state) {
  1701. if (ws && ws.readyState === WebSocket.OPEN) {
  1702. all_contrl(state);
  1703. showToast("发送全部" + (state ? "开启" : "关闭") + "指令", 1000);
  1704. } else {
  1705. showToast("WebSocket未连接,无法发送指令", 1500);
  1706. }
  1707. }
  1708. function deleteUserApi(userId) {
  1709. let user = users[userId].user;
  1710. userDeleteConfig(user);
  1711. }
  1712. function escapeHtml(str) { return str.replace(/[&<>]/g, function(m){ if(m==='&') return '&amp;'; if(m==='<') return '&lt;'; if(m==='>') return '&gt;'; return m;}); }
  1713. function openUserEditModal(userId) {
  1714. // let user = usersList.find(u => u.id === userId);
  1715. // if (!user) return;
  1716. let i = userId;
  1717. document.getElementById('userModalTitle').innerText = '编辑用户';
  1718. document.getElementById('userName').value = users[userId].user;
  1719. document.getElementById('userName').setAttribute('disabled','disabled');
  1720. document.getElementById('userOldPassword').value = '';
  1721. document.getElementById('userNewPassword').value = '';
  1722. document.getElementById('userRole').value = (users[userId].role === 1 )? "superadmin" : (users[userId].role === 2 ) ? "admin" : "user";
  1723. document.getElementById('userModal').classList.add('active');
  1724. }
  1725. function renderUserTable() {
  1726. let tbody = document.getElementById('userTableBody');
  1727. if (!tbody) return;
  1728. if (!user_count.cnt) { tbody.innerHTML = '<tr><td colspan="3" style="text-align:center;">暂无用户数据</td></tr>'; return; }
  1729. let html = '';
  1730. for (let i = 0;i < user_count.cnt;i++) {
  1731. let roleText = (users[i].role === 1) ? '超级管理员' : (users[i].role === 2 ? '管理员' : '普通用户');
  1732. html += `<tr>
  1733. <td style="text-align:center;">${escapeHtml(users[i].user)}</td>
  1734. <td style="text-align:center;">${roleText}</td>
  1735. <td style="text-align:center;">${users[i].date} ${users[i].time}</td>
  1736. <td style="text-align:center;"><button class="btn-icon success" data-id="${i}" data-action="edit">编辑</button>
  1737. <button class="btn-icon danger" data-id="${i}" data-action="delete">删除</button></td>
  1738. </tr>`;
  1739. }
  1740. tbody.innerHTML = html;
  1741. // 绑定编辑删除事件
  1742. tbody.querySelectorAll('[data-action="edit"]').forEach(btn => {
  1743. btn.addEventListener('click', (e) => { let id = parseInt(btn.getAttribute('data-id')); openUserEditModal(id); });
  1744. });
  1745. tbody.querySelectorAll('[data-action="delete"]').forEach(btn => {
  1746. btn.addEventListener('click', (e) => { let id = parseInt(btn.getAttribute('data-id')); if(confirm('确定删除该用户吗?')) deleteUserApi(id); });
  1747. });
  1748. }
  1749. function updateEnvDisplay() {
  1750. document.getElementById('tempStatus').innerHTML= `<div class="${tempValue.status ? 'status-on' : 'status-off'}">${tempValue.status ? "在线" : "离线"}<div>`;
  1751. document.getElementById('humiStatus').innerHTML= `<div class="${humiValue.status ? 'status-on' : 'status-off'}">${humiValue.status ? "在线" : "离线"}<div>`;
  1752. document.getElementById('smokeStatus').innerHTML= `<div class="${smokeValue.status ? 'status-on' : 'status-off'}">${smokeValue.status ? "在线" : "离线"}<div>`
  1753. document.getElementById('co2Status').innerHTML= `<div class="${co2Value.status ? 'status-on' : 'status-off'}">${co2Value.status ? "在线" : "离线"}<div>`
  1754. document.getElementById('tempValue').innerHTML = tempValue.value.toFixed(2) + '<span class="env-unit">°C</span>';
  1755. document.getElementById('humiValue').innerHTML = humiValue.value.toFixed(2) + '<span class="env-unit">%RH</span>';
  1756. document.getElementById('smokeValue').innerHTML = Math.floor(smokeValue.value) + '<span class="env-unit">ppm</span>';
  1757. document.getElementById('co2Value').innerHTML = Math.floor(co2Value.value) + '<span class="env-unit">ppm</span>';
  1758. const tempCard = document.getElementById('tempCard');
  1759. const humiCard = document.getElementById('humiCard');
  1760. const smokeCard = document.getElementById('smokeCard');
  1761. const co2Card = document.getElementById('co2Card');
  1762. tempCard.classList.remove('alarm-warning', 'alarm-danger');
  1763. humiCard.classList.remove('alarm-warning', 'alarm-danger');
  1764. smokeCard.classList.remove('alarm-warning', 'alarm-danger');
  1765. co2Card.classList.remove('alarm-warning', 'alarm-danger');
  1766. if (tempValue.v_max_wrn) tempCard.classList.add('alarm-danger');
  1767. else if(tempValue.v_min_wrn) tempCard.classList.add('alarm-warning');
  1768. if (humiValue.v_max_wrn) humiCard.classList.add('alarm-danger');
  1769. if (smokeValue.v_max_wrn) smokeCard.classList.add('alarm-danger');
  1770. if (co2Value.v_max_wrn) co2Card.classList.add('alarm-danger');
  1771. }
  1772. function updateAlarmBadge() {
  1773. const badge = document.getElementById('alarmCountBadge');
  1774. if (badge) {
  1775. const count = alarmList.length;
  1776. if (count > 0) {
  1777. badge.textContent = count > 10 ? '10' : count;
  1778. badge.style.display = 'inline-flex';
  1779. } else {
  1780. badge.style.display = 'none';
  1781. }
  1782. }
  1783. }
  1784. function updateAlarmUI() {
  1785. const container = document.getElementById('alarmListContainer');
  1786. if (!container) return;
  1787. if (alarmList.length === 0) {
  1788. container.innerHTML = '<div style="text-align:center;color:#888;padding:20px;">暂无报警记录</div>';
  1789. return;
  1790. }
  1791. let html = '<table class="alarm-table"><thead><tr><th>报警ID</th><th>时间</th><th>报警类型</th><th>详细信息</th></tr></thead><tbody>';
  1792. for (let i = 0; i < alarmList.length; i++) {
  1793. const a = alarmList[i];
  1794. html += '<tr><td class="alarm-id">' + a.id + '</td><td>' + a.time + '</td><td><span style="background:#ffe0e0;padding:2px 8px;border-radius:20px;font-size:0.7rem;">' + a.type + '</span></td><td>' + a.message + '</td></tr>';
  1795. }
  1796. html += '</tbody></table>';
  1797. container.innerHTML = html;
  1798. }
  1799. function clearAlarm() {
  1800. clean_alarm();
  1801. alarmList = [];
  1802. showToast("报警记录已清空", 1000);
  1803. }
  1804. function showToast(msg, dur) {
  1805. if (!dur) dur = 1500;
  1806. const t = document.createElement('div');
  1807. t.innerText = msg;
  1808. t.style.cssText = 'position:fixed; bottom:70px; right:24px; background:#1e2f3ce6; color:#fff; padding:8px 20px; border-radius:40px; font-size:0.8rem; z-index:2100;';
  1809. document.body.appendChild(t);
  1810. setTimeout(function() {
  1811. t.style.opacity = '0';
  1812. setTimeout(function() {
  1813. t.remove();
  1814. }, 300);
  1815. }, dur);
  1816. }
  1817. function updateTableData() {
  1818. for (let i = 0; i < device_info.chs; i++) {
  1819. //if (!tableRows[i]) continue;
  1820. const row = tableRows[i];
  1821. if (!row) continue;
  1822. let tele= channelTelemetry[i]|| {};
  1823. let target = channelSwitchStates[i];
  1824. let delays = channelDelays[i];
  1825. let thr = channelThresholds[i];
  1826. let info = channel_info[i];
  1827. // 检查阈值告警
  1828. const alarms = [];
  1829. alarmClass = '';
  1830. const cells = row.cells;
  1831. if (cells[0]) {
  1832. cells[0].innerHTML = '<div class="channel-badge"><span class="ch-icon">CH' + (i + 1) + '</span> <strong>' + (info.name) + '</strong></div>';
  1833. //cells[0].className = 'channel-badge ';
  1834. }
  1835. if (cells[1]) {
  1836. cells[1].innerHTML = (tele.voltage || 0).toFixed(2) + '<span class="unit-sub">V</span>' + (info.l1_v_lower ? '🔻' : (info.l1_v_upper ? '🔺' : ''));
  1837. if(info.l1_v_lower) {
  1838. alarmClass = 'threshold-low-alarm';
  1839. }else if(info.l1_v_upper) {
  1840. alarmClass = 'threshold-alarm';
  1841. }
  1842. cells[1].className = 'numeric-value ' + alarmClass;
  1843. }
  1844. // 电流 (第3列,索引2)
  1845. if (cells[2]) {
  1846. cells[2].innerHTML = (tele.current || 0).toFixed(2) + '<span class="unit-sub">A</span>';
  1847. cells[2].className = 'numeric-value ' + ((info.l1_c_upper) ? 'threshold-alarm' : '');
  1848. }
  1849. // 有功功率 (第4列,索引3)
  1850. if (cells[3]) {
  1851. cells[3].innerHTML = ((tele.activePower || 0).toLocaleString()) + '<span class="unit-sub">kW</span>';
  1852. cells[3].className = 'numeric-value power-bg ' + (info.l1_p_upper? 'threshold-alarm' : '');
  1853. }
  1854. // 无功功率 (第5列,索引4)
  1855. if (cells[4]) {
  1856. cells[4].innerHTML = ((tele.reactivePower || 0).toLocaleString()) + '<span class="unit-sub">kvar</span>';
  1857. cells[4].className = 'numeric-value reactive-bg';
  1858. }
  1859. // 视在功率 (第6列,索引5)
  1860. if (cells[5]) {
  1861. cells[5].innerHTML = ((tele.apparentPower || 0).toLocaleString()) + '<span class="unit-sub">kVA</span>';
  1862. }
  1863. // 功率因数 (第7列,索引6)
  1864. if (cells[6]) {
  1865. cells[6].innerHTML = (tele.powerFactor || 0).toFixed(3);
  1866. }
  1867. // 频率 (第8列,索引7)
  1868. if (cells[7]) {
  1869. cells[7].innerHTML = tele.frequency.toFixed(2) + '<span class="unit-sub">Hz</span>';
  1870. }
  1871. // 耗电量 (第9列,索引8)
  1872. if (cells[8]) {
  1873. cells[8].innerHTML = '<span class="energy-value">' + ((tele.energyKwh || 0).toFixed(2)) + '</span><span class="unit-sub">kWh</span>';
  1874. cells[8].className = 'energy-cell numeric-value ' + (info.l1_w_upper? 'threshold-alarm' : '');
  1875. }
  1876. // 开关延时 (第10列,索引9)
  1877. if (cells[9]) {
  1878. cells[9].innerHTML = '<div class="delay-badge delay-idle">开:' + (info.open) + 's/关:' + (info.close) + 's</div>';
  1879. }
  1880. // 开关状态 (第11列,索引10)
  1881. if (cells[10]) {
  1882. cells[10].innerHTML = '<span class="' + (info.status ? 'status-on' : 'status-off') + ' status-span">' + (info.status ? '开' : '关') + '</span>';
  1883. }
  1884. // 操作按钮 (第12列,索引11)
  1885. if (cells[11]) {
  1886. if (channelSwitchStates[i] != channel_info[i].status) {
  1887. cells[11].innerHTML = '<div class="switch-control"><button class="switch-btn ' + (info.status ? 'switch-on' : 'switch-off') + '" data-idx="' + i + '">' + (info.status ? '关闭' : '打开') + '</button></div>';
  1888. channelSwitchStates[i] = channel_info[i].status
  1889. }
  1890. }
  1891. // 行样式
  1892. if (target) {
  1893. row.classList.remove('channel-off');
  1894. row.classList.add('channel-on');
  1895. } else {
  1896. row.classList.remove('channel-on');
  1897. row.classList.add('channel-off');
  1898. }
  1899. }
  1900. }
  1901. function renderTable() {
  1902. const tbody = document.getElementById('tableBody');
  1903. if (!tbody) return;
  1904. tbody.innerHTML = '';
  1905. tableRows = [];
  1906. for (let i = 0; i < device_info.chs; i++) {
  1907. const tele = channelTelemetry[i] || {};
  1908. const target = channelSwitchStates[i];
  1909. const delays = channelDelays[i];
  1910. const thr = channelThresholds[i];
  1911. const info = channel_info[i];
  1912. const alarms = [];
  1913. if (channel_info.l1_v_lower) alarms.push('欠压');
  1914. if (channel_info.l1_v_upper) alarms.push('过压');
  1915. if (channel_info.l1_c_upper) alarms.push('过流');
  1916. if (channel_info.l1_p_upper) alarms.push('过功率');
  1917. if (channel_info.l1_w_upper) alarms.push('能耗超限');
  1918. const alarmClass = alarms.length > 0 ? 'threshold-alarm' : '';
  1919. const row = document.createElement('tr');
  1920. if (!info.status) row.classList.add('channel-off');
  1921. channelSwitchStates[i] = channel_info[i].status;
  1922. row.innerHTML = `
  1923. <td><div class="channel-badge"><span class="ch-icon">CH${i+1}</span> <strong>${info.name} ${i+1}</strong></div></td>
  1924. <td class="numeric-value ${alarmClass}">${(tele.voltage || 0).toFixed(1)}<span class="unit-sub">V</span>${alarms.includes('欠压') ? '🔻' : (alarms.includes('过压') ? '🔺' : '')}</td>
  1925. <td class="numeric-value ${alarms.includes('过流') ? 'threshold-alarm' : ''}">${(tele.current || 0).toFixed(2)}<span class="unit-sub">A</span></td>
  1926. <td class="numeric-value power-bg ${alarms.includes('过功率') ? 'threshold-alarm' : ''}">${(tele.activePower || 0).toLocaleString()}<span class="unit-sub">W</span></td>
  1927. <td class="numeric-value reactive-bg">${(tele.reactivePower || 0).toLocaleString()}<span class="unit-sub">kvar</span></td>
  1928. <td class="numeric-value">${(tele.apparentPower || 0).toLocaleString()}<span class="unit-sub">kVA</span></td>
  1929. <td class="pf-cell numeric-value">${(tele.powerFactor || 0).toFixed(3)}</td>
  1930. <td class="numeric-value">${(tele.frequency || 50).toFixed(2)}<span class="unit-sub">Hz</span></td>
  1931. <td class="energy-cell numeric-value ${alarms.includes('能耗超限') ? 'threshold-alarm' : ''}"><span class="energy-value">${(tele.energyKwh || 0).toFixed(2)}</span><span class="unit-sub">kWh</span></td>
  1932. <td><div class="delay-badge delay-idle">开:${info.close.toFixed(1)}s/关:${info.open.toFixed(1)}s</div></td>
  1933. <td><span class="${target ? 'status-on' : 'status-off'}">${info.status ? '开' : '关'}</span></td>
  1934. <td><div class="switch-control"><button class="switch-btn ${info.status ? 'switch-on' : 'switch-off'}" data-idx="${i}">${info.status ? '关闭' : '打开'}</button></div></td>
  1935. `;
  1936. tbody.appendChild(row);
  1937. tableRows.push(row)
  1938. }
  1939. tbody.removeEventListener('click', handleSwitchClick);
  1940. tbody.addEventListener('click', handleSwitchClick);
  1941. }
  1942. function handleSwitchClick(e) {
  1943. const btn = e.target.closest('.switch-btn');
  1944. if (!btn) return;
  1945. e.stopPropagation();
  1946. const idx = parseInt(btn.getAttribute('data-idx'));
  1947. if (!isNaN(idx) && idx >= 0 && idx < device_info.chs) {
  1948. sendSwitchCommand(idx, !channelSwitchStates[idx]);
  1949. }
  1950. }
  1951. function sendSwitchCommand(channel, state) {
  1952. if (ws && ws.readyState === WebSocket.OPEN) {
  1953. single_ctrl(channel,state);
  1954. showToast("通道" + (channel + 1) + " " + (state ? "开启" : "关闭"), 1000);
  1955. } else {
  1956. showToast("WebSocket未连接,无法发送指令", 1500);
  1957. }
  1958. }
  1959. function allOn() { sendAllSwitchCommand(true);}
  1960. function allOff() { sendAllSwitchCommand(false);}
  1961. function openAlarmModal() {
  1962. updateAlarmUI();
  1963. document.getElementById('alarmModal').classList.add('active');
  1964. }
  1965. function closeModal(modalId) {document.getElementById(modalId).classList.remove('active');}
  1966. function loadNetConfig() {
  1967. document.getElementById("ipMode").value = netConfig.ipMode;
  1968. document.getElementById("ipAddress").value = netConfig.ipAddress;
  1969. document.getElementById("subnetMask").value = netConfig.subnetMask;
  1970. document.getElementById("gateway").value = netConfig.gateway;
  1971. document.getElementById("dns1").value = netConfig.dns1;
  1972. document.getElementById("dns2").value = netConfig.dns2;
  1973. updateStaticIpFieldsEnable();
  1974. updateNetStatusMsg();
  1975. }
  1976. function loadNtpConfig()
  1977. {
  1978. document.getElementById("ntpZone").value = ntpConfig.ntpZone;
  1979. }
  1980. function updateStaticIpFieldsEnable() {
  1981. const isManual = document.getElementById("ipMode").value === "manual";
  1982. console.log(document.getElementById("ipMode").value);
  1983. ["ipAddress", "subnetMask", "gateway"].forEach(function(id) {
  1984. const el = document.getElementById(id);
  1985. if (el){
  1986. //el.hidden = !isManual;
  1987. isManual ? el.removeAttribute('disabled') : el.setAttribute('disabled','disabled');
  1988. // if(isManual)
  1989. // {
  1990. // el.removeAttribute('disabled');
  1991. // }else
  1992. // {
  1993. // el.setAttribute('disabled','disabled');
  1994. // }
  1995. }
  1996. });
  1997. // ["ip_lable","mask_lable","gate_lable"].forEach(function(id) {
  1998. // const el = document.getElementById(id);
  1999. // if (el){
  2000. // el.style.display = (!isManual) ? 'none' : 'inline';
  2001. // }
  2002. // });
  2003. }
  2004. function saveNetConfig() {
  2005. confirmNetwork();
  2006. showToast("网络配置已保存", 1200);
  2007. }
  2008. function updateNetStatusMsg() {
  2009. const msgDiv = document.getElementById("netStatusMsg");
  2010. if (msgDiv) {
  2011. const modeText = netConfig.ipMode === "auto" ? "自动(DHCP)" : "静态IP: " + netConfig.ipAddress;
  2012. msgDiv.innerHTML = "📡 IP模式: " + modeText + "<br>🌐 DNS: " + netConfig.dns1 + " / " + netConfig.dns2;
  2013. }
  2014. }
  2015. function loadRs485Config() {
  2016. const saved = sessionStorage.getItem("powerMonitorRs485Config");
  2017. document.getElementById("rs485Mode").value = rs485Config.mode;
  2018. document.getElementById("rs485Addr").value = rs485Config.address;
  2019. document.getElementById("rs485Baudrate").value = rs485Config.baudrate;
  2020. updateRs485StatusMsg();
  2021. }
  2022. function saveRs485Config() {
  2023. rs_data = [];
  2024. rs_data.rs485Mode = (document.getElementById('rs485Mode').value == "master") ? 0 : 1;
  2025. rs_data.rs485Addr = document.getElementById('rs485Addr').value || 1;
  2026. rs_data.rs485Baudrate = document.getElementById('rs485Baudrate').value;
  2027. rs_data.len = 6;
  2028. if (rs_data.rs485Addr < 1) rs_data.rs485Addr = 1;
  2029. if (rs_data.rs485Addr > 247) rs_data.rs485Addr = 247;
  2030. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_RS485,0, 0, 0, rs_data);
  2031. ws.send(a);
  2032. showToast("RS485配置已保存", 1200);
  2033. }
  2034. function updateRs485StatusMsg() {
  2035. const msgDiv = document.getElementById("rs485StatusMsg");
  2036. if (msgDiv) {
  2037. const modeText = rs485Config.mode === "master" ? "主机模式" : "从机模式";
  2038. msgDiv.innerHTML = "🔧 模式: " + modeText + " | 地址: " + rs485Config.address + " | 波特率: " + rs485Config.baudrate + " bps";
  2039. }
  2040. }
  2041. function loadSnmpConfig() {
  2042. document.getElementById("snmpReadCommunity").value = snmpConfig.readCommunity;
  2043. document.getElementById("snmpWriteCommunity").value = snmpConfig.writeCommunity;
  2044. document.getElementById("snmpTrapIp").value = snmpConfig.trapIp;
  2045. document.getElementById("snmpTrapStatus").value = snmpConfig.trapStatus;
  2046. updateSnmpEnable();
  2047. updateSnmpStatusMsg();
  2048. }
  2049. function updateSnmpEnable()
  2050. {
  2051. const isManual = document.getElementById("snmpTrapStatus").value === "open";
  2052. const el = document.getElementById("snmpTrapIp");
  2053. if(el) el.hidden = !isManual;
  2054. document.getElementById("trapAddr").style.display = !isManual ? 'none' :'inline';
  2055. }
  2056. function saveSnmpConfig() {
  2057. snmp_data = [];
  2058. snmp_data.mode = 1;
  2059. snmp_data.version = 0;
  2060. snmp_data.snmpReadCommunity = document.getElementById("snmpReadCommunity").value;
  2061. snmp_data.snmpWriteCommunity = document.getElementById("snmpWriteCommunity").value;
  2062. snmp_data.snmpTrapIp = document.getElementById("snmpTrapIp").value;
  2063. snmp_data.snmpTrapStatus = document.getElementById("snmpTrapStatus").value == "open" ? 1 : 0;
  2064. snmp_data.len = 55;
  2065. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_SNMP,0, 0, 0, snmp_data);
  2066. ws.send(a);
  2067. showToast("SNMP配置已保存", 1200);
  2068. }
  2069. function userAddConfig(user,password,role)
  2070. {
  2071. user_data = [];
  2072. user_data.role = role;
  2073. user_data.user = user;
  2074. user_data.password = password;
  2075. user_data.len = 65;
  2076. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_USER,SUB_USER_TYPE.NEW_SUB_PKT_TYPE_USER_ADD, 0, 0, user_data);
  2077. ws.send(a);
  2078. showToast("增加用户",user, 1200);
  2079. }
  2080. function userDeleteConfig(user)
  2081. {
  2082. user_data=[];
  2083. user_data.user = user;
  2084. user_data.len = 32;
  2085. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_USER,SUB_USER_TYPE.NEW_SUB_PKT_TYPE_USER_DELETE, 0, 0, user_data);
  2086. ws.send(a);
  2087. showToast("删除用户",user, 1200);
  2088. }
  2089. function userUpdateConfig(user,old_pass,new_pass,role)
  2090. {
  2091. user_data = [];
  2092. user_data.role = role
  2093. user_data.user = user;
  2094. user_data.old_password = old_pass;
  2095. user_data.new_password = new_pass;
  2096. user_data.len = 97;
  2097. a = pkt_to_bin(0, PKT_TYPE.NEW_PKT_TYPE_USER,SUB_USER_TYPE.NEW_SUB_PKT_TYPE_USER_UPDATE, 0, 0, user_data);
  2098. ws.send(a);
  2099. showToast("修改用户",user, 1200);
  2100. }
  2101. function updateSnmpStatusMsg() {
  2102. const msgDiv = document.getElementById("snmpStatusMsg");
  2103. if (msgDiv) {
  2104. const trapInfo = snmpConfig.trapIp ? (snmpConfig.trapIp) : "未配置";
  2105. msgDiv.innerHTML = "🔐 只读社团: " + snmpConfig.readCommunity + " | ✏️ 读写社团: " + snmpConfig.writeCommunity + "<br>📡 TRAP目标: " + trapInfo;
  2106. }
  2107. }
  2108. function updateDeviceStatusMsg()
  2109. {
  2110. document.getElementById('deviceChannel').textContent = device_info.chs;
  2111. document.getElementById("powerType").textContent = (device_info.power_type == 0)? 'AC-AC' : (device_info.power_type == 1)
  2112. ? 'DC-DC' : (device_info.power_type == 3)
  2113. ? 'AC3-AC3': (device_info.power_type == 4 || device_info.power_type == 5)
  2114. ? 'AC3-AC1': (device_info.power_type == 6)
  2115. ? 'AC3-AC2':'Other';
  2116. document.getElementById("controlBoard").textContent = device_info.board_cnt;
  2117. document.getElementById("softVersion").textContent = device_info.soft_version;
  2118. document.getElementById("zone").textContent = device_info.zone;
  2119. }
  2120. function updateTimeDisplay() {
  2121. const display = document.getElementById("systemTimeDisplay");
  2122. if (display) {
  2123. display.innerHTML = device_info.date + ' ' + device_info.time;
  2124. }
  2125. }
  2126. function startTimeUpdater() {
  2127. clearInterval(device_info_id);
  2128. power_all_id = setInterval(function() { ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_ALL, 0, 0));}, 600);
  2129. power_data_id = setInterval(function() {
  2130. ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_L1, 0, 0));
  2131. setTimeout(() => {ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_L2, 0, 0));},40);
  2132. setTimeout(() => {ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_L3, 0, 0));},40);
  2133. setTimeout(() => {ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_INFO, 0, 0));},40);
  2134. }, 300);
  2135. power_thr_id = setInterval(function() { ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_THR, 0, 0));}, 500);
  2136. sensor_id = setInterval(function() { ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_SENSOR,0, 0, 0));}, 1000);
  2137. wanning_id = setInterval(function() { ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_WANNING,0, 0, 0));}, 3000);
  2138. }
  2139. function setSystemTime(newDate) {
  2140. systemTime = newDate;
  2141. updateTimeDisplay();
  2142. showToast("系统时间已更新为: " + systemTime.toLocaleString(), 1500);
  2143. }
  2144. function switchPage(pageId) {
  2145. document.querySelectorAll('.page').forEach(function(p){ p.classList.remove('active-page'); });
  2146. document.getElementById(pageId + 'Page').classList.add('active-page');
  2147. document.querySelectorAll('.tab-btn').forEach(function(btn){ btn.classList.remove('active'); });
  2148. document.querySelector('.tab-btn[data-page="' + pageId + '"]').classList.add('active');
  2149. if(pageId == 'network')
  2150. {
  2151. clearInterval(power_all_id);
  2152. clearInterval(power_thr_id);
  2153. clearInterval(power_data_id);
  2154. clearInterval(device_info_id);
  2155. clearInterval(sensor_id);
  2156. clearInterval(wanning_id);
  2157. ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_NETWORK,0, 0, 0));
  2158. }else if(pageId == 'rs485')
  2159. {
  2160. clearInterval(power_all_id);
  2161. clearInterval(power_thr_id);
  2162. clearInterval(power_data_id);
  2163. clearInterval(device_info_id);
  2164. clearInterval(sensor_id);
  2165. clearInterval(wanning_id);
  2166. ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_RS485,0, 0, 0));
  2167. }else if(pageId == 'user')
  2168. {
  2169. clearInterval(power_all_id);
  2170. clearInterval(power_thr_id);
  2171. clearInterval(power_data_id);
  2172. clearInterval(device_info_id);
  2173. clearInterval(sensor_id);
  2174. clearInterval(wanning_id);
  2175. ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_USER,0, 0, 0));
  2176. }
  2177. else if(pageId =='monitor')
  2178. {
  2179. clearInterval(device_info_id);
  2180. power_all_id = setInterval(function() { ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_ALL, 0, 0));}, 600);
  2181. power_data_id = setInterval(function() {
  2182. ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_L1, 0, 0));
  2183. setTimeout(() => {ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_L2, 0, 0));},40);
  2184. setTimeout(() => {ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_L3, 0, 0));},40);
  2185. setTimeout(() => {ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_INFO, 0, 0));},40);
  2186. }, 300);
  2187. power_thr_id = setInterval(function() { ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_ELE_INFO, SUB_POWER_TYPE.NEW_SUB_PKT_TYPE_ELE_DATA_THR, 0, 0));}, 500);
  2188. sensor_id = setInterval(function() { ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_SENSOR,0, 0, 0));}, 1000);
  2189. wanning_id = setInterval(function() { ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_WANNING,0, 0, 0));}, 3000);
  2190. }else if(pageId =='snmp')
  2191. {
  2192. clearInterval(power_all_id);
  2193. clearInterval(power_thr_id);
  2194. clearInterval(power_data_id);
  2195. clearInterval(device_info_id);
  2196. clearInterval(sensor_id);
  2197. clearInterval(wanning_id);
  2198. ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_SNMP, 0, 0, 0));
  2199. }
  2200. else if(pageId == 'device')
  2201. {
  2202. clearInterval(power_all_id);
  2203. clearInterval(power_thr_id);
  2204. clearInterval(power_data_id);
  2205. clearInterval(sensor_id);
  2206. clearInterval(wanning_id);
  2207. ntpConfig.ntpZone = device_info.zone;
  2208. loadNtpConfig();
  2209. device_info_id = setInterval(function() { ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_DEVICE_INFO,0, 0, 0));}, 1000);
  2210. }
  2211. }
  2212. function connectWebSocket() {
  2213. const wsUrl = 'ws://' + window.location.host + '/ws';
  2214. //const wsUrl = 'ws://' + '192.168.9.102' + '/ws';
  2215. updateWsStatus(false, '正在连接...');
  2216. const led = document.getElementById('wsLed');
  2217. led.className = 'ws-led connecting';
  2218. try {
  2219. ws = new WebSocket(wsUrl);
  2220. ws.onopen = function() {
  2221. console.log('WebSocket连接成功');
  2222. wsConnected = true;
  2223. reconnectAttempts = 0;
  2224. updateWsStatus(true, '已连接');
  2225. ws.send(mk_hdr_bin(0, PKT_TYPE.NEW_PKT_TYPE_DEVICE_INFO, 0, 0, 0));
  2226. };
  2227. ws.binaryType = 'arraybuffer';
  2228. ws.onmessage = onWsMessage;
  2229. ws.onerror = function(error) { console.error('WebSocket错误:', error); updateWsStatus(false, '连接错误'); };
  2230. ws.onclose = function() {
  2231. console.log('WebSocket连接关闭');
  2232. wsConnected = false;
  2233. updateWsStatus(false, '连接已断开');
  2234. if (reconnectTimer)
  2235. clearTimeout(reconnectTimer);
  2236. if (reconnectAttempts < maxReconnectAttempts) {
  2237. reconnectAttempts++;
  2238. reconnectTimer = setTimeout(function() {
  2239. connectWebSocket();
  2240. }, 3000);
  2241. } else {
  2242. updateWsStatus(false, '连接失败,请刷新页面重试');
  2243. }
  2244. };
  2245. } catch(e) { console.error('创建WebSocket失败:', e); updateWsStatus(false, '创建连接失败'); }
  2246. }
  2247. function openAddUserModal() {
  2248. currentEditUserId = null;
  2249. document.getElementById('userModalTitle').innerText = '新增用户';
  2250. document.getElementById('userName').value = '';
  2251. document.getElementById('userAddPassword').value = '';
  2252. document.getElementById('userAddModal').classList.add('active');
  2253. }
  2254. function confirmUserAddAction()
  2255. {
  2256. let username = document.getElementById('userAddName').value.trim();
  2257. let password = document.getElementById('userAddPassword').value;
  2258. let role_s = document.getElementById('userRole').value;
  2259. let role = (role_s == "superadmin") ? 1 : (role_s == "admin") ? 2 : 3;
  2260. if (!username || !password) { showToast("用户名或密码不能为空", 1000); return; }
  2261. userAddConfig(username,password,role)
  2262. }
  2263. function confirmUserAction() {
  2264. let username = document.getElementById('userName').value.trim();
  2265. let old_password = document.getElementById('userOldPassword').value;
  2266. let new_password = document.getElementById('userNewPassword').value;
  2267. let role_s = document.getElementById('userRole').value;
  2268. let role = (role_s == "superadmin") ? 1 : (role_s == "admin") ? 2 : 3;
  2269. if(!old_password || !new_password)
  2270. {
  2271. showToast("编辑用户必须填写密码", 1000); return;
  2272. }
  2273. userUpdateConfig(username,old_password,new_password,role);
  2274. }
  2275. function initApp() {
  2276. connectWebSocket();
  2277. startTimeUpdater();
  2278. document.getElementById('openDelayConfigBtn').addEventListener('click', openDelayModal);
  2279. document.getElementById('openThresholdConfigBtn').addEventListener('click', openThresholdModal);
  2280. document.getElementById('openResetEnergyModalBtn').addEventListener('click', openResetEnergyModal);
  2281. document.getElementById('allOnBtn').addEventListener('click', allOn);
  2282. document.getElementById('allOffBtn').addEventListener('click', allOff);
  2283. document.getElementById('openAlarmModalBtn').addEventListener('click', openAlarmModal);
  2284. document.getElementById('cleanSoundLight').addEventListener('click', clean_sound_light);
  2285. document.getElementById('tempCard').addEventListener('click',openTempModal);
  2286. document.getElementById('humiCard').addEventListener('click',openHumiModal);
  2287. document.getElementById('smokeCard').addEventListener('click',openSmokeModal);
  2288. document.getElementById('co2Card').addEventListener('click',openCo2Modal);
  2289. document.getElementById('setSystemRebootBtn').addEventListener('click',rebootSystem);
  2290. document.getElementById('addUserBtn')?.addEventListener('click', openAddUserModal);
  2291. document.getElementById('closeUserModalBtn')?.addEventListener('click',()=>closeModal('userModal'));
  2292. document.getElementById('cancelUserModalBtn')?.addEventListener('click',()=>closeModal('userModal'));
  2293. document.getElementById('closeUserAddModalBtn')?.addEventListener('click',()=>closeModal('userAddModal'));
  2294. document.getElementById('cancelUserAddModalBtn')?.addEventListener('click',()=>closeModal('userAddModal'));
  2295. document.getElementById('confirmUserAddBtn')?.addEventListener('click', confirmUserAddAction);
  2296. document.getElementById('confirmUserBtn')?.addEventListener('click', confirmUserAction);
  2297. document.getElementById('closeAlarmModalBtn').addEventListener('click', function() {
  2298. closeModal('alarmModal');
  2299. });
  2300. document.getElementById('cancelAlarmModalBtn').addEventListener('click', function() {
  2301. closeModal('alarmModal');
  2302. });
  2303. document.getElementById('clearAlarmBtn').addEventListener('click', clearAlarm);
  2304. document.getElementById('saveNetConfigBtn').addEventListener('click', saveNetConfig);
  2305. document.getElementById('saveRs485ConfigBtn').addEventListener('click', saveRs485Config);
  2306. document.getElementById('saveSnmpConfigBtn').addEventListener('click', saveSnmpConfig);
  2307. document.getElementById('ipMode').addEventListener('change', updateStaticIpFieldsEnable);
  2308. document.getElementById('snmpTrapStatus').addEventListener('change', updateSnmpEnable);
  2309. document.getElementById('reconnectBtn').addEventListener('click', function() {
  2310. if (ws) ws.close();
  2311. connectWebSocket();
  2312. });
  2313. document.getElementById('setSystemTimeBtn').addEventListener('click', function() {
  2314. confirmNtp();
  2315. });
  2316. const tabs = document.querySelectorAll('.tab-btn');
  2317. for (let j = 0; j < tabs.length; j++) tabs[j].addEventListener('click', function() {
  2318. switchPage(this.dataset.page);
  2319. });
  2320. document.getElementById('closeTempModalBtn').addEventListener('click', function() {
  2321. closeModal('tempCardModal');
  2322. });
  2323. document.getElementById('closeHumiModalBtn').addEventListener('click', function() {
  2324. closeModal('humiCardModal');
  2325. });
  2326. document.getElementById('closeSmokeModalBtn').addEventListener('click', function() {
  2327. closeModal('smokeCardModal');
  2328. });
  2329. document.getElementById('closeCo2ModalBtn').addEventListener('click', function() {
  2330. closeModal('co2CardModal');
  2331. });
  2332. document.getElementById('closeDelayModalBtn').addEventListener('click', function() {
  2333. closeModal('delayModal');
  2334. });
  2335. document.getElementById('cancelDelayModalBtn').addEventListener('click', function() {
  2336. closeModal('delayModal');
  2337. });
  2338. document.getElementById('closeThresholdModalBtn').addEventListener('click', function() {
  2339. closeModal('thresholdModal');
  2340. });
  2341. document.getElementById('cancelThresholdModalBtn').addEventListener('click', function() {
  2342. closeModal('thresholdModal');
  2343. });
  2344. document.getElementById('closeResetModalBtn').addEventListener('click', function() {
  2345. closeModal('resetEnergyModal');
  2346. });
  2347. document.getElementById('cancelResetModalBtn').addEventListener('click', function() {
  2348. closeModal('resetEnergyModal');
  2349. });
  2350. document.getElementById('confirmTempThrBtn').addEventListener('click', confirmTempThr);
  2351. document.getElementById('confirmHumiThrBtn').addEventListener('click', confirmHumiThr);
  2352. document.getElementById('confirmSmokeThrBtn').addEventListener('click', confirmSmokeThr);
  2353. document.getElementById('confirmCo2ThrBtn').addEventListener('click', confirmCo2Thr);
  2354. document.getElementById('confirmResetEnergyBtn').addEventListener('click', confirmResetEnergy);
  2355. document.getElementById('selectAllChannels').addEventListener('click', selectAllChannels);
  2356. document.getElementById('deselectAllChannels').addEventListener('click', deselectAllChannels);
  2357. const modals = ['delayModal', 'thresholdModal', 'resetEnergyModal', 'alarmModal'];
  2358. for (let k = 0; k < modals.length; k++)(function(id) {
  2359. const el = document.getElementById(id);
  2360. if (el) el.addEventListener('click', function(e) {
  2361. if (e.target === document.getElementById(id)) closeModal(id);
  2362. });
  2363. })(modals[k]);
  2364. document.getElementById('reconnectBtn').addEventListener('click',()=>{ if(ws)ws.close(); connectWebSocket(); });
  2365. document.getElementById('logoutBtn').addEventListener('click',()=>{ sessionStorage.removeItem(STORAGE_KEY);
  2366. if(ws && ws.readyState===WebSocket.OPEN)
  2367. ws.close();
  2368. if(power_all_id) clearInterval(power_all_id);
  2369. if(power_data_id) clearInterval(power_data_id);
  2370. if(power_thr_id) clearInterval(power_thr_id);
  2371. if(sensor_id) clearInterval(sensor_id);
  2372. if(wanning_id) clearInterval(wanning_id);
  2373. if(device_info_id) clearInterval(device_info_id);
  2374. document.getElementById('loginContainer').style.display='flex';
  2375. document.getElementById('dashboardApp').style.display='none';
  2376. document.getElementById('loginUsername').value='';
  2377. document.getElementById('loginPassword').value='';
  2378. document.getElementById('loginErrorMsg').innerText=''; });
  2379. const pwdInput = document.getElementById('loginPassword');
  2380. if (pwdInput) {
  2381. pwdInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') document.getElementById('loginBtn').click(); });
  2382. }
  2383. }
  2384. function performLogin(username, password) {
  2385. const errorDiv = document.getElementById('loginErrorMsg');
  2386. if (!username || !password) { errorDiv.innerText = '请输入用户名和密码'; return; }
  2387. errorDiv.innerText = '正在验证...';
  2388. //const wsUrl = 'ws://192.168.9.102/ws';
  2389. const wsUrl = 'ws://' + window.location.host + '/ws';
  2390. tempws = new WebSocket(wsUrl);
  2391. tempws.binaryType = 'arraybuffer';
  2392. let responded = false;
  2393. tempws.onopen = function() {
  2394. const encoder = new TextEncoder();
  2395. const unameBuf = new Uint8Array(32);
  2396. const pwdBuf = new Uint8Array(32);
  2397. unameBuf.set(encoder.encode(username).slice(0,32));
  2398. pwdBuf.set(encoder.encode(password).slice(0,32));
  2399. const payload = new ArrayBuffer(64);
  2400. const view = new Uint8Array(payload);
  2401. view.set(unameBuf,0); view.set(pwdBuf,32);
  2402. const header = mk_hdr_bin(0,0x08,0,0,0,64);
  2403. const fullMsg = array_cat(header,payload);
  2404. tempws.send(fullMsg);
  2405. };
  2406. tempws.onmessage = function(event) {
  2407. if(responded) return;
  2408. try {
  2409. if (event.data instanceof ArrayBuffer) {
  2410. var obj = bin_parse(event.data);
  2411. if (obj.login != undefined) {
  2412. if(obj.login.flag === 1)
  2413. {
  2414. responded=true;
  2415. tempws.close();
  2416. errorDiv.innerText='';
  2417. document.getElementById('loginContainer').style.display='none';
  2418. document.getElementById('dashboardApp').style.display='block';
  2419. sessionStorage.setItem(STORAGE_KEY,'true');
  2420. initApp();
  2421. }else if(obj.login.flag === 2)
  2422. {
  2423. responded=true;
  2424. tempws.close();
  2425. errorDiv.innerText='密码错误';
  2426. }else if(obj.login.flag === 0)
  2427. {
  2428. responded=true;
  2429. tempws.close();
  2430. errorDiv.innerText='用户名错误';
  2431. }
  2432. }
  2433. }
  2434. }catch (e) {
  2435. console.error('解析WebSocket消息失败:', e);
  2436. }
  2437. };
  2438. tempws.onerror = function() {
  2439. if(!responded){
  2440. responded=true;
  2441. errorDiv.innerText='连接服务器失败';
  2442. }
  2443. };
  2444. tempws.onclose = function() { if(!responded) errorDiv.innerText='连接已断开'; };
  2445. }
  2446. const STORAGE_KEY = 'power_monitor_auth';
  2447. const MAGIC = 0xdead;
  2448. let power_all_id = 0;
  2449. let power_thr_id = 0;
  2450. let power_data_id = 0;
  2451. let device_info_id = 0;
  2452. let sensor_id = 0;
  2453. let wanning_id = 0;
  2454. let device_info = [];
  2455. let channelDelays = [];
  2456. let channelThresholds = [];
  2457. let tempValue = [];
  2458. let humiValue = [];
  2459. let smokeValue = [];
  2460. let co2Value = [];
  2461. let currentSelectedChannels = [];
  2462. let channelTelemetry = [];
  2463. let channelTelemetry1 = [];
  2464. let channelTelemetry2 = [];
  2465. let channelSwitchStates = [];
  2466. let envData = {temp: 25,humi: 50,smoke: 100,co2: 400};
  2467. let alarmList = [];
  2468. let nextAlarmId = 1;
  2469. let all_channel = [];
  2470. let all_l_channel = [];
  2471. let channel_info = [];
  2472. let update_flag = 0;
  2473. let show_l_info = 0;
  2474. let tableRows = [];
  2475. let netConfig = {
  2476. ipMode: "auto",
  2477. ipAddress: "192.168.1.100",
  2478. subnetMask: "255.255.255.0",
  2479. gateway: "192.168.1.1",
  2480. dns1: "114.114.114.114",
  2481. dns2: "8.8.8.8"
  2482. };
  2483. let ntpConfig = {
  2484. ntpZone:"8",
  2485. };
  2486. let rs485Config = {
  2487. mode: "master",
  2488. address: 1,
  2489. baudrate: "9600"
  2490. };
  2491. let snmpConfig = {
  2492. status : "open",
  2493. readCommunity: "public",
  2494. writeCommunity: "private",
  2495. trapStatus: "open",
  2496. trapIp: "",
  2497. };
  2498. let users = [];
  2499. let user_cnt = [];
  2500. let systemTime = new Date();
  2501. // WebSocket 连接
  2502. let ws = null;
  2503. let wsConnected = false;
  2504. let reconnectAttempts = 0;
  2505. const maxReconnectAttempts = 10;
  2506. let reconnectTimer = null;
  2507. document.getElementById('loginBtn').addEventListener('click',()=>{
  2508. let user=document.getElementById('loginUsername').value.trim();
  2509. let pwd=document.getElementById('loginPassword').value;
  2510. performLogin(user,pwd);
  2511. });
  2512. if(sessionStorage.getItem(STORAGE_KEY)==='true'){
  2513. document.getElementById('loginContainer').style.display='none';
  2514. document.getElementById('dashboardApp').style.display='block';
  2515. initApp();
  2516. } else {
  2517. document.getElementById('loginContainer').style.display='flex';
  2518. document.getElementById('dashboardApp').style.display='none';
  2519. }
  2520. </script>
  2521. </body>
  2522. </html>