-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1df3ab3
commit d822958
Showing
3 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.pyc | ||
.idea | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import json, socket | ||
|
||
class Server(object): | ||
""" | ||
A JSON socket server used to communicate with a JSON socket client. All the | ||
data is serialized in JSON. How to use it: | ||
server = Server(host, port) | ||
while True: | ||
server.accept() | ||
data = server.recv() | ||
# shortcut: data = server.accept().recv() | ||
server.send({'status': 'ok'}) | ||
""" | ||
|
||
backlog = 5 | ||
client = None | ||
|
||
def __init__(self, host, port): | ||
self.socket = socket.socket() | ||
self.socket.bind((host, port)) | ||
self.socket.listen(self.backlog) | ||
|
||
def __del__(self): | ||
self.close() | ||
|
||
def accept(self): | ||
# if a client is already connected, disconnect it | ||
if self.client: | ||
self.client.close() | ||
self.client, self.client_addr = self.socket.accept() | ||
return self | ||
|
||
def send(self, data): | ||
if not self.client: | ||
raise Exception('Cannot send data, no client is connected') | ||
_send(self.client, data) | ||
return self | ||
|
||
def recv(self): | ||
if not self.client: | ||
raise Exception('Cannot receive data, no client is connected') | ||
return _recv(self.client) | ||
|
||
def close(self): | ||
if self.client: | ||
self.client.close() | ||
self.client = None | ||
if self.socket: | ||
self.socket.close() | ||
self.socket = None | ||
|
||
|
||
class Client(object): | ||
""" | ||
A JSON socket client used to communicate with a JSON socket server. All the | ||
data is serialized in JSON. How to use it: | ||
data = { | ||
'name': 'Patrick Jane', | ||
'age': 45, | ||
'children': ['Susie', 'Mike', 'Philip'] | ||
} | ||
client = Client() | ||
client.connect(host, port) | ||
client.send(data) | ||
response = client.recv() | ||
# or in one line: | ||
response = Client().connect(host, port).send(data).recv() | ||
""" | ||
|
||
socket = None | ||
|
||
def __del__(self): | ||
self.close() | ||
|
||
def connect(self, host, port): | ||
self.socket = socket.socket() | ||
self.socket.connect((host, port)) | ||
self.socket.settimeout(3) | ||
return self | ||
|
||
def send(self, data): | ||
if not self.socket: | ||
raise Exception('You have to connect first before sending data') | ||
_send(self.socket, data) | ||
return self | ||
|
||
def recv(self): | ||
if not self.socket: | ||
raise Exception('You have to connect first before receiving data') | ||
return _recv(self.socket) | ||
|
||
def recv_and_close(self): | ||
data = self.recv() | ||
self.close() | ||
return data | ||
|
||
def close(self): | ||
if self.socket: | ||
self.socket.close() | ||
self.socket = None | ||
|
||
|
||
## helper functions ## | ||
|
||
def _send(socket, data): | ||
try: | ||
serialized = json.dumps(data) | ||
except (TypeError, ValueError), e: | ||
raise Exception('You can only send JSON-serializable data') | ||
socket.sendall(serialized) | ||
socket.send('\n') | ||
|
||
def _recv(socket): | ||
# read the length of the data, letter by letter until we reach EOL | ||
data_in = '' | ||
char = socket.recv(1) | ||
while char != '\n': | ||
data_in += char | ||
char = socket.recv(1) | ||
try: | ||
deserialized = json.loads(data_in) | ||
except (TypeError, ValueError), e: | ||
raise Exception('Data received was not in JSON format') | ||
return deserialized |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python | ||
|
||
from jsonsocket import Client | ||
from datetime import time, datetime | ||
from time import sleep | ||
|
||
host = '127.0.0.1' | ||
port = 5204 | ||
dimmingStartHour = 22 | ||
dimmingEndHour = 8 | ||
dimmedBrightness = 0.25 | ||
nonDimmedBrightness = 1 | ||
|
||
client = Client() | ||
inDimmingMode = False | ||
|
||
|
||
def setBrightness(newBrightness): | ||
print "setting Brightness to %s" % newBrightness | ||
client.connect(host, port) | ||
client.send( | ||
{ | ||
"method": "setBrightness", | ||
"params": {"brightness": newBrightness} | ||
} | ||
) | ||
client.send( | ||
{ | ||
"method": "loadModel" | ||
} | ||
) | ||
response = client.recv() | ||
currentBrightness = response['params']['brightness'] | ||
if not(int(currentBrightness * 100) == int(newBrightness * 100)): | ||
raise Exception("Failed setting brightness, sent %s but got %s" % (currentBrightness, response['brightness'])) | ||
sleep(0.5) | ||
client.close() | ||
|
||
while True: | ||
t = datetime.now() | ||
shouldBeInDimmingMode = not (dimmingStartHour > t.hour > dimmingEndHour) | ||
if inDimmingMode and not shouldBeInDimmingMode: | ||
try: | ||
setBrightness(nonDimmedBrightness) | ||
inDimmingMode = False | ||
print "Success" | ||
except Exception as e: | ||
print "Error changing brightness" | ||
print e.message | ||
if not inDimmingMode and shouldBeInDimmingMode: | ||
try: | ||
setBrightness(dimmedBrightness) | ||
inDimmingMode = True | ||
print "Success" | ||
except Exception as e: | ||
print "Error changing brightness" | ||
print e.message | ||
sleep(60) |