smartgw.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. 'use strict';
  2. 'require uci';
  3. 'require view';
  4. 'require poll';
  5. 'require rpc';
  6. 'require ui';
  7. return view.extend({
  8. callGetJsonStatus: rpc.declare({
  9. object: 'olsrinfo',
  10. method: 'getjsondata',
  11. params: ['otable', 'v4_port', 'v6_port'],
  12. }),
  13. fetch_jsoninfo: function (otable) {
  14. var jsonreq4 = '';
  15. var jsonreq6 = '';
  16. var v4_port = parseInt(uci.get('olsrd', 'olsrd_jsoninfo', 'port') || '') || 9090;
  17. var v6_port = parseInt(uci.get('olsrd6', 'olsrd_jsoninfo', 'port') || '') || 9090;
  18. var json;
  19. var self = this;
  20. return new Promise(function (resolve, reject) {
  21. L.resolveDefault(self.callGetJsonStatus(otable, v4_port, v6_port), {})
  22. .then(function (res) {
  23. json = res;
  24. jsonreq4 = JSON.parse(json.jsonreq4);
  25. jsonreq6 = json.jsonreq6 !== '' ? JSON.parse(json.jsonreq6) : [];
  26. var jsondata4 = {};
  27. var jsondata6 = {};
  28. var data4 = [];
  29. var data6 = [];
  30. var has_v4 = false;
  31. var has_v6 = false;
  32. if (jsonreq4 === '' && jsonreq6 === '') {
  33. window.location.href = 'error_olsr';
  34. reject([null, 0, 0, true]);
  35. return;
  36. }
  37. if (jsonreq4 !== '') {
  38. has_v4 = true;
  39. jsondata4 = jsonreq4 || {};
  40. if (otable === 'status') {
  41. data4 = jsondata4;
  42. } else {
  43. data4 = jsondata4[otable] || [];
  44. }
  45. for (var i = 0; i < data4.length; i++) {
  46. data4[i]['proto'] = '4';
  47. }
  48. }
  49. if (jsonreq6 !== '') {
  50. has_v6 = true;
  51. jsondata6 = jsonreq6 || {};
  52. if (otable === 'status') {
  53. data6 = jsondata6;
  54. } else {
  55. data6 = jsondata6[otable] || [];
  56. }
  57. for (var j = 0; j < data6.length; j++) {
  58. data6[j]['proto'] = '6';
  59. }
  60. }
  61. for (var k = 0; k < data6.length; k++) {
  62. data4.push(data6[k]);
  63. }
  64. resolve([data4, has_v4, has_v6, false]);
  65. })
  66. .catch(function (err) {
  67. console.error(err);
  68. reject([null, 0, 0, true]);
  69. });
  70. });
  71. },
  72. action_smartgw: function () {
  73. var self = this;
  74. return new Promise(function (resolve, reject) {
  75. self
  76. .fetch_jsoninfo('gateways')
  77. .then(function ([data, has_v4, has_v6, error]) {
  78. if (error) {
  79. reject(error);
  80. }
  81. function compare(a, b) {
  82. if (a.proto === b.proto) {
  83. return a.cost < b.cost;
  84. } else {
  85. return a.proto < b.proto;
  86. }
  87. }
  88. data.ipv4.sort(compare);
  89. data.ipv6.sort(compare);
  90. var result = { gws: data, has_v4: has_v4, has_v6: has_v6 };
  91. resolve(result);
  92. })
  93. .catch(function (err) {
  94. reject(err);
  95. });
  96. });
  97. },
  98. load: function () {
  99. var self = this;
  100. poll.add(function () {
  101. self.render();
  102. }, 5);
  103. return Promise.all([uci.load('olsrd'), uci.load('luci_olsr')]);
  104. },
  105. render: function () {
  106. var gws_res;
  107. var has_v4;
  108. var has_v6;
  109. var self = this;
  110. return this.action_smartgw()
  111. .then(function (result) {
  112. gws_res = result.gws;
  113. has_v4 = result.has_v4;
  114. has_v6 = result.has_v6;
  115. var fieldset = E('fieldset', { 'class': 'cbi-section' }, [
  116. E('legend', {}, _('Overview of smart gateways in this network')),
  117. E('div', { 'class': 'table cbi-section-table', 'id': 'olsrd_smartgw' }, [
  118. E('div', { 'class': 'tr cbi-section-table-titles' }, [
  119. E('div', { 'class': 'th cbi-section-table-cell' }, _('Gateway')),
  120. E('div', { 'class': 'th cbi-section-table-cell' }, _('Selected')),
  121. E('div', { 'class': 'th cbi-section-table-cell' }, _('ETX')),
  122. E('div', { 'class': 'th cbi-section-table-cell' }, _('Hops')),
  123. E('div', { 'class': 'th cbi-section-table-cell' }, _('Uplink')),
  124. E('div', { 'class': 'th cbi-section-table-cell' }, _('Downlink')),
  125. E('div', { 'class': 'th cbi-section-table-cell' }, _('IPv4')),
  126. E('div', { 'class': 'th cbi-section-table-cell' }, _('IPv6')),
  127. E('div', { 'class': 'th cbi-section-table-cell' }, _('Prefix')),
  128. ]),
  129. ]),
  130. ]);
  131. var has_smartgw;
  132. uci.sections('olsrd', 'olsrd', function (s) {
  133. if (s.SmartGateway && s.SmartGateway === 'yes') {
  134. has_smartgw = true;
  135. }
  136. });
  137. var rv = [];
  138. for (var k = 0; k < gws_res.ipv4.length; k++) {
  139. var gw = gws_res.ipv4[k];
  140. gw.cost = parseFloat(gw.cost) / 1024 || 0;
  141. if (gw.cost >= 100) {
  142. gw.cost = 0;
  143. }
  144. rv.push({
  145. proto: gw.IPv4 ? '4' : '6',
  146. originator: gw.originator,
  147. selected: gw.selected ? luci.i18n.translate('yes') : luci.i18n.translate('no'),
  148. cost: gw.cost > 0 ? gw.cost.toFixed(3) : luci.i18n.translate('infinite'),
  149. hops: gw.hops,
  150. uplink: gw.uplink,
  151. downlink: gw.downlink,
  152. v4: gw.IPv4 ? luci.i18n.translate('yes') : luci.i18n.translate('no'),
  153. v6: gw.IPv6 ? luci.i18n.translate('yes') : luci.i18n.translate('no'),
  154. prefix: gw.prefix,
  155. });
  156. }
  157. var smartgwdiv = document.getElementById('olsrd_smartgw');
  158. if (smartgwdiv) {
  159. var s =
  160. '<div class="tr cbi-section-table-titles">' +
  161. '<div class="th cbi-section-table-cell">Gateway</div>' +
  162. '<div class="th cbi-section-table-cell">Selected</div>' +
  163. '<div class="th cbi-section-table-cell">ETX></div>' +
  164. '<div class="th cbi-section-table-cell">Hops></div>' +
  165. '<div class="th cbi-section-table-cell">Uplink</div>' +
  166. '<div class="th cbi-section-table-cell">Downlink</div>' +
  167. '<div class="th cbi-section-table-cell">IPv4</div>' +
  168. '<div class="th cbi-section-table-cell">IPv6</div>' +
  169. '<div class="th cbi-section-table-cell">Prefix</div>' +
  170. '</div>';
  171. for (var idx = 0; idx < rv.length; idx++) {
  172. var smartgw = rv[idx];
  173. var linkgw;
  174. s += '<div class="tr cbi-section-table-row cbi-rowstyle-' + (1 + (idx % 2)) + ' proto-' + smartgw.proto + '">';
  175. if (smartgw.proto == '6') {
  176. linkgw = '<a href="http://[' + smartgw.originator + ']/cgi-bin-status.html">' + smartgw.originator + '</a>';
  177. } else {
  178. linkgw = '<a href="http://' + smartgw.originator + '/cgi-bin-status.html">' + smartgw.originator + '</a>';
  179. }
  180. s +=
  181. '<div class="td cbi-section-table-cell left">' +
  182. linkgw +
  183. '</div>' +
  184. '<div class="td cbi-section-table-cell left">' +
  185. smartgw.selected +
  186. '</div>' +
  187. '<div class="td cbi-section-table-cell left">' +
  188. smartgw.cost +
  189. '</div>' +
  190. '<div class="td cbi-section-table-cell left">' +
  191. smartgw.hops +
  192. '</div>' +
  193. '<div class="td cbi-section-table-cell left">' +
  194. smartgw.uplink +
  195. '</div>' +
  196. '<div class="td cbi-section-table-cell left">' +
  197. smartgw.downlink +
  198. '</div>' +
  199. '<div class="td cbi-section-table-cell left">' +
  200. smartgw.v4 +
  201. '</div>' +
  202. '<div class="td cbi-section-table-cell left">' +
  203. smartgw.v6 +
  204. '</div>' +
  205. '<div class="td cbi-section-table-cell left">' +
  206. smartgw.prefix +
  207. '</div>';
  208. s += '</div>';
  209. }
  210. smartgwdiv.innerHTML = s;
  211. }
  212. var i = 1;
  213. if (has_smartgw) {
  214. for (var k = 0; k < gws_res.ipv4.length; k++) {
  215. var gw = gws_res.ipv4[k];
  216. gw.cost = parseInt(gw.cost) / 1024 || 0;
  217. if (gw.cost >= 100) {
  218. gw.cost = 0;
  219. }
  220. var tr = E('div', { 'class': 'tr cbi-section-table-row cbi-rowstyle-' + i + ' proto-' + gw.proto }, [
  221. gw.proto === '6'
  222. ? E('div', { 'class': 'td cbi-section-table-cell left' }, [E('a', { 'href': 'http://[' + gw.originator + ']/cgi-bin-status.html' }, gw.originator)])
  223. : E('div', { 'class': 'td cbi-section-table-cell left' }, [E('a', { 'href': 'http://' + gw.originator + '/cgi-bin-status.html' }, gw.originator)]),
  224. E('div', { 'class': 'td cbi-section-table-cell left' }, [gw.selected ? luci.i18n.translate('yes') : luci.i18n.translate('no')]),
  225. E('div', { 'class': 'td cbi-section-table-cell left' }, [gw.cost > 0 ? string.format('%.3f', gw.cost) : luci.i18n.translate('infinite')]),
  226. E('div', { 'class': 'td cbi-section-table-cell left' }, gw.hops),
  227. E('div', { 'class': 'td cbi-section-table-cell left' }, gw.uplink),
  228. E('div', { 'class': 'td cbi-section-table-cell left' }, gw.downlink),
  229. E('div', { 'class': 'td cbi-section-table-cell left' }, gw.IPv4 ? luci.i18n.translate('yes') : luci.i18n.translate('no')),
  230. E('div', { 'class': 'td cbi-section-table-cell left' }, gw.IPv6 ? luci.i18n.translate('yes') : luci.i18n.translate('no')),
  231. E('div', { 'class': 'td cbi-section-table-cell left' }, gw.prefix),
  232. ]);
  233. fieldset.appendChild(tr);
  234. i = (i % 2) + 1;
  235. }
  236. var h2 = E('h2', { 'name': 'content' }, _('SmartGW announcements'));
  237. var divToggleButtons = E('div', { 'id': 'togglebuttons' });
  238. var statusOlsrCommonJs = null;
  239. if (has_v4 && has_v6) {
  240. statusOlsrCommonJs = E('script', { 'type': 'text/javascript', 'src': L.resource('common/common_js.js') });
  241. }
  242. var result = E([], {}, [h2, divToggleButtons, fieldset, statusOlsrCommonJs]);
  243. return result;
  244. } else {
  245. return E('h2', {}, _('SmartGateway is not configured on this system'));
  246. }
  247. })
  248. .catch(function (error) {
  249. console.error(error);
  250. });
  251. },
  252. handleSaveApply: null,
  253. handleSave: null,
  254. handleReset: null
  255. });