diff --git a/desktop_software/ubuntu/desk_attendant.py b/desktop_software/ubuntu/desk_attendant.py new file mode 100644 index 0000000..83fa2e8 --- /dev/null +++ b/desktop_software/ubuntu/desk_attendant.py @@ -0,0 +1,148 @@ +# Copyright 2012,2014,2015 Robert W Igo +# +# bob@igo.name +# http://bob.igo.name +# http://www.linkedin.com/profile/view?id=22294307 +# +# This file is part of DeskAttendant. +# +# DeskAttendant is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# DeskAttendant is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with DeskAttendant. If not, see . + +import sys, os, datetime +import subprocess +from subprocess import call + +# NOTE: These must all match the Arduino sketch. +unknown_status=0 +walker_absent=1 +walker_present=2 +last_status=unknown_status + +# +# Check arguments. +# +port = "none" + +def print_usage(): + print("USAGE: See desk_attendant.sh") + sys.exit + +if len(sys.argv) < 1: + print('missing parameter') + print_usage() +else: + port = sys.argv[1] + +# +# get a human-readable name for the numeric state code +# +def get_name_for_state(x): + return { + 1: 'pause', + 2: 'unpause', + }[x] + +# +# put these apps into the right mode +# +def take_action(mode): + # First, make sure the desktop is unlocked by looking for the "power" icon. + + app2 = [] + for app in ["amarok", "pithos", "hamster"]: + cmd1 = "pgrep " + app + p = subprocess.Popen(cmd1, shell=True, executable="/bin/bash", stdout=subprocess.PIPE) + out,err = p.communicate() + if out != "": + #print app + " is running" + app2.append(app) + + for app in app2: + print "putting " + app + " into " + mode + " mode" + # put 'app' in state 'mode' + if app == "amarok": + cmd1 = "pgrep amarok" + p = subprocess.Popen(cmd1, shell=True, executable="/bin/bash", stdout=subprocess.PIPE) + out,err = p.communicate() + if out != "": + if mode == "pause": + cmd = "amarok --pause" + else: + cmd = "amarok --play" + elif app == "hamster": + if mode == "pause": + cmd = "hamster-cli stop" + + cmd2 = "notify-send \"pausing tasks\"" + p = subprocess.Popen(cmd2, shell=True, executable="/bin/bash", stdout=subprocess.PIPE) + out,err = p.communicate() + + else: + # get the last-used task + cmd1 = "hamster-cli list | tail -1 | awk -F '|' '{print $2}' | awk -F '@' '{print $1}'" + p = subprocess.Popen(cmd1, shell=True, executable="/bin/bash", stdout=subprocess.PIPE) + out,err = p.communicate() + task = out.strip() + + cmd2 = "notify-send \"resuming task: " + task + "\"" + p = subprocess.Popen(cmd2, shell=True, executable="/bin/bash", stdout=subprocess.PIPE) + out,err = p.communicate() + + # start the last-used task + cmd = "hamster-cli start " + task + subprocess.Popen(cmd, shell=True, executable="/bin/bash") + + elif app == "pithos": + # figure out what pithos is doing + cmd1 = "dbus-send --print-reply --dest=net.kevinmehall.Pithos /net/kevinmehall/Pithos net.kevinmehall.Pithos.IsPlaying | grep boolean | sed 's/boolean//'" + p = subprocess.Popen(cmd1, shell=True, executable="/bin/bash", stdout=subprocess.PIPE) + out,err = p.communicate() + playing = out.strip() + if (playing == "true" and mode == "pause") or (playing == "false" and mode == "unpause"): + cmd = "dbus-send --print-reply --dest=net.kevinmehall.Pithos /net/kevinmehall/Pithos net.kevinmehall.Pithos.PlayPause" + + #print " INVOKING: " + cmd + # set the state + p = subprocess.Popen(cmd, shell=True, executable="/bin/bash", stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out,err = p.communicate() + + print "----" + +# +# Open 'port'. +# +try: + ser=os.open(port, os.O_RDONLY) +except: + print("unable to read from serial port " + port) + +print("Desk Attendant is ready on serial port " + port) + +# +# Read from 'port' as long as it exists. +# +while os.path.exists(port): + for line in os.read(ser,1): + try: + this_status = int(line) + except ValueError: + pass # sometimes, crap comes through on the serial port + if this_status != last_status: + this_status_name=get_name_for_state(this_status) + print(unicode(datetime.datetime.now()) + ": " + this_status_name) + if this_status != unknown_status: + take_action(this_status_name) + last_status=this_status + +print("exited") diff --git a/desktop_software/ubuntu/desk_attendant.sh b/desktop_software/ubuntu/desk_attendant.sh index 46b5e67..5cb73f5 100755 --- a/desktop_software/ubuntu/desk_attendant.sh +++ b/desktop_software/ubuntu/desk_attendant.sh @@ -56,4 +56,5 @@ stty -F $port raw ispeed $baud ospeed $baud time 3 min 1 -hupcl # NOTE: The paths used (or omitted) here must be compatible with your Sikuli installation and # with the location where you installed the deskattendant.sikuli directory. -sikuli-ide -r /repos/DeskAttendant/desktop_software/sikuli/deskattendant.sikuli -args $port +#sikuli-ide -r /repos/DeskAttendant/desktop_software/sikuli/deskattendant.sikuli -args $port +python /repos/DeskAttendant/desktop_software/ubuntu/desk_attendant.py $port