Last active
September 12, 2017 03:32
-
-
Save Ari-Roda/e00211d946966c9de9ed139e43637a03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
import socket | |
import json | |
################################################# | |
## set socket to connect to air conditioner 4,5 # | |
################################################# | |
s = socket.socket() | |
host = "air con ip" | |
port = 8001 | |
s.connect((host, port)) | |
# | |
# list of txt files that store data for each individual sensor | |
# | |
list = ["sensor-data.txt","sensor-data1.txt","sensor-data2.txt","sensor-data3.txt","sensor-data4.txt", "sensor-data5.txt" ] | |
# | |
# Function that extracts temperature from the text files. | |
# | |
def readsplit(x): | |
data = open(x, "r") | |
data = file.read(data) | |
data = data[0:4] | |
return data | |
while True: | |
dvid = 0 #setting dvid to 0 for for loop | |
summary = "psql -tq -c \"select * from devices\"" # using psql cmd to set summary for display | |
# | |
# reading average of user inputs manipulating and storing | |
# | |
average = "psql -tq -c \"select avg(temp) from devices\"> avg_file.txt" | |
average = os.system(average) | |
average = open("avg_file.txt", "r").read() | |
average = average[:3] | |
in_average = "psql -q -c \"UPDATE DEVICES SET avg ="+average+"\"" | |
# data block to be sent to air conditioner | |
data = { | |
'block':4, | |
'group':5, | |
'temperature':average | |
} | |
# update for devices 1 - 6 based on txt files. | |
for i in list: | |
dvid += 1 | |
cmd = "psql -tq -c \"UPDATE DEVICES SET room_temp = "+readsplit(i)+" where devid="+str(dvid)+"\"" | |
os.system(cmd) | |
os.system(in_average) # update average in db | |
xjson = json.dumps(data) # change to json format | |
s.send(xjson) # send to air conditioner | |
print "######################################" | |
print "|DEVID | temp | r_temp | GRP |AVG |" | |
print "######################################" | |
print os.system(summary) | |
print "######################################" | |
print "" | |
time.sleep(15) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
import time | |
import pty, sys | |
import pty | |
from os import waitpid, execv, read, write | |
def split(x): | |
if "." in x: | |
tx = x.split(".")[0] | |
tx = tx[-2:] | |
y = x.split(".")[1] | |
y = y[:1] | |
z = (tx+"."+y) | |
else: | |
z = x[-4:-2] | |
return z | |
while True: | |
data = os.popen('influx -database sensor-data -execute "select value from temp where time > now() - 10s and device=\'Sensor-3\'"').read() | |
dataa = os.popen('influx -database sensor-data -execute "select value from temp where time > now() - 10s and device=\'Sensor-2\'"').read() | |
data_d3 = os.popen('influx -database sensor-data -execute "select value from temp where time > now() - 10s and device=\'Sensor-1\'"').read() | |
data_d4 = os.popen('influx -database sensor-data -execute "select value from temp where time > now() - 10s and device=\'Sensor-4\'"').read() | |
data_d5 = os.popen('influx -database sensor-data -execute "select value from temp where time > now() - 10s and device=\'Sensor-5\'"').read() | |
data_d6 = os.popen('influx -database sensor-data -execute "select value from temp where time > now() - 10s and device=\'Sensor-6\'"').read() | |
print " SENSOR DATA " | |
print "################" | |
data3 = split(data) | |
dataa = split(dataa) | |
data_d3 = split(data_d3) | |
data_d4 = split(data_d4) | |
data_d5 = split(data_d5) | |
data_d6 = split(data_d6) | |
print "Sensor 3 "+data3+" #" | |
print "Sensor 2 "+dataa+" #" | |
print "Sensor 1 "+data_d3+" #" | |
print "Sensor 4 "+data_d4+" #" | |
print "Sensor 5 "+data_d5+" #" | |
print "Sensor 6 "+data_d6+" #" | |
print "################" | |
print "" | |
command = "echo "+data3 +" > /home/aciot/sensor-data.txt" | |
command1 = "echo "+dataa +" > /home/aciot/sensor-data1.txt" | |
command2 = "echo "+data_d3 +" > /home/aciot/sensor-data2.txt" | |
command3 = "echo "+data_d4 +" > /home/aciot/sensor-data3.txt" | |
command4 = "echo "+data_d5 +" > /home/aciot/sensor-data4.txt" | |
command5 = "echo "+data_d6 +" > /home/aciot/sensor-data5.txt" | |
time.sleep(5) | |
class ssh(): | |
def __init__(self, host, execute='echo "done" > /root/testing.txt', | |
askpass=False, user='aciot', password=b'aciot'): | |
self.exec_ = execute | |
self.host = host | |
self.user = user | |
self.password = password | |
self.askpass = askpass | |
self.run() | |
def run(self): | |
command = [ | |
'/usr/bin/ssh', | |
self.user+'@'+self.host, | |
'-o', 'NumberOfPasswordPrompts=1', | |
self.exec_, | |
] | |
# PID = 0 for child, and the PID of the child for the parent | |
pid, child_fd = pty.fork() | |
if not pid: # Child process | |
# Replace child process with our SSH process | |
execv(command[0], command) | |
## if we havn't setup pub-key authentication | |
## we can loop for a password promt and "insert" the password. | |
while self.askpass: | |
try: | |
output = read(child_fd, 1024).strip() | |
except: | |
break | |
lower = output.lower() | |
# Write the password | |
if b'password:' in lower: | |
write(child_fd, self.password + b'\n') | |
break | |
elif b'are you sure you want to continue connecting' in lower: | |
# Adding key to known_hosts | |
write(child_fd, b'yes\n') | |
else: | |
print('Error:',output) | |
# See if there's more output to read after the password has been sent, | |
# And capture it in a list. | |
output = [] | |
while True: | |
try: | |
output.append(read(child_fd, 1024).strip()) | |
except: | |
break | |
waitpid(pid, 0) | |
return ''.join(output) | |
if __name__ == "__main__": | |
s = ssh("air con ip",execute=command,askpass=True) | |
s1 = ssh("air con ip",execute=command1,askpass=True) | |
s2 = ssh("air con ip",execute=command2,askpass=True) | |
s3 = ssh("air con ip",execute=command3,askpass=True) | |
s4 = ssh("air con ip",execute=command4,askpass=True) | |
s5 = ssh("air con ip",execute=command5,askpass=True) | |
s.run() | |
s1.run() | |
s2.run() | |
s3.run() | |
s4.run() | |
s5.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment