def get_speed(self):
        speed_test = speedtest.Speedtest()

        speed_test.get_servers(self.__config['isp']['servers'])
        speed_test.get_best_server()
        speed_test.download(threads=self.__config['isp']['threads'])
        speed_test.upload(threads=self.__config['isp']['threads'], pre_allocate=False)
        result = speed_test.results.dict()

        return {
            'download_speed_mb': "{0:.2f} Mb/s ".format(hr.BitPerSecond("{} bps".format(result['download'])).mega_bps),
            'upload_speed_mb': "{0:.2f} Mb/s".format(hr.BitPerSecond("{} bps".format(result['upload'])).mega_bps),
            "ping_ms": "{0:.2f} ms".format(result['ping'])
        }
Beispiel #2
0
    async def update_dl_progress(chat_id, message_id, d):
        global cl
        global progress_status_time

        if time.time() - int(progress_status_time[f"{chat_id}+{message_id}"]
                             ["last_updated"]) > 3:
            dl_speed_raw = d['speed'] if d['speed'] else 0
            dl_speed_converted_raw = hr.BitPerSecond(
                str(dl_speed_raw),
                default_unit=hr.BitPerSecond.Unit.BPS).mega_bps
            dl_speed_split = str(dl_speed_converted_raw).split(".")[0]
            try:
                await cl.edit_message_text(
                    chat_id=int(chat_id),
                    message_id=int(message_id),
                    text=f"<code>Progress: Downloading \n"
                    f"Downloaded: {humanfriendly.format_size(int(d['downloaded_bytes']), binary=True)} of "
                    f"{humanfriendly.format_size(int(d['total_bytes']), binary=True)} \n"
                    f"Speed: {dl_speed_split} MiBps</code>")
                job_update_timing = {
                    f"{chat_id}+{message_id}": {
                        "last_updated": time.time()
                    }
                }
                progress_status_time.update(job_update_timing)
            except MessageNotModified as e:
                logging.error(str(e))
            except FloodWait as e:
                logging.error(str(e))
                await asyncio.sleep(e.x)
Beispiel #3
0
    async def upload_progress_update(upload, total, chat_id, message_id,
                                     start_time):
        global cl
        global progress_status_time

        diff = time.time() - start_time if time.time() == start_time else 1
        up_speed_converted_raw = hr.BitPerSecond(
            str(upload / diff), default_unit=hr.BitPerSecond.Unit.BPS).mega_bps
        up_speed_split = str(up_speed_converted_raw).split(".")[0]

        if time.time() - int(progress_status_time[f"{chat_id}+{message_id}"]
                             ["last_updated"]) > 3:
            try:
                await cl.edit_message_text(
                    chat_id=chat_id,
                    message_id=message_id,
                    text=f"<code>Progress: Uploading \n"
                    f"Uploaded: {humanfriendly.format_size(int(upload), binary=True)} of "
                    f"{humanfriendly.format_size(int(total), binary=True)}\n"
                    f"Speed: {up_speed_split} MiBps</code>")
                job_update_timing = {
                    f"{chat_id}+{message_id}": {
                        "last_updated": time.time()
                    }
                }
                progress_status_time.update(job_update_timing)
            except MessageNotModified as e:
                logging.error(str(e))
            except FloodWait as e:
                logging.error(str(e))
                await asyncio.sleep(e.x)
Beispiel #4
0
def _get_iproute2_upper_limite_rate():
    """
    :return: Upper bandwidth rate limit of iproute2 [Kbps].
    :rtype: int
    """

    # upper bandwidth rate limit of iproute2 was 34,359,738,360
    # bits per second older than 3.14.0
    # http://git.kernel.org/cgit/linux/kernel/git/shemminger/iproute2.git/commit/?id=8334bb325d5178483a3063c5f06858b46d993dc7

    return hr.BitPerSecond("32Gbps")
Beispiel #5
0
def get_upper_limit_rate(tc_device):
    if typepy.is_null_string(tc_device):
        return _get_iproute2_upper_limite_rate()

    try:
        speed_value = _read_iface_speed(tc_device)
    except OSError:
        return _get_iproute2_upper_limite_rate()

    if speed_value < 0:
        # default to the iproute2 upper limit when speed value is -1 in
        # paravirtualized network interfaces
        return _get_iproute2_upper_limite_rate()

    return min(hr.BitPerSecond("{}Mbps".format(speed_value)), _get_iproute2_upper_limite_rate())
Beispiel #6
0
    def __normalize_bandwidth_rate(self, bandwidth_rate):
        if not bandwidth_rate:
            return None

        hr_bps = hr.BitPerSecond(bandwidth_rate)
        upper_limit_rate = get_upper_limit_rate(self.__device)

        if hr_bps > upper_limit_rate:
            logger.info(
                dedent("""\
                    clipping specified bandwidth rate limit with the {device} maximum bandwidth rate
                    ({value}Mbps -> {limit}Mbps)
                    """).format(value=hr_bps.mega_bps,
                                limit=upper_limit_rate.mega_bps,
                                device=self.__device))
            hr_bps = upper_limit_rate

        return hr_bps
Beispiel #7
0
def is_invalid_param(rate, delay, packet_loss, packet_duplicate, corrupt,
                     reordering):
    param_values = [packet_loss, packet_duplicate, corrupt, reordering]

    print("rate={}, params={}".format(rate, param_values))

    is_invalid = all([
        not RealNumber(param_value).is_type() or param_value <= 0
        for param_value in param_values
    ] + [hr.Time(delay, hr.Time.Unit.MILLISECOND).milliseconds <= 0])

    try:
        hr.BitPerSecond(rate).bps
    except (TypeError, ValueError):
        pass
    else:
        is_invalid = False

    return is_invalid
Beispiel #8
0
 def test_normal(self):
     assert _get_iproute2_upper_limite_rate() == hr.BitPerSecond("32Gbps")
