Created
March 13, 2016 12:02
-
-
Save darkwave/7fe8198306e1611eb797 to your computer and use it in GitHub Desktop.
Intel IoT Hackathon entry
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
{ | |
"extensionName": "WeatherCast", | |
"extensionPort": 12345, | |
"blockSpecs": [ | |
[" ", "connect %m.sensors sensor to %m.digitalpin", "connect"], | |
[" ", "connect %m.analogsensors analog sensor to %m.analogpin", "connect"], | |
[" ", "connect %m.leds led to %m.digitalpin", "connect"], | |
[" ", "connect %m.actuators to %m.digitalpin", "connect"], | |
[" ", "Turn %m.leds led %m.onoff", "turn"], | |
[" ", "Buzzer %m.onoff", "buzzer"], | |
[" ", "Motor %m.onoff", "motor"], | |
["b", "touch", "touch"], | |
["b", "vibration", "vibration"], | |
["r", "A0", "A0"], | |
["r", "A1", "A1"], | |
["r", "A2", "A2"], | |
["r", "A3", "A3"], | |
["r", "D2", "D2"], | |
["r", "D3", "D3"], | |
["r", "D4", "D4"], | |
["r", "D5", "D5"], | |
["r", "D6", "D6"], | |
["r", "D7", "D7"], | |
["r", "D8", "D8"], | |
], | |
"menus": { | |
"analogpin": ["A0", "A1", "A2", "A3"], | |
"digitalpin": ["D2", "D3", "D4", "D5", "D6", "D7"], | |
"sensors": ["touch", "vibration", "light"], | |
"analogsensors": ["sound", "temperature", "light"], | |
"actuators": ["buzzer", "motor"], | |
"leds": ["red", "green", "blue"], | |
"onoff": ["on", "off"], | |
}, | |
} |
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
#!/usr/bin/python | |
# This is a simple port-forward / proxy, written using only the default python | |
# library. If you want to make a suggestion or fix something you can contact-me | |
# at voorloop_at_gmail.com | |
# Distributed over IDC(I Don't Care) license | |
import socket | |
import select | |
import time | |
import sys | |
# Changing the buffer_size and delay, you can improve the speed and bandwidth. | |
# But when buffer get to high or delay go too down, you can broke things | |
# 112/191 | |
buffer_size = 4096 | |
delay = 0.0001 | |
forward_to = ('172.16.38.112', 12345) | |
class Forward: | |
def __init__(self): | |
self.forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
def start(self, host, port): | |
try: | |
self.forward.connect((host, port)) | |
return self.forward | |
except Exception, e: | |
print e | |
return False | |
class TheServer: | |
input_list = [] | |
channel = {} | |
def __init__(self, host, port): | |
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
self.server.bind((host, port)) | |
self.server.listen(200) | |
def main_loop(self): | |
self.input_list.append(self.server) | |
while 1: | |
time.sleep(delay) | |
ss = select.select | |
inputready, outputready, exceptready = ss(self.input_list, [], []) | |
for self.s in inputready: | |
if self.s == self.server: | |
self.on_accept() | |
break | |
self.data = self.s.recv(buffer_size) | |
if len(self.data) == 0: | |
self.on_close() | |
break | |
else: | |
self.on_recv() | |
def on_accept(self): | |
forward = Forward().start(forward_to[0], forward_to[1]) | |
clientsock, clientaddr = self.server.accept() | |
if forward: | |
print clientaddr, "has connected" | |
self.input_list.append(clientsock) | |
self.input_list.append(forward) | |
self.channel[clientsock] = forward | |
self.channel[forward] = clientsock | |
else: | |
print "Can't establish connection with remote server.", | |
print "Closing connection with client side", clientaddr | |
clientsock.close() | |
def on_close(self): | |
print self.s.getpeername(), "has disconnected" | |
#remove objects from input_list | |
self.input_list.remove(self.s) | |
self.input_list.remove(self.channel[self.s]) | |
out = self.channel[self.s] | |
# close the connection with client | |
self.channel[out].close() # equivalent to do self.s.close() | |
# close the connection with remote server | |
self.channel[self.s].close() | |
# delete both objects from channel dict | |
del self.channel[out] | |
del self.channel[self.s] | |
def on_recv(self): | |
data = self.data | |
# here we can parse and/or modify the data before send forward | |
print data | |
self.channel[self.s].send(data) | |
if __name__ == '__main__': | |
server = TheServer('localhost', 12345) | |
try: | |
server.main_loop() | |
except KeyboardInterrupt: | |
print "Ctrl C - Stopping server" | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment