def startServer():
    
    global tv_listings_dict
    global tv_channels
    global tv_dict

    with open('helpers/lineup.json') as json_data:
        tv_json = json.load(json_data)
        for chan in tv_json:
            tv_channels.append(chan[0])
            tv_channels.append(chan[1])
            tv_listings_dict[chan[0]] = chan
            tv_listings_dict[chan[1]] = chan


    if 'wpvi' in tv_channels:
        tv_channels.append('abc')
        tv_listings_dict['abc'] = tv_listings_dict['wpvi']

    if 'wtxf' in tv_channels:
        tv_channels.append('fox')
        tv_listings_dict['fox'] = tv_listings_dict['wtxf']


    for tv in tvconfig.tvs:
        tv_dict[tv['tv_mac_address']] = tv
    
    clientid = prefHelper.deviceUUID()
    myMQTTClient = AWSIoTMQTTClient(clientid)
    myMQTTClient.configureEndpoint("afkx1f9takwol.iot.us-east-1.amazonaws.com", 8883)
    myMQTTClient.configureCredentials(".auth/root.pem", ".auth/private.pem.key", ".auth/certificate.pem.crt")
    myMQTTClient.configureOfflinePublishQueueing(-1)  # Infinite offline Publish queueing
    myMQTTClient.configureDrainingFrequency(2)  # Draining: 2 Hz
    myMQTTClient.configureConnectDisconnectTimeout(10)  # 10 sec
    myMQTTClient.configureMQTTOperationTimeout(5)  # 5 sec
    print('starting server...')
    myMQTTClient.connect()
    
    myMQTTClient.subscribe("power/" + clientid, 1, power)
    myMQTTClient.subscribe("channel/" + clientid, 1, channel)
    myMQTTClient.subscribe("speaker/" + clientid, 1, speaker)
    myMQTTClient.subscribe("playback/" + clientid, 1, playback)

    #myMQTTClient.unsubscribe("myTopic")
    #myMQTTClient.disconnect()
    print('server running. Pres CTRL + C to stop')

    counter = 0
    while True:
        time.sleep(1)
        if counter == 0:
            payload ={"uuid": prefHelper.deviceUUID()}
            headers = {'content-type': 'application/json', 'jwt': prefHelper.deviceToken()}
            try:
                response = requests.post('https://alexasmarttv.tk/api/v1/ping', data=json.dumps(payload), headers=headers)
            except:
                print('failed to ping')

        counter += 1
        counter = counter%900
if args[0] == 'register':
    if not prefHelper.loggedIn():
        print('Error: please log in before registering device.')
    else:
        print("Registering device...")
        tvs = []
        for tv in tvconfig.tvs:
            tvs.append({
                'name': tv['tv_name'],
                'mac_address': tv['tv_mac_address']
            })

        payload = {"name": tvconfig.device_name, "tvs": tvs}
        reregister = False
        if prefHelper.deviceRegistered():
            payload['uuid'] = prefHelper.deviceUUID()
            reregister = True

        headers = {
            'content-type': 'application/json',
            'jwt': prefHelper.deviceToken()
        }

        response = requests.post(url + '/api/v1/register_device',
                                 data=json.dumps(payload),
                                 headers=headers)
        json_data = json.loads(response.text)
        if 'error' in json_data:
            print(json_data['error']['message'])
        else:
            file = open('.auth/uuid', 'w')