Commit a4d35068 authored by Morten Brekkevold's avatar Morten Brekkevold
Browse files

Sync room coordinates along with rooms

This means also syncing changed coordinates.
parent a5cbacf4
......@@ -8,6 +8,7 @@ import copy
import functools
import os
import socket
import decimal
from collections import defaultdict
import logging
from ipaddress import ip_address
......@@ -18,6 +19,8 @@ import argparse
from slack_log_handler import SlackLogHandler
import utm
from kindnavsync.navapi import NAVAPI, ObjectNotFound, ClientError
KIND_SERVICE_URL = "https://kind.uninett.no/autoregister/utstyr_eksport.json"
......@@ -495,25 +498,58 @@ def insert_one_into_nav(nav_api, kind_device):
netbox["read_only"] = DEFAULT_SNMP_COMMUNITY
netbox["snmp_version"] = DEFAULT_SNMP_VERSION
ensure_room_exists_and_is_up_to_date(kind_device, nav_api)
try:
nav_api.get_room(kind_device.room)
nav_api.post_netbox(netbox)
except ClientError as error:
LOG.error(
"Netbox post feilet for %s. Feilrespons: %s", netbox.get("sysname"), error
)
raise
def ensure_room_exists_and_is_up_to_date(kind_device, nav_api):
position = utm_to_latlon(kind_device.utm)
try:
response = nav_api.get_room(kind_device.room)
room = response.body
except ObjectNotFound:
room = dict(id=kind_device.room, location=DEFAULT_LOCATION)
new_room = dict(
id=kind_device.room, location=DEFAULT_LOCATION, position=position
)
try:
nav_api.post_room(room)
nav_api.post_room(new_room)
LOG.debug("Adding new room to NAV:", new_room)
except ClientError as error:
LOG.error(
"Room post feilet for %s. Feilrespons: %s", kind_device.room, error
)
raise
else:
if position and _are_positions_equal(room.get("position"), position):
LOG.debug(
"Updating coordinates of room %s: %r != %r",
kind_device.room,
position,
room.get("position"),
)
try:
nav_api.patch_room(room.get("id"), {"position": position})
except ClientError as error:
LOG.error(
"Room update coordinates failed for %s. Error: %s",
kind_device.room,
error,
)
raise
try:
nav_api.post_netbox(netbox)
except ClientError as error:
LOG.error(
"Netbox post feilet for %s. Feilrespons: %s", netbox.get("sysname"), error
)
raise
def _are_positions_equal(position1, position2):
pos1 = [decimal.Decimal(x) for x in position1]
pos2 = [decimal.Decimal(x) for x in position2]
LOG.debug("%r != %r", pos1, pos2)
return pos1 == pos2
@functools.lru_cache(1)
......@@ -606,11 +642,12 @@ def can_be_deleted(netbox):
class KindDevice(object):
def __init__(self, id, name, serial, room, type, status=None):
def __init__(self, id, name, serial, room, utm, type, status=None):
self.id = id
self.name = name
self.serial = serial
self.room = room
self.utm = utm
self.type = type
self.status = status
......@@ -624,14 +661,15 @@ class KindDevice(object):
return cls(
record["id"],
record["navn"],
record.get("serienummer", None),
record.get("nettinstallasjon", None),
record.get("type", None),
status=record.get("status_tekst", None),
record.get("serienummer"),
record.get("nettinstallasjon"),
record.get("utm"),
record.get("type"),
status=record.get("status_tekst"),
)
def __repr__(self):
ret = "KindDevice({id!r}, {name!r}, {serial!r}, {room!r}, {type!r})"
ret = "KindDevice({id!r}, {name!r}, {serial!r}, {room!r}, {utm!r}, {type!r})"
return ret.format(**vars(self))
def __str__(self):
......@@ -650,5 +688,18 @@ class KindDevice(object):
return self.status == "Installert"
def utm_to_latlon(kind_coordinate):
"""Converts a Kind-provided UTM coordinate into latitude/longitude number.
:type kind_coordinate: str
"""
max_12 = decimal.Context(prec=12) # Never use more than 12 decimals for NAV coords
if kind_coordinate:
coord = [int(x) for x in kind_coordinate.split()]
assert len(coord) == 3
coord = [max_12.create_decimal(x) for x in utm.to_latlon(*reversed(coord), "U")]
return [str(x) for x in coord]
if __name__ == "__main__":
main()
main()
\ No newline at end of file
......@@ -62,6 +62,9 @@ class NAVAPI(object):
def post_room(self, room):
return self.api.room.create(body=room)
def patch_room(self, roomid, patch):
return self.api.room.partial_update(roomid, body=patch)
@functools.lru_cache(1)
def get_version(self):
try:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment