Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python制作电源接口_Python_Python 2.7_Raspberry Pi_Python Multithreading_Wake On Lan - Fatal编程技术网

使用python制作电源接口

使用python制作电源接口,python,python-2.7,raspberry-pi,python-multithreading,wake-on-lan,Python,Python 2.7,Raspberry Pi,Python Multithreading,Wake On Lan,首先,我对python非常陌生。我有一个树莓pi通过局域网连接到几个节点。我希望pi成为节点的电源接口。当按下按钮时,神奇的数据包被发送到节点以唤醒它们。一旦节点通电,相同的瞬时开关将能够通过ssh连接到每个节点并执行断电命令来关闭节点。当机器启动和关闭电源时,LED应闪烁 到目前为止,我的尝试具有我描述的大部分功能,但是LED闪烁不稳定,我注意到我的一些线程正在被冗余执行 我的问题是,如何让led闪烁以更准确地表示机器的状态变化 以下是脚本: #!/usr/bin/python import

首先,我对python非常陌生。我有一个树莓pi通过局域网连接到几个节点。我希望pi成为节点的电源接口。当按下按钮时,神奇的数据包被发送到节点以唤醒它们。一旦节点通电,相同的瞬时开关将能够通过ssh连接到每个节点并执行断电命令来关闭节点。当机器启动和关闭电源时,LED应闪烁

到目前为止,我的尝试具有我描述的大部分功能,但是LED闪烁不稳定,我注意到我的一些线程正在被冗余执行

我的问题是,如何让led闪烁以更准确地表示机器的状态变化

以下是脚本:

#!/usr/bin/python
import RPi.GPIO as GPIO
import threading
import time
import os
from subprocess import call

nodes = [
    {
        "name": "node-1",
        "mac":  "80:EE:73:AE:AA:7C",
        "ip": "10.15.1.254",
    },
   #more nodes...
]


#function to blink the leds
def blink(pin,number):
    try:
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(pin, GPIO.OUT)        
        count =0
        while count < number:
            GPIO.output(pin,GPIO.HIGH)
            time.sleep(.5)
            GPIO.output(pin,GPIO.LOW)
            time.sleep(.5)

            count +=1
        return
    except Exception as inst:
        print "error: could not blink" 


#function to turn LED on or off based on power state        
def led(pin, state):
    try:
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(pin, GPIO.OUT)
        if state ==1:
            GPIO.output(pin, GPIO.HIGH)
        else:
            GPIO.output(pin, GPIO.LOW)
    except:
        print "error: could not turn on LED"


#function to wake up a node
def wake(node, mac):
    try: 

        print "waking" +node
        call(["wakeonlan", mac])
    except Exception as inst:
        print "error: could not wake " + node


#function to power off nodes
def shutdown(node, ip):
    try:
        print "shutting down " +node
        call(["ssh", "-o", "StrictHostKeyChecking=no", "root@"+ip, "poweroff"])
    except Exception as inst:
        print "error: could not shutdown " + node


#function to check the state of the nodes (if node can be pinged, it is up)
def checkstate(node,ip):
    try: 
        response = os.system("ping -i 0.1 -c 1 -w2 " + ip + " > /dev/null 2>&1")
        if response == 0:
            state =1
        else:
            state =0
        return state
    except:
        print "error could not ping"


#function checks if all node states are the same
def checksame(list):
    try: 
        list = iter(list)
        first = next(list)
        return all(first == rest for rest in list)
    except StopIteration:
        return list[0]


def main():

    while True:
        state = []
        for node in nodes:
            node_name = node.get('name', '')
            node_ip = node.get('ip', '')
            state.append(checkstate(node_name,node_ip)) #write each node state to an array
        power_state = state[1]
        if power_state ==1:
            led(40,1) #turn LED on because all the nodes are on
        if not checksame(state): #checks that all values in state array are the same. If not, led should blink because there is a state change or an error in a node
            t = threading.Thread(target=blink, args=(40,8) )
            t.start()
        else:
            GPIO.setmode(GPIO.BOARD)
            GPIO.setup(05, GPIO.IN, pull_up_down=GPIO.PUD_UP)
            button_state = GPIO.input(05)
            if button_state == False: #this is the button press detection
                if power_state ==1:
                    #button has been pressed, and machines are on, need to be off
                    for node in nodes:
                        node_name = node.get('name', '')
                        node_ip = node.get('ip', '')
                        shutdown(node_name, node_ip)
                        print "Nodes have been shut down"
                else:
                    #button has been pressed, machines are off, need to be on
                    for node in nodes:
                        node_name = node.get('name', '')
                        node_mac = node.get('mac', '')
                        wake(node_name,node_mac)
                        print "Nodes have been powered on"
                    t = threading.Thread(target=blink, args=(40, 180) )#this is a hack to show the LED blinking until the nodes can be pinged, otherwise, the LED would be off until the nodes were ping-able
                    t.start()
        time.sleep(0.2)        


if __name__ == "__main__":
    main()

请提供一个。删除了冗余节点和一些异常语句,但我认为其余的都是相关的。如果您将您的问题重新表述为……嗯,问题。现在有一个实际的问题