#!/usr/bin/python
# -*- coding: utf-8 -*-
## Powered by Sergey Semenov sergey@vshome.net
import subprocess
import os
import sys
import time

c_button="cat /sys/class/gpio/gpio4/value"
c_alarm="cat /sys/class/gpio/gpio17/value"
c_status="cat /sys/class/gpio/gpio21/value"
system=""

while 1<2:
	f_button = subprocess.Popen(c_button, shell=True, stdout=subprocess.PIPE)
	res_button = f_button.stdout.readline()
	button = res_button[0:-1]
	f_alarm = subprocess.Popen(c_alarm, shell=True, stdout=subprocess.PIPE)
	res_alarm = f_alarm.stdout.readline()
	alarm = res_alarm[0:-1]
	f_status = subprocess.Popen(c_status, shell=True, stdout=subprocess.PIPE)
	res_status = f_status.stdout.readline()
	status = res_status[0:-1]
	a = subprocess.call("/home/sergey/testups", shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
	if alarm == "1":
		if status == "1":
			system = "alarmoff"
		elif status == "0":
			system = "alarm"
	elif alarm == "0":
		if status == "0":
			a = subprocess.call("killall light", shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
			a = subprocess.call("/home/sergey/light off", shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
			if button == "0":
				system="on"
		elif status == "1":
			if button == "0":
				i=1
				while i<5:
					time.sleep(2)
					f_button = subprocess.Popen(c_button, shell=True, stdout=subprocess.PIPE)
					res_button = f_button.stdout.readline()
					button = res_button[0:-1]
					if button == "0":
						i +=1
						if i == 5:
							system="off"
					elif button == "0":
						i=5
						system=""

	if system == "on":
		a = subprocess.Popen("/home/sergey/light power", shell=True, stdout=subprocess.PIPE)
		a = subprocess.call("/home/sergey/power on", shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
		a = subprocess.call("killall light", shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
		a = subprocess.call("/home/sergey/light on", shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
		system=""
	elif system == "off":
		a = subprocess.Popen("/home/sergey/light power", shell=True, stdout=subprocess.PIPE)
		a = subprocess.call("/home/sergey/power off", shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
		a = subprocess.call("killall light", shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
		a = subprocess.call("/home/sergey/light off", shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
		system=""
	elif system == "alarmoff":
		a = subprocess.Popen("/home/sergey/light alarm", shell=True, stdout=subprocess.PIPE)
		a = subprocess.call("/home/sergey/power off", shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
		a = subprocess.call("killall light", shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
		a = subprocess.call("/home/sergey/light off", shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
		system=""
	elif system == "alarm":
		a = subprocess.call("killall light", shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
		a = subprocess.Popen("/home/sergey/light alarm", shell=True, stdout=subprocess.PIPE)
		system=""





