<commit_before>import nipplejs from 'nipplejs'
import {throttle, zipObjectDeep, merge} from 'lodash'

let save = (prefix, state) => {
    let key = Object.keys(state);    
    if (prefix) { prefix = prefix + '.'; }
    localStorage.setItem(prefix + key, JSON.stringify(state[key]));
    return state;
}

const actions = {
    restoreState: v => state => {
        let props = Object.keys(localStorage);
        props.forEach((prop) => {
            let zipped = zipObjectDeep([prop], [JSON.parse(localStorage[prop])]);
            merge(state, zipped);
            console.log('Restore', prop, zipped);
        });
        console.log('State after restore', state);
    },

    setBootScreenState: value => state => ({ showBootScreen: value }),
    setMode: value => state => save('', { mode: value }),

    settings: {
        setVisibility: value => state => save('settings', { isVisible: !state.isVisible }),
        setVisibleCategory: value => state => save('settings', {category: value }),
    },

    telemetry: {
        setBatteryLevel: value => state => ({ batteryLevel: value }),
        setSignalLevel: value => state => ({ signalLevel: value }),
        setTemperature: value => state => ({ temperature: value }),
    },

    motors: null,
  

    joystick: ({el, motors}) => {
        
        let manager = nipplejs.create({
        zone: el,
        mode: 'static',
        position: {left: '50%', top: '50%'},
        size: el.clientHeight,
        // dataOnly: true
        });
        
        manager.on('start', function (evt, nipple) {
            console.log(evt);
            nipple.on('move', (evt, data) => motorsThrottled(evt, data));
        });

        let motorsThrottled = throttle((evt, data) => {
            if (!data.hasOwnProperty('direction')) { return; }
            motors.set(treshold(data.force), convertToArrOfDirections(data.direction.angle));
            console.log(treshold(data.force), convertToArrOfDirections(data.direction.angle));        
        }, 100, { 'trailing': false });

        let treshold = (force) => force >= 1 ? 100 : (force * 100).toFixed(0);
        
        manager.on('end', function(evt, nipple) {   
            console.log(evt);
            // nipple.off('start move end dir plain');
            motors.stop();
        });

        const convertToArrOfDirections = (angle) => {
            switch (angle) {
                case 'up':
                    return motors.direction.forward;
                case 'down':
                    return motors.direction.backward;
                case 'left':
                    return motors.direction.left;
                case 'right':
                    return motors.direction.right;
                default:
                    break;
            }
        }
    },
<commit_msg>send data in intervals via joystick<commit_after>import nipplejs from 'nipplejs'
import {throttle, zipObjectDeep, merge} from 'lodash'

let save = (prefix, state) => {
    let key = Object.keys(state);    
    if (prefix) { prefix = prefix + '.'; }
    localStorage.setItem(prefix + key, JSON.stringify(state[key]));
    return state;
}

const actions = {
    restoreState: v => state => {
        let props = Object.keys(localStorage);
        props.forEach((prop) => {
            let zipped = zipObjectDeep([prop], [JSON.parse(localStorage[prop])]);
            merge(state, zipped);
            console.log('Restore', prop, zipped);
        });
        console.log('State after restore', state);
    },

    setBootScreenState: value => state => ({ showBootScreen: value }),
    setMode: value => state => save('', { mode: value }),

    settings: {
        setVisibility: value => state => save('settings', { isVisible: !state.isVisible }),
        setVisibleCategory: value => state => save('settings', {category: value }),
    },

    telemetry: {
        setBatteryLevel: value => state => ({ batteryLevel: value }),
        setSignalLevel: value => state => ({ signalLevel: value }),
        setTemperature: value => state => ({ temperature: value }),
    },

    motors: null,
  

    joystick: ({el, motors}) => {
        
        let manager = nipplejs.create({
            zone: el,
            mode: 'static',
            position: {left: '50%', top: '50%'},
            size: el.clientHeight,
            // dataOnly: true
        });

        var force = 0;
        var angle = "up";
        var interval;
        
        manager.on('start', function (evt, nipple) {
            // console.log(evt);
            nipple.on('move', (evt, data) => motorsThrottled(evt, data, force, angle));

            interval = setInterval(function() {
                motors.set(force, angle);               
            }, 100);



        });

        let motorsThrottled = throttle((evt, data) => {
            if (!data.hasOwnProperty('direction')) { return; }
            force = treshold(data.force);
            angle = convertToArrOfDirections(data.direction.angle);
            console.log('[joystick]', treshold(data.force), convertToArrOfDirections(data.direction.angle));        
        }, 100, { 'trailing': false });

        let treshold = (force) => force >= 1 ? 100 : (force * 100).toFixed(0);
        
        manager.on('end', function(evt, nipple) { 
            clearInterval(interval);
            console.log("[joystick interval]", interval);
             
            console.log(evt);
            motors.stop();
        });

        const convertToArrOfDirections = (angle) => {
            switch (angle) {
                case 'up':
                    return motors.direction.forward;
                case 'down':
                    return motors.direction.backward;
                case 'left':
                    return motors.direction.left;
                case 'right':
                    return motors.direction.right;
                default:
                    break;
            }
        }
    },

{'fraction_non_alphanumeric': 0.14357852213432082, 'fraction_numerical': 0.00572294226561185, 'mean_word_length': 1.8677606177606179, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 51, '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': '1981717', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1617, 'n_words': 370}