<commit_before>/**
 * This serial lib simulates the behaviour of the node server socket
 * in the case of a Chrome packaged application.
 *
 * (c) 2014 <NAME>, <EMAIL>
 *
 * One of the roles of this lib is also to act as a middleman for the
 * instrument driver: it receives data from the serial port, passes it to
 * the instrument driver parser, and whenever the parser sends data back, it
 * passes it to the intrument driver 'format' command.
 */

define(function(require) {
    
    "use strict";
    
    var Backbone = require('backbone');
    var Serialport = require('serialport');
    var DeviceLog = require('app/models/devicelog');

    var serialLib = function() {

        // Public methods

        // Same API as for the socket object... pretty basic implementation, I know
        this.emit = function(cmd, args) {
            switch (cmd) {
                   case 'portstatus':
                    portStatus(args);
                    break;
                   case 'openinstrument':
                    openInstrument(args);
                    break;
                   case 'closeinstrument':
                    closeInstrument(args);
                    break;
                   case 'controllerCommand':
                    controllerCommand(args);
                    break;
                   case 'ports':
                    getPorts(args);
                    break;
                   case 'driver':
                    setDriver(args);
                    break;
                   case 'startrecording':
                    startRecording(args);
                    break;
                   case 'stoprecording':
                    stopRecording(args);
                    break;
                   default:
                    break;
            }
        };

        // Trick: 
        this.connect = function() {return this; };
        
        /////////////
        // Recorder API (public)
        /////////////
        this.record = function(data) {
            console.log("Recording " + data);
            var entry = new DeviceLog.LogEntry({
                timestamp: new Date().getTime(),
                logsessionid: currentLog.id,
                data: data
            });
            currentLog.entries.add(entry);
            entry.save();
        }

        // Needs to be public (defined with this.) because
        // it is called from a callback
        this.driver = null;
        this.portOpen = false;
        this.portSettings  = null;
        this.connectionId = null;

        // Private methods / properties:
        var Debug = true;
        var recording = false;
        var currentLog = null;
        var self = this;
        // Parser does not need to be public...
        var parser = null;
        
        // Utility function (chrome serial wants array buffers for sending)
        // Convert string to ArrayBuffer
        function str2ab(str) {
            var buf=new ArrayBuffer(str.length);
            var bufView=new Uint8Array(buf);
            for (var i=0; i<str.length; i++) {
                bufView[i]=str.charCodeAt(i);
            }
            return buf;
        };

        function ab2str(buf) {
            return String.fromCharCode.apply(null, new Uint8Array(buf));
        };
        
        
        //////////////
        //
        //   Implementation of socket API
        //
        //////////////
        function portStatus() {
           self.trigger('status', {portopen: self.portOpen});
        };

        // This is where we hook up the serial parser - Chrome version
        function setDriver(driver) {
            console.log("Setting driver - should be " + driver);
            // We can use our instrumentManager to ask for the right driver
            // (in server-side mode, "driver" is passed as the driver name, we
            //  don't need it here!)
            instrumentManager.getBackendDriver(self, function(dr) {
                self.driver = dr;
                self.portSettings = self.driver.portSettings();
                parser = self.portSettings.parser;
            });
        }
        
        function openInstrument(port) {
            console.log("chromeSerialLib: openInstrument");
            var path = instrumentManager.getInstrument().get("port");<commit_msg>Preserve state of recording button through screen changes<commit_after>/**
 * This serial lib simulates the behaviour of the node server socket
 * in the case of a Chrome packaged application.
 *
 * (c) 2014 <NAME>, <EMAIL>
 *
 * One of the roles of this lib is also to act as a middleman for the
 * instrument driver: it receives data from the serial port, passes it to
 * the instrument driver parser, and whenever the parser sends data back, it
 * passes it to the intrument driver 'format' command.
 */

define(function(require) {
    
    "use strict";
    
    var Backbone = require('backbone');
    var Serialport = require('serialport');
    var DeviceLog = require('app/models/devicelog');

    var serialLib = function() {

        // Public methods

        // Same API as for the socket object... pretty basic implementation, I know
        this.emit = function(cmd, args) {
            switch (cmd) {
                   case 'portstatus':
                    portStatus(args);
                    break;
                   case 'openinstrument':
                    openInstrument(args);
                    break;
                   case 'closeinstrument':
                    closeInstrument(args);
                    break;
                   case 'controllerCommand':
                    controllerCommand(args);
                    break;
                   case 'ports':
                    getPorts(args);
                    break;
                   case 'driver':
                    setDriver(args);
                    break;
                   case 'startrecording':
                    startRecording(args);
                    break;
                   case 'stoprecording':
                    stopRecording(args);
                    break;
                   default:
                    break;
            }
        };

        // Trick: 
        this.connect = function() {return this; };
        
        /////////////
        // Recorder API (public)
        /////////////
        this.record = function(data) {
            console.log("Recording " + data);
            var entry = new DeviceLog.LogEntry({
                timestamp: new Date().getTime(),
                logsessionid: currentLog.id,
                data: data
            });
            currentLog.entries.add(entry);
            entry.save();
        }

        // Needs to be public (defined with this.) because
        // it is called from a callback
        this.driver = null;
        this.portOpen = false;
        this.portSettings  = null;
        this.connectionId = null;

        // Private methods / properties:
        var Debug = true;
        var recording = false;
        var currentLog = null;
        var self = this;
        // Parser does not need to be public...
        var parser = null;
        
        // Utility function (chrome serial wants array buffers for sending)
        // Convert string to ArrayBuffer
        function str2ab(str) {
            var buf=new ArrayBuffer(str.length);
            var bufView=new Uint8Array(buf);
            for (var i=0; i<str.length; i++) {
                bufView[i]=str.charCodeAt(i);
            }
            return buf;
        };

        function ab2str(buf) {
            return String.fromCharCode.apply(null, new Uint8Array(buf));
        };
        
        
        //////////////
        //
        //   Implementation of socket API
        //
        //////////////
        function portStatus() {
           self.trigger('status', {portopen: self.portOpen, recording: recording});
        };

        // This is where we hook up the serial parser - Chrome version
        function setDriver(driver) {
            console.log("Setting driver - should be " + driver);
            // We can use our instrumentManager to ask for the right driver
            // (in server-side mode, "driver" is passed as the driver name, we
            //  don't need it here!)
            instrumentManager.getBackendDriver(self, function(dr) {
                self.driver = dr;
                self.portSettings = self.driver.portSettings();
                parser = self.portSettings.parser;
            });
        }
        
        function openInstrument(port) {
            console.log("chromeSerialLib: openInstrument");
            var path = instrumentManager.getInstrument().get("port");
{'fraction_non_alphanumeric': 0.10055350553505535, 'fraction_numerical': 0.0020756457564575647, 'mean_word_length': 1.3427876823338736, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '3935989', 'n_tokens_mistral': 2111, 'n_tokens_neox': 2016, 'n_words': 638}