Exemplo n.º 1
0
def set_color():
    phone_number = request.values.get('From', None)
    color_name = request.values.get('Body', None)
    color_name = clean_name(color_name)

    if color_name == "black":
        response = MessagingResponse()
        response.message("Haha... please use a color that contains light.")
        return str(response)

    if color_name == "previous":
        prev_color = PreviousColorsRedis().getPreviousColor()
        response = MessagingResponse()
        response.message("The previous color was {}".format(clean_name(prev_color)))
        return str(response)

    if color_name == "colors":
        colors_link = 'http://localhost:5000/colors'  # the link of the end point for '/colors'
        response = MessagingResponse()
        response.message("{}".format(colors_link))
        return str(response)

    message = controller.set_color(color_name)
    percent = colorPercent(file,color_name)
    date = firstEntryDate(file)
    response = MessagingResponse()
    response.message(message + " This entry has been chosen " + str(percent) + "% of the time since " + date + "!")
    logging.info("Color " + color_name + " has been set by the phone number " + phone_number + ".")
    writeFile(file,str(phone_number), str(color_name), str(message))
    if colorsRedis().is_color(color_name):
        PreviousColorsRedis().updatePreviousColor(color_name)

    return str(response)
Exemplo n.º 2
0
 def testNumberOfInvalidColors(self):
     fileCreator.createTestFile(self)
     writeFile(filename, "856-938-4220", "foo", foo_message)
     writeFile(filename, "856-938-4220", "foo", foo_message)
     invalidList = invalidColors(filename)
     self.assertEquals(invalidList[0], "foo")
     self.assertEqual(len(invalidList), 2)
     os.remove(filePath)
Exemplo n.º 3
0
def go():
    test = "."
    while (test != ""):
        test = input("Please enter a color: ")
        color_name = clean_name(test)
        print(test)
        if color_name == "black":
            print("No black")
        else:
            percent = colorPercent(file, color_name)
            message = controller.set_color(color_name)
            date = firstEntryDate(file)
            writeFile(file, "856-938-4220", str(color_name), str(message))
            print(message + " This entry has been chosen " + str(percent) +
                  "% of the time since " + date + "!")
Exemplo n.º 4
0
def set_color():
    phone_number = request.values.get('From', None)
    color_name = request.values.get('Body', None)
    color_name = clean_name(color_name)

    if color_name == "black":
        response = MessagingResponse()
        response.message("Haha... please use a color that contains light.")
        return str(response)

    message = controller.set_color(color_name)
    percent = colorPercent(file, color_name)
    date = firstEntryDate(file)
    response = MessagingResponse()
    response.message(message + " This entry has been chosen " + str(percent) +
                     "% of the time since " + date + "!")
    logging.info("Color " + color_name + " has been set by the phone number " +
                 phone_number + ".")
    writeFile(file, str(phone_number), str(color_name), str(message))

    return str(response)
Exemplo n.º 5
0
 def testListMostRecentSmallFile(self):
     writeFile(filename, "856-938-4220", "red", red_message)
     writeFile(filename, "856-938-4220", "blue", blue_message)
     writeFile(filename, "856-938-4220", "green", green_message)
     expectedColors = ["red", "blue", "green"]
     self.assertListEqual(expectedColors, mostRecentColors(filename))
     os.remove(filePath)
