Repository navigation
Expand file tree
/
Copy pathswitch.py
More file actions
183 lines (137 loc) · 4.54 KB
/
Copy pathswitch.py
File metadata and controls
183 lines (137 loc) · 4.54 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python
# IMPORT
from __future__ import print_function
import subprocess
import sys
import os
import time
from blue_st_sdk.manager import Manager
from blue_st_sdk.manager import ManagerListener
from blue_st_sdk.node import NodeListener
from utils.protocol import ProtocolGenerator
from websocket import create_connection
# CONSTANTS
# Presentation message.
INTRO = """##################
# Switch #
##################"""
# Bluetooth Scanning time in seconds (optional).
SCANNING_TIME_s = 5
til = "BCN-423"
ws = create_connection("ws://localhost:8000")
time.sleep(2)
name = "switch"
sendName =ProtocolGenerator("name", name)
ws.send(sendName.create())
# FUNCTIONS
#
# Printing intro.
#
def print_intro():
print('\n' + INTRO + '\n')
# INTERFACES
#
# Implementation of the interface used by the Manager class to notify that a new
# node has been discovered or that the scanning starts/stops.
#
class MyManagerListener(ManagerListener):
#
# This method is called whenever a discovery process starts or stops.
#
# @param manager Manager instance that starts/stops the process.
# @param enabled True if a new discovery starts, False otherwise.
#
def on_discovery_change(self, manager, enabled):
print('Discovery %s.' % ('started' if enabled else 'stopped'))
if not enabled:
print()
#
# This method is called whenever a new node is discovered.
#
# @param manager Manager instance that discovers the node.
# @param node New node discovered.
#
def on_node_discovered(self, manager, node):
print('New device discovered: %s.' % (node.get_name()))
#
# Implementation of the interface used by the Node class to notify that a node
# has updated its status.
#
class MyNodeListener(NodeListener):
#
# To be called whenever a node connects to a host.
#
# @param node Node that has connected to a host.
#
def on_connect(self, node):
print('Device %s connected.' % (node.get_name()))
#
# To be called whenever a node disconnects from a host.
#
# @param node Node that has disconnected from a host.
# @param unexpected True if the disconnection is unexpected, False otherwise
# (called by the user).
#
def on_disconnect(self, node, unexpected=False):
print('Device %s disconnected%s.' % \
(node.get_name(), ' unexpectedly' if unexpected else ''))
if unexpected:
# Exiting.
print('\nExiting...\n')
sys.exit(0)
#
# MAIN APPLICATION
#
def main(argv):
# Printing intro.
print_intro()
try:
# Creating Bluetooth Manager.
manager = Manager.instance()
manager_listener = MyManagerListener()
manager.add_listener(manager_listener)
while True:
isConnected = False
discovered_devices = []
while not isConnected:
# Synchronous discovery of Bluetooth devices.
print('Scanning Bluetooth devices...\n')
manager.discover(SCANNING_TIME_s)
# Getting discovered devices.
discovered_devices = manager.get_nodes()
# Listing discovered devices.
if not discovered_devices:
isConnected = False
print("retry...")
else:
isConnected = True
necklace = False
for device in discovered_devices:
if device.get_name() == til:
necklace = True
print('%s: [%s]' % (device.get_name(), device.get_tag()))
# Connecting to the device.
print('Connecting to %s...' % (device.get_name()))
""" if not device.connect():
print('Connection failed.\n')
continue """
print("Connected !")
data = ProtocolGenerator(name, "1")
ws.send(data.create())
print("Data send, sleep 10s and loop")
# subprocess.run(['espeak','-vfr+f4','-s150', "Bonjour, Humain !"])
time.sleep(10)
device.disconnect()
print('Disconnection done.')
if not device.connect():
print("Loop")
isConnected = False
except KeyboardInterrupt:
try:
# Exiting.
print('\nExiting...\n')
sys.exit(0)
except SystemExit:
os._exit(0)
if __name__ == "__main__":
main(sys.argv[1:])