Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Siglo D-Bus daemon #105

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions build-aux/meson/postinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
call(['glib-compile-schemas', path.join(datadir, 'glib-2.0', 'schemas')])



6 changes: 5 additions & 1 deletion com.github.alexr4535.siglo.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
"--share=network",
"--share=ipc",
"--socket=fallback-x11",
"--socket=session-bus",
"--system-talk-name=org.bluez",
"--socket=wayland"
"--socket=wayland",
"--socket=session-bus",
"--own-name=com.github.alexr4535.siglo",
"--own-name=com.github.alexr4535.siglo.Daemon"
],
"cleanup" : [
"/include",
Expand Down
3 changes: 3 additions & 0 deletions data/com.github.alexr4535.siglo.Daemon.service.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[D-BUS Service]
Name=com.github.alexr4535.siglo.Daemon
Exec=@bindir@/siglo --daemon
26 changes: 20 additions & 6 deletions data/meson.build
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
prefix = get_option('prefix')
datadir = get_option('datadir')
libexecdir = join_paths(prefix, get_option('libexecdir'))

desktop_file = i18n.merge_file(
input: 'com.github.alexr4535.siglo.desktop.in',
output: 'com.github.alexr4535.siglo.desktop',
type: 'desktop',
po_dir: '../po',
install: true,
install_dir: join_paths(get_option('datadir'), 'applications')
install_dir: join_paths(datadir, 'applications')
)

desktop_utils = find_program('desktop-file-validate', required: false)
Expand All @@ -15,17 +19,15 @@ if desktop_utils.found()
endif

install_data(join_paths('icons', 'com.github.alexr4535.siglo.svg'),
install_dir: join_paths(get_option('datadir'), 'icons')
install_dir: join_paths(datadir, 'icons')
)

install_data('siglo.service', install_dir: '/etc/systemd/user/')

appstream_file = i18n.merge_file(
input: 'com.github.alexr4535.siglo.appdata.xml.in',
output: 'com.github.alexr4535.siglo.appdata.xml',
po_dir: '../po',
install: true,
install_dir: join_paths(get_option('datadir'), 'metainfo')
install_dir: join_paths(datadir, 'metainfo')
)

appstream_util = find_program('appstream-util', required: false)
Expand All @@ -36,7 +38,19 @@ if appstream_util.found()
endif

install_data('com.github.alexr4535.siglo.gschema.xml',
install_dir: join_paths(get_option('datadir'), 'glib-2.0/schemas')
install_dir: join_paths(datadir, 'glib-2.0/schemas')
)

service_conf = configuration_data()
service_conf.set('appid', 'com.github.alexr4535.siglo.Daemon')
service_conf.set('bindir', join_paths(get_option('prefix'), get_option('bindir')))

configure_file(
input: 'com.github.alexr4535.siglo.Daemon.service.in',
output: 'com.github.alexr4535.siglo.Daemon.service',
configuration: service_conf,
install: true,
install_dir: join_paths(datadir,'dbus-1', 'services')
)

compile_schemas = find_program('glib-compile-schemas', required: false)
Expand Down
6 changes: 0 additions & 6 deletions data/siglo.service

This file was deleted.

2 changes: 1 addition & 1 deletion meson.build
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ subdir('data')
subdir('src')
subdir('po')

meson.add_install_script('build-aux/meson/postinstall.py')
meson.add_install_script('build-aux/meson/postinstall.py')
99 changes: 39 additions & 60 deletions src/bluetooth.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
import struct
from gi.repository import GObject, Gio
from .config import config
from .notification_service import NotificationService
from .music_service import MusicService

BTSVC_TIME = "00001805-0000-1000-8000-00805f9b34fb"
BTSVC_INFO = "0000180a-0000-1000-8000-00805f9b34fb"
BTSVC_BATT = "0000180f-0000-1000-8000-00805f9b34fb"
BTSVC_ALERT = "00001811-0000-1000-8000-00805f9b34fb"
BTSVC_MUSIC = "00000000-78fc-48fe-8e23-433b3a1942d0"
BTCHAR_FIRMWARE = "00002a26-0000-1000-8000-00805f9b34fb"
BTCHAR_CURRENTTIME = "00002a2b-0000-1000-8000-00805f9b34fb"
BTCHAR_NEWALERT = "00002a46-0000-1000-8000-00805f9b34fb"
BTCHAR_BATTLEVEL = "00002a19-0000-1000-8000-00805f9b34fb"


def get_current_time():
now = datetime.datetime.now()

Expand Down Expand Up @@ -57,15 +58,10 @@ def get_default_adapter():
class InfiniTimeManager(gatt.DeviceManager):
def __init__(self):
self.conf = config()
self.device_set = set()
self.device_set = []
self.aliases = dict()
if not self.conf.get_property("paired"):
self.scan_result = False
self.adapter_name = get_default_adapter()
self.conf.set_property("adapter", self.adapter_name)
else:
self.scan_result = True
self.adapter_name = self.conf.get_property("adapter")
self.daemon = None
self.adapter_name = get_default_adapter()
self.mac_address = None
super().__init__(self.adapter_name)

Expand All @@ -78,16 +74,13 @@ def get_device_set(self):
return self.device_set

def get_adapter_name(self):
if self.conf.get_property("paired"):
return self.conf.get_property("adapter")
return get_default_adapter()

def set_mac_address(self, mac_address):
self.mac_address = mac_address

def get_mac_address(self):
if self.conf.get_property("paired"):
self.mac_address = self.conf.get_property("last_paired_device")
self.mac_address = self.conf.get_property("last_paired_device")
return self.mac_address

def set_timeout(self, timeout):
Expand All @@ -98,20 +91,20 @@ def device_discovered(self, device):
if device.alias().startswith(prefix):
self.scan_result = True
self.aliases[device.mac_address] = device.alias()
self.device_set.add(device.mac_address)
self.device_set.append(device.mac_address)

def scan_for_infinitime(self):
def scan_for_infinitime(self,timeout):
self.start_discovery()
self.set_timeout(1.5 * 1000)
self.set_timeout(timeout * 1000)
self.run()


class InfiniTimeDevice(gatt.Device):
def __init__(self, mac_address, manager, thread):
def __init__(self, manager, mac_address):
self.conf = config()
self.mac = mac_address
self.manager = manager
self.thread = thread
self.is_connected = False
super().__init__(mac_address, manager)

def connect(self):
Expand All @@ -123,95 +116,81 @@ def connect_succeeded(self):
print("[%s] Connected" % (self.mac_address))
print("self.mac", self.mac)
self.conf.set_property("last_paired_device", self.mac)
self.is_connected = True

def connect_failed(self, error):
super().connect_failed(error)
self.successful_connection = False
print("[%s] Connection failed: %s" % (self.mac_address, str(error)))
self.daemon.Error("connect","connect failed")

def disconnect_succeeded(self):
super().disconnect_succeeded()
print("[%s] Disconnected" % (self.mac_address))
self.is_connected = False

def characteristic_write_value_succeeded(self, characteristic):
if not self.conf.get_property("paired"):
self.disconnect()
pass

def services_resolved(self):
super().services_resolved()
infosvc = None
timesvc = None
battsvc = None
alertsvc = None
for svc in self.services:
if svc.uuid == BTSVC_INFO:
infosvc = svc
self.infosvc = svc
elif svc.uuid == BTSVC_TIME:
timesvc = svc
self.timesvc = svc
elif svc.uuid == BTSVC_BATT:
battsvc = svc
self.battsvc = svc
elif svc.uuid == BTSVC_ALERT:
alertsvc = svc
self.alertsvc = svc
elif svc.uuid == BTSVC_MUSIC:
self.musicsvc = svc

if timesvc:
if self.timesvc:
currenttime = next(
c
for c in timesvc.characteristics
for c in self.timesvc.characteristics
if c.uuid == BTCHAR_CURRENTTIME
)

# Update watch time on connection
currenttime.write_value(get_current_time())

self.firmware = b"n/a"
if infosvc:
if self.infosvc:
info_firmware = next(
c
for c in infosvc.characteristics
for c in self.infosvc.characteristics
if c.uuid == BTCHAR_FIRMWARE
)

# Get device firmware
self.firmware = info_firmware.read_value()

if alertsvc:
self.new_alert = next(
c
for c in alertsvc.characteristics
if c.uuid == BTCHAR_NEWALERT
)
if self.alertsvc:
self.alertsvc_obj = NotificationService(self.alertsvc)

self.battery = -1
if battsvc:
if self.battsvc:
battery_level = next(
c
for c in battsvc.characteristics
for c in self.battsvc.characteristics
if c.uuid == BTCHAR_BATTLEVEL
)

# Get device firmware
self.battery = int(battery_level.read_value()[0])
if self.thread:
self.services_done()

def send_notification(self, alert_dict):
message = alert_dict["message"]
alert_category = "0" # simple alert
alert_number = "0" # 0-255
title = alert_dict["sender"]
msg = (
str.encode(alert_category)
+ str.encode(alert_number)
+ str.encode("\0")
+ str.encode(title)
+ str.encode("\0")
+ str.encode(message)
)

# arr = bytearray(message, "utf-8")
# self.new_alert_characteristic.write_value(arr)
self.new_alert.write_value(msg)
if self.musicsvc:
self.musicsvc_obj = MusicService(self.musicsvc)

self.manager.daemon.ServicesResolved()

def characteristic_value_updated(self, characteristic, value):
try:
self.musicsvc_obj.characteristic_value_updated(characteristic, value)
except:
pass

class BluetoothDisabled(Exception):
pass
Expand Down
Loading