forked from ClearBlade/BACnet-Adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BACnetAdapter.py
58 lines (49 loc) · 1.75 KB
/
BACnetAdapter.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
import threading, json
from bacpypes.app import BIPSimpleApplication
from bacpypes.apdu import WhoIsRequest
from bacpypes.pdu import GlobalBroadcast
from Device import Device
from MQTT import MQTT
class BACnetAdapter(BIPSimpleApplication):
def __init__(self, device, hostname, args):
BIPSimpleApplication.__init__(self, device, hostname)
self.who_is_request = None
self.credentials = args
self.interval = args["whoisInterval"]
self.mqtt = None
def request(self, apdu):
if isinstance(apdu, WhoIsRequest):
self.who_is_request = apdu
BIPSimpleApplication.request(self, apdu)
def do_IAmRequest(self, apdu):
if not self.who_is_request:
return
else:
print apdu.iAmDeviceIdentifier
device = Device(apdu.iAmDeviceIdentifier, apdu.pduSource, self)
device.get_object_list()
def send_props_to_platform(self, device, obj, props):
obj_to_send = {
'device': {
'id': device.id,
'source': device.source
},
'object': obj,
'properties': props
}
try:
msg = json.dumps(obj_to_send, ensure_ascii=False, default=json_serial)
self.mqtt.PublishTopic("bacnet/in", str(msg))
except Exception as e:
print e
def start(self):
if self.mqtt is None:
self.mqtt = MQTT(self.credentials)
self.who_is(None, None, GlobalBroadcast())
timer = threading.Timer(self.interval, self.start)
timer.daemon = True
timer.start()
def json_serial(obj):
"""JSON serializer for objects not serializable by default json code"""
#if isinstance(obj, TimeStamp):
return str(obj)