index.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. {% extends "bootstrap/base.html" %}
  2. {% block title %}Brake-MQTT example{% endblock %}
  3. {% block styles %}
  4. {{ super() }}
  5. {% endblock %}
  6. {% block scripts %}
  7. {{ super() }}
  8. <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>
  9. <script type="text/javascript" charset="utf-8">
  10. $(document).ready(function() {
  11. //建立連接
  12. var socket = io.connect('http://' + document.domain + ':' + location.port);
  13. MQTT();
  14. var duration = 1000 * 60 * 1;
  15. var timer = setInterval(MQTT, duration);
  16. function MQTT(){
  17. console.log("start to send mqtt...");
  18. var topic = $('#topic').val();
  19. var command = $('#command').val();
  20. var qos = $('#qos').val();
  21. var data = '{"topic": "' + topic + '", "command": "' + command + '", "qos": ' + qos + '}';
  22. //發送訊息到app.py
  23. socket.emit('publish', data=data);
  24. };
  25. //接收回傳訊息
  26. socket.on('mqtt_message', function(data) {
  27. var text = data['payload'];
  28. var $textarea = $('#subscribe_messages');
  29. $textarea.val($textarea.val() + text + '\n');
  30. console.log("recieve mqtt message...");
  31. if(data['error']){
  32. alert("fail over three times!Please check);
  33. };
  34. })
  35. });
  36. </script>
  37. {% endblock %}
  38. {% block content %}
  39. <div class="container-fluid">
  40. <div class="row">
  41. <div class="col-xs-12">
  42. <h1>Brake-MQTT</h1>
  43. </div>
  44. </div>
  45. <div class="row">
  46. <div class="col-xs-6">
  47. <div class="panel panel-default">
  48. <div class="panel-heading">
  49. <h3 class="panel-title">Publish MQTT Message</h3>
  50. </div>
  51. <div class="panel-body">
  52. <div class="col-xs-12">
  53. <div class="row">
  54. <div class="form-horizontal">
  55. <div class="form-group">
  56. <label class="control-label col-xs-4">Topic: </label>
  57. <div class="col-xs-8">
  58. <input id="topic" class="form-control" value="AISKY/AppleFarm/MK-G/9c:65:f9:1e:66:b3">
  59. </div>
  60. </div>
  61. <div class="form-group">
  62. <label class="control-label col-xs-4">Command: </label>
  63. <div class="col-xs-8">
  64. <input id="command" class="form-control">
  65. </div>
  66. </div>
  67. <div class="form-group">
  68. <label class="control-label col-xs-4">Qos: </label>
  69. <div class="col-xs-8">
  70. <select id="qos" class="form-control">
  71. <option value=0>0</option>
  72. <option value=1>1</option>
  73. <option value=2>2</option>
  74. </select>
  75. </div>
  76. </div>
  77. <div class="form-group">
  78. <div class="col-xs-8 col-xs-offset-4">
  79. <button id="publish" class="btn btn-primary">Publish</button>
  80. </div>
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. </div>
  86. </div>
  87. </div>
  88. <div class="col-xs-6">
  89. <div class="panel panel-default">
  90. <div class="panel-heading">
  91. <h3 class="panel-title">Subscribe MQTT Messages</h3>
  92. </div>
  93. <div class="panel-body">
  94. <div class="col-xs-12">
  95. <div class="row">
  96. <div class="form-horizontal">
  97. <div class="form-group">
  98. <label class="control-label col-xs-4">Messages:</label>
  99. <div class="col-xs-8">
  100. <textarea id="subscribe_messages" class="form-control" rows=10></textarea>
  101. </div>
  102. </div>
  103. </div>
  104. </div>
  105. </div>
  106. </div>
  107. </div>
  108. </div>
  109. </div>
  110. </div>
  111. {% endblock %}