#!/usr/bin/env python

import time
import sys
import json
import types
import threading
import signal
import socket
import logging
import RPi.GPIO as GPIO
from messenger import Messenger
import config
import led
import dht
import stepper_motor
import magnet_sw

logging.basicConfig()

def turn_on_living_light(freq, dc):
    print('turn_on_living_light: %d, %d' % (freq, dc))
    led.turn_on(config.LED_LIVING, freq, dc)

def turn_off_living_light():
    print('turn_off_living_light')
    led.turn_off(config.LED_LIVING)

def turn_on_bedroom_light(freq, dc):
    print('turn_on_bedroom_light: %d, %d' % (freq, dc))
    led.turn_on(config.LED_BEDROOM, freq, dc)

def turn_off_bedroom_light():
    print('turn_off_bedroom_light')
    led.turn_off(config.LED_BEDROOM)

def turn_on_porch_light(freq, dc):
    print('turn_on_porch_light: %d, %d' % (freq, dc))
    led.turn_on(config.LED_PORCH, freq, dc)

def turn_off_porch_light():
    print('turn_off_porch_light')
    led.turn_off(config.LED_PORCH)

def open_front_door():
    print('open_front_door')
    stepper_motor.forward(256)

def close_front_door():
    print('close_front_door')
    stepper_motor.backward(256)

def message_callback(msg):
#    print('message_callback:')
#    print(msg)

    if not isinstance(msg, dict):
        return

    if msg['topic'] != config.ALIAS:
        return

    print('get a message:')
    try:
        m = json.loads(msg['msg'])
    except Exception as e:
        print('json.loads exception:')
        print(e)
        return

    print('act: %s' % m['act'])
    if m['act'] == 'turn_on_living_light':
        turn_on_living_light(m['freq'], m['dc'])
    elif m['act'] == 'turn_off_living_light':
        turn_off_living_light()
    elif m['act'] == 'turn_on_bedroom_light':
        turn_on_bedroom_light(m['freq'], m['dc'])
    elif m['act'] == 'turn_off_bedroom_light':
        turn_off_bedroom_light()
    elif m['act'] == 'turn_on_porch_light':
        turn_on_porch_light(m['freq'], m['dc'])
    elif m['act'] == 'turn_off_porch_light':
        turn_off_porch_light()
    elif m['act'] == 'open_front_door':
        open_front_door()
    elif m['act'] == 'close_front_door':
        close_front_door()

def report_ht(messenger):
    ht = dht.get_ht()
    m = {}
    m['act'] = 'report_ht'
    m['h'] = ht[0]
    m['t'] = ht[1]

    msg = json.dumps(m)
    messenger.publish(msg, 1)

def report_door(messenger):
    m = {}
    m['act'] = 'report_door'
    m['st'] = magnet_sw.get_sw_status()

    msg = json.dumps(m)
    messenger.publish(msg, 1)

def report_light(messenger):
    m = {}
    m['act'] = 'report_light'
    m['living'] = led.get_status(config.LED_LIVING)
    m['bedroom'] = led.get_status(config.LED_BEDROOM)
    m['porch'] = led.get_status(config.LED_PORCH)

    msg = json.dumps(m)
    messenger.publish(msg, 1)

def sig_handler(sig, frame):
    print 'receive a signal:', sig
    sys.exit()

def is_network_ok():
  try:
    host = socket.gethostbyname('www.baidu.com')
    s = socket.create_connection((host, 80), 2)
    return True
  except:
     pass
  return False

def main():
    signal.signal(signal.SIGTERM, sig_handler)
    #signal.signal(signal.SIGINT, sig_handler)

    led.turn_on(config.LED_LIVING, 1, 50) #checking network
    led.turn_on(config.LED_BEDROOM, 1, 100)
    led.turn_on(config.LED_PORCH, 1, 100)

    while True:
        if is_network_ok():
            break
        time.sleep(2)
    led.turn_on(config.LED_LIVING, 4, 50) #network is ok, connecting socktio

    messenger = Messenger(message_callback)

    while True: 
        report_ht(messenger)
        time.sleep(0.5)
        report_door(messenger)
        time.sleep(0.5)
        report_light(messenger)
        time.sleep(2)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        GPIO.cleanup()
    except Exception as e:
        GPIO.cleanup()
        print(e)

@@ -253,16 +253,42 @@
 agnet_sw
+%0Afrom player import Player
 %0A%0Aloggin
@@ -302,16 +302,34 @@
 Config()
+%0Aplayer = Player()
 %0A%0Adef tu
@@ -1203,24 +1203,285 @@
 kward(256)%0A%0A
+def media_play(path):%0A    print('media_play')%0A    player.play(path)%0A%0Adef media_stop():%0A    print('media_stop')%0A    player.stop()%0A%0Adef media_pause():%0A    print('media_pause')%0A    player.pause()%0A%0Adef media_resume():%0A    print('media_resume')%0A    player.resume()%0A%0A
 def message_
@@ -2463,32 +2463,32 @@
 se_front_door':%0A
-
         close_fr
@@ -2497,16 +2497,255 @@
 t_door()
+%0A    elif m%5B'act'%5D == 'media_play':%0A        media_play(m%5B'path'%5D)%0A    elif m%5B'act'%5D == 'media_stop':%0A        media_stop()%0A    elif m%5B'act'%5D == 'media_pause':%0A        media_pause()%0A    elif m%5B'act'%5D == 'media_resume':%0A        media_resume()
 %0A%0Adef re

Add media play messges