|
- const { app, BrowserWindow, powerMonitor, Tray, Menu } = require('electron');
- const path = require('path');
- const os = require('os');
-
- let tray;
- let mainWindow;
- const endpoint = "https://workx.webtrigon.com/api/v1/device/presence/";
-
- app.on('ready', () => {
- createMainWindow();
- createTray();
-
- const macAddress = getMacAddress();
- console.log('MAC Address:', macAddress);
-
- // Automatically check-in when app starts
- sendAPIRequest({ mac_address: macAddress, is_online: true });
-
- // Handle system events
- powerMonitor.on('suspend', () => {
- console.log('System is going to sleep.');
- sendAPIRequest({ mac_address: macAddress, is_online: false });
- });
-
- powerMonitor.on('shutdown', () => {
- console.log('System is shutting down.');
- sendAPIRequest({ mac_address: macAddress, is_online: false });
- });
-
- powerMonitor.on('lock-screen', () => {
- console.log('System is locking.');
- sendAPIRequest({ mac_address: macAddress, is_online: false });
- });
-
- powerMonitor.on('resume', () => {
- console.log('System is resuming.');
- sendAPIRequest({ mac_address: macAddress, is_online: true });
- mainWindow.show();
- });
- });
-
- function createMainWindow() {
- mainWindow = new BrowserWindow({
- width: 800,
- height: 600,
- webPreferences: {
- preload: path.join(__dirname, 'preload.js'),
- contextIsolation: true,
- nodeIntegration: false,
- },
- });
-
- mainWindow.loadFile('index.html');
-
- // Hide the window instead of quitting
- mainWindow.on('close', (event) => {
- event.preventDefault();
- mainWindow.hide();
- });
- }
-
- function sendAPIRequest(data = {}) {
- fetch(endpoint, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify(data),
- }).then(function (response) {
- console.log("Pass: ", response);
- }, function (error) {
- console.log("Error: ", error)
- });
- }
-
- function getMacAddress() {
- const interfaces = os.networkInterfaces();
- for (const name of Object.keys(interfaces)) {
- for (const iface of interfaces[name]) {
- // Skip over non-IPv4 and internal (127.0.0.1) addresses
- if (iface.family === 'IPv4' && !iface.internal) {
- return iface.mac;
- }
- }
- }
- return null; // If no MAC address found
- }
-
- function createTray() {
- tray = new Tray(path.join(__dirname, 'logo.png'));
- const contextMenu = Menu.buildFromTemplate([
- {
- label: 'Show App',
- click: () => {
- mainWindow.show(); // Show the main window
- },
- },
- {
- label: 'Quit',
- click: () => {
- app.quit(); // Quit the app
- },
- },
- ]);
-
- tray.setToolTip('Monitoring Tool');
- tray.setContextMenu(contextMenu);
-
- tray.on('click', () => {
- mainWindow.show(); // Show the main window when the tray icon is clicked
- });
- }
|