<commit_before>
var treadmill;


function zeropad(number) {
    return (number<10) ? '0'+number : number;
}

function Treadmill()
{
    // variables
	this.speed = -1;
    this.incline = -1;
	
	this.goaltime = 1.5*3600;
    
    // internals
	var _treadmill = this;
	this.eventHandlers = {
	};
}

function ws_status(str)
{
    $("#ConnectionStatus").text(str);
}

Treadmill.prototype.connect = function(url)
{
  if ("WebSocket" in window)
  {
     var _treadmill = this;
     
     // Let us open a web socket
     ws_status("connecting to "+window.location.hostname+"...");
     this.connection = new WebSocket("ws://"+window.location.hostname+":27001/echo");
     this.connection.onopen = function()
     {
        // Web Socket is connected, send data using send()
        ws_status("Connected.");
		_treadmill.parseEvent("connected");
		
		// request some objects from the treadmill service
		_treadmill.request("users");
		_treadmill.request("user");		
	};
     this.connection.onmessage = function (evt) 
     {
        var received_msg = evt.data;
        var msg = JSON.parse(received_msg);
        if(msg!=null)
            _treadmill.parseMessage(msg);        
     };
     this.connection.onclose = function()
     { 
        // websocket is closed.
        ws_status("Connection closed."); 
        this.connection = null;
		_treadmill.parseEvent("closed");
     };
     this.connection.onerror = function(evt)
     {
        ws_status("Connection error : "+evt.data);
	setTimeout(function(){ _treadmill.connect(url); }, 5000);
     }
  } else
    alert("web sockets not supported");
}

Treadmill.prototype.request = function(schema)
{
    if(this.connection)
		this.connection.send(JSON.stringify({ Get: schema }));
}

Treadmill.prototype.on = function(eventName, callback)
{
	this.eventHandlers[eventName] = callback;
}

Treadmill.prototype.parseMessage = function(msg)
{
    if(msg.type=="status") {
        this.updateRunningTime(msg.runningTime);
        
        if(this.speed != msg.currentSpeed)
        {
            this.speed = msg.currentSpeed;
            if(this.onSpeedChanged)
                this.onSpeedChanged(msg.currentSpeed);
        }
        
        if(this.incline != msg.currentIncline)
        {
            this.incline = msg.currentIncline;
            if(this.onInclineChanged)
                this.onInclineChanged(msg.currentIncline);
                
        }
    } else if(msg.type=="event") {
		this.parseEvent(msg.name, msg.data);
    } else if(msg.type=="response") {
		//console.log(msg);
		if(msg.schema=="users")
			this.users = msg.response;
		else if(msg.schema=="user")
		{
			if(msg.response && msg.response.userid>0) {
				this.user = msg.response;
				this.users[this.user.userid] = this.user;
				this.goaltime = this.user.goaltime;
				this.goaldistance = this.user.goaldistance;
			}
		}
		
		if(this.eventHandlers && this.eventHandlers[msg.schema]!=null)
			this.eventHandlers[msg.schema](msg.response);
    }
}

Treadmill.prototype.setUser = function(user, weightUpdate) 
{
    if(this.connection)
        this.connection.send(JSON.stringify({ User: user, Weight: weightUpdate }));
}
	
Treadmill.prototype.parseEvent = function(name, data)
{
}

Treadmill.prototype.setSpeed = function(value) 
{
	if(this.resetTimer!=null)
		clearTimeout(this.resetTimer);
				
    if(this.connection)
        this.connection.send(JSON.stringify({ Speed: value }));
}

Treadmill.prototype.setIncline = function(value) 
{
    if(this.connection)
        this.connection.send(JSON.stringify({ Incline: value }));
}

Treadmill.prototype.increaseSpeed = function() 
{
    return this.setSpeed("++");
}

Treadmill.prototype.decreaseSpeed = function() 
{
    return this.setSpeed("--");
}<commit_msg>UI now connects to dns/ip in the host url, or you can override with ?host= parameter in query string<commit_after>
var treadmill;


function zeropad(number) {
    return (number<10) ? '0'+number : number;
}

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