Exemplo n.º 6
0
def set_color():
    is_random = False
    is_Fuzzy = False
    is_Hex = False
    database = redis.Redis(host='localhost', port=6379, db=0)

    list_of_colors = get_colors_list_from_redis(database)

    phone_number = request.values.get('From', None)
    unclean_color_name = request.values.get('Body', None)
    if unclean_color_name.startswith("#"):
        is_Hex = True
    color_name = clean_name(unclean_color_name)

    if color_name == "black":
        response = MessagingResponse()
        response.message("Haha... please use a color that contains light.")
        return str(response)

    if color_name == "options":
        response = MessagingResponse()
        response.message("\n***Options***\n-------------------------------\n"
                         + "'Options' - list all options for Philips Light functions\n" +
                         "'Colors List' - link to list of color choices\n"
                         "'Random' - chooses a random color for the light")
        return str(response)
    if color_name == "colors list":
        response = MessagingResponse()
        response.message(
            "List of color choices:" + "https://en.wikipedia.org/wiki/List_of_Crayola_crayon_colors"
        )
        return str(response)

    try:
        controller.connect()
    except PhueException:
        logging.info("Server unable to connect to the Hue Light")
        response = MessagingResponse()
        response.message("Server unable to connect to the Hue Light")
        return str(response)

    if color_name == "random":
        is_random = True
        color_sum = int(database.get('color_sum').decode('utf-8'), base=10)
        random_int = random.randint(1, color_sum)
        color_name = list_of_colors[random_int]
        database.hincrby('color_totals', 'random', 1)
        database.incr('total', 1)
    else:
        if color_name not in list_of_colors:
            # Checking to see if color is not in list_of_colors due to possible misspelling
            fuzzy_Color = getFuzzyColor(color_name)
            if fuzzy_Color is not None:
                is_Fuzzy = True
                color_name = clean_name(fuzzy_Color)

        if color_name in list_of_colors:
            database.hincrby('color_totals', color_name, 1)
            database.incr('total', 1)

    if is_Hex:
        rgb_values = HEX_to_RGB(unclean_color_name)

    else:
        rgb_values = getColor(color_name)

    if rgb_values is None:
        logging.info("Color " + color_name + " was not recognized")
        response = MessagingResponse()
        response.message("I'm sorry, but I don't recognize the color \"{}\".".format(color_name))
        return str(response)

    [x, y, saturation_val] = convert(rgb_values)

    try:
        controller.light.xy = (x, y)
        controller.light.saturation = saturation_val
        logging.info("The light was changed to the color " + color_name)
        if is_random:
            message = "The light was changed to the color \"{}\". Random was used." \
                .format(clean_name(color_name))
        elif is_Fuzzy:
            message = "We found a color similar to what you requested... The light was changed to the color \"{}\"".format(
                clean_name(color_name))
        elif is_Hex:
            message = "You requested a Hex Color... The light was changed to the Hex \"{}\"".format(
                unclean_color_name)
        else:
            message = "The light was changed to the color \"{}\"." \
                .format(clean_name(color_name))
    except PhueException:
        logging.info("Server unable to connect to the Hue Light")
        response = MessagingResponse()
        response.message("I'm sorry, but I cannot connect to the Hue Light. Please try again later.")
        return str(response)

    if is_random:
        color_name = 'random'
    if is_Hex:
        response = MessagingResponse()
        response.message(message)
        return str(response)

    percent = color_percent(color_name)
    writeFile(file, str(phone_number), str(color_name), str(message))
    date = first_entry_date(file)
    response = MessagingResponse()
    response.message(
        message + " This entry has been chosen {:.1f}".format(percent) + "% of the time since " + date + "!")
    logging.info("Color " + color_name + " has been set by the phone number " + phone_number + ".")

    return str(response)
Exemplo n.º 7
0
 def createTestFile(self):
     writeFile(filename, "856-938-4220", "red", red_message)
     writeFile(filename, "856-938-4220", "blue", blue_message)
     writeFile(filename, "856-938-4220", "green", green_message)
     writeFile(filename, "856-938-4220", "yellow", yellow_message)
     writeFile(filename, "856-938-4220", "blue", blue_message)
     writeFile(filename, "856-938-4220", "red", red_message)
     writeFile(filename, "856-938-4220", "white", white_message)
     writeFile(filename, "856-938-4220", "blue", blue_message)
     writeFile(filename, "856-938-4220", "red", red_message)
     writeFile(filename, "856-938-4220", "blue", blue_message)
     writeFile(filename, "856-938-4220", "green", green_message)
     writeFile(filename, "856-938-4220", "yellow", yellow_message)
     writeFile(filename, "856-938-4220", "blue", blue_message)
     writeFile(filename, "856-938-4220", "red", red_message)
     writeFile(filename, "856-938-4220", "white", white_message)
     writeFile(filename, "856-938-4220", "blue", blue_message)