View Single Post
Posts: 277 | Thanked: 319 times | Joined on Jan 2010
#1611
Originally Posted by mr666acdc View Post
Can you make that config file to this script?
Sorry, haven't checked back here in a while. Here's a quick and dirty modification to it that will save your options to a 'scriptconfigfile.ini' and will automatically start with those options the next time you start it. If you want to choose again just remove or rename the config file from /home/user. If you need multiple instances running for multiple devices to follow, just rename the config file in the script for each instance (both places!).

Code:
#!/usr/bin/python
# Trigger Profilematic rule by BT connected/disconnected
# author: slarti

import dbus, gobject
from ConfigParser import SafeConfigParser

# Check for a configfile and create one if it doesn't exist
cfgfile = open('scriptconfigfile.ini', 'a+')


# Set up profilematic interface and get a list of rule names:
sesbus = dbus.SessionBus()
pm_obj = sesbus.get_object('org.ajalkane.profilematic', '/org/ajalkane/profilematic')
pm_intf = dbus.Interface(pm_obj, 'org.ajalkane.profilematic')
rulenames = pm_intf.getRuleNames()

# Set up some functions to use when handling the dbus event:
def menu(list_1, question):
        print (30 * '-')
        print ("Pick one from these:")
        for entry in list_1:
                print "  ",1 + list_1.index(entry),
                print ") " + entry
        print (30 * '-')

        return list_1[(int(raw_input(question)) - 1)]

def get_disconnected_rule():
        rule_list = ["No action"] + rulenames
        disconnected_rule = menu(rule_list, "Pick the rule whose actions you want to execute when device is disconnected: ")
        print "The actions of (%s) will be executed on device disconnect" %disconnected_rule

        return disconnected_rule

def get_connected_rule():
        rule_list = ["No action"] + rulenames
        connected_rule = menu(rule_list, "Pick the rule whose actions you want to execute when device is connected: ")
        print "The actions of (%s) will be executed on device connect" %connected_rule

        return connected_rule

def trigger_action(rulename):
        if rulename == 0:
                print "No action wanted."
        else:
                pm_intf.executeActionsByRuleName(rulename)
                print rulename, "executed."

# Set up loop to listen for dbus signal:
from dbus.mainloop.glib import DBusGMainLoop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)


# Get the BT adapter path and link device names to device paths in a dict:
bus = dbus.SystemBus()
bluez = bus.get_object('org.bluez', '/')
adapter_path = bluez.ListAdapters(dbus_interface='org.bluez.Manager')[0]
adapter = bus.get_object('org.bluez', adapter_path)
devices = adapter.ListDevices(dbus_interface='org.bluez.Adapter')
device_dict = {}

for device_path in devices:
        deviceprops = bus.get_object('org.bluez', device_path).GetProperties(dbus_interface='org.bluez.Device')
        device_dict.update({deviceprops ['Alias']:device_path})

# Get device, connect rule and disconnect rule from user:

settings = SafeConfigParser()
settings.read('scriptconfigfile.ini')
if settings.has_section('settings'):
        pass
else:
        settings.add_section('settings')

if settings.has_option('settings', 'device'):
        device = settings.get('settings', 'device')
else:
        device = menu(device_dict.keys(),"The name of the device to follow: ")
        settings.set('settings', 'device', device)

device_path = device_dict [device]
print "Now following the device: ",device, "in path: ",device_path

if settings.has_option('settings', 'connected_rule'):
        connected_rule = settings.get('settings', 'connected_rule')
else:
        connected_rule = get_connected_rule()

if connected_rule == "No action":
        device_connected = 0
else:
        device_connected = connected_rule
settings.set('settings', 'connected_rule', connected_rule)

if settings.has_option('settings', 'disconnected_rule'):
        disconnected_rule = settings.get('settings', 'disconnected_rule')
else:
        disconnected_rule = get_disconnected_rule()

if disconnected_rule == "No action":
        device_disconnected = 0
else:
        device_disconnected = disconnected_rule
settings.set('settings', 'disconnected_rule', disconnected_rule)
settings.write(cfgfile)
cfgfile.close()

print "Try connecting/disconnecting device %s now" %device

# Handle dbus signal:
def process_signal(property, message):
        if message == 0:
                print device, "disconnected. Triggering action."
                trigger_action(device_disconnected)
        else:
                print device, "connected. Triggering action."
                trigger_action(device_connected)

# Add signal receiver:
bus.add_signal_receiver(process_signal,path=device_path,dbus_interface='org.bluez.Device',signal_name='PropertyChanged')

loop = gobject.MainLoop()
loop.run()
 

The Following 3 Users Say Thank You to slarti For This Useful Post: