-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprobe_manager.js
148 lines (119 loc) · 3.09 KB
/
probe_manager.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
function ProbeManager() {
this.probesCache = {};
}
ProbeManager.prototype.loadProbes = function() {
try {
if(localStorage['probes']){
return JSON.parse(localStorage['probes']);
}else{
probes = new Object();
localStorage['probes'] = JSON.stringify(probes);
}
} catch (e) {
console.log('loadProbes excepiton');
}
};
ProbeManager.prototype.setProbe = function(probe) {
try {
console.log('Set Probe:[' + probe.serviceId + '_' + probe.query
+ ']');
var probes = JSON.parse(localStorage['probes']);
/*
* added probes if not exist
*/
if (!probes) {
probes = new Object();
window.localStorage.setItem('probes', probes);
console.log('No probes found add create new probes array');
}
if (probe.id == undefined){
probe.id = this._createUUID();
}
probes[escape(probe.id)] = probe;
localStorage['probes'] = JSON.stringify(probes);
// window.localStorage.setItem('probes', probes);
} catch (e) {
console.log("Error writing probe to localstorage");
console.log(e);
}
}
ProbeManager.prototype.removeProbeById = function(id) {
console.log('remove Probe:' + id);
try {
var probes = JSON.parse(localStorage['probes']);
/*
* added probes if not exist
*/
if (!probes) {
console.log('No probe found[' + id + ']:');
return null;
}
var probe = probes[escape(id)];
if (probe) {
delete probes[escape(id)];
localStorage['probes'] = JSON.stringify(probes);
return true;
}
} catch (e) {
console.log("Error getting item to localstorage for id:" + id);
console.log(e);
return null;
}
}
ProbeManager.prototype.getProbeById = function(id) {
console.log('get Probe:' + id);
try {
var probes = JSON.parse(localStorage['probes']);
/*
* added probes if not exist
*/
if (!probes) {
console.log('No probe found[' + id + ']:');
return null;
}
var probe = probes[escape(id)];
if (probe) {
return probe;
}
} catch (e) {
console.log("Error getting item to localstorage for id:" + id);
console.log(e);
return null;
}
}
ProbeManager.prototype.countProbes = function() {
console.log('countProbes');
try {
var probes = JSON.parse(localStorage['probes']);
var count = 0;
/*
* added probes if not exist
*/
if (!probes) {
console.log('No probe found[' + id + ']:');
return count;
}
for (var j in probes) {
count++;
}
return count;
} catch (e) {
console.log("Error counting probes");
console.log(e);
return null;
}
}
ProbeManager.prototype._createUUID = function() {
// http://www.ietf.org/rfc/rfc4122.txt
var s = [];
var hexDigits = "0123456789ABCDEF";
for (var i = 0; i < 32; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the
// clock_seq_hi_and_reserved
// to 01
var uuid = s.join("");
return uuid;
}