#!/usr/bin/python
# -*- coding: UTF8 -*-

# -----------------------------------------------------------------------------
# Tux Droid - ping_phone script
# Copyright (C) 2007 Vincent Fretin <vincent.fretin@gmail.com>
#
# This program 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 2, or (at your option)
# any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
# -----------------------------------------------------------------------------

"""
Ping your phone regularly to detect your presence.
When you come back, Tux will ask you if you have had a good day.
If you leave the house, Tux will be sad too.

Requirements:
You need PyBluez, a phone with bluetooth enabled and bluetooth on your PC.

How to configure this script:
1) execute "sdptool browse" to collect information about your phone
2) modify the target_address variable with the address of your phone
3) modify CHECK_PROFILE with a profile available in your phone

CHANGES
=======
2007/04/19 - version 1.0:
    - initial version
"""

__author__ = "Vincent Fretin (vincent.fretin@gmail.com)"
__version__ = "1.0"
__date__ = "2007/04/19"
__copyright__ = "Copyright (C) 2007 Vincent Fretin"
__license__ = "GPL2+"


import sys
sys.path.append('/opt/tuxdroid/api/python')
from tux import *

import bluetooth

#target_name = "N70"
target_address = "00:18:0F:BD:91:C5"
CHECK_PROFILE = "Hands-Free Audio Gateway"

STATE_AWAY = 0
STATE_HERE = 1
seconds = 5

state = STATE_AWAY
tux.tts.select_voice_fr_male_tuxed()
tux.tts.on_sound_on=tux.cmd.mouth_open
tux.tts.on_sound_off=tux.cmd.mouth_close

while True:
    service_matches = bluetooth.find_service(address=target_address, name=CHECK_PROFILE)
    if len(service_matches) == 0:
        if state != STATE_AWAY:
            state = STATE_AWAY
            tux.tts.speak("Vinssant ? Tu es parti ?")
            seconds = 5
    else:
        if state != STATE_HERE:
            state = STATE_HERE
            tux.tts.speak("Salut Vinssant. T'as passé une bonne journée ?")
            seconds = 20
    tux.sys.wait(seconds)
