<commit_before>

/**
 * SARK100/Mini50 bluetooth interface
 */
org.open60.App = function() {

	var that = this;

	Object.defineProperties(this, {
		"ready": {
			get: function() {
				return this._ready;
			},
			set: function(v) {
				this._ready = v;
			}
		},
		"connected": {
			get: function() {
				return this._connected;
			},
			set: function(v) {
				var b = document.getElementById("cmd-button");
				if (b) {
					var msg = v ? "disconnect" : "connect";
					b.textContent = msg;
				}
				this._connected = v;
			}
		},
		"range": {
			get: function() {
				return this._range;
			},
      set: function(v) {
        this._range = v;
      }
		}
	});

	this.config = org.open60.config;
	this.ready = false;
	this.connected = false;
	this.rangeIndex = 0;
	this.range = this.config.ranges[0];
  this.graph = new org.open60.Graph(this);

	function trace(msg) {
		console.log(msg);
	}

	function alert(msg) {
		if (navigator.showToast) {
			navigator.showToast(msg);
		}
		console.log("alert: " + msg);
	}

	function error(msg) {
		console.log("error: " + msg);
		if (navigator.showToast) {
			navigator.showToast(msg);
		}
	}

	this.next = function() {
		var len = this.config.ranges.length;
		this.rangeIndex = (this.rangeIndex + 1) % len;
		this.range = this.config.ranges[this.rangeIndex];
	}

	this.checkConnect = function() {
		function success() {
			that.connected = true;
		}

		function failure() {
			that.connected = false;
		}
		bluetoothSerial.isConnected(success, failure);
	};

	this.connect = function(device, cb) {
		function receive(data) {
			that.receive(data);
		}

		function subscribeFailure(msg) {
			error("subscribe: " + msg);
		}

		function connectSuccess() {
			that.connected = true;
			bluetoothSerial.subscribe('\n', receive, subscribeFailure);
			if (cb) {
				cb();
			}
		}

		function connectFailure(msg) {
			that.connected = false;
			error("connect: " + msg);
		}
		bluetoothSerial.connect(device.address, connectSuccess, connectFailure);
	};

	this.disconnect = function() {
		function success() {
			that.connected = false;
		}

		function failure(msg) {
			that.connected = false;
		}
		if (that.connected) {
			bluetoothSerial.unsubscribe();
			bluetoothSerial.disconnect(success, failure);
		}
	};

	this.receive = function(data) {
		// console.log(data);
		data = data.trim();
		if (data.startsWith('Start')) {
			this.graph.startScan();
		} else if (data.startsWith("End")) {
			this.graph.endScan();
		} else {
			var arr = data.split(',');
			if (arr.length === 4) {
				var dp = {
					swr: parseFloat(arr[0]),
					r: parseFloat(arr[1]),
					x: parseFloat(arr[2]),
					z: parseFloat(arr[3])
				};
				this.graph.update(dp);
			}
		}
	};

	this.send = function(msg) {
		if (!this.connected) {
			return;
		}

		function success() {
			trace("sent!");
		}

		function failure(msg) {
			error("send: " + msg);
		}
		bluetoothSerial.write(msg + '\r\n', success, failure);
	};

	this.findDeviceAndConnect = function(cb) {
		var deviceName = this.config.deviceName;
		function success(devices) {
			var dev = devices.find(function(d) {
				var name = d.name;
				return name.startsWith(deviceName);
			});
			if (!dev) {
				error("Paired device 'MINI60' not found");
			}
			that.connect(dev, cb);
		}

		function failure(msg) {
			error("cannot list paired devices");
		}
		bluetoothSerial.list(success, failure);
	};

	this.scan = function() {
			var r = this.range;
			var start = r.start * 1000;
			var end = r.end * 1000;
			var step = r.step * 1000;
			var cmd = "scan " + start + " " + end + " " + step;
			this.send(cmd);
	};

	this.checkConnectAndScan = function() {
		if (this.connected) {
			this.scan();
		} else {
			this.findDeviceAndConnect(function() {
				that.scan();
			});
		}
	};

	this.startHeartbeat = function() {<commit_msg>improve error messages<commit_after>

/**
 * SARK100/Mini50 bluetooth interface
 */
org.open60.App = function() {

	var that = this;

	Object.defineProperties(this, {
		"ready": {
			get: function() {
				return this._ready;
			},
			set: function(v) {
				this._ready = v;
			}
		},
		"connected": {
			get: function() {
				return this._connected;
			},
			set: function(v) {
				var b = document.getElementById("cmd-button");
				if (b) {
					var msg = v ? "disconnect" : "connect";
					b.textContent = msg;
				}
				this._connected = v;
			}
		},
		"range": {
			get: function() {
				return this._range;
			},
      set: function(v) {
        this._range = v;
      }
		}
	});

	this.config = org.open60.config;
	this.ready = false;
	this.connected = false;
	this.rangeIndex = 0;
	this.range = this.config.ranges[0];
  this.graph = new org.open60.Graph(this);

	function trace(msg) {
		console.log(msg);
	}

	function alert(msg) {
		if (navigator.showToast) {
			navigator.showToast(msg);
		}
		console.log("alert: " + msg);
	}

	function error(msg) {
		console.log("error: " + msg);
		if (navigator.showToast) {
			navigator.showToast(msg);
		}
	}

	this.next = function() {
		var len = this.config.ranges.length;
		this.rangeIndex = (this.rangeIndex + 1) % len;
		this.range = this.config.ranges[this.rangeIndex];
	}

	this.checkConnect = function() {
		function success() {
			that.connected = true;
		}

		function failure() {
			that.connected = false;
		}
		if (typeof bluetoothSerial !== "undefined") {
			bluetoothSerial.isConnected(success, failure);
		}
	};

	this.connect = function(device, cb) {
		function receive(data) {
			that.receive(data);
		}

		function subscribeFailure(msg) {
			error("subscribe: " + msg);
		}

		function connectSuccess() {
			that.connected = true;
			bluetoothSerial.subscribe('\n', receive, subscribeFailure);
			if (cb) {
				cb();
			}
		}

		function connectFailure(msg) {
			that.connected = false;
			error("connecting to '" + device.name + "' : " + msg);
		}
		bluetoothSerial.connect(device.address, connectSuccess, connectFailure);
	};

	this.disconnect = function() {
		function success() {
			that.connected = false;
		}

		function failure(msg) {
			that.connected = false;
		}
		if (that.connected) {
			bluetoothSerial.unsubscribe();
			bluetoothSerial.disconnect(success, failure);
		}
	};

	this.receive = function(data) {
		// console.log(data);
		data = data.trim();
		if (data.startsWith('Start')) {
			this.graph.startScan();
		} else if (data.startsWith("End")) {
			this.graph.endScan();
		} else {
			var arr = data.split(',');
			if (arr.length === 4) {
				var dp = {
					swr: parseFloat(arr[0]),
					r: parseFloat(arr[1]),
					x: parseFloat(arr[2]),
					z: parseFloat(arr[3])
				};
				this.graph.update(dp);
			}
		}
	};

	this.send = function(msg) {
		if (!this.connected) {
			return;
		}

		function success() {
			trace("sent!");
		}

		function failure(msg) {
			error("send: " + msg);
		}
		bluetoothSerial.write(msg + '\r\n', success, failure);
	};

	this.findDeviceAndConnect = function(cb) {
		var deviceName = this.config.deviceName;
		function success(devices) {
			var dev = devices.find(function(d) {
				var name = d.name;
				return name.startsWith(deviceName);
			});
			if (!dev) {
				error("Paired device '" + deviceName + "' not found");
			}
			that.connect(dev, cb);
		}

		function failure(msg) {
			error("cannot list paired devices");
		}
		bluetoothSerial.list(success, failure);
	};

	this.scan = function() {
			var r = this.range;
			var start = r.start * 1000;
			var end = r.end * 1000;
			var step = r.step * 1000;
			var cmd = "scan " + start + " " + end + " " + step;
			this.send(cmd);
	};

	this.checkConnectAndScan = function() {
		if (this.connected) {
			this.scan();
		} else {
			this.findDeviceAndConnect(function() {
				that.scan();
			});
		}
	};

	this.startHeartbeat = function() {
{'fraction_non_alphanumeric': 0.16534922897499027, 'fraction_numerical': 0.008293378255798886, 'mean_word_length': 2.896012115093387, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, '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': '4495490', 'n_tokens_mistral': 3275, 'n_tokens_neox': 2716, 'n_words': 524}