summaryrefslogtreecommitdiff
path: root/server/locations/xmlrpc.py
blob: 5d22bdf1bbb8e5c08d07a197a0dbaef0e7474fc3 (plain)
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
from locations.models import *
from math import *

# TODO: Authentication!!!
# TODO: Better error handling

def GetLocationList():
	"""Returns a list of locations."""

	locations = Location.objects.all()		

	results = []

	for location in locations:
		results.append({ 
			'id': location.id, 
			'lat': location.lat, 
			'lon': location.lon, 
			'name': location.name, 
			'locname': location.locname, 
			'website': location.website
		})

	return results

def GetLocationDetails(locationid):
	location = Location.objects.get(id=locationid)
	if location.id:
		vals = {"location": location}
		equipcnt = location.equipment_set.count()
		if equipcnt > 0:
			equip = []
			for equipment in location.equipment_set.iterator():
				caps = []
				for capability in equipment.type.capabilities.iterator():
					caps.append(capability.name)
				equip.append({"type": equipment.type.name, "maker": equipment.type.maker, "capabilities": caps, "notes": equipment.notes})
			vals['equipment'] = equip
		
		return vals
	else:
		return {}

def GetEquipmentTypes():
	equiptypes = EquipmentType.objects.all()
	
	results = []
	for e in equiptypes.iterator:
		caps = []
		for capability in e.capabilities.iterator():
			caps.append(capability.name)
		results.append({'name': e.name, 'maker': e.maker, 'capabilities': caps})
	
	return results
	
def GetCapabilities():
	caps = EquipmentCapability.objects.all()
	ret = []
	for a in caps.iterator():
		ret.append({'id': a.id, 'name': a.name})
	return ret
	
def AddEquipmentType(name, maker, capabilities):
	# Register an equipment type
	et = EquipmentType(name=name, maker=maker)
	et.save()
	for a in capabilities:
		if type(a) is int:
			et.capabilities.add(a)
	et.save()
	return {'saved': True}
	
def AddEquipment(locationid, equipmenttype, notes):
	# Register equipment
	eq = Equipment(type=equipmenttype, location=locationid, notes=notes)
	eq.save()
	return {'saved': True}

def AddLocation(name, locname, lat, lon, website):
	# Add a location.
	manager = 1
	# FIXME: Here we should make manager be the same as the authed user.
	location = Location(name=name, locname=locname, lat=lat, lon=lon, website=website, manager=manager)
	location.save()
	return {'location': location, 'saved': True}
	
def SetLocation(locationid, name, locname, lat, lon, website):
	location = Location(locationid=locationid)
	if name:		location.name = name
	if locname:	location.locname = locname
	if latitude:	location.latitude = lat
	if longitude: 	location.longitude = lon
	if website:	location.website = website
	location.save()
	
def AddCapability(capability):
	# Add a capability class
	cap = EquipmentCapability(name=capability)
	cap.save()
	return {'saved': True, 'id': cap.id}
	
def FindEquipmentByCapability(capid):
	# Return a list of equipment, and each equipment type contains a list of locations where it exists.
	
	return {}
	


def Haversine(lon1, lat1, lon2, lat2):
	# TODO: Move this to some utilities thing?
	"""
	Find the distance in km between two points on the surface of the earth
	using the haversine formula. http://mathforum.org/library/drmath/view/51879.html
	http://www.mpi.nl/world/persons/private/micdunn/misc/haversine.py
	"""
	lon1 = radians(lon1)
	lon2 = radians(lon2)
	lat1 = radians(lat1)
	lat2 = radians(lat2)
	dlon = lon2 - lon1
	dlat = lat2 - lat1

	a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
	c = 2 * atan2(sqrt(a), sqrt(1-a)) 
	km = 6367 * c 
	return km