-
Notifications
You must be signed in to change notification settings - Fork 1
/
scale.js
150 lines (141 loc) · 3.67 KB
/
scale.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
/**
* @author williamw
*/
var scale_applet;
var scale_timeout;
var scale_unit;
var scale_interval;
var scale_element;
var scale_after;
var scale_freeze_func;
var scale_lastWeight;
var scale_auto_freeze;
var scale_stable_count;
var scale_disable_timeout;
var scale_freeze_after_num_dups;
window.onunload = function(){
if (scale_applet){
closeConnection();
}
}
/**
* Wait until scale applet loaded then set up scale connection
* @param {string} u Unit of measure to return. One of: "mg", "g", "kg", "carat", "tael", "gr", "dwt", "t", "tn", "ozt", "oz", "lb"
* @param {string} e ID of input field where weight will be put
* @param {string} a Name of function to call whenever a weight is returned
* @param {int} i Polling interval in milliseconds. Use 0 to disable automatic polling.
* @param {boolean} autofreeze Should we tell your application to stop requesting weights after the scale stabilizes?
* @param {string} freeze_func Optional function to call if autofreeze is set to true
* @returns {undefined}
*/
function scaleSetup(u, e, a, i, autofreeze, freeze_func){
scale_applet = document.getElementById('scaleApplet');
scale_unit = u;
scale_element = e;
scale_after = a;
scale_interval = i;
scale_freeze_after_num_dups = 2;
// Auto-freeze options
scale_auto_freeze = (autofreeze === true) ? true : false;
scale_freeze_func = (freeze_func) ? freeze_func : null;
scale_stable_count = 0;
scale_disable_timeout = false;
}
/**
* This function is called automatically when the scale is ready.
*/
function cioScaleReady(){
//console.log('Scale is ready!');
if (scale_interval && scale_interval > 0){
startWeight(scale_interval);
}
else{
getWeight();
}
}
function closeConnection(){
//console.log('Attempting to close scale connection');
if(scale_applet){
scale_applet.closeConnection();
}
}
// Polling interval in milliseconds
function startWeight(interval){
if (interval){
scale_interval = interval;
}
if(!scale_interval){
scale_interval = 200;
}
getWeight();
if(scale_disable_timeout === true){
scale_disable_timeout = false;
scale_stable_count = 0;
}
else{
scale_timeout = setTimeout(function() { startWeight(scale_interval); }, scale_interval);
}
}
function stopWeight(){
scale_disable_timeout = true;
clearTimeout(scale_timeout);
}
function getWeight(){
if(scale_applet){
var weight_box = document.getElementById(scale_element);
var weight = 0;
var u = null;
if (scale_unit != null && scale_unit != ''){
weight = scale_applet.getWeight(scale_unit);
}
else{
weight = scale_applet.getWeight();
u = scale_applet.getUnit();
}
var message = "";
if (weight == -1){
message = scale_applet.getMessage();
weight = 0;
}
weight = roundNumber(weight, 2);
if(scale_auto_freeze){
if(weight > 0 && scale_lastWeight === weight){
scale_stable_count++;
}
else{
scale_stable_count = 0;
}
if(scale_stable_count == scale_freeze_after_num_dups){
stopWeight();
if(scale_freeze_func){
window[scale_freeze_func]();
}
}
}
cur_weight = weight_box
? roundNumber(weight_box.value, 2)
: weight;
if (weight_box && weight != scale_lastWeight || weight != cur_weight){
scale_lastWeight = weight;
weight_box.value = weight;
if (scale_unit == null || scale_unit == ''){
document.getElementById('unit').innerHTML = u;
}
if (scale_after != null && scale_after != ''){
window[scale_after]();
}
}
document.getElementById('message').innerHTML = message;
}
return true;
}
/**
*
* @param {int} num Number to round
* @param {int} d Number of decimal places to round to
*/
function roundNumber(num, d) {
if (d == 0)
return Math.round(num);
return Math.round(num*Math.pow(10,d))/Math.pow(10,d);
}