-
Notifications
You must be signed in to change notification settings - Fork 1
/
interchange.py
90 lines (73 loc) · 2.88 KB
/
interchange.py
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
# -*- coding: utf-8 -*-
import sys
import logging
import datetime
import pytz
try:
from qpid.messaging import Connection, Message, MessagingError, Empty, ConnectionError
except ImportError as ie:
logging.exception("Unable to find 'qpid' module. Do you have it in sys.path / PYTHONPATH?")
sys.exit(1)
class NordicWayIC:
def __init__(self, url, sender, receiver, username, password, options=None):
self.options = options if options else {}
self.url = url
self._queue_sender = sender
self._queue_receiver = receiver
self._credentials = {"username": username, "password": password}
self.log = logging.getLogger("geofencebroker")
def __enter__(self):
self.connect()
return self
def __exit__(self, type, value, traceback):
self.close()
def connect(self):
self.connection = Connection(self.url,
username=self._credentials.get("username"),
password=self._credentials.get("password"),
**self.options)
self.connection.open()
self.session = self.connection.session()
self.sender = self.session.sender(self._queue_sender)
self.receiver = self.session.receiver(self._queue_receiver)
def send_messsage(self, msg):
try:
self.sender.send(msg)
self.sender.check_error()
except MessagingError:
self.log.exception("Error sending message!")
except Exception:
self.log.exception("Exception occured while sending..")
self.session.acknowledge()
def send_obj(self, datex_obj):
"""
Use data from the 'datex2' object to construct a proper
AMQP object with all the required properties set.
"""
tz = pytz.timezone("Europe/Oslo")
now_iso_timestamp = datetime.datetime.now(tz).isoformat()
centroid = datex_obj.centroid
prop = {
"who": "Norwegian Public Roads Administration",
"how": "Datex2",
"what": "PredefinedLocation",
"lat": centroid[0],
"lon": centroid[1],
"where1": "no",
"when": now_iso_timestamp
}
m = Message(user_id=self._credentials.get("username"),
properties=prop,
content=str(datex_obj))
self.log.debug(u"Sending message: version={}, name={}".format(
datex_obj.version, datex_obj.name))
self.send_messsage(m)
def close(self):
self.connection.close()
def recv(self, timeout=None):
msg = self.receiver.fetch(timeout=timeout)
return msg
def __repr__(self):
return "<{} url={}, sender={}, receiver={}, options={}>".format(
self.__class__.__name__, self.url,
self._queue_sender, self._queue_receiver, self.options)