index.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. {% extends "base.html" %}
  2. {% block content %}
  3. <section class="platform-section">
  4. <h2>云平台连接设置</h2>
  5. <div class="platform-controls">
  6. <div class="platform-select">
  7. <label for="platform">云平台选择</label>
  8. <select id="platform">
  9. {% for key, platform in platform_c.items() %}
  10. <option value="{{ key }}" {% if current_platform == key %}selected{% endif %}>{{ platform.name }}</option>
  11. {% endfor %}
  12. </select>
  13. </div>
  14. <button id="connect-btn" class="disconnected">已断开</button>
  15. </div>
  16. <div class="platform-info" id="platform-info">
  17. <div>IP地址: <span id="platform-ip">加载中...</span></div>
  18. <div class="connection-status" id="connection-status"></div>
  19. <div>后台已运行: <span id="run-time">加载中...</span></div>
  20. </div>
  21. <div class="mqtt-publish">
  22. <h3>发布测试消息</h3>
  23. <div class="form-group">
  24. <label for="mqtt-message">消息内容</label>
  25. <input type="text" id="mqtt-message" placeholder="输入要发布的消息..." value='{"status": "online"}'>
  26. </div>
  27. <button id="publish-btn" class="publish-btn">发布消息</button>
  28. </div>
  29. </section>
  30. <section class="feature-nav">
  31. <div class="feature-card">
  32. <h3>固件包管理</h3>
  33. <p>固件管理:上传、下载、删除、编辑</p>
  34. <a href="{{ url_for('firmware') }}" class="feature-btn">进入</a>
  35. </div>
  36. <div class="feature-card">
  37. <h3>设备配置</h3>
  38. <p>管理设备,进行批量操作和参数配置</p>
  39. <a href="{{ url_for('devices') }}" class="feature-btn">进入</a>
  40. </div>
  41. <div class="feature-card">
  42. <h3>用户中心</h3>
  43. <p>查看用户信息,管理账户设置</p>
  44. <a href="{{ url_for('login') }}" class="feature-btn">进入</a>
  45. </div>
  46. <div class="feature-card">
  47. <h3>应用管理</h3>
  48. <p>重启应用,更新应用</p>
  49. <a href="{{ url_for('app_manage') }}" class="feature-btn">进入</a>
  50. </div>
  51. </section>
  52. {% endblock %}
  53. {% block scripts %}
  54. <script>
  55. // 初始化连接状态为已断开
  56. document.addEventListener('DOMContentLoaded', function() {
  57. // 更新状态显示
  58. const statusEl = document.getElementById('connection-status');
  59. statusEl.textContent = '状态: 已断开';
  60. statusEl.className = 'connection-status status-disconnected';
  61. // 更新按钮显示
  62. const connectBtn = document.getElementById('connect-btn');
  63. connectBtn.className = 'disconnected';
  64. connectBtn.textContent = '已断开';
  65. // 初始加载云平台状态
  66. loadPlatformStatus(currentPlatform);
  67. });
  68. // 更新运行时长
  69. function updateRunTime() {
  70. fetch("{{ url_for('get_run_time') }}")
  71. .then(response => response.json())
  72. .then(data => {
  73. document.getElementById('run-time').textContent = data.run_time;
  74. })
  75. .catch(error => console.error('获取运行时长失败:', error));
  76. }
  77. // 当前选中的云平台
  78. let currentPlatform = '{{ current_platform }}';
  79. // 云平台数据
  80. const platformData = {{ platform_c|tojson }};
  81. console.log('云平台数据:', platformData); // 调试信息
  82. // 确保platforms是对象且包含platform_c
  83. // const platformData = platform_c && platform_c.platform_c ? platform_c.platform_c : {};
  84. // 更新云平台信息显示
  85. function updatePlatformInfo(platformId) {
  86. const platform = platformData[platformId];
  87. if (!platform) return;
  88. console.log('云平台数据:', platform.name, platform.status); // 调试信息
  89. // 更新信息显示
  90. document.getElementById('platform-ip').textContent = platform.ip;
  91. // 删除了对platform-sub-topic的更新,因为该元素已不存在
  92. // 更新状态显示
  93. const statusEl = document.getElementById('connection-status');
  94. statusEl.textContent = `状态: ${platform.status === 'connected' ? '已连接' : '已断开'}`;
  95. statusEl.className = `connection-status ${platform.status === 'connected' ? 'status-connected' : 'status-disconnected'}`;
  96. // 更新按钮显示
  97. const connectBtn = document.getElementById('connect-btn');
  98. connectBtn.className = platform.status === 'connected' ? 'connected' : 'disconnected';
  99. connectBtn.textContent = platform.status === 'connected' ? '已连接' : '已断开';
  100. }
  101. // 加载云平台状态
  102. function loadPlatformStatus(platformId) {
  103. fetch(`{{ url_for('get_platform_status') }}?platform_id=${platformId}`)
  104. .then(response => response.json())
  105. .then(data => {
  106. if (data.status === 'success') {
  107. platformData[platformId] = data.platform;
  108. updatePlatformInfo(platformId);
  109. }
  110. })
  111. .catch(error => console.error('加载云平台状态失败:', error));
  112. }
  113. // 选择云平台
  114. document.getElementById('platform').addEventListener('change', function() {
  115. const selectedPlatform = this.value;
  116. currentPlatform = selectedPlatform;
  117. // 通知后端选择了新云平台
  118. fetch("{{ url_for('select_platform') }}", {
  119. method: 'POST',
  120. headers: {
  121. 'Content-Type': 'application/json',
  122. },
  123. body: JSON.stringify({ platform_id: selectedPlatform })
  124. })
  125. .then(response => response.json())
  126. .then(data => {
  127. if (data.status === 'success') {
  128. platformData[selectedPlatform] = data.platform;
  129. updatePlatformInfo(selectedPlatform);
  130. }
  131. });
  132. });
  133. // 连接/断开按钮
  134. document.getElementById('connect-btn').addEventListener('click', function() {
  135. const connectBtn = this;
  136. // 防止重复点击
  137. if (connectBtn.disabled) return;
  138. connectBtn.disabled = true;
  139. connectBtn.textContent = '处理中...';
  140. // 切换连接状态
  141. fetch("{{ url_for('toggle_connection') }}", {
  142. method: 'POST',
  143. headers: {
  144. 'Content-Type': 'application/json',
  145. },
  146. body: JSON.stringify({ platform_id: currentPlatform })
  147. })
  148. .then(response => response.json())
  149. .then(data => {
  150. if (data.status === 'success') {
  151. // 立即更新状态显示
  152. platformData[currentPlatform].status = data.new_status;
  153. updatePlatformInfo(currentPlatform);
  154. }
  155. })
  156. .catch(error => {
  157. console.error('切换连接状态失败:', error);
  158. // 恢复按钮状态
  159. loadPlatformStatus(currentPlatform);
  160. })
  161. .finally(() => {
  162. connectBtn.disabled = false;
  163. });
  164. });
  165. // 发布消息按钮
  166. document.getElementById('publish-btn').addEventListener('click', function() {
  167. const message = document.getElementById('mqtt-message').value;
  168. if (!message) {
  169. alert('请输入消息内容');
  170. return;
  171. }
  172. fetch("{{ url_for('publish_message') }}", {
  173. method: 'POST',
  174. headers: {
  175. 'Content-Type': 'application/json',
  176. },
  177. body: JSON.stringify({
  178. platform_id: currentPlatform,
  179. message: message
  180. })
  181. })
  182. .then(response => response.json())
  183. .then(data => {
  184. alert(data.message);
  185. });
  186. });
  187. // 更新运行时长
  188. function updateRunTime() {
  189. fetch("{{ url_for('get_run_time') }}")
  190. .then(response => response.json())
  191. .then(data => {
  192. document.getElementById('run-time').textContent = data.run_time;
  193. })
  194. .catch(error => console.error('获取运行时长失败:', error));
  195. }
  196. // 统一更新函数
  197. function updateAll() {
  198. updateRunTime();
  199. loadPlatformStatus(currentPlatform);
  200. }
  201. // 初始加载
  202. document.addEventListener('DOMContentLoaded', function() {
  203. updatePlatformInfo(currentPlatform);
  204. updateRunTime(); // 初始化运行时长
  205. // 定期刷新所有数据
  206. setInterval(updateAll, 1000);
  207. });
  208. </script>
  209. {% endblock %}