From ec50d50f5c0f9e283581854ffb3f15b4190f9983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergiusz=20Baza=C5=84ski?= Date: Mon, 8 Apr 2013 20:18:08 +0200 Subject: [PATCH] Added LCD and debug. --- adsl.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++------ lcd.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 lcd.py diff --git a/adsl.py b/adsl.py index 4826550..4589dda 100644 --- a/adsl.py +++ b/adsl.py @@ -2,7 +2,10 @@ import os import time import sys import subprocess +import re + import config +import lcd def _get_process_list(): @@ -57,15 +60,54 @@ def _check_connectivity(): return True +def _get_public_ip(): + p = subprocess.Popen(["ip", "a", "list", "dev", "ppp0"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stderr, stdout = p.communicate() + if p.returncode != 0: + return "UNKNOWN" + else: + for line in stderr.split("\n"): + m = re.match("^inet ([\.0-9]*) peer", line.strip()) + if m: + return m.group(1) + return "UNKNOWN" + + def loop(): + screen = lcd.LCDScreen() + + def log(text, text2=None, clear=True): + if clear: + screen.clear() + text = text[:16] + text = ' ' * ((16 - len(text))/2) + text + screen.text_at(0, 0, text) + if text2: + text2 = text2[:16] + text2 = ' ' * ((16 - len(text2))/2) + text2 + screen.text_at(0, 1, text2) + while True: pppd_pid = _get_pppd_pid() if not pppd_pid: + log("Starting PPPd...") _start_pppd() - time.sleep(60) + screen.clear() + + timeout = 30 + while timeout >= 0: + log("PPPd started.", "Testing in %i s. " % timeout, False) + time.sleep(1) + timeout -= 1 + + log("PPPd started.", "Testing...") + + if not _check_connectivity(): + sys.stderr.write("Restarting PPPd.\n") + log("ADSL down!", "Restarting PPPd...") + _kill_pppd() + _start_pppd() + log("ADSL down!", "PPPd restarted!") else: - if not _check_connectivity(): - sys.stderr.write("Restarting PPPd.\n") - _kill_pppd() - _start_pppd() - time.sleep(60) + log("ADSL up!", _get_public_ip()) + time.sleep(60) diff --git a/lcd.py b/lcd.py new file mode 100644 index 0000000..3ead514 --- /dev/null +++ b/lcd.py @@ -0,0 +1,40 @@ +import serial + + +class LCDScreen(object): + def _command(self, command): + self.serial.write("\xFE" + chr(command)) + self.serial.flush() + + def clear(self): + self._command(1) + + def home(self): + self._command(2) + + def blank(self): + self._command(8) + + def unblank(self): + self._command(12) + + def location(self, x, y): + self._command(128 + 0x40*y + x) + + def text(self, text): + sanitized = "".join(c for c in text if c.lower() in "1234567890abcdefghjiklmnopqrstuvwxyz{}[]/+-_. ") + print sanitized + self.serial.write(sanitized) + self.serial.flush() + + def text_at(self, x, y, text): + self.location(x, y) + self.text(text) + + def __init__(self): + self.serial = serial.Serial("/dev/ttyS1", 2400) + self.clear() + self.home() + + def close(self): + self.serial.close()