SafeNet/SDMaps/src/cs/Utils.coffee
2021-02-24 13:50:23 +02:00

91 lines
3.7 KiB
CoffeeScript

class Utils
#this will attempt to find point groups based on the latitude and longitude fields
@getPointGroupsStr : (str) ->
re = /((-?\d{1,3}\.\d{2,},){2})/g
prev = 0
(str[prev..(prev=g.index)-2] while (g = re.exec(str)))[1..-1]
.concat [str[prev..-1]]
parseDate = (str) ->
# time = Date.parse str.split(' ').join('T') #add a T between date and time ISO standard
time = moment(str, 'YYYY-MM-DD HH:mm:ss').toDate()
# console.log "For date `#{str}` time is #{time}."
if isNaN time
console.warning "Error: Received an invalid date: '#{str}'" if str.trim().length > 1
new Date()
else new Date time
parseBoolean = (str) ->
result = try
JSON.parse str?.toLowerCase()
catch e
console.warning "Error: Received an invalid boolean: '#{str}' Error = #{e}"
!!result
checkIcon = (path) ->
# $.ajax(path).fail ()-> console.warning "ERROR: #{path} does not exist."
path
normDecimals = (float,n=5) ->
parseFloat parseFloat(float).toFixed(n)
#back compatibility with True or Flase
parseBooleanOrInt = (str, defaultValue) ->
return switch "#{str}"?.toLowerCase()
when 'true' then defaultValue
when 'false' then 0
else _.parseInt str
@parsePoints : (args) ->
# console.log args
points = Utils.getPointGroupsStr(args.join()).map (pg) -> pg.split ','
props = ['latitude', 'longitude', 'time','id', 'speed', 'altitude','address','icon','hasVoice', 'hasText', 'isInCall']
points.map (p, idx) ->
console.warning? "Error: Wrong number of point arguments #{p.length} != #{props.length}, #{p} " if p.length isnt props.length
new DataSetPoint().merge props.reduce (a,e,i) ->
a[e] = switch e
when 'latitude','longitude', 'altitude' then normDecimals(+p[i])
when 'id' then (if p[i] and p[i].length > 0 then p[i] else "\##{idx+1}")
when 'time' then parseDate p[i]
when 'isInCall' then parseBoolean p[i]
when 'hasText' then _.parseInt p[i]
when 'hasVoice' then parseBooleanOrInt p[i], 3
when 'icon' then checkIcon p[i]
else p[i]
a
,{}
#callback will be called multiple times when an ICE candidate becomes available
#callback({priority: 2313549943, ip: "10.120.1.120"})
#the lowest priority is most likely to be the real IP, the others VPNs and such
@getLocalIP : (callback) ->
RTCPeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
console.warning RTCPeerConnection
if RTCPeerConnection
rtc = new RTCPeerConnection iceServers:[]
rtc.createDataChannel '', reliable:false
rtc.onicecandidate = (evt) ->
if evt.candidate?
parts = evt.candidate.candidate.split ' ' #http://tools.ietf.org/html/rfc5245#section-15.1
console.warning? "ICE candidate: #{parts}"
callback? {priority : +parts[3], ip : parts[4]} if parts[2] is 'tcp'
rtc.createOffer (d) -> rtc.setLocalDescription d
this?.Utils = Utils # in global context, this === window
# example = "44.4189744768664,26.0982935316861,yyyy-MM-dd HH:mm:ss,TRBO 103,0kph,0,Bulevardul Mara?e?ti 29 Bucure?ti Romania,,pin_people_06.png,True,True,True"
# x = Utils.parsePoints example.split(',')
# # # # console.log getPointGroupsStr "27.32854,-82.58386,2015-06-12 16:04:38,Demo102,83kph,0,John Ringling Parkway Sarasota FL 34236 USA,cars/pin_ambulance_02.png,True,True,True,27.30351,-82.57038,2015-08-27 14:55:10,Demo101,58kph,125,1398 Benjamin Franklin Drive Sarasota FL 34236 USA,cars/pin_truck_06.png,True,True"
# console.log JSON.stringify(x,null, ' ')
# console.log new Utils().parseBooleanOrInt 0, 32;