-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowerLogic.js
172 lines (94 loc) · 3.03 KB
/
browerLogic.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
const dbUser = new PouchDB('user');
const dbPoints = new PouchDB('points');
const dbWaring = new PouchDB('warnings');
const userId = 'uuid';
function addWaring(warnings){
let cache = Array.from(warnings);
warnings = [];
dbWaring.bulkDocs(cache);
}
function register(){
dbUser.put({
_id : userId,
referencePoints : [{lat : 29.28737, lon : 72.8964}],
covidIds :[]
})
}
// ------------------- geolocation logic starts --------------------------------------------------------------------
let userPoints =[]
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
userPoints.push({
_id : new Date().toISOString(),
lat : crd.latitude,
lon : crd.longitude
});
console.log(userPoints);
}
function error(err) {
console.log(`ERROR(${err.code}): ${err.message}`);
}
function addLocation(){
navigator.geolocation.getCurrentPosition(success, error, options);
}
function addToDb(){
let cache = Array.from(userPoints);
userPoints = [];
dbPoints.bulkDocs(cache);
}
// setInterval(addLocation, 4000);
// setInterval(addToDb,20000 );
// ---------------------- geoloaction logic ends ----------------------------------------------------------
// -------------------- warning algorithm calculation ----------------------------------------------------------
// Converts numeric degrees to radians
function toRad(Value)
{
return Value * Math.PI / 180;
}
function calcCrow(lat1, lon1, lat2, lon2)
{
var R = 6371; // km
var dLat = toRad(lat2-lat1);
var dLon = toRad(lon2-lon1);
var lat1 = toRad(lat1);
var lat2 = toRad(lat2);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d*1000;
}
function calcWarning(userData, covidData){
let warningPoints = 0;
let n = userData.length();
let m = covidData.length();
for(let i = 0;i<n;i++){
for(let j=0;j<m;j++){
let {_id : uid, lat : ulat, lon : ulon} = covidData[j];
let {_id : cid, lat : clat, lon : clon} = covidData[j];
let days = abs(new Date(uid).getDate() - new Date(cid).getDate());
if(days<=3 && calcCrow(ulat, ulon, clat, clon) <= 100)WarningPoints++;
}
}
return warningPoints;
}
let warningPoints = [];
async function covidListener(){
let user = await dbUser.get(userId);
let covidPoints = user['covidIds'];
let res = await fetch('');
let obj = await res.json();
// some more logic
let userDataPoints = await dbPoints.allDocs({});
// more logic
if(statement){
warningPoints.push({_id : new Date().toISOString(), points: calcWarning(userDataPoints, obj)});
}
}
// ----------------------------Dom manipulations -------------------------------------------
// on refresh update warning list