<commit_before>const {app, Menu, Tray} = require('electron');

const Wemo = require('wemo-client');
var wemo = new Wemo();

var devices = [];

const search = function() {
    console.log('Searching for devices...');
    wemo.discover(function(device) {
        console.log('Device "' + device.friendlyName + '" found.');
        var client = wemo.client(device);
        client.on('binaryState', function(value) {
            for (var i = 0; i < devices.length; i++) {
                if (devices[i].device === this.device) {
                    console.log('State of device "' + this.device.friendlyName + '" changed to ' + value + '.');
                    tray.setImage('icons/icon-' + (value == 1 ? 'on' : 'off') + '.png')
                    devices[i].state = value;
                }
            }
            updateMenu();
        });
        devices.push({
            device: device,
            client: client,
            state: device.binaryState
        });
        updateMenu();
    });
}

const menuOptions = [
    {label: 'Search', click: search},
    {type: 'separator'},
    {label: 'Quit', click: app.quit}
];

const updateMenu = function() {
    var deviceMenuItems = [];
    if(devices.length > 0) {
        for(var i = 0; i < devices.length; i++) {
            var device = devices[0]
            deviceMenuItems.push({
                label: device.device.friendlyName,
                type: 'checkbox',
                checked: device.state == 1,
                click: toggleSwitch(device)
            });
        }
    }else {
        deviceMenuItems.push({label: 'No devices found.', enabled: false});
    }
    contextMenu = Menu.buildFromTemplate(deviceMenuItems.concat(menuOptions));

    tray.setContextMenu(contextMenu);
}

let tray = null;
var contextMenu = null;

app.on('ready', () => {
    tray = new Tray('icons/icon-off.png');
    tray.setToolTip('Boolb');

    tray.on('click', (event) => {
        if(devices.length > 0) {
            toggleSwitch(devices[0])();
        }
    });

    updateMenu();

    search();
});

const toggleSwitch = function(device) {
    return function() {
        console.log('Toggling "' + device.device.friendlyName + '".');
        console.log('Setting state to ' + (device.state == 1 ? 'off' : 'on') + '.');
        device.client.setBinaryState(device.state == 1 ? 0 : 1);
    }
}<commit_msg>Changes to how the context menu behaves<commit_after>const {app, Menu, Tray} = require('electron');

const Wemo = require('wemo-client');
var wemo = new Wemo();

let tray;
var contextMenu;

let selectedDevice = 0;
let devices = [];

const search = function() {
    console.log('Searching for devices...');

    wemo.discover(function(device) {
        console.log('Device "%s" found.', device.friendlyName);
        
        var client = wemo.client(device);
        client.on('binaryState', function(value) {
            for (var i = 0; i < devices.length; i++) {
                if (devices[i].device === this.device) {
                    console.log('State of device "%s" changed to %s.', this.device.friendlyName, value);
                    setIcon(value == 1 ? 'on' : 'off');
                    devices[i].state = value;
                    updateMenu();
                }
            }
        });

        devices.push({
            device: device,
            client: client,
            state: device.binaryState
        });

        updateMenu();
    });
}

const menuOptions = [
    {type: 'separator'},
    {label: 'Search', click: search},
    {label: 'Quit', role: 'quit'}
];

const updateMenu = function() {
    var deviceMenuItems = [];
    if(devices.length > 0) {
        deviceMenuItems.push({label: 'Devices:', enabled: false});
        for(var i = 0; i < devices.length; i++) {
            var currentDevice = i;
            deviceMenuItems.push({
                label: devices[i].device.friendlyName + ' (' + (devices[i].state == 1 ? 'On' : 'Off') + ')',
                type: 'radio',
                checked: selectedDevice === i ? true : false,
                click: () => {selectedDevice = currentDevice}
            });
        }
    } else {
        deviceMenuItems.push({label: 'No devices found.', enabled: false});
    }

    contextMenu = Menu.buildFromTemplate(deviceMenuItems.concat(menuOptions));

    tray.setContextMenu(contextMenu);
}

app.on('ready', () => {
    tray = new Tray('icons/icon-off.png');
    tray.setToolTip('Boolb');

    tray.on('click', (event) => {
        if(devices.length > 0) {
            toggleDevice();
        }
    });

    updateMenu();

    search();
});

const setIcon = function(state) {
    tray.setImage('icons/icon-' + state + '.png');
}

const toggleDevice = function() {
    var device = devices[selectedDevice];

    console.log('Toggling device "%s".', device.device.friendlyName);
    console.log('Setting state to %s.', device.state == 1 ? 'off' : 'on');

    device.client.setBinaryState(device.state == 1 ? 0 : 1);
}
{'fraction_non_alphanumeric': 0.1634789611435474, 'fraction_numerical': 0.004630561707267968, 'mean_word_length': 2.0972568578553616, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4760775', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1441, 'n_words': 323}