You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

112 lines
2.7 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. function sendAPIRequest(data = {}) {
  51. fetch(endpoint, {
  52. method: 'POST',
  53. headers: {
  54. 'Content-Type': 'application/json',
  55. },
  56. body: JSON.stringify(data),
  57. }).then(function (response) {
  58. console.log("Pass: ", response);
  59. }, function (error) {
  60. console.log("Error: ", error)
  61. });
  62. }
  63. function getMacAddress() {
  64. const interfaces = os.networkInterfaces();
  65. for (const name of Object.keys(interfaces)) {
  66. for (const iface of interfaces[name]) {
  67. // Skip over non-IPv4 and internal (127.0.0.1) addresses
  68. if (iface.family === 'IPv4' && !iface.internal) {
  69. return iface.mac;
  70. }
  71. }
  72. }
  73. return null; // If no MAC address found
  74. }
  75. function createTray() {
  76. tray = new Tray(path.join(__dirname, 'logo.png'));
  77. const contextMenu = Menu.buildFromTemplate([
  78. {
  79. label: 'Show App',
  80. click: () => {
  81. mainWindow.show(); // Show the main window
  82. },
  83. },
  84. {
  85. label: 'Quit',
  86. click: () => {
  87. app.quit(); // Quit the app
  88. },
  89. },
  90. ]);
  91. tray.setToolTip('Monitoring Tool');
  92. tray.setContextMenu(contextMenu);
  93. tray.on('click', () => {
  94. mainWindow.show(); // Show the main window when the tray icon is clicked
  95. });
  96. }