# -*- coding: utf-8 -*-

import RPi.GPIO as GPIO
import serial
import requests
import os
kierunek_RS485 = 20
GPIO.setmode(GPIO.BCM)
GPIO.setup(kierunek_RS485, GPIO.OUT)
GPIO.setwarnings(False)

uart = serial.Serial(
  port='/dev/ttyAMA0',
  baudrate = 38400,
  parity=serial.PARITY_NONE,
  stopbits=serial.STOPBITS_ONE,
  bytesize=serial.EIGHTBITS,
  timeout=1
)

sensory = [['+','A',2],['+','B',3],['+','C',4],['+','D',5],['+','E',5],['+','F',5],['+','G',6],['+','H',7],['+','n',8],['+','o',9],['+','p',10],['+','q',11],['+','r',12],['+','s',68],['+','t',67],['*','S',13],['^','I',14],['^','J',14],['-','k',15],['-','l',16],['^','K',17],['^','L',17],['-','m',18],['+','u',69]]
GPIO.output(kierunek_RS485,GPIO.LOW)


try:
    while 1:
        GPIO.output(kierunek_RS485,GPIO.LOW)
        odebrane = uart.readline()
        print ("odebrane")
        print odebrane
        odebrane = odebrane.replace('\r\n','')
        dane = odebrane.split(" ")
        print ("dane: ")
        print dane
        for element in sensory:
            if element[1] == dane[0]:
                if element[0] == "+":       #TEMPERATURY - WARTOsCI
                    sendstring = 'http://192.168.1.131:8080/json.htm?type=command&param=udevice&nvalue=0&idx='
                    sendstring += str(element[2])   #IDX w domoticz
                    sendstring += '&svalue='
                    odebrane = odebrane.replace('^C','')
                    dane = odebrane.split(" ")
                    if dane [2] == "":  #jesli dane 2 sa puste to wtedy
                        sendstring += dane[3]   #dane z 3 pozycji (gdyz jest na 2 jest puste)
                    else:
                        sendstring += dane[2]   #dane z 2 pozycji (na poz 2 jest wartosc)
                    result = requests.get(sendstring)   #No i wyslij temp. do domoticz
                    if(result.status_code == 200): print("OK - WARTOSC ZAKTUALIZOWANA")

                if element[0] == "^":       #STATUS ZAWORÓW I BOJLERA - OPISOWE - PRZElaCZNIKI ON/OFF
                    sendstring = 'http://192.168.1.131:8080/json.htm?type=command&param=switchlight&idx='
                    sendstring += str(element[2])
                    sendstring += '&switchcmd='
                    if element[1] == "I":
                        sendstring += "Off"
                    elif element[1] == "J":
                        sendstring += "On"
                    elif element[1] == "K":
                        sendstring += "On"
                    elif element[1] == "L":
                        sendstring += "Off"
                    result = requests.get(sendstring)   #no i wyslij podlaczone do domoticz
                    if(result.status_code == 200): print("OK - WARTOSC ZAKTUALIZOWANA")

                if element[0] == "-":       #POZOSTA£E PRZELACZNIKI - k: pompkaCO, l: wle, m: pompkaPOD. 0-wy³¹czona, 1-w³¹czona
                    sendstring = 'http://192.168.1.131:8080/json.htm?type=command&param=switchlight&idx='
                    sendstring += str(element[2])
                    sendstring += '&switchcmd='
                    if dane[1] == "0":      #jesli odebrane dane to "0"
                        sendstring += "Off"     #znaczy, ze jest wylaczone
                    else:                       # a jesli "1"
                        sendstring += "On"      #to wlaczone
                    result = requests.get(sendstring)   #no i wyslij podlaczone do domoticz
                    if(result.status_code == 200): print("OK - WARTOSC ZAKTUALIZOWANA")

                if element[0] == "*":       #CZAS PRACY BOJLERA
                    odebrane = odebrane.replace('Dzien=','')
                    odebrane = odebrane.replace('Noc=','')
                    odebrane = odebrane.replace('min','')
                    dane = odebrane.split(" ")
                    sendstring = 'http://192.168.1.131:8080/json.htm?type=command&param=udevice&nvalue=0&idx='
                    sendstring += str(element[2])
                    sendstring += '&svalue='
                    if dane[2] == "0":      #jesli w dzien wartosc=0 to znaczy ze odczytujemy noc
                        sendstring += dane[8]
                    else:
                        sendstring += dane[2]
                    sendstring += ' min.'
                    result = requests.get(sendstring)   #no i wyslij podlaczone do domoticz
                    if(result.status_code == 200): print("OK - WARTOSC ZAKTUALIZOWANA")

    uart.close()

finally:
    GPIO.output(kierunek_RS485,GPIO.LOW)
    GPIO.cleanup();
    uart.close()