Commit f49711f1 authored by Caughlin Bohn's avatar Caughlin Bohn
Browse files

Updated to use ssh

parent 737e27f5
import time
from random import randint
import socket
import subprocess
workers = {
"worker1": {"status":0,"time":0},
"worker2": {"status":0,"time":0},
......@@ -12,15 +13,12 @@ workers = {
"worker8": {"status":0,"time":0}
}
### Format = from,To,Amount
def sendCommand(command):
p = subprocess.Popen(command.split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
### Format = from,To,Amount
def sendLights(command):
HOST = '192.168.1.6' # Symbolic name meaning all available interfaces
PORT = 50007
command += " %f" % time.time()
for i in range(1,9):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(command)
s.close()
sendCommand("bash /work/net.sh %s" % command)
def writeWeb(data):
payload = "{\"worker1\":%s,\"worker2\":%s,\"worker3\":%s,\"worker4\":%s,\"worker5\":%s,\"worker6\":%s,\"worker7\":%s,\"worker8\":%s}" % (data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7])
f =open("/var/www/html/control/index.html",'w')
......@@ -28,16 +26,17 @@ def writeWeb(data):
f.close()
def workersToData(workers):
data = []
for k,v in workers.iteritems():
data.append(v["status"])
for i in range(1,9):
data.append(workers["worker%d" % i]["status"])
writeWeb(data)
start = time.time()
print("Starting")
sendLights("head worker1 5") ### Send data to master
time.sleep(5)
workers["worker1"]["status"] = 1 ### Turn on master
workersToData(workers)
time.sleep(5)
time.sleep(1)
print("Master On")
for i in range(2,9): ### Send master to slave data
......@@ -53,7 +52,10 @@ workersToData(workers)
print("Slaves on and working")
for i in range(1,100):
currentWorker = "worker"+str(randint(1,8))
sendLights("%s %s 1" % ("worker"+str(randint(2,8)),"worker"+str(randint(2,8))))
w1 = "worker"+str(randint(2,8))
w2 = "worker"+str(randint(2,8))
sendLights("%s %s 1" % (w1,w2))
sendLights("%s %s 1" % (w2,w1))
for i in range(2,9): ### Send data back
currentWorker = "worker"+str(i)
print(currentWorker)
......
import time
import socket
from random import randint
import subprocess
workers = {
"worker1": {"status":0,"time":0},
"worker2": {"status":0,"time":0},
......@@ -11,16 +12,13 @@ workers = {
"worker7": {"status":0,"time":0},
"worker8": {"status":0,"time":0}
}
def sendCommand(command):
p = subprocess.Popen(command.split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
### Format = from,To,Amount
def sendLights(command):
HOST = '192.168.1.6' # Symbolic name meaning all available interfaces
PORT = 50007
command += " %f" % time.time()
for i in range(1,9):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(command)
s.close()
sendCommand("bash /work/net.sh %s" % command)
def writeWeb(data):
payload = "{\"worker1\":%s,\"worker2\":%s,\"worker3\":%s,\"worker4\":%s,\"worker5\":%s,\"worker6\":%s,\"worker7\":%s,\"worker8\":%s}" % (data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7])
f =open("/var/www/html/control/index.html",'w')
......@@ -28,25 +26,31 @@ def writeWeb(data):
f.close()
def workersToData(workers):
data = []
for k,v in workers.iteritems():
data.append(v["status"])
for i in range(1,9):
data.append(workers["worker%d" % i]["status"])
writeWeb(data)
start = time.time()
while (time.time() - start) < 45:
currentWorker = "worker"+str(randint(1,8))
if workers[currentWorker]["status"] == 0: ### If the worker is not active, turn it on and send data to it from head
sendLights("head %s 1" % currentWorker)
workers[currentWorker]["status"] = 1
workers[currentWorker]["time"] = int(time.time())
if workers[currentWorker]["status"] == 1:
if (time.time() - workers[currentWorker]["time"]) > 5:
sendLights("%s head 1" % currentWorker)
workers[currentWorker]["status"] = 0
workersToData(workers)
for i in range(1,9):
currentWorker = "worker%d" % i
print(currentWorker)
sendLights("head %s 1" % currentWorker)
time.sleep(0.02)
workers[currentWorker]["status"] = 1
workersToData(workers)
print(workers[currentWorker]["status"])
time.sleep(0.3)
time.sleep(15)
for i in range(1,9):
currentWorker = "worker%d" % i
print(currentWorker)
sendLights("%s head 1" % currentWorker)
time.sleep(0.2)
workers[currentWorker]["status"] = 0
workersToData(workers)
time.sleep(1)
print(workers)
print(workers[currentWorker]["status"])
time.sleep(0.3)
workers = {
"worker1": {"status":0,"time":0},
"worker2": {"status":0,"time":0},
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment