-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsms_functionality.py
More file actions
203 lines (169 loc) · 6.03 KB
/
sms_functionality.py
File metadata and controls
203 lines (169 loc) · 6.03 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import dbus
import bluetooth
from guizero import App, PushButton, ListBox, Combo, Text
try:
from gi.repository import GObject
except ImportError:
import gobject as GObject
from pprint import pformat
import time
from threading import Thread
BUS_NAME='org.bluez.obex'
PATH = '/org/bluez/obex'
CLIENT_INTERFACE = 'org.bluez.obex.Client1'
SESSION_INTERFACE = 'org.bluez.obex.Session1'
MESSAGE_ACCESS_INTERFACE = 'org.bluez.obex.MessageAccess1'
OUTBOX_PATH = 'telecom/msg/outbox'
TEST_MSG = '''BEGIN:BMSG<CR><LF>\
VERSION:1.0<CR><LF>\
STATUS:UNREAD<CR><LF>\
TYPE:SMS_GSM<CR><LF>\
FOLDER:telecom/msg/outbox<CR><LF>\
BEGIN:VCARD<CR><LF>\
VERSION:2.1<CR><LF>\
FN:Mark Jens<CR><LF>\
N:Jens, Mark<CR><LF>\
TEL:0040745754188<CR><LF>\
EMAIL:<CR><LF>\
END:VCARD<CR><LF>\
BEGIN:BENV<CR><LF>\
BEGIN:VCARD<CR><LF>\
VERSION:2.1<CR><LF>\
FN:Jens Hanssen<CR><LF>\
N:<CR><LF>\
TEL:0040745757086<CR><LF>\
EMAIL:<CR><LF>\
END:VCARD<CR><LF>\
BEGIN:BBODY<CR><LF>\
CHARSET:UTF-8<CR><LF>\
LENGTH:103<CR><LF>\
BEGIN:MSG<CR><LF>\
Bonjour, je serai en retard. Envoyé depuis ma voiture (aucune réponse attendue)<CR><LF>\
END:MSG<CR><LF>\
END:BBODY<CR><LF>\
END:BENV<CR><LF>'''
session = None
map = None
map_devices = {}
def unwrap(x):
"""Hack to unwrap D-Bus values, so that they\'re easier to read when
printed. Taken from d-feet """
if isinstance(x, list):
return map(unwrap, x)
if isinstance(x, tuple):
return tuple(map(unwrap, x))
if isinstance(x, dict):
return dict([(unwrap(k), unwrap(v)) for k, v in x.items()])
for t in [str, int, float, bool]:
if isinstance(x, t):
return t(x)
return x
def dbus_loop():
print ('starting dbus loop')
print (dbus)
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
mainloop = GObject.MainLoop()
mainloop.run()
#callbacksCLIENT_INTERFACE
def bt_choose(sel):
global session
global map
print (sel)
print("Creating Session")
#tDBus = Thread(target=dbus_loop)
#tDBus.start()
bus = dbus.SessionBus()
#dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
#mainloop = GObject.MainLoop()
#tDBus = Thread(target=mainloop.run())
client = dbus.Interface(bus.get_object(BUS_NAME, PATH), CLIENT_INTERFACE)
print ('connecting to {}'.format(map_devices[sel]))
path = client.CreateSession(map_devices[sel], { "Target": "map" })
obj = bus.get_object(BUS_NAME, path)
session = dbus.Interface(obj, SESSION_INTERFACE)
map = dbus.Interface(obj, MESSAGE_ACCESS_INTERFACE)
time.sleep(1)
if not map:
print ('WARNING:no map instance available')
return
with open('bmessage1') as f:
content = f.readline()
print (content)
#print ('push message')
#map.PushMessage(TEST_MSG, "telecom/msg/outbox", dict(), reply_handler=create_transfer_reply, error_handler=error)
def discover_dev():
global map_devices
combo.clear()
devices = bluetooth.discover_devices(lookup_names = True)
print ('devices found', len(devices))
for addr, name in devices:
map_devices[name] = addr
combo.append(name)
#print (name, addr)
#print (generate_bluez_device_id(addr))
def create_transfer_reply(path, properties):
print ('reply')
transfer_path = path
props[path] = properties
if self.verbose:
print("Transfer created: %s (file %s)" % (path, properties["Filename"]))
def errorss(err):
print(err)
mainloop.quit()
def push_message():
global map
print (map)
print ('push message:' + TEST_MSG)
map.PushMessage(TEST_MSG, "telecom/msg/outbox", dict(), reply_handler=create_transfer_reply, error_handler=errorss)
def list_folders():
global map
print (map)
print ('list folders')
for i in map.ListFolders(dict()):
print("%s/" % (i["Name"]))
map.SetFolder(i["Name"])
for j in map.ListFolders(dict()):
print(" %s/" % (j["Name"]))
map.SetFolder(j['Name'])
for k in map.ListFolders(dict()):
print(" %s/" % (k["Name"]))
#if k["Name"] == 'draft':
if k["Name"] == 'draft':
map.SetFolder(k['Name'])
print ('push message:' + TEST_MSG)
map.PushMessage(TEST_MSG, "", dict(), reply_handler=create_transfer_reply, error_handler=errorss)
#try:
# print ('list messages')
# ret = map.ListMessages('', dict())
# print(pformat(unwrap(ret)))
#except dbus.exceptions.DBusException as e:
# print ('ERROR: ListMessages failed' + e.get_dbus_message())
def list_messages():
global map
print (map)
print ('list messages')
try:
ret = map.ListMessages('telecom/msg/draft', dict())
print(pformat(unwrap(ret)))
except dbus.exceptions.DBusException as e:
print ('ERROR: ListMessages failed' + e.get_dbus_message())
#print ('list messages')
#try:
# ret = map.ListMessages('telecom', dict())
# print(pformat(unwrap(ret)))
#except dbus.exceptions.DBusException as e:
# print ('ERROR: ListMessages failed' + e.get_dbus_message())
#print ('push message')
#map.PushMessage('bmessage1', "telecom/msg/outbox", dict(), reply_handler=create_transfer_reply, error_handler=errorss)
if __name__ == '__main__':
print ('ds')
#bus = dbus.SessionBus()
mainloop = GObject.MainLoop()
app = App(layout="grid",bg="white",title="Bluetooth Player",width = 490,height = 300)
combo = Combo(app, grid = [2,1], options=[], command=bt_choose)
PushButton(app,grid = [0,1], command=discover_dev, text='discover')
btn_play = PushButton(app,grid = [1,1],command=push_message, text='send_sms')
btn_play = PushButton(app,grid = [3,1],command=list_folders, text='list folders')
btn_play = PushButton(app,grid = [4,1],command=list_messages, text='list messages')
tDBus = Thread(target=mainloop.run)
tDBus.start()