Skip to content

Commit

Permalink
Missing packets fix!
Browse files Browse the repository at this point in the history
  • Loading branch information
monster1025 committed Mar 5, 2017
1 parent 84db91e commit 54eda12
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import yamlparser
from xiaomihub import XiaomiHub

logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)
_LOGGER = logging.getLogger(__name__)

def process_gateway_messages(gateway, client):
Expand Down Expand Up @@ -40,14 +40,18 @@ def read_motion_data(gateway, client, polling_interval, polling_models):
if (model not in polling_models):
continue
sid = device['sid']
_LOGGER.debug("Polling " + str(model) + " with sid: " + str(sid))

sensor_resp = gateway.get_from_hub(sid)
if (sensor_resp['sid'] != sid):
print("Error: Response sid(" + sensor_resp['sid'] + ") differs from requested(" + sid + "). Skipping.")
continue;

data = json.loads(sensor_resp['data'])
state = data.get("status", None)
short_id = sensor_resp['short_id']
if (device['data'] != data or first):
if ( device['data'] != data or first):
device['data'] = data
_LOGGER.debug("Polling result differs for " + str(model) + " with sid(First: " + str(first) + "): " + str(sid) + "; " + str(data))
client.publish(model, sid, data)
first = False
except Exception as e:
Expand Down Expand Up @@ -97,4 +101,4 @@ def process_mqtt_messages(gateway, client):
t3.start()

while True:
time.sleep(10)
time.sleep(10)
16 changes: 16 additions & 0 deletions src/xiaomihub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import struct
import json
import logging
import sys
import select
from collections import defaultdict
from queue import Queue
from threading import Thread
Expand Down Expand Up @@ -111,9 +113,23 @@ def _discover_devices(self):
def _send_cmd(self, cmd, rtnCmd):
return self._send_socket(cmd, rtnCmd, self.GATEWAY_IP, self.GATEWAY_PORT)

def _read_unwanted_data(self):
try:
socket = self._socket
socket_list = [sys.stdin, socket]
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
for sock in read_sockets:
if sock == socket:
data = sock.recv(4096)
print("Not recieved data: " + str(data))
except Exception as e:
_LOGGER.error("Cannot read unwanted data: " + str(e))

def _send_socket(self, cmd, rtnCmd, ip, port):
socket = self._socket
try:
self._read_unwanted_data()

socket.settimeout(30.0)
socket.sendto(cmd.encode(), (ip, port))
socket.settimeout(30.0)
Expand Down

0 comments on commit 54eda12

Please sign in to comment.