#!/usr/bin/python

# This is based off the Zeo Raw Data Library at the bottom of
# http://zeorawdata.sourceforge.net/starting.html
# This script is a much simpler version of how to log basic data than the cool
# and pretty GUI_Viewer from http://zeorawdata.sourceforge.net/examples.html
# This script is an easier example to steal from for simple logging/integration.

# By Marc MERLIN <marc_soft@merlins.org> / 2011/12/17
# License: GPLv3.

#gandalfthegrey [mc]$ ./logsleep.py
# 2011-12-17 12:19:10: HeadbandDocked
# 2011-12-17 12:19:15: HeadbandUnDocked
# 2011-12-17 12:19:22: Sleep state: Undefined
# 2011-12-17 12:19:52: Sleep state: Undefined
# 2011-12-17 12:20:22: Sleep state: Undefined
# 2011-12-17 12:20:52: Sleep state: Undefined
# 2011-12-17 12:21:22: Sleep state: Undefined
# 2011-12-17 12:21:52: Sleep state: Undefined
# 2011-12-17 12:22:22: Sleep state: Undefined
# 2011-12-17 12:22:52: Sleep state: Undefined
# 2011-12-17 12:23:22: Sleep state: Undefined
# 2011-12-17 12:23:52: NightStart
# 2011-12-17 12:23:52: Sleep state: Awake
# 2011-12-17 12:24:22: Sleep state: Awake
# 2011-12-17 12:24:52: Sleep state: Awake
# 2011-12-17 12:25:22: Sleep state: Awake
# 2011-12-17 12:25:52: Sleep state: Awake
# 2011-12-17 12:26:23: Sleep state: Awake
# 2011-12-17 12:26:32: HeadbandDocked

# System Libraries
import time
import sys

# Zeo Libraries
from ZeoRawData import BaseLink, Parser
from ZeoRawData.Utility import *

# User customizable variables
port = '/dev/ttyUSB0'


class Callbacks:
    def logtime(self):
	return time.strftime("%Y-%m-%d %H:%M:%S: ", time.localtime())

    def SliceCallback(self, slice):
        if slice['SleepStage']:
	    print self.logtime() + "Sleep state: " + slice['SleepStage']

    def EventCallback(self, logtime, version, event):
	print self.logtime() + event

if __name__ == "__main__":
    link = BaseLink.BaseLink(port)
    parser = Parser.Parser()

    callbacks = Callbacks()

    # Add Callbacks and Start the Link
    link.addCallback(parser.update)
    parser.addSliceCallback(callbacks.SliceCallback)
    parser.addEventCallback(callbacks.EventCallback)
    link.start()

    # Infinitely loop to allow the script to run continuously
    while(True):
        time.sleep(5)


# vim:sts=4:sw=4
