Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 

124 rindas
3.2 KiB

  1. const { app, BrowserWindow, powerMonitor, Tray, Menu } = require('electron');
  2. const path = require('path');
  3. const os = require('os');
  4. let tray;
  5. let mainWindow;
  6. const endpoint = "https://workx.webtrigon.com/api/v1/device/presence/";
  7. app.on('ready', () => {
  8. createMainWindow();
  9. createTray();
  10. const macAddress = getMacAddress();
  11. console.log('MAC Address:', macAddress);
  12. // Automatically check-in when app starts
  13. sendAPIRequest({ mac_address: macAddress, is_online: true });
  14. // Handle system events
  15. powerMonitor.on('suspend', () => {
  16. console.log('System is going to sleep.');
  17. sendAPIRequest({ mac_address: macAddress, is_online: false });
  18. });
  19. powerMonitor.on('shutdown', () => {
  20. console.log('System is shutting down.');
  21. sendAPIRequest({ mac_address: macAddress, is_online: false });
  22. });
  23. powerMonitor.on('lock-screen', () => {
  24. console.log('System is locking.');
  25. sendAPIRequest({ mac_address: macAddress, is_online: false });
  26. });
  27. powerMonitor.on('resume', () => {
  28. console.log('System is resuming.');
  29. sendAPIRequest({ mac_address: macAddress, is_online: true });
  30. mainWindow.show();
  31. });
  32. });
  33. function createMainWindow() {
  34. mainWindow = new BrowserWindow({
  35. width: 800,
  36. height: 600,
  37. webPreferences: {
  38. preload: path.join(__dirname, 'preload.js'),
  39. contextIsolation: true,
  40. nodeIntegration: false,
  41. },
  42. });
  43. mainWindow.loadFile('index.html');
  44. // Hide the window instead of quitting
  45. mainWindow.on('close', (event) => {
  46. event.preventDefault();
  47. mainWindow.hide();
  48. });
  49. }
  50. async function sendAPIRequest(data = {}) {
  51. try {
  52. const response = await fetch(endpoint, {
  53. method: 'POST',
  54. headers: {
  55. 'Content-Type': 'application/json',
  56. },
  57. body: JSON.stringify(data),
  58. });
  59. const textResponse = await response.text();
  60. try {
  61. const result = JSON.parse(textResponse.toString());
  62. if (!response.ok) {
  63. console.error(`API call failed with is_online: ${response.is_online}`, result);
  64. } else {
  65. console.log('API call succeeded:', result.toString());
  66. }
  67. } catch (jsonError) {
  68. console.error('Error parsing JSON response:', textResponse.toString());
  69. }
  70. } catch (error) {
  71. console.error('Error during API call:', error.toString());
  72. }
  73. }
  74. function getMacAddress() {
  75. const interfaces = os.networkInterfaces();
  76. for (const name of Object.keys(interfaces)) {
  77. for (const iface of interfaces[name]) {
  78. // Skip over non-IPv4 and internal (127.0.0.1) addresses
  79. if (iface.family === 'IPv4' && !iface.internal) {
  80. return iface.mac;
  81. }
  82. }
  83. }
  84. return null; // If no MAC address found
  85. }
  86. function createTray() {
  87. tray = new Tray(path.join(__dirname, 'logo.png'));
  88. const contextMenu = Menu.buildFromTemplate([
  89. {
  90. label: 'Show App',
  91. click: () => {
  92. mainWindow.show(); // Show the main window
  93. },
  94. },
  95. {
  96. label: 'Quit',
  97. click: () => {
  98. app.quit(); // Quit the app
  99. },
  100. },
  101. ]);
  102. tray.setToolTip('Monitoring Tool');
  103. tray.setContextMenu(contextMenu);
  104. tray.on('click', () => {
  105. mainWindow.show(); // Show the main window when the tray icon is clicked
  106. });
  107. }