def assistant(counter):
    greetMe(counter)
    sleepTimer = 0
    awake_time = None
    hour = ''
    minutes = ''
    while True:
        if awake_time is not None:
            print('Alarm Time: ' + awake_time)
            while True:
                if hour == datetime.datetime.now(
                ).hour and minutes == datetime.datetime.now().minute:
                    speak('Sir, please wakeup!')
                    wake_stat = takeCommand()
                    if 'okay' in wake_stat or 'i am up' in wake_stat or 'i\'m up' in wake_stat:
                        speak('Okay sir.')
                        awake_time = None
                        hour = ''
                        minutes = ''
                        break
                    else:
                        pass
                elif hour == datetime.datetime.now(
                ).hour and datetime.datetime.now().minute == minutes + 1:
                    awake_time = None
                    hour = ''
                    minutes = ''
                else:
                    break

        command = takeCommand()

        if command.strip() == 'hey jarvis':
            speak('Hello sir.')

        if 'hey' in command:
            command = command.replace('hey', '')
        if 'hi' in command:
            command = command.replace('hi', '')
        if 'jarvis' in command:
            command = command.replace('jarvis', '')

        if command == 'none':
            sleepTimer += 1
        else:
            sleepTimer = 0
            print(f'User: {command}')

        # Logic for executing tasks based on commands
        ##### Application Tasks
        if 'open' in command or 'i want to work on' in command or 'i want to build' in command:
            open_module.open_module(command)

        elif 'close' in command or 'i am done with' in command:
            close_module.close_module(command)

        elif 'play a movie' in command or 'play movie' in command or 'i want to watch a movie' in command or\
                'watch a movie' in command or 'watch movie' in command or 'movie' in command:
            speak('Which movie do you wish to watch?')
            command = takeCommand()
            movies_module.run_movie(command)

        elif 'play work out songs' in command or 'play workout songs' in command or 'workout' in command or\
            'work out' in command:
            speak('Searching for workout songs on youtube')
            search_youtube('workout songs')

        elif 'play' in command:
            song = command.replace('play', '')
            speak('Playing' + song)
            pywhatkit.playonyt(song)

        elif 'screenshot' in command or 'screen shot' in command or 'capture the screen' in command:
            try:
                cur_time = datetime.datetime.now().time().strftime('%H_%M_%S')
                print(cur_time)
                imgName = 'Screenshot_' + cur_time + '.jpg'
                picturesDir = 'C:\\Users\\Siddharth Sircar\\Pictures\\Screenshots\\'
                speak('Please stay on the screen for a while longer.')
                img = pyautogui.screenshot()
                speak('Saving Image')
                img.save(f'{picturesDir}{imgName}')
                speak('You can find the screenshot in the Pictures folder')
            except:
                speak(
                    'Did not take the screenshot. Confidential information on screen.'
                )
        #####

        ##### Personal commands
        elif 'set an alarm for' in command or 'wake me up at' in command:
            alarm_time = ''
            try:
                if 'set an alarm for' in command:
                    alarm_time = command.split('for ')[1].strip()
                if 'wake me up at' in command:
                    alarm_time = command.split('at ')[1].strip()
                alarm_time = alarm_time.replace('.', '')
                alarm_time = alarm_time.upper()
                # print(alarm_module.set_alarm(awake_time))
                awake_time, hour, minutes = alarm_module.set_alarm(alarm_time)
                speak(f'Alarm set for {alarm_time}')
            except:
                speak('Unable to set the alarm sir.')

        elif 'do some calculations' in command:
            speak('what do you wish to calculate?')
            command = takeCommand().lower()
            math_module.perform_calculations(command)

        elif 'calculate' in command:
            command = command.replace('calculate', '')
            math_module.perform_calculations(command)

        elif 'who are you' in command:
            speak(
                random.choice([
                    'I am Batman', 'I\'m groot', 'I am jarvis',
                    'I, am, ironman', 'I\'m inevitable'
                ]))

        elif 'are you up' in command:
            speak('I am here sir.')

        elif 'we need to add more tasks for you' in command or 'you can handle more tasks' in command or\
            'you should handle more work' in command or 'you can handle more work' in command or \
                'you need to handle more work' in command:
            speak('I am ready for more work')

        elif 'how are you' in command:
            speak('I have been good. Thank you for asking.')
            speak(random.choice(['How have you been lately?', 'How are you?']))

        elif 'i am good' in command or 'i am also good' in command or\
            'i am great' in command or 'i am amazing' in command or 'i have been good' in command or\
                'ive been good' in command:
            speak('That\'s good to hear')

        elif 'not good' in command or 'not that good' in command or\
            'not great' in command:
            speak('Sorry to hear that sir')
            speak('Anything I can do to help?')

        elif 'tell me a joke' in command:
            try:
                speak(pyjokes.get_joke())
            except:
                speak('I don\'t feel like entertaining you today')

        elif 'you are funny' in command or 'you\'re funny' in command or\
                'you are really funny' in command or 'you\'re really funny' in command or\
            'you really funny' in command or 'very funny' in command:
            response_dict = ['I try sir.', 'People call me Mr. Hilarious']
            speak(random.choice(response_dict))

        elif 'i didn\'t sleep' in command:
            response_dict = [
                'One should have sleep for an average of 6 or 7 hours',
                'you should take care of your health sir!'
            ]
            speak(random.choice(response_dict))

        elif 'you can sleep' in command or 'you may go to sleep' in command or 'go to sleep' in command:
            speak('Okay sir, Let me know when you need me.')
            break

        elif 'you can go' in command:
            speak('Good bye sir. Let me know when you need me.')
            break

        elif 'goodnight' in command or 'good night' in command or 'good bye' in command or \
                'goodbye' in command or 'bye' in command or 'thank you' in command or 'thankyou' in command:
            com = gratitude_module.gratitude_module(command)
            if 'sleep' in com:
                break
            else:
                pass

        elif 'i did not say anything' in command or 'i didn\'t say anything' in command:
            speak('okay.')

        elif 'shut up' in command or 'shutup' in command or 'be quiet' in command:
            speak('okay.')
        #####

        ##### Information Tasks
        elif 'time' in command:
            cur_time = datetime.datetime.now().strftime('%I:%M %p')
            speak(f'It\'s {cur_time}')
            cur_hour = int(datetime.datetime.now().hour)
            if cur_hour >= 0 and cur_hour < 4:
                speak('you should go to sleep now sir. It\'s pretty late')

        elif 'date' in command:
            today = datetime.date.today()
            speak(f'It\'s {today}')

        elif 'current location' in command or 'where are we' in command or 'where am i' in command \
                or 'location' in command or 'locate us' in command:
            location_module.get_current_location()

        elif 'temperature' in command or 'weather' in command:
            weather_module.get_weather()

        elif 'news' in command or 'headlines' in command:
            speak('Fetching the latest news')
            try:
                if 'headlines' in command:
                    news_module.get_news()
                else:
                    news_module.get_news()
                    speak('Do you wish to know more about a certain headline?')
                    response = takeCommand().lower()
                    if 'yes' in response or 'yup' in response or 'yeah' in response:
                        speak('Which one?')
                        response = takeCommand().lower()
                        news_module.get_specific_news(response)
                    elif 'no' in command or 'nope' in command or 'nah' in command:
                        speak('Ok sir.')
            except:
                speak('Sorry sir, could not find latest news.')

        elif ('friday' not in command and 'day' in command):
            day_name = [
                'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
                'Saturday', 'Sunday'
            ]
            day = datetime.datetime.today().weekday()
            speak(f'It\'s {day_name[day]}')

        elif "where is" in command:
            command = command.replace('where is', '')
            location = command
            speak(f'Locating {location}')
            webbrowser.open(f'https://www.google.com/maps/place/{location}')

        elif 'who is' in command:
            query = command.replace('who is', '')
            search_wiki(query)

        elif 'tell me about' in command:
            query = command.replace('tell me about', '')
            search_wiki(query)

        elif 'how far are we from' in command:
            try:
                query = command.replace('how far are we from', '')
                search_query = (f'distance from {query.strip()}')
                search_google(search_query)
            except:
                speak('I did not get you')

        elif 'search google' in command:
            try:
                speak('What should I search for?')
                query = takeCommand()
                search_google(query)
            except:
                speak('I did not get you')

        elif 'search youtube' in command:
            try:
                speak('What should I search for?')
                query = takeCommand().strip()
                search_youtube(query)
            except:
                speak('I did not get you')

        elif 'run a search on' in command or 'run search on' in command or 'search on' in command:
            try:
                query = command.replace('run a search on', '')
                search_google(query)
            except:
                speak('I did not get you')

        elif 'i want to learn' in command or 'learn' in command or 'study' in command or\
            'find courses on' in command:
            # query=''
            if 'i want to' in command:
                command = command.replace('i want to', '')
            if 'learn' in command:
                command = command.replace('learn', '')
            if 'study' in command:
                command = command.replace('study', '')
            if 'find courses on' in command:
                command = command.replace('find courses on', '')
            if 'courses on' in command:
                command = command.replace('courses on', '')
            query = command
            search_google(f'courses on {query}')
            webbrowser.open(
                f'https://www.youtube.com/results?search_query={query}')

        elif 'how to' in command:
            how_to_module.get_howto_result(command)
        #####

        ##### Smartphone Commands
        elif 'receive the call' in command or 'pick up the call' in command or 'pic up the call' in command or \
                'pickup the call' in command or 'pickup' in command:
            smartphone_module.receive_call()

        elif 'cut the call' in command or 'hang up the call' in command or 'hangup the call' in command or\
                'disconnect' in command or 'hangup' in command or 'hang up' in command or 'cut' in command:
            smartphone_module.disconnect_call()

        elif 'make a call' in command:
            try:
                status, _ = smartphone_module.check_device_connected()
                if status:
                    speak('Who should I call?')
                    person = takeCommand().strip()
                    smartphone_module.make_a_call(person)
                else:
                    speak('I am unable to access your phone.')
            except:
                speak('Mobile not connected')
                speak('Should I try to connect?')
                command = takeCommand()
                if 'yes' in command:
                    smartphone_module.connect_device()
                else:
                    pass

        elif 'call' in command:
            try:
                status, _ = smartphone_module.check_device_connected()
                if status:
                    person = command.replace('call', '').strip()
                    smartphone_module.make_a_call(person)
                else:
                    speak('I am unable to access your phone.')
            except:
                speak('Mobile not connected')
                speak('Should I try to connect?')
                command = takeCommand()
                if 'yes' in command:
                    smartphone_module.connect_device()
                else:
                    pass
        #####

        ##### System Tasks
        elif 'speak up' in command or 'increase the volume' in command or 'volumeup' in command or\
                'volume up' in command or 'enough' in command:
            satisfied = False
            while satisfied is False:
                pyautogui.press('volumeup')
                speak('Is that okay sir?')
                command = takeCommand()
                if 'yes' in command:
                    satisfied = True
                    speak('ok')
                else:
                    satisfied = False

        elif 'decrease the volume' in command or 'volumedown' in command or 'volume down' in command:
            satisfied = False
            pyautogui.press('volumedown')
            speak('Is that okay sir?')
            while satisfied is False:
                pyautogui.press('volumedown')
                command = takeCommand()
                if 'yes' in command or 'this is ok' in command or 'this is okay' in command or 'okay' in command or\
                        'ok' in command or 'enough' in command:
                    satisfied = True
                    speak('ok')
                else:
                    satisfied = False

        elif 'mute' in command or 'quiet' in command or 'silent' in command:
            pyautogui.press('mute')

        elif 'i am going to sleep' in command or 'i\'m going to sleep' in command or 'going to sleep' in command or 'i\'m going out' in command or 'i am going out' in command or 'see you' in command or \
                ('sleep' in command and 'system' in command):
            speak('Sir, should I put system to sleep?')
            response = takeCommand().lower()
            if 'yes' in response or 'yup' in response or 'yeah' in response:
                speak('Good Bye sir. Putting all systems to sleep')
                os.system('rundll32.exe powrprof.dll,SetSuspendState 0,1,0')

        elif 'shutdown' in command or 'shut down' in command or (
                'shut' and 'down' in command):
            speak('Are you sure?')
            command = takeCommand()
            if 'yes' in command:
                speak('All systems going offline.')
                speak('Good Bye sir.')
                os.system('shutdown /s /t 5')
            else:
                speak('Canceling shut down procedure')
                pass

        elif 'restart' in command or 'reboot' in command:
            speak('Resetting all systems')
            speak('See you soon sir.')
            os.system('shutdown /r /t 5')

        elif 'power' in command or 'power level' in command or 'battery' in command or\
                'how much power left' in command or 'how much power is left' in command or\
                'do we need to connect to a power source' in command or 'power source' in command:
            battery = psutil.sensors_battery()
            percentage = battery.percent
            seconds = battery.secsleft
            seconds = seconds % (24 * 3600)
            hour = seconds // 3600
            seconds %= 3600
            minutes = seconds // 60
            seconds %= 60

            speak(f'System at {percentage} percent')
            if percentage == 100 and battery.power_plugged:
                speak(
                    'We are running at full power, you can disconnect the power source'
                )
            elif (percentage >= 95
                  and percentage < 100) and battery.power_plugged:
                speak(
                    'We have enough power, you can disconnect the power source'
                )
            if (percentage >= 40
                    and percentage < 70) and battery.power_plugged is False:
                speak('We should connect a power source')
                speak(
                    f'We can remain operational for {hour} hours and {minutes} minutes'
                )
            elif (percentage >= 20
                  and percentage < 40) and battery.power_plugged is False:
                speak('Power levels low. Please connect a power source')
                speak(
                    f'We can remain operational for {hour} hours and {minutes} minutes only'
                )
            elif percentage < 20 and battery.power_plugged is False:
                speak('Power levels critical. Connect a power source asap')
                speak(
                    f'We can remain operational for {hour} hours and {minutes} minutes only'
                )

        elif 'internet speed' in command:
            speak('Calculating internet speed.')
            try:
                st = speedtest.Speedtest()
                down = st.download()
                print("'{}' to Mbps -> {}".format(
                    down,
                    hr.BitPerSecond(down).mega_bps))
                up = st.upload()

                speak(
                    f'Sir, we are getting {down} bit per second download speed and {up} bit per second upload speed'
                )
            except:
                speak('Unable to calculate internet speed')
        #####

        elif 'none' in command or command is None:
            if sleepTimer > 30:
                speak('I am going to lie down a bit.')
                response = takeCommand()
                if 'no' in response or 'wait' in response:
                    pass
                else:
                    break
            else:
                pass

        else:
            speak('I did not quite get you.')
