Raspberry Pi Home Monitor

时间:2017-10-20 22:39:47

标签: python-3.x notifications raspberry-pi monitoring raspbian

我一直在墙上砸我的头太久了。我把这个程序拼凑在一起,无法实现。它应该从pipins.conf接收输入以选择输入哪些引脚以及引脚的名称。这将是前。 " 2门"下一行" 15窗口"等等。我试图获取该文件并使用文本ex创建一个二维数组。 [[2,门],[15,窗口]]。我也为电子邮件地址做了同样的事情。程序应该获取该信息并观察输入到文件中的引脚然后如果引脚被触发(基本上引脚接地并且开路打开)程序应该发送一个电子邮件,其中包含引脚名称和状态。只有状态更改的日期和时间的引脚然后获取相同的信息并将其附加到log.txt文件。我在哪里错了。我对编程非常陌生,现在基本上需要这个哑巴让我掌握。

#!/usr/bin/env python
from gpiozero import Button
from signal import pause
import time
from datetime import datetime
import subprocess
from string import Template

SLEEP_TIME  = 0.01

NOTIFY_LIST = []
with open('email.conf','r') as notify:
  for word in notify.read().split():
    NOTIFY_LIST.append(word)

ACTPINS = {}
with open('pipins.conf','r') as my_file:
   for line in my_file:
     l_split = line.split()
     ACTPINS[int(l_split[0])] = l_split[1]

def current_date (fmt="%a %d-%m-%Y @ %H:%M:%S"):
    return datetime.strftime(datetime.now(), fmt)

NOTIFY_CMD = {pin: Template("""echo "$date $sensor $state" | mail -s "Pi: $sensor $state" $email""") for pin in ACTPINS}

def notify (id, state, sensor_name):
    """Send each of the email addresses in NOTIFY_LIST a message"""
    for email in NOTIFY_LIST:
        shell_cmd = NOTIFY_CMD[id].substitute(date=current_date(),
                                  state=state, sensor=sensor_name, email=email)
        proc = subprocess.Popen(shell_cmd, shell=True,
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout_value, stderr_value = proc.communicate()
        with open('log.txt', 'a') as f:
          f.write('{}\n'.format(shell_cmd))

def do_action(button):
  # function to send email etc will only run when opened or closed
  state = 'closed' if button.is_pressed else 'opened'
  notify(button.pin, state, ACTPINS[button.pin])
  print(state)

buttons = {}
for id in ACTPINS:
  buttons[id] = Button(id)
  buttons[id].when_pressed = do_action
  buttons[id].when_released = do_action
  print(id)
  print(buttons)
  print(ACTPINS)
  print(NOTIFY_LIST)

pause()

0 个答案:

没有答案