Advertisement
Guest User

main.py

a guest
Jul 26th, 2014
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.95 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4.  
  5. from subprocess import Popen
  6. from time import sleep
  7. from sys import exit
  8. import commands # TODO: Austauschen, da nur unter Python2 verfuegbar!
  9.  
  10. import pitouch
  11. import datum_einstellen
  12.  
  13.  
  14. def main(p):
  15.     p.gen_grid(3, 3)
  16.    
  17.     p.locate(0, 0)
  18.     p.set_text("Benutzerdefinierte\nAnwendungen")
  19.     p.set_bgcolor("#88ff88")
  20.     p.set_nextcommand(user_apps)
  21.    
  22.     p.locate(2, 0)
  23.     p.set_text("System-\nEinstellungen\nändern")
  24.     p.set_bgcolor("#88ff88")
  25.     p.set_nextcommand(systemsettings)
  26.    
  27.     p.locate(2, 2)
  28.     p.set_text("Raspberry Pi\nherunterfahren")
  29.     p.set_bgcolor("#ff8888")
  30.     p.set_nextcommand(rpi_shutdown)
  31.  
  32.  
  33. def user_apps(p):
  34.     p.gen_grid(3, 3)
  35.    
  36.     p.locate(2, 0)
  37.     p.set_text("<-----\nZurück")
  38.     p.set_bgcolor("#88ffff")
  39.     p.set_nextcommand(main)
  40.  
  41.     p.locate(0, 0)
  42.    
  43.     if Popen("pgrep motion", shell=True).wait() == 0:
  44.         p.set_text("Aufnahme\nstoppen")
  45.         p.set_bgcolor("#ff8888")
  46.         p.set_nextcommand(stop_capture)
  47.     else:
  48.         p.set_text("Aufnahme\nstarten")
  49.         p.set_bgcolor("#88ff88")
  50.         p.set_nextcommand(start_capture)
  51.        
  52.         p.locate(0, 1)
  53.         p.set_text("Screenshot\nanzeigen")
  54.         p.set_bgcolor("#88ff88")
  55.         p.set_nextcommand(do_screenshot)
  56.        
  57.         p.locate(1, 1)
  58.         p.set_text("Video\nzeigen")
  59.         p.set_bgcolor("#88ff88")
  60.         p.set_nextcommand(show_video)
  61.    
  62.     p.locate(0, 2)
  63.     p.set_text("Bildschirm\nausschalten")
  64.     p.set_bgcolor("#88ff88")
  65.     p.set_nextcommand(blank_screen)
  66.  
  67. def show_video(p):
  68.     p.gen_grid(1, 1)
  69.     cmd = Popen("mplayer tv:// -tv driver=v4l2:width=320:height=240:device=/dev/video0:input=1 -fps 20 -fs -nosound -noconsolecontrols", shell=True)
  70.     sleep(5)
  71.     Popen("v4l2-ctl -c brightness=-5", shell=True).wait()
  72.     Popen("v4l2-ctl -c contrast=16", shell=True).wait()
  73.     Popen("v4l2-ctl -c saturation=12", shell=True).wait()
  74.     Popen("v4l2-ctl -c hue=0", shell=True).wait()
  75.     cmd.wait()
  76.     user_apps(p)
  77.  
  78.  
  79. def blank_screen(p):
  80.     p.msgbox_showinfo(None, "Das Touch-Display einfach\nberühren, um es wieder\neinzuschalten.")
  81.     p.gen_grid(1, 1)
  82.     sleep(1)
  83.     Popen("xset dpms force off", shell=True).wait()
  84.    
  85.     p.locate(0, 0)
  86.     p.set_text("<-----\nZurück")
  87.     p.set_bgcolor("#88ffff")
  88.     p.set_nextcommand(user_apps)
  89.  
  90.  
  91. def ask_dhcp(p):
  92.     p.gen_grid(1, 1)
  93.     Popen("systemctl restart dhcpcd", shell=True).wait()
  94.     tmp = commands.getoutput("ifconfig -a | grep inet | grep -v 127. | awk '{ print $2 }'")
  95.     p.msgbox_showinfo(None, "IP wird angefordert...\n" + tmp)
  96.     systemsettings(p)
  97.  
  98.  
  99. def stop_smb(p):
  100.     p.gen_grid(1, 1)
  101.     Popen("systemctl stop smbd", shell=True).wait()
  102.     systemsettings(p)
  103.  
  104.  
  105. def start_smb(p):
  106.     p.gen_grid(1, 1)
  107.     Popen("systemctl start smbd", shell=True).wait()
  108.     systemsettings(p)
  109.  
  110.  
  111. def do_screenshot(p):
  112.     p.gen_grid(1, 1)
  113.     Popen("fswebcam -r 320x240 --skip 5 --save /public/screenshot.jpeg", shell=True).wait()
  114.     p.set_image("/public/screenshot.jpeg")
  115.     p.set_nextcommand(user_apps)
  116.  
  117.  
  118. def stop_capture(p):
  119.     p.gen_grid(1, 1)
  120.     Popen("pkill motion", shell=True).wait()
  121.     while Popen("pgrep motion", shell=True).wait() == 0:
  122.         sleep(2)
  123.    
  124.     Popen("systemctl stop smbd", shell=True).wait()
  125.     Popen("sync", shell=True).wait()
  126.     Popen("umount /dev/sda", shell=True).wait()
  127.     Popen("sync", shell=True).wait()
  128.     Popen("systemctl start smbd", shell=True).wait()
  129.     user_apps(p)
  130.  
  131.  
  132. def start_capture(p):
  133.     p.gen_grid(1, 1)
  134.     Popen("mount /dev/sda /public -o sync", shell=True).wait()
  135.     Popen("motion &", shell=True)
  136.     user_apps(p)
  137.  
  138.  
  139. def rpi_shutdown(p):
  140.     if p.get_msgbox_askyesno(None, "Den Raspberry Pi wirklich\nherunterfahren?"):
  141.         p.window.destroy()
  142.         Popen("systemctl poweroff", shell=True)
  143.         exit()
  144.  
  145.  
  146. def systemsettings(p):
  147.     p.gen_grid(3, 3)
  148.    
  149.     p.locate(0, 0)
  150.     p.set_text("Datum\neinstellen")
  151.     p.set_bgcolor("#88ff88")
  152.     p.set_nextcommand(datum_einstellen.datum_einstellen)
  153.    
  154.     p.locate(2, 0)
  155.     p.set_text("<-----\nZurück")
  156.     p.set_bgcolor("#88ffff")
  157.     p.set_nextcommand(main)
  158.    
  159.     p.locate(0, 1)
  160.     if Popen("pgrep smbd", shell=True).wait() == 0:
  161.         p.set_text("Freigabe\nbeenden")
  162.         p.set_bgcolor("#ff8888")
  163.         p.set_nextcommand(stop_smb)
  164.     else:
  165.         p.set_text("Freigabe\nstarten")
  166.         p.set_bgcolor("#88ff88")
  167.         p.set_nextcommand(start_smb)
  168.        
  169.         p.locate(2, 2)
  170.         p.set_text("USB-Stick\nformatieren")
  171.         p.set_bgcolor("#ff8888")
  172.         p.set_nextcommand(del_usb)
  173.        
  174.     p.locate(0, 2)
  175.     p.set_text("DHCP-Anfrage\nsenden")
  176.     p.set_bgcolor("#88ff88")
  177.     p.set_nextcommand(ask_dhcp)
  178.    
  179.     p.locate(1, 0)
  180.     p.set_text("Mit WLAN\nverbinden")
  181.     p.set_bgcolor("#88ff88")
  182.     p.set_nextcommand(wlan_connect)
  183.    
  184.     p.locate(1, 2)
  185.     p.set_text("/public\nlöschen")
  186.     p.set_bgcolor("#ff8888")
  187.     p.set_nextcommand(del_public)
  188.    
  189.     p.locate(1, 1)
  190.     p.set_bgcolor("#88ff88")
  191.     if Popen('grep "/dev/sda " /proc/mounts', shell=True).wait() != 0:
  192.         p.set_text("USB-Stick\neinhängen")
  193.         p.set_nextcommand(usb_mount)
  194.     else:
  195.         p.set_text("USB-Stick\naushängen")
  196.         p.set_nextcommand(usb_umount)
  197.  
  198.  
  199. def usb_mount(p):
  200.     p.gen_grid(1, 1)
  201.     Popen("mount /dev/sda /public", shell=True).wait()
  202.     systemsettings(p)
  203.    
  204.  
  205. def usb_umount(p):
  206.     p.gen_grid(1, 1)
  207.     Popen("umount /public", shell=True).wait()
  208.     systemsettings(p)
  209.  
  210.    
  211. def wlan_connect(p):
  212.     Popen("pkill wpa_supplicant", shell=True).wait()
  213.     p.gen_grid(1, 3)
  214.    
  215.     p.locate(2, 0)
  216.     p.set_text("<-----\nZurück")
  217.     p.set_bgcolor("#88ffff")
  218.     p.set_nextcommand(systemsettings)
  219.    
  220.     p.locate(0, 0)
  221.     p.set_text("Verbinde mit:\n3er-WG")
  222.     p.set_bgcolor("#88ff88")
  223.     p.set_nextcommand(connect_3erwg)
  224.  
  225.  
  226. def connect_3erwg(p):
  227.     Popen("wpa_supplicant -iwlan0 -Dwext -c /etc/wpa_supplicant/3erwg.conf -B", shell=True)
  228.     systemsettings(p)
  229.    
  230. def del_usb(p):
  231.     if p.get_msgbox_askyesno(None, "ACHTUNG: Alle Daten\nauf dem Stick\nwerden gelöscht!\nFortfahren?"):
  232.         p.gen_grid(1, 1)
  233.         Popen("systemctl stop smbd", shell=True).wait()
  234.         Popen("umount /dev/sda", shell=True).wait()
  235.         Popen("dd if=/dev/zero of=/dev/sda bs=5000 count=1", shell=True).wait()
  236.         Popen("mkfs.vfat -I -F 32 /dev/sda", shell=True).wait()
  237.         systemsettings(p)
  238.  
  239.  
  240. def del_public(p):
  241.     if p.get_msgbox_askyesno(None, "/public wirklich\nlöschen?"):
  242.         p.gen_grid(1, 1)
  243.         Popen("rm -rf /public/*", shell=True).wait()
  244.         systemsettings(p)
  245.  
  246.  
  247. if __name__ == "__main__":
  248.     p = pitouch.pitouch()
  249.     Popen("xinput --set-prop 'ADS7846 Touchscreen' 'Evdev Axis Inversion' 0 1", shell=True).wait()
  250.     Popen("xinput --set-prop 'ADS7846 Touchscreen' 'Evdev Axes Swap' 1", shell=True).wait()
  251.     Popen("xset -dpms", shell=True).wait()
  252.     main(p)
  253.     p.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement