-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnative.hardware.js
149 lines (133 loc) · 3.74 KB
/
native.hardware.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
/* global __hw, VM, vm, $hw, _hw */
var Hardware = function () {
var T = this;
const api = _hw;
// delete _hw;
var buf = [];
T.active = false;
T.delay = 250;
var events = [];
var act = false;
const IsinBuf = function (id, buf) {
for (var i = 0; i < buf.length; i++) {
if (id === buf[i].id) {
return true;
}
}
return false;
};
const parsePath = function (str) {
var res = [];
if (typeof str === "undefined" || str === "") {
return res;
}
if (str.indexOf(',') > -1) {
var items = str.split(',');
for (var i = 0; i < items.length; i++) {
res.push(items[i].split('\\'));
}
} else {
res.push(str.split('\\'));
cb(res);
}
};
const devFilter = function (list, o) {
if (list.length > 0) {
if (typeof o.d === "undefined") {
o.c(list);
} else {
for (var I = 0; I < list.length; I++) {
if (list[I].name.indexOf(o.d) > -1) {
o.c(list[I]);
return;
}
}
}
}
};
const CheckNew = function (tmp, res) {
if (buf.length === tmp.length) {
return;
}
var list = [];
var I = 0;
tmp.forEach(function (i) {
I++;
if (IsinBuf(i.id, buf) === false) {
//i.path = T.parsePath(i.path);
list.push(i);
}
if (I >= tmp.length) {
buf = tmp;
res(list);
return;
}
});
};
const CheckGone = function (tmp, res) {
if (buf.length === tmp.length) {
return;
}
var list = [];
var I = 0;
buf.forEach(function (i) {
I++;
if (IsinBuf(i.id, tmp) === false) {
//i.path = T.parsePath(i.path);
list.push(i);
}
if (I >= buf.length) {
buf = tmp;
res(list);
return;
}
});
};
const Callback = function (incoming) {
setTimeout(function () {
var tmp = T.list();
events.forEach(function (o, i) {
if (o.e === "arrival" && incoming === 1) {
CheckNew(tmp, function (list) {
devFilter(list, o);
});
}
if (o.e === "removal" && incoming === 0) {
CheckGone(tmp, function (list) {
devFilter(list, o);
});
}
});
}, T.delay);
};
T.list = function () {
return api.list().toString().parse();
};
T.exists = function (devname) {
for (var i = 0; i < T.buf.length; i++) {
if (buf[i].name.indexOf(devname) > -1) {
return true;
}
}
return false;
};
T.free = function () {
T.active = false;
T.buf = null;
events = null;
};
T.on = function (event, callback, devId) {
events.push({e: event, c: callback, d: devId});
if (act === false) {
act = true;
api.onDevice(function (e) {
if (T.active === true) {
Callback(parseInt(e));
}
});
}
};
T.active = true;
T.buf = T.enum();
return T;
};