summaryrefslogtreecommitdiff
path: root/tools/mmd/MmdLocation.py
blob: e50360f625c3031933feffa5ac23de50feee2295 (plain) (blame)
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
"""
Location class for MURSAT1 Mission Dashboard
"""
import hashlib
from cgi import parse_qs, escape
from MmdDb import Db

def checkLongitude (longitude):
  if type (longitude) != type (0.0):
    longitude = float (longitude)
  if longitude < -180.0 or longitude > 180.0:
    return False
  return longitude

def checkLatitude (latitude):
  if type (latitude) != type (0.0):
    latitude = float (latitude)
  if latitude < -90.0 or latitude > 90.0:
    return False
  return latitude

class Location:

  def __init__ (self, location_id = False):
    self.db = Db ()
    if location_id:
      self.location_id = location_id
      self.load ()
    else:
      self.name = 'Graz/Austria'
      self.longitude = 15.44226
      self.latitude = 47.06576
      self.altitude = 376

  def create (self, name, longitude, latitude, altitude, is_default, user_id):
    self.name = name
    self.is_default = is_default
    self.longitude = checkLongitude (longitude)
    self.latitude = checkLatitude (latitude)
    self.altitude = altitude
    
    if not self.longitude:
      self.longitude = 15.44226
    if not self.latitude:
      self.latitude = 47.06576
    if not self.altitude:
      self.altitude = 376
    if not self.name:
      self.name = 'Graz/Austria'

    self.location_id = hashlib.sha1 ('{0}{1}{2}'.format (user_id, self.longitude, self.latitude)).hexdigest ()
    return self.db.locationCreate (
      self.location_id,
      self.name,
      self.longitude,
      self.latitude,
      self.altitude,
      user_id,
      self.is_default)

  def findUserId (self, user_id):
    l = self.db.locationFindUserId (user_id)
    if not l:
      return False
    self.name = l['name']
    self.longitude = float (l['longitude'])
    self.latitude = float (l['latitude'])
    self.is_default = l['is_default']
    self.location_id = l['id']

  def load (self, location_id = False):
    if location_id:
      self.location_id = location_id
    l = self.db.locationFindId (self.location_id)
    if not l:
      return False
    self.name = l['name']
    self.longitude = float (l['longitude'])
    self.latitude = float (l['latitude'])
    self.altitude = float (l['altitude'])
    self.is_default = l['is_default']
    self.user_id = l['user_id']
    return True

  def delete (self, location_id = False):
    if location_id:
      self.location_id = location_id
    self.db.locationDeleteId (self.location_id)

if __name__ == "__main__":
  pass

# vim: tw=0 ts=2 expandtab
# EOF