try:
    while dictionary.isAttachedToServer() == False:
        pass
    else:
        print("Connected: %s" % (dictionary.isAttachedToServer()))
        print("Server: %s:%s" % (dictionary.getServerAddress(), dictionary.getServerPort()))
except PhidgetException as e:
    print("Phidget Exception %i: %s" % (e.code, e.details))
    print("Exiting....")
    exit(1)

try:
    print("Now we'll add some keys...")
    sleep(1)
    
    dictionary.addKey("test1", "ok")
    dictionary.addKey("test2", "ok", True)
    dictionary.addKey("test3", "ok", False)
    dictionary.addKey("test4", "ok", True)
    sleep(2)
    
    print("Now we will test for key 'test2' being in the dictionary.")
    sleep(1)
    
    value = dictionary.getKey("test2")
    print("Key: test2  Value: %s" % (value))
    
    print("Now we will remove one of the keys...")
    sleep(1)
    
    dictionary.removeKey("test4")
        pass
    else:
        print("Connected: %s" % (dictionary.isAttachedToServer()))
        print("Server: %s:%s" %
              (dictionary.getServerAddress(), dictionary.getServerPort()))
except PhidgetException as e:
    print("Phidget Exception %i: %s" % (e.code, e.details))
    print("Exiting....")
    exit(1)
# Set revolving values
try:
    print("Now we'll add some keys...")
    sleep(1)

    ai1, ai2, ai3, dev_state = 0, 0, 0, True
    dictionary.addKey("ai1", str(ai1))
    dictionary.addKey("ai2", str(ai2))
    dictionary.addKey("ai3", str(ai3))
    dictionary.addKey("dev_state", str(dev_state))
    print("got here")

    # Update Values
    while True:
        dice = random.random()
        if dice < 1.0:
            ai1 = random.randint(-100, 0)
            dictionary.addKey("ai1", str(ai1))
        if dice < 0.6:
            ai2 = random.randint(0, 50)
            dictionary.addKey("ai2", str(ai2))
        if dice < 0.2:
        print("Server: %s:%s" % (dictionary.getServerAddress(), dictionary.getServerPort()))
except PhidgetException as e:
    print("Phidget Exception %i: %s" % (e.code, e.details))
    print("Exiting....")
    exit(1)


#############
# Main Loop #
#############
ORDERS = ['v360_on', 'v360_off', 'atv_on', 'atv_off', 'fc_on', 'fc_off',
          'rr_on', 'rr_off', 'wifi_on', 'wifi_off']

for order in ORDERS:
    cdict[order] = 'INITIALIZED'
    dictionary.addKey(order, 'INITIALIZED')

cdict['LATCH'] = 'INITIALIZED'
cdict['STATUS'] = 'INITIALIZED'

while(True):
    sleep(1)
    #~ print cdict.keys()
    #~ print '--------'
    #~ print cdict.values()

    for command in ORDERS:
        if cdict[command] == 'PLEASE':
            if cdict['LATCH'] == 'SET':
                try:
                    shell_command = command
    while dictionary.isAttachedToServer() == False:
        pass
    else:
        print("Connected: %s" % (dictionary.isAttachedToServer()))
        print("Server: %s:%s" %
              (dictionary.getServerAddress(), dictionary.getServerPort()))
except PhidgetException as e:
    print("Phidget Exception %i: %s" % (e.code, e.details))
    print("Exiting....")
    exit(1)

try:
    print("Now we'll add some keys...")
    sleep(1)

    dictionary.addKey("test1", "ok")
    dictionary.addKey("test2", "ok", True)
    dictionary.addKey("test3", "ok", False)
    dictionary.addKey("test4", "ok", True)
    sleep(2)

    print("Now we will test for key 'test2' being in the dictionary.")
    sleep(1)

    value = dictionary.getKey("test2")
    print("Key: test2  Value: %s" % (value))

    print("Now we will remove one of the keys...")
    sleep(1)

    dictionary.removeKey("test4")
    while dictionary.isAttachedToServer() == False:
        pass
    else:
        print("Connected: %s" % (dictionary.isAttachedToServer()))
        print("Server: %s:%s" % (dictionary.getServerAddress(), dictionary.getServerPort()))
