-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.coffee
More file actions
executable file
·71 lines (58 loc) · 2.21 KB
/
Copy pathserver.coffee
File metadata and controls
executable file
·71 lines (58 loc) · 2.21 KB
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
Db = require 'db'
Event = require 'event'
App = require 'app'
Geoloc = require 'geoloc'
Timer = require 'timer'
exports.onInstall = exports.onConfig = (config) !->
return if !config?
oldBase = Db.shared?.get('base')
if base = config.base
base = base.split(',')
base = {latitude: base[0], longitude: base[1]}
Db.shared.set 'base', base
else
base = {}
Db.shared.remove 'base'
oldRadius = Db.shared?.get('radius')
if radius = config.radius
Db.shared.set 'radius', radius
if !oldBase || oldRadius != radius || oldBase.latitude != base.latitude || oldBase.longitude != base.longitude
Db.shared.remove 'nearby'
for userId,geoloc of Db.backend.get('last')
onGeoloc userId, geoloc
# maybe also notify when distance of previous base location is far away
if !oldBase and base.latitude and base.longitude and App.title()
Event.create
unit: 'other'
text: "#{App.userName()} set the base location for '#{App.title()}'"
exports.client_update = !->
userIds = Geoloc.request('all')
log 'updating', userIds
for userId in userIds
Timer.cancel 'cancelLoad', userId
Timer.set 10000, 'cancelLoad', userId
log 'setting timer', 10000, 'cancelLoad', userId
Db.shared.set 'nearby', userId, 'loading', true
exports.cancelLoad = (userId) !->
log 'cancelLoad', userId
Db.shared.remove 'nearby', userId, 'loading'
exports.onGeoloc = onGeoloc = (userId, geoloc) !->
log 'onGeoloc', userId, JSON.stringify(geoloc)
return if typeof geoloc isnt 'object'
Db.backend.set 'last', userId, geoloc
base = Db.shared.get('base')
threshold = Db.shared.get('radius')||150
return if !base || !geoloc.latitude? || !geoloc.longitude?
distance = calcDist(base.latitude, base.longitude, geoloc.latitude, geoloc.longitude)*1000
log 'userId, distance', userId, distance
nearby = if distance < threshold && (geoloc.accuracy||0) < threshold then 1 else 0
Db.shared.set 'nearby', userId,
nearby: nearby
time: geoloc.time*1000
deg2rad = (deg) -> deg * (3.1415/180)
calcDist = (lat1,lon1,lat2,lon2) ->
dlat = deg2rad(lat2-lat1)
dlon = deg2rad(lon2-lon1)
a = Math.sin(dlat/2) * Math.sin(dlat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dlon/2) * Math.sin(dlon/2)
6371 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))