#!/usr/bin/env python
# GPL. (C) 2014 Paolo Patruno.

# 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 of the License, 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 
# 

from rmap import daemon
import dbus
import dbus.mainloop.glib
import gobject

#busaddress='tcp:host=192.168.1.180,port=1234'
busaddress=None

poweroffd = daemon.Daemon(
        stdin="/dev/null",
        stdout="/tmp/poweroffd.log",
        stderr="/tmp/poweroffd.err",
        pidfile="/tmp/poweroffd.lock",
#        user=autoradio.settings.user,
#        group=autoradio.settings.group
)



def poweroff():

    bus = dbus.SystemBus()
    ck = bus.get_object('org.freedesktop.ConsoleKit',
                        '/org/freedesktop/ConsoleKit/Manager')
    
    p = dbus.Interface(ck,dbus_interface='org.freedesktop.ConsoleKit.Manager')
    p.Stop()

def signal_handler(interface,properties,arg0):
    print "signal received"
    print "interface=",interface
    for prop in properties.keys():
        print prop, properties[prop]
        if prop == "Status" and properties[prop] == 0 :
            poweroff()
    #print arg0


def main(self):

    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

    if busaddress is None:
        bus = dbus.SystemBus()
    else:
        bus = dbus.bus.BusConnection(busaddress)

    proxy = bus.get_object('org.gpio.myboard.Pins','/org/gpio/myboard')
    properties_manager = dbus.Interface(proxy, 'org.freedesktop.DBus.Properties')
    board_manager= dbus.Interface(proxy, 'org.gpio.myboard')

    if not properties_manager.Get('org.gpio.myboard', 'Running'):
        # start management of board pins
        board_manager.Raise('org.gpio.myboard')

    #pin 18 in
    properties_manager.Set('org.gpio.myboard.pins.channel18','Pull',"up")
    properties_manager.Set('org.gpio.myboard.pins.channel18','Mode',"in")
    print "18 status=", properties_manager.Get('org.gpio.myboard.pins.channel18','Status')

    bus.add_signal_receiver(signal_handler, dbus_interface = dbus.PROPERTIES_IFACE, signal_name = "PropertiesChanged")

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

    except KeyboardInterrupt :
        # exit the management daemon
        board_manager.Quit('org.gpio.myboard')


if __name__ == '__main__':

    import sys, os
    poweroffd.cwd=os.getcwd()

    if poweroffd.service():

        sys.stdout.write("Daemon started with pid %d\n" % os.getpid())
        sys.stdout.write("Daemon stdout output\n")
        sys.stderr.write("Daemon stderr output\n")

        main(poweroffd)  # (this code was run as script)

        for proc in poweroffd.procs:
            proc.wait()

        sys.exit(0)