Beispiel #10
0
client.server_hostname = client_hostname
client.port = server_port
client.protocol = server_proto
client.num_streams = int(num_streams)
client.duration = int(duration)
client.verbose = False
client.json_output = True
result = client.run()


if result.error:
    syslog.syslog("Unable to connect to iperf server " + server_hostname)
    syslog.syslog(result.error)
else:
    # test_id:  yy-mm-dd-##:src_cloud:src_region:src_size:dst_cloud:dst_region:dst_size:insane_mode:ha
    now = datetime.now()
    id = "%s-%s-%s-%s" % (now.year, '{:02d}'.format(now.month), '{:02d}'.format(now.day), client_num)
    test_id = "%s:%s:%s:%s:%s:%s:%s:%s:%s" % (id, src_cloud, src_region, src_size, dst_cloud, dst_region, dst_size, insane_mode, ha)
    r = "{} bps".format(result.json['end']['sum_received']['bits_per_second'])
    s = "{} bps".format(result.json['end']['sum_sent']['bits_per_second'])
    rm = int(float(format(hr.BitPerSecond(r).mega_bps)))
    rm = int(round(rm))
    sm = int(float(format(hr.BitPerSecond(s).mega_bps)))
    sm = int(round(sm))
    syslog.syslog("Received sum: " + format(r))
    syslog.syslog("Received sum: " + format(hr.BitPerSecond(r).mega_bps))
    syslog.syslog("Sent sum: " + format(s))
    syslog.syslog("Sent sum: " + format(hr.BitPerSecond(s).mega_bps))
    update_results(test_id, s, r, str(rm), str(sm))