WhatsUp commands
# -*- coding: utf-8 -*-
import os
import re
import shlex
import subprocess
from subprocess import Popen, PIPE
def cmd_help():
help_output = ''
for command, info in COMMANDS_INVERTED.items():
help_line = '/%s\n_%s_ \n\n' % (command, info[0])
help_output += help_line
return help_output.strip()
def cmd_temperature():
cmd = shlex.split('/opt/vc/bin/vcgencmd measure_temp')
p = Popen(cmd, stderr=PIPE, stdout=PIPE)
o = p.communicate()[0] # Example :: temp=45.6'C
temp = o[5:].decode('utf-8') # Add [:-2] if you want to strip 'C
return 'My temperature is: *%s*' % temp
def cmd_monitor(state):
monitor_file = os.path.join('/sys/class/backlight/rpi_backlight/bl_power')
if state[0] == 'on':
f = open(monitor_file, 'w')
f.write('0')
f.close()
return 'Monitor turned ON'
else:
f = open(monitor_file, 'w')
f.write('1')
f.close()
return 'Monitor turned OFF'
COMMANDS = {
'help': (
'Shows this list',
cmd_help
),
'temperature': (
'Returns the current temperature of the Pi',
cmd_temperature
),
'monitor [on|off]': (
'Turn monitor ON or OFF',
cmd_monitor
)
}
COMMANDS_INVERTED = {
k: v for k, v in COMMANDS.items()
}
def is_command(message):
first_character = message[:1]
if first_character == '/':
return True
return False
def execute_command(message):
# Check if the message is a command (first character must be a forward-slash /)
if is_command(message):
cmd = message[1:].split(' ')[0] # Strip the forward-slash from the command
args = message[1:].split(' ')[1:]
for command, info in COMMANDS.items():
command = re.sub(r'\[[^]]*\]', '', command).strip()
if cmd == command:
try:
if args:
return info[1](args)
return info[1]()
except:
# return 'Command does not exist'
pass
else:
# return 'Hello, this is a Raspberry Pi!'
pass
return 'Unknown command, type /help to view available commands' # Fallback