function Treadmill()
{
    // variables
	this.speed = -1;
    this.incline = -1;
	
	this.goaltime = 1.5*3600;
    
    // internals
	var _treadmill = this;
	this.eventHandlers = {
	};

    // determine host
    this.host = getParameterByName("host");
    if(!this.host)
        this.host = window.location.hostname;
    console.log("treadmill host: "+this.host);
}

function ws_status(str)
{
    $("#ConnectionStatus").text(str);
}

Treadmill.prototype.connect = function(url)
{
  if ("WebSocket" in window)
  {
     var _treadmill = this;

     // Let us open a web socket
     ws_status("connecting to "+this.host+"...");
     this.connection = new WebSocket("ws://"+this.host+":27001/echo");
     this.connection.onopen = function()
     {
        // Web Socket is connected, send data using send()
        ws_status("Connected.");
		_treadmill.parseEvent("connected");
		
		// request some objects from the treadmill service
		_treadmill.request("users");
		_treadmill.request("user");		
	};
     this.connection.onmessage = function (evt) 
     {
        var received_msg = evt.data;
        var msg = JSON.parse(received_msg);
        if(msg!=null)
            _treadmill.parseMessage(msg);        
     };
     this.connection.onclose = function()
     { 
        // websocket is closed.
        ws_status("Connection closed."); 
        this.connection = null;
		_treadmill.parseEvent("closed");
     };
     this.connection.onerror = function(evt)
     {
        ws_status("Connection error : "+evt.data);
	setTimeout(function(){ _treadmill.connect(url); }, 5000);
     }
  } else
    alert("web sockets not supported");
}

Treadmill.prototype.request = function(schema)
{
    if(this.connection)
		this.connection.send(JSON.stringify({ Get: schema }));
}

Treadmill.prototype.on = function(eventName, callback)
{
	this.eventHandlers[eventName] = callback;
}

Treadmill.prototype.parseMessage = function(msg)
{
    if(msg.type=="status") {
        this.updateRunningTime(msg.runningTime);
        
        if(this.speed != msg.currentSpeed)
        {
            this.speed = msg.currentSpeed;
            if(this.onSpeedChanged)
                this.onSpeedChanged(msg.currentSpeed);
        }
        
        if(this.incline != msg.currentIncline)
        {
            this.incline = msg.currentIncline;
            if(this.onInclineChanged)
                this.onInclineChanged(msg.currentIncline);
                
        }
    } else if(msg.type=="event") {
		this.parseEvent(msg.name, msg.data);
    } else if(msg.type=="response") {
		//console.log(msg);
		if(msg.schema=="users")
			this.users = msg.response;
		else if(msg.schema=="user")
		{
			if(msg.response && msg.response.userid>0) {
				this.user = msg.response;
				this.users[this.user.userid] = this.user;
				this.goaltime = this.user.goaltime;
				this.goaldistance = this.user.goaldistance;
			}
		}
		
		if(this.eventHandlers && this.eventHandlers[msg.schema]!=null)
			this.eventHandlers[msg.schema](msg.response);
    }
}

Treadmill.prototype.setUser = function(user, weightUpdate) 
{
    if(this.connection)
        this.connection.send(JSON.stringify({ User: user, Weight: weightUpdate }));
}
	
Treadmill.prototype.parseEvent = function(name, data)
{
    // this should not be edited, the main app will override this member
}

Treadmill.prototype.setSpeed = function(value) 
{
	if(this.resetTimer!=null)
		clearTimeout(this.resetTimer);
				
    if(this.connection)
        this.connection.send(JSON.stringify({ Speed: value }));
}

Treadmill.prototype.setIncline = function(value) 
{
    if(this.connection)
        this.connection.send(JSON.stringify({ Incline: value }));
}

Treadmill.prototype.increaseSpeed = function() 
{
    return this.setSpeed("++");
}

Treadmill.prototype.decreaseSpeed = function() 
{
    return this.setSpeed("--");
}
{'fraction_non_alphanumeric': 0.14803439803439802, 'fraction_numerical': 0.005405405405405406, 'mean_word_length': 2.8328625235404896, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, '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': '1644210', 'n_tokens_mistral': 2790, 'n_tokens_neox': 2637, 'n_words': 441}