disc-air.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from django.core.management.base import BaseCommand
  2. from django.contrib.gis.geos import Point
  3. from services.models import IPResource, Antenna, AntennaAllocation, Tunnel
  4. from djadhere.utils import get_active_filter
  5. import os, json, yaml
  6. from ipaddress import ip_network
  7. class Command(BaseCommand):
  8. help = 'Import des informatios du cache de disc-air'
  9. def add_arguments(self, parser):
  10. parser.add_argument('disc-air', help='Cache disc-air')
  11. def handle(self, *args, **options):
  12. cache = options['disc-air']
  13. for tunnel in Tunnel.objects.all():
  14. if not os.path.isdir(os.path.join(cache, tunnel.name)):
  15. continue
  16. gps_file = os.path.join(cache, '..', 'gps-%s.yaml' % tunnel.name)
  17. if os.path.isfile(gps_file):
  18. with open(gps_file) as fd:
  19. gps = yaml.load(fd)
  20. for network in tunnel.networks.all():
  21. for ip in network.ipresource_set.all():
  22. status_file = os.path.join(cache, tunnel.name, ip.ip, 'fd694727ed4810b9d242c45906ea9417')
  23. if not os.path.isfile(status_file):
  24. continue
  25. with open(status_file) as fd:
  26. try:
  27. status = json.load(fd)
  28. except Exception as e:
  29. continue
  30. try:
  31. hostname = status['host']['hostname']
  32. mode = status['wireless']['mode']
  33. ssid = status['wireless']['essid']
  34. mac = status['wireless']['apmac']
  35. except KeyError:
  36. continue
  37. mode = 1 if mode == 'ap' else 2
  38. if ip.in_use:
  39. update = False
  40. antenna = ip.allocations.filter(get_active_filter()).first().antenna # peut faire mieux …
  41. if antenna.label:
  42. if antenna.label != hostname:
  43. #print(ip, ", djadhere:", antenna.label, ", disc-air:", hostname)
  44. continue
  45. else:
  46. #print("update hostname")
  47. antenna.label = hostname
  48. update = True
  49. if antenna.mode:
  50. if antenna.mode != mode:
  51. #print(ip, ", djadhere:", antenna.mode, ", disc-air:", mode)
  52. continue
  53. else:
  54. #print("update mode")
  55. antenna.mode = mode
  56. update = True
  57. if antenna.ssid:
  58. if antenna.ssid != ssid:
  59. #print(ip, ", djadhere:", antenna.ssid, ", disc-air:", ssid)
  60. continue
  61. else:
  62. #print("update ssid")
  63. antenna.ssid = ssid
  64. update = True
  65. if antenna.mac:
  66. if antenna.mac != mac:
  67. #print(ip, ", djadhere:", antenna.mac, ", disc-air:", mac)
  68. continue
  69. else:
  70. #print("update mac")
  71. antenna.mac = mac
  72. update = True
  73. if ip.ip in gps:
  74. coords = gps[ip.ip]
  75. if len(coords) == 3:
  76. latitude, longitude, orientation = coords
  77. else:
  78. latitude, longitude = coords
  79. orientation = None
  80. assert(41 < latitude and latitude < 45)
  81. assert(1 < longitude and longitude < 2)
  82. position = Point(longitude, latitude, srid=4326)
  83. if antenna.position:
  84. if antenna.position != position:
  85. #print(ip, ", djadhere:", antenna.position, ", disc-air:", position)
  86. continue
  87. else:
  88. antenna.position = position
  89. update = True
  90. if antenna.orientation:
  91. if antenna.orientation != orientation:
  92. #print(antenna.orientation, orientation)
  93. continue
  94. elif orientation:
  95. antenna.orientation = orientation
  96. update = True
  97. if update:
  98. antenna.save()
  99. else:
  100. #print("created")
  101. antenna = Antenna.objects.create(label=hostname, mode=mode, ssid=ssid, mac=mac)
  102. AntennaAllocation.objects.create(resource=ip, antenna=antenna)