except PhidgetException as e:
    print("Phidget Exception %i: %s" % (e.code, e.details))
    print("Exiting....")
    exit(1)
# Set revolving values
try:
    print("Now we'll add some keys...")
    sleep(1)
    
    ai1, ai2, ai3, dev_state = 0, 0, 0, True
    dictionary.addKey("ai1", str(ai1))
    dictionary.addKey("ai2", str(ai2))
    dictionary.addKey("ai3", str(ai3))
    dictionary.addKey("dev_state", str(dev_state))
    print("got here")
    
    # Update Values
    while True:
        dice = random.random()
        if dice < 1.0:
            ai1 = random.randint(-100, 0)
            dictionary.addKey("ai1", str(ai1))
        if dice < 0.6:
            ai2 = random.randint(0, 50)
            dictionary.addKey("ai2", str(ai2))
        if dice < 0.2:
        print("Connected: %s" % (dictionary.isAttachedToServer()))
        print("Server: %s:%s" % (dictionary.getServerAddress(), dictionary.getServerPort()))
except PhidgetException as e:
    print("Phidget Exception %i: %s" % (e.code, e.details))
    print("Exiting....")
    exit(1)

##################################################################


### TESTING KEYS
try:
    print("Now we'll add some keys...")
    sleep(1)

    dictionary.addKey("LTC_ON", "None")
    dictionary.addKey("Command Check", "None")
    dictionary.addKey("Command", "None")
    sleep(2)
    dictionary.addKey("Command", "Nope")

    print("Now we will test the LTC_ON Command.")
    sleep(1)
    dictionary.addKey("LTC_ON", "ff")
except PhidgetException as e:
    print("Phidget Exception %i: %s" % (e.code, e.details))
    print("Exiting....")
    exit(1)

print("Press Enter to quit....")
except PhidgetException as e:
    print("Phidget Exception %i: %s" % (e.code, e.details))
    print("Exiting....")
    exit(1)

#############
# Main Loop #
#############
ORDERS = [
    'v360_on', 'v360_off', 'atv_on', 'atv_off', 'fc_on', 'fc_off', 'rr_on',
    'rr_off', 'wifi_on', 'wifi_off'
]

for order in ORDERS:
    cdict[order] = 'INITIALIZED'
    dictionary.addKey(order, 'INITIALIZED')

cdict['LATCH'] = 'INITIALIZED'
cdict['STATUS'] = 'INITIALIZED'

while (True):
    sleep(1)
    #~ print cdict.keys()
    #~ print '--------'
    #~ print cdict.values()

    for command in ORDERS:
        if cdict[command] == 'PLEASE':
            if cdict['LATCH'] == 'SET':
                try:
                    shell_command = command
        print("Connected: %s" % (dictionary.isAttachedToServer()))
        print("Server: %s:%s" %
              (dictionary.getServerAddress(), dictionary.getServerPort()))
except PhidgetException as e:
    print("Phidget Exception %i: %s" % (e.code, e.details))
    print("Exiting....")
    exit(1)

##################################################################

### TESTING KEYS
try:
    print("Now we'll add some keys...")
    sleep(1)

    dictionary.addKey("LTC_ON", "None")
    dictionary.addKey("Command Check", "None")
    dictionary.addKey("Command", "None")
    sleep(2)
    dictionary.addKey("Command", "Nope")

    print("Now we will test the LTC_ON Command.")
    sleep(1)
    dictionary.addKey("LTC_ON", "ff")
except PhidgetException as e:
    print("Phidget Exception %i: %s" % (e.code, e.details))
    print("Exiting....")
    exit(1)

print("Press Enter to quit....")