#!/usr/bin/python3
import socket
host = "192.168.1.249" # socket.gethostname()
port = 9001                   # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.sendall(b'Connected to server!')
import sort
import control_motor
import control_pixel
control_pixel.ledOn()
while True:
    control_pixel.ledOn()
    try:
        data = s.recv(1024)
        #control_pixel.ledOn()
        if not data: break
        if str(data)[2:8] == "Pause ":
            control_motor.stopInput()
        elif str(data)[2:8] == "Intake":
            control_motor.startInput()
        elif str(data)[2:8] == "Object":
            print("[ INFO ] Item name: "+ str(data)[2:len(str(data))-1])
            selectedBin = sort.sort(str(data)[2:len(str(data))-1])
            control_pixel.ledOff()
            control_motor.goToBin(selectedBin)
            control_pixel.ledOn()
            s.sendall(b'Continue')
    except socket.error:
        print("[ EXIT ] Socket connection error.")
        break
s.close()
