-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
executable file
·101 lines (76 loc) · 2.87 KB
/
plugin.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
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
from waggle import beehive
import pika
import waggle.pipeline
import time
import sys
import os
import subprocess
import json
from coresense import create_connection, NoPacketError
#Upgrade PyWaggle module
os.system('pip3 install git+https://github.com/waggle-sensor/pywaggle')
#Start RabbitMQ Server
os.system('rabbitmq-server start &')
time.sleep(25)
################################################################################
output = os.popen('curl --unix-socket /var/run/docker.sock http:/containers/json').read()
#print(output)
python_dict = json.loads(output)
containers=[]
for dict in python_dict:
containers.append(dict['Id'])
#print(dict['Id'])
waggle_output = os.popen('find /dev -name "waggle_coresense[0-9]*"').readlines()
waggle_output = list(map(lambda s: s.strip(), waggle_output))
print(waggle_output)
print(containers)
containers.sort()
print(containers)
bind_dict = {}
for i in range(len(waggle_output)): #Since (Number of Containers) >= (Number of Waggle Coresense Boards).
bind_dict[containers[i]] = waggle_output[i]
print(bind_dict)
node_dict = {}
node_output = os.popen('find /usr/lib/waggle/SSL/vnode/ -mindepth 1 -name "vnode*"')
node_output = list(map(lambda t: t.strip(), node_output))
for i in range(len(waggle_output)):
node_dict[containers[i]] = node_output[i]
print(node_dict)
command = "cat /proc/self/cgroup | grep 'docker' | sed 's/^.*\///' | tail -n1"
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=True)
container_id = process.communicate()
container_id = container_id[0].decode("utf-8").rstrip()
print(container_id)
#if container_id in bind_dict:
#print(bind_dict[container_id])
src = bind_dict[container_id]
dest = '/dev/waggle_coresense'
os.symlink(src, dest)
config = beehive.ClientConfig(
host='10.10.10.5',
port=23181,
node='000002000000000'+ str(int((node_dict[container_id])[-1])-1),
username = 'fib-publisher',
password = 'waggle',
cacert='/usr/lib/waggle/SSL/waggleca/cacert.pem',
cert= node_dict[container_id]+'/cert.pem',
key= node_dict[container_id]+'/key.pem')
client = beehive.PluginClient(
name='test:1',
config=config)
device = os.environ.get('CORESENSE_DEVICE', '/dev/waggle_coresense')
while True:
try:
print('Connecting to device: {}'.format(device), flush=True)
with create_connection(device) as conn:
print('Connected to device: {}'.format(device), flush=True)
while True:
message = conn.recv()
if message is not None:
print('Received frame.', flush=True)
#self.send(sensor='frame', data=message.frame)
client.publish('frame', message.frame)
time.sleep(5)
except NoPacketError:
print('No packets are being received. Resetting the serial connection...')