codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
from __future__ import print_function import os import time import can import os.path import bottle from threading import Thread from wsgiref.simple_server import make_server from ws4py.websocket import WebSocket from ws4py.server.wsgirefserver import WSGIServer, WebSocketWSGIRequestHandler from ws4py.server.wsgiutils import WebSocketWSGIApplication from random import randrange import RPi.GPIO as GPIO # Bring up can0 interface at 500kbps os.system("sudo /sbin/ip link set can0 up type can bitrate 500000") GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(4, GPIO.OUT) GPIO.setup(4, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(17, GPIO.OUT) GPIO.setup(17, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(27, GPIO.OUT) GPIO.setup(27, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(22, GPIO.OUT) GPIO.setup(22, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(5, GPIO.OUT) GPIO.setup(5, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(6, GPIO.OUT) GPIO.setup(6, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(13, GPIO.OUT) GPIO.setup(13, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(19, GPIO.OUT) GPIO.setup(19, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(26, GPIO.OUT) GPIO.setup(26, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(18, GPIO.OUT) GPIO.setup(18, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(24, GPIO.OUT) GPIO.setup(24, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(23, GPIO.OUT) GPIO.setup(23, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(25, GPIO.OUT) GPIO.setup(25, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(12, GPIO.OUT) GPIO.setup(12, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(16, GPIO.OUT) GPIO.setup(16, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(20, GPIO.OUT) GPIO.setup(20, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(21, GPIO.OUT) GPIO.setup(21, GPIO.OUT, initial=GPIO.LOW) DEBUG = 1 #------------------------------------------------------------------- def printD(message): if DEBUG: print(message) def getUptime(): with open('/proc/uptime', 'r') as f: uptime_seconds = float(f.readline().split()[0]) uptime = str(timedelta(seconds = uptime_seconds)) return uptime def getPiRAM(): with open('/proc/meminfo', 'r') as mem: tmp = 0 for i in mem: sline = i.split() if str(sline[0]) == 'MemTotal:': total = int(sline[1]) elif str(sline[0]) in ('MemFree:', 'Buffers:', 'Cached:'): tmp += int(sline[1]) free = tmp used = int(total) - int(free) usedPerc = (used * 100) / total return usedPerc def getPiTemperature(): with open("/sys/class/thermal/thermal_zone0/temp", 'r') as f: content = f.read().splitlines() return float(content[0]) / 1000.0 #------------------------------------------------------------------- ### Parse request from webif #required format-> command:value def WebRequestHandler(requestlist): returnlist = "" for request in requestlist: request = request.strip() requestsplit = request.split(':') requestsplit.append("dummy") command = requestsplit[0] value = requestsplit[1] if value == "dummy": value = "0" if command == "localping": returnlist += "\n localping:ok" elif command == "LoadAVRnum": returnlist += "\n LoadAVRnum:"+open("/proc/loadavg").readline().split(" ")[:3][0] elif command == "Uptime": returnlist += "\n Uptime:"+str(getUptime()).split(".")[0] elif command == "RAMperc": returnlist += "\n RAMperc:"+str(getPiRAM()) #returnlist += "\n RAMperc:"+str(psutil.phymem_usage().percent) elif command == "PiTEMP": returnlist += "\n PiTEMP:"+str(getPiTemperature()) elif command == "System.Power": if value == "off": subprocess.Popen(["shutdown","-h","now"]) return "System.Power:ok" elif value == "reboot": subprocess.Popen(["shutdown","-r","now"]) return "System.Power:ok" elif command == "gpio41": GPIO.output(4,1) returnlist += "\n gpio4:1" elif command == "gpio40": GPIO.output(4,0) returnlist += "\n gpio4:0" elif command == "gpio171": GPIO.output(17,1) returnlist += "\n gpio17:1" elif command == "gpio170": GPIO.output(17,0) returnlist += "\n gpio17:0" elif command == "gpio271": GPIO.output(27,1) returnlist += "\n gpio27:1" elif command == "gpio270": GPIO.output(27,0) returnlist += "\n gpio27:0" elif command == "gpio221": GPIO.output(22,1) returnlist += "\n gpio22:1" elif command == "gpio220": GPIO.output(22,0) returnlist += "\n gpio22:0" elif command == "gpio51": GPIO.output(5,1) returnlist += "\n gpio5:1" elif command == "gpio50": GPIO.output(5,0) returnlist += "\n gpio5:0" elif command == "gpio61": GPIO.output(6,1) returnlist += "\n gpio6:1" elif command == "gpio60": GPIO.output(6,0) returnlist += "\n gpio6:0" elif command == "gpio131": GPIO.output(13,1) returnlist += "\n gpio13:1" elif command == "gpio130": GPIO.output(13,0) returnlist += "\n gpio13:0" elif command == "gpio191": GPIO.output(19,1) returnlist += "\n gpio19:1" elif command == "gpio190": GPIO.output(19,0) returnlist += "\n gpio19:0" elif command == "gpio261": GPIO.output(26,1) returnlist += "\n gpio26:1" elif command == "gpio260": GPIO.output(26,0) returnlist += "\n gpio26:0" elif command == "gpio181": GPIO.output(18,1) returnlist += "\n gpio18:1" elif command == "gpio180": GPIO.output(18,0) returnlist += "\n gpio18:0" elif command == "gpio231": GPIO.output(23,1) returnlist += "\n gpio23:1" elif command == "gpio230": GPIO.output(23,0) returnlist += "\n gpio23:0" elif command == "gpio241": GPIO.output(24,1) returnlist += "\n gpio24:1" elif command == "gpio240": GPIO.output(24,0) returnlist += "\n gpio24:0" elif command == "gpio251": GPIO.output(25,1) returnlist += "\n gpio25:1" elif command == "gpio250": GPIO.output(25,0) returnlist += "\n gpio25:0" elif command == "gpio121": GPIO.output(12,1) returnlist += "\n gpio12:1" elif command == "gpio120": GPIO.output(12,0) returnlist += "\n gpio12:0" elif command == "gpio161": GPIO.output(16,1) returnlist += "\n gpio16:1" elif command == "gpio160": GPIO.output(16,0) returnlist += "\n gpio16:0" elif command == "gpio201": GPIO.output(20,1) returnlist += "\n gpio20:1" elif command == "gpio200": GPIO.output(20,0) returnlist += "\n gpio20:0" elif command == "gpio211": GPIO.output(21,1) returnlist += "\n gpio21:1" elif command == "gpio210": GPIO.output(21,0) returnlist += "\n gpio21:0" elif command == "canCOMM": bus = can.interface.Bus(channel='can0', bustype='socketcan_native') msg = can.Message(arbitration_id=0x512,data=[0x40,0x18,0x00, 0x00, 0x00, 0x00,0x00,0x00],extended_id=False) bus.send(msg) print(msg) elif command == "can": cansplit=value.split("#") canid= "0x" + cansplit[0] canbyte1="0x" + cansplit[1] canbyte2="0x" + cansplit[2] canbyte3="0x" + cansplit[3] canbyte4="0x" + cansplit[4] canbyte5="0x" + cansplit[5] canbyte6="0x" + cansplit[6] canbyte7="0x" + cansplit[7] canbyte8="0x" + cansplit[8] print(canid) print(canbyte1) print(canbyte2) print(canbyte3) print(canbyte4) print(canbyte5) print(canbyte6) print(canbyte7) print(canbyte8) msg=can.Message(arbitration_id=0x512, data=[0x40,0x18,0,0,0,0,0,0], extended_id=False) bus = can.interface.Bus(channel='can0', bustype='socketcan_native') bus.send(msg) print(cansplit[0]) print(msg) returnlist += "\n Can Nachricht " + str(canid) +"#" + str(cansplit[1]) + str(cansplit[2]) + str(cansplit[3]) + str(cansplit[4]) + str(cansplit[5]) + str(cansplit[6]) + str(cansplit[7]) + str(cansplit[8]) + " " + "erfolgreich gesendet" return returnlist class myWebSocketHandler(WebSocket): connections = [] def opened(self): printD("New WebSocket client connected") self.send("You are connected") self.connections.append(self) def received_message(self, message): msg = message.data.decode() printD("Message from WebIf: >>>"+msg+"<<<") requestlist = msg.splitlines() self.send(WebRequestHandler(requestlist)) def closed(self, code, reason): printD("WebSocket closed %s %s" % (code, reason)) self.connections.remove(self) #------------------------------------------------------------------- @bottle.route('/') def MainHandler(): values = { 'test': randrange(1, 1000), 'debug': 1, } return bottle.template('index.html', values) @bottle.route('/static/<filename>') def StaticHandler(filename): if filename.endswith(".css"): bottle.response.content_type = 'text/css' elif filename.endswith(".js"): bottle.response.content_type = 'text/javascript' elif filename.endswith(".png"): bottle.response.content_type = 'image/png' return bottle.static_file(filename, root=os.path.join(os.path.dirname(__file__), 'static')) try: websocket_server = make_server( '', 7070, server_class=WSGIServer, handler_class=WebSocketWSGIRequestHandler, app=WebSocketWSGIApplication(handler_cls=myWebSocketHandler) ) websocket_server.initialize_websockets_manager() # Start Child Thread for WebSocket print('Starting Child Thread for WebSocket_Server') ws = Thread(target=websocket_server.serve_forever) ws.setDaemon(True) ws.start() #bottle.debug(True) #sollte spaeter ausgeschaltet werden! bottle.TEMPLATE_PATH.insert(0, os.path.join(os.path.dirname(__file__), 'templates')) bottle.run(host='0.0.0.0', port='8080', debug=True, quiet=False) except KeyboardInterrupt: pass finally: print('Shutting down Servers') ws.join(1) try: ws.shutdown() except: pass
Private
[
?
]
Run code
Submit