# Advanced Wled control
# Strip is controlled per LED

import sys
import time
import json
import math
import httplib

multi = 1700 # Multiplikator to make light brighter

def popen():
	spidev = file('/usr/aufruf.log', "wb")
	ip1 = "192.168.69.46" #WLED IP
	url = '/json/state'

	while True:
		color = []
		colorjson = []
		valid = 0
		i = 0
		eingabe = sys.stdin.readline()

		if len(eingabe)>0:

			data = eingabe.split(' ')	

			for temp in data:		  
			  color.append(temp)
			  i = i + 1

			  if i == 3:
			   valid = 1
			   red = float(color[0])
			   green = float(color[1])
			   blue = float(color[2])

			   red = red * multi
			   green = green * multi
			   blue = blue * multi

			   if red > 255:
			    red = 255

			   if green > 255:
			    green = 255
			
			   if blue > 255:
			    blue = 255

			   stringred = str(red)
			   stringgreen = str(green)
			   stringblue = str(blue)

			   colorjson.append('[' + stringred + ',' + stringgreen + ',' + stringblue + ']')
			   i = 0
			   color = []
	   
			if valid == 1:
			 jasondata = '{"seg":{"i":['
			 count = 0
			 for temp in colorjson:
			  if count > 0:
			   jasondata = jasondata + ','        
			  jasondata = jasondata + temp
			  count = count +1

			 jasondata = jasondata + ']}}'

			 try:
			  connection = httplib.HTTPConnection(ip1, timeout=10)
				
			  connection.request('PUT', url, json.dumps(jasondata))
			  response = connection.getresponse()
				
			  data = response.read()
				
			  connection.close()
			 except:
			  spidev.write("Exception cought")

		else:
			break

time.sleep(7)
popen()