Changeset 10729 in genesis for tools


Ignore:
Timestamp:
May 8, 2012, 9:37:52 PM (13 years ago)
Author:
rick
Message:

Verbeter alle compass waardes, door het uit te rekenen met coordinaten.

Fixes: nodefactory#100

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/gformat.py

    r10715 r10729  
    3131import time
    3232import rdnap
     33import math
    3334import make_network_kml
    3435from pprint import pprint
     
    140141
    141142
    142 
    143 def make_relations():
     143def network(ip):
     144  addr, mask = ip.split('/')
     145  # Not parsing of these folks please
     146  addr = parseaddr(addr)
     147  mask = int(mask)
     148  network = addr & ~((1 << (32 - mask)) - 1)
     149  return network
     150 
     151
     152
     153def make_relations(datadumps=None):
    144154  """ Process _ALL_ yaml files to get connection relations """
    145   errors = ""
     155  errors = []
    146156  poel = defaultdict(list)
    147   for host in get_hostlist():
     157
     158  if not datadumps:
     159    for host in get_hostlist():
     160      datadumps[host] = get_yaml(host)
     161
     162  for host, datadump in datadumps.iteritems():
    148163    try:
    149       datadump = get_yaml(host)
    150164      for iface_key in datadump['autogen_iface_keys']:
    151         l = datadump[iface_key]['ip']
    152         addr, mask = l.split('/')
    153 
    154         # Not parsing of these folks please
    155         if not valid_addr(addr):
    156           continue
    157 
    158         addr = parseaddr(addr)
    159         mask = int(mask)
    160         network = addr & ~((1 << (32 - mask)) - 1)
    161         poel[network] += [(host,datadump[iface_key])]
     165        net_addr = network(datadump[iface_key]['ip'])
     166        poel[net_addr] += [(host,datadump[iface_key])]
    162167    except (KeyError, ValueError), e:
    163       errors += "[FOUT] in '%s' interface '%s'" % (host,iface_key)
    164       errors += e
     168      errors.append("[FOUT] in '%s' interface '%s' (%s)" % (host,iface_key, e))
    165169      continue
    166170  return (poel, errors)
     
    189193  """
    190194  dy = lat2 - lat1
    191   dx = math.cos(math.pi/180*lat1)*(long2 - long1)
     195  dx = math.cos(lat1)*(long2 - long1)
    192196  angle = math.atan2(dy,dx)
    193197  return angle
    194198
     199
     200
    195201def angle_to_cd(angle):
    196202  """ Return Dutch Cardinal Direction estimation in 'one digit' of radian angle """
     
    198204  # For easy conversion get positive degree
    199205  degrees = math.degrees(angle)
    200   if degrees < 0:
    201     360 - abs(degrees)
     206  abs_degrees = 360 + degrees if degrees < 0 else degrees
    202207
    203208  # Numbers can be confusing calculate from the 4 main directions
    204209  p = 22.5
    205   if degrees < p:
    206     return "n"
    207   elif degrees < (90 - p):
    208     return "no"
    209   elif degrees < (90 + p):
    210     return "o"
    211   elif degrees < (180 - p):
    212     return "zo"
    213   elif degrees < (180 + p):
    214     return "z"
    215   elif degrees < (270 - p):
    216     return "zw"
    217   elif degrees < (270 + p):
    218     return "w"
    219   elif degrees < (360 - p):
    220     return "nw"
     210  if abs_degrees < p:
     211    cd = "n"
     212  elif abs_degrees < (90 - p):
     213    cd = "no"
     214  elif abs_degrees < (90 + p):
     215    cd = "o"
     216  elif abs_degrees < (180 - p):
     217    cd = "zo"
     218  elif abs_degrees < (180 + p):
     219    cd = "z"
     220  elif abs_degrees < (270 - p):
     221    cd = "zw"
     222  elif abs_degrees < (270 + p):
     223    cd = "w"
     224  elif abs_degrees < (360 - p):
     225    cd = "nw"
    221226  else:
    222     return "n"
     227    cd = "n"
     228  return cd
     229
     230
     231
     232def cd_between_hosts(hostA, hostB, datadumps):
     233  # Using RDNAP coordinates
     234  dx = float(int(datadumps[hostA]['rdnap_x']) - int(datadumps[hostB]['rdnap_x'])) * -1
     235  dy = float(int(datadumps[hostA]['rdnap_y']) - int(datadumps[hostB]['rdnap_y'])) * -1
     236  return angle_to_cd(math.atan2(dx,dy))
     237
     238  # GPS coordinates seems to fail somehow
     239  #latA = float(datadumps[hostA]['latitude'])
     240  #latB = float(datadumps[hostB]['latitude'])
     241  #lonA = float(datadumps[hostA]['longitude'])
     242  #lonB = float(datadumps[hostB]['longitude'])
     243  #return angle_to_cd(angle_between_points(latA, latB, lonA, lonB))
    223244
    224245
     
    333354def is_member(ip, mask, canidate):
    334355  """ Return True if canidate is part of ip/mask block"""
    335   ip_addr = gformat.parseaddr(ip)
    336   ip_canidate = gformat.parseaddr(canidate)
     356  ip_addr = parseaddr(ip)
     357  ip_canidate = parseaddr(canidate)
    337358  mask = int(mask)
    338359  ip_addr = ip_addr & ~((1 << (32 - mask)) - 1)
     
    13201341      # First generate all datadumps
    13211342      datadumps = dict()
     1343      ssid_to_node = dict()
    13221344      for host in get_hostlist():
    13231345        logger.info("# Processing: %s", host)
     
    13271349        datadumps[datadump['autogen_realname']] = datadump
    13281350
     1351      (poel, errors) = make_relations(datadumps)
     1352      print "\n".join(["# WARNING: %s" % x for x in errors])
    13291353
    13301354      for host,datadump in datadumps.iteritems():
     
    13641388          if not datadump[iface_key].has_key('desc'):
    13651389            datadump[iface_key]['desc'] = 'autogen-FIXME'
     1390          # Set the compass value based on the angle between the poels
     1391          if datadump[iface_key].has_key('ns_ip'):
     1392            my_pool = poel[network(datadump[iface_key]['ip'])]
     1393            remote_hosts = list(set([x[0] for x in my_pool]) - set([host]))
     1394            if remote_hosts:
     1395              compass_target = remote_hosts[0]
     1396              datadump[iface_key]['compass'] = cd_between_hosts(host, compass_target, datadumps)
     1397           
    13661398        store_yaml(datadump)
    13671399    elif sys.argv[1] == "list":
Note: See TracChangeset for help on using the changeset viewer.