screen.py 4.54 KB
Newer Older
U+039b committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
import time
import json
import urllib2

import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

import subprocess

# Raspberry Pi pin configuration:
RST = 24
# Note the following are only used with SPI:
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0

# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)

# Initialize library.
disp.begin()

# Clear display.
disp.clear()
disp.display()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0

U+039b committed
51 52
font = ImageFont.truetype('/home/pi/pi-router-oled/Minecraftia.ttf', 8)
font2 = ImageFont.truetype('/home/pi/pi-router-oled/Minecraftia.ttf', 10)
U+039b committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

# Correctly display the bandwidth
def bdw(rate):
    rate /= 1.
    if rate > 1024:
       rate /= 1024.
       if rate > 1024:
           rate /= 1024.
           return "{0:.1f}".format(rate)+"Mbps"
       return "{0:.1f}".format(rate)+"Kbps"
    return "{0:.1f}".format(rate)+"bps"

while True:
    missing_lte_dongle = False
    # Draw a black filled box to clear the image.
    draw.rectangle((0,0,width,height), outline=0, fill=0)

    cmd = "hostname -I | cut -d\' \' -f1"
    IP = subprocess.check_output(cmd, shell = True )
    cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
    CPU = subprocess.check_output(cmd, shell = True )
    cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'"
    MemUsage = subprocess.check_output(cmd, shell = True )
    cmd = "systemctl | grep openvpn@client | grep running | wc -l"
    VPN_status = subprocess.check_output(cmd, shell = True )

    # Get LTE infos
    try:
        j = urllib2.urlopen('http://192.168.0.1/goform/goform_get_cmd_process?multi_data=1&isTest=false&sms_received_flag_flag=0&sts_received_flag_flag=0&cmd=modem_main_state%2Cpin_status%2Cloginfo%2Cnew_version_state%2Ccurrent_upgrade_state%2Cis_mandatory%2Csignalbar%2Cnetwork_type%2Cnetwork_provider%2Cppp_status%2CEX_SSID1%2Csta_ip_status%2CEX_wifi_profile%2Cm_ssid_enable%2CRadioOff%2Csimcard_roam%2Clan_ipaddr%2Cstation_mac%2Cbattery_charging%2Cbattery_vol_percent%2Cbattery_pers%2Cspn_display_flag%2Cplmn_display_flag%2Cspn_name_data%2Cspn_b1_flag%2Cspn_b2_flag%2Crealtime_tx_bytes%2Crealtime_rx_bytes%2Crealtime_time%2Crealtime_tx_thrpt%2Crealtime_rx_thrpt%2Cmonthly_rx_bytes%2Cmonthly_tx_bytes%2Cmonthly_time%2Cdate_month%2Cdata_volume_limit_switch%2Cdata_volume_limit_size%2Cdata_volume_alert_percent%2Cdata_volume_limit_unit%2Croam_setting_option%2Cupg_roam_switch%2Cap_station_mode%2Chmcc%2Chmnc%2Csms_received_flag%2Csts_received_flag%2Csms_unread_num&_=1493562678540', timeout=1)
        j_obj = json.load(j)
    except Exception:
        missing_lte_dongle = True

    lte_connected = False
    rx = -1
    tx = -1
    if not missing_lte_dongle:
        if j_obj["modem_main_state"] == "modem_init_complete":
            rx = bdw(int(j_obj["realtime_rx_thrpt"]))
            tx = bdw(int(j_obj["realtime_tx_thrpt"]))
            lte_connected = True

    if lte_connected:
        cmd = "ping -c 2 80.67.169.12 | tail -1| awk '{print $4}' | cut -d '/' -f 2"
        Ping = subprocess.check_output(cmd, shell = True )
    else:
        Ping = 'not connected'

    VPN = "VPN: down"
    if int(VPN_status) == 1:
        VPN = "VPN: connected"

    # Write two lines of text.
    draw.text((x, top), "  U+039b - LTE/Wifi",  font=font2, fill=255)
    draw.text((x, top+17), "IP: "+str(IP),  font=font, fill=255)
    draw.text((x, top+26), str(VPN),  font=font, fill=255)
    if missing_lte_dongle:
        draw.text((x, top+34), "LTE dongle not connected", font=font, fill=255)
    else:
        draw.text((x, top+34), str(j_obj["network_provider"]) + " - " + str(j_obj["network_type"]), font=font, fill=255)
    if lte_connected:
        draw.text((x, top+43), str(rx) + " - " + str(tx), font=font, fill=255)
        draw.text((x, top+52), "Ping: {0:.1f}".format(float(Ping))+"ms",  font=font, fill=255)
    else:
        draw.text((x, top+43), "No valid connection found", font=font, fill=255)

    # Display image.
    disp.image(image)
    disp.display()
    time.sleep(.1)