-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
219 lines (186 loc) · 6.23 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"use strict";
var app;
(function () {
app = angular.module('rn4020', ['ngMaterial', 'ngMdIcons'])
.config(function ($mdThemingProvider) {
$mdThemingProvider.theme('default')
.primaryPalette('blue')
.accentPalette('blue-grey');
$mdThemingProvider.theme('success-toast');
$mdThemingProvider.theme('error-toast');
$mdThemingProvider.alwaysWatchTheme(true);
})
})();
app.run(['$document', '$window', function($document, $window) {
var document = $document[0];
document.addEventListener('click', function(event) {
var hasFocus = document.hasFocus();
if (!hasFocus) $window.focus();
});
}]);
app.service('rn4020Service', function () {
var term = new rn4020(navigator.bluetooth);
console.log(term);
return term;
});
app.controller('mainController', function ($scope, $mdToast, $mdDialog, rn4020Service) {
$scope.rn4020 = rn4020Service;
$scope.terminaldata = '';
$scope.outputMessage = '';
// Disabling the mouse right click event
document.addEventListener('contextmenu', function(event) { event.preventDefault();});
function goodToast(message) {
$mdToast.show(
$mdToast.simple()
.textContent(message)
.position('top')
.theme("success-toast")
.hideDelay(2500)
);
}
function badToast(message) {
$mdToast.show(
$mdToast.simple()
.textContent(message)
.position('top')
.theme('error-toast')
.hideDelay(2500)
);
}
function showLoadingIndicator($event, text) {
var parentEl = angular.element(document.body);
$mdDialog.show({
parent: parentEl,
targetEvent: $event,
clickOutsideToClose: false,
template: '<md-dialog style="width: 250px;top:95px;margin-top: -170px;" aria-label="loadingDialog" ng-cloak>' +
'<md-dialog-content>' +
'<div layout="row" layout-align="center" style="padding: 40px;">' +
'<div style="padding-bottom: 20px;">' +
'<md-progress-circular md-mode="indeterminate" md-diameter="40" style="right: 20px;bottom: 10px;">' +
'</md-progress-circular>' +
'</div>' +
'</div>' +
'<div layout="row" layout-align="center" style="padding-bottom: 20px;">' +
'<label>' + text + '</label>' +
'</div>' +
'</md-dialog-content>' +
'</md-dialog>',
locals: {
items: $scope.items
},
controller: DialogController
});
function DialogController($scope, $mdDialog, items) {
$scope.items = items;
$scope.closeDialog = function () {
$mdDialog.hide();
}
}
}
function dismissLoadingIndicator() {
$mdDialog.cancel();
}
$scope.rn4020.updateUI = function (value) {
var tmpArray = new Uint8Array(value.buffer);
var outputString = Utf8ArrayToStr(tmpArray);
$scope.terminaldata = $scope.terminaldata + outputString;
// console.log('citam '+$scope.outputMessage);
$scope.$apply();
};
$scope.onClear = function () {
$scope.terminaldata = '';
};
$scope.onSend = function () {
if ($scope.inputData === undefined) {
badToast('Enter Command to Send');
return;
}
if ($scope.inputData === '') {
badToast('Enter Command to Send');
return;
}
var bytes = [];
var command = "<="+$scope.inputData+"\r\n";
bytes = stringToUintArray(command);
var bufView = new Uint8Array(bytes);
$scope.rn4020.writeCommand(bufView)
.then(function () {
$scope.terminaldata = $scope.terminaldata + ' <' + $scope.inputData + '> ';
$scope.inputData = '';
$scope.$apply();
})
.catch(function (error) {
badToast('Unable to send data.');
});
};
$scope.onConnect = function () {
showLoadingIndicator('', 'Connecting ....');
$scope.rn4020.connect()
.then(function () {
dismissLoadingIndicator();
goodToast('Connected...');
})
.catch(function (error) {
dismissLoadingIndicator();
console.error('Argh!', error, error.stack ? error.stack : '');
badToast('Unable to connect.');
});
};
if (!navigator.bluetooth) {
badToast('Bluetooth not supported, which is required.');
} else if (navigator.bluetooth.referringDevice) {
$scope.onConnect();
}
});
app.filter('range', function () {
return function (input, total) {
total = parseInt(total);
for (var i = 0; i < total; i++) {
input.push(i);
}
return input;
};
});
function stringToUintArray(str) {
var charList = str.split('');
var uintArray = [];
for (var i = 0; i < charList.length; i++) {
uintArray.push(charList[i].charCodeAt(0));
}
return uintArray;
}
function c(array, encoding) {
return new Buffer(array).toString(encoding);
}
function Utf8ArrayToStr(array) {
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
while(i < len) {
c = array[i++];
switch(c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}