ui.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. function getAllData(){
  2. $.ajax({
  3. type: "GET",
  4. url: 'data.db',
  5. success: function(ret) {
  6. $.each(ret.data, function(i, item) {
  7. setSingleData(item);
  8. });
  9. }
  10. });
  11. }
  12. function setSingleData(singleData){
  13. //remove data
  14. $( "#" + singleData.seriennummer ).remove();
  15. container = document.getElementById(singleData.activity);
  16. entry = document.createElement("div");
  17. $(entry).attr("id",singleData.seriennummer);
  18. $(entry).html("<span>Code: " + singleData.produktcode + "</span><span> SN: " + singleData.seriennummer + "</span>");
  19. container.appendChild(entry);
  20. }
  21. function showDocument() {
  22. $.ajax({
  23. type: "GET",
  24. url: 'style.url',
  25. success: function(ret) {
  26. $('head link.custom').attr('href',ret);
  27. }
  28. });
  29. }
  30. function clearDocument() {
  31. $('#languages').addClass('hidden');
  32. $('#nope').removeClass('hidden');
  33. $('#control .added').remove();
  34. $('#content .added').remove();
  35. $('#reason').text(reason);
  36. }
  37. function init() {
  38. es = new EventSource('sse/');
  39. es.onopen = function() {
  40. getAllData();
  41. };
  42. es.onmessage = function(e) {
  43. console.log("Got SSE");
  44. if (e.data == 'new') {
  45. reason = '';
  46. showDocument();
  47. }
  48. if (e.data == 'reset') {
  49. reason = '';
  50. showDocument();
  51. }
  52. else{
  53. if(e.data == "update"){
  54. alert("update")
  55. }
  56. if(e.data != "keepalive" && e.data != "started"){
  57. try {
  58. var prdata = JSON.parse(e.data)
  59. //alert("LastIf" + prdata.seriennummer)
  60. setSingleData(prdata);
  61. }
  62. catch (e) {
  63. }
  64. }
  65. }
  66. };
  67. es.onerror = function() {
  68. reason = 'Server down.';
  69. clearDocument();
  70. es.close();
  71. setTimeout(init, 10000);
  72. };
  73. }
  74. $(document).ready(function() {
  75. init();
  76. });