Missing packets fix!
This commit is contained in:
+6
-2
@@ -9,7 +9,7 @@ import mqtt
|
|||||||
import yamlparser
|
import yamlparser
|
||||||
from xiaomihub import XiaomiHub
|
from xiaomihub import XiaomiHub
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
def process_gateway_messages(gateway, client):
|
def process_gateway_messages(gateway, client):
|
||||||
@@ -40,14 +40,18 @@ def read_motion_data(gateway, client, polling_interval, polling_models):
|
|||||||
if (model not in polling_models):
|
if (model not in polling_models):
|
||||||
continue
|
continue
|
||||||
sid = device['sid']
|
sid = device['sid']
|
||||||
_LOGGER.debug("Polling " + str(model) + " with sid: " + str(sid))
|
|
||||||
|
|
||||||
sensor_resp = gateway.get_from_hub(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'])
|
data = json.loads(sensor_resp['data'])
|
||||||
state = data.get("status", None)
|
state = data.get("status", None)
|
||||||
short_id = sensor_resp['short_id']
|
short_id = sensor_resp['short_id']
|
||||||
if ( device['data'] != data or first):
|
if ( device['data'] != data or first):
|
||||||
device['data'] = data
|
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)
|
client.publish(model, sid, data)
|
||||||
first = False
|
first = False
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import socket
|
|||||||
import struct
|
import struct
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import sys
|
||||||
|
import select
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
@@ -111,9 +113,23 @@ class XiaomiHub:
|
|||||||
def _send_cmd(self, cmd, rtnCmd):
|
def _send_cmd(self, cmd, rtnCmd):
|
||||||
return self._send_socket(cmd, rtnCmd, self.GATEWAY_IP, self.GATEWAY_PORT)
|
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):
|
def _send_socket(self, cmd, rtnCmd, ip, port):
|
||||||
socket = self._socket
|
socket = self._socket
|
||||||
try:
|
try:
|
||||||
|
self._read_unwanted_data()
|
||||||
|
|
||||||
socket.settimeout(30.0)
|
socket.settimeout(30.0)
|
||||||
socket.sendto(cmd.encode(), (ip, port))
|
socket.sendto(cmd.encode(), (ip, port))
|
||||||
socket.settimeout(30.0)
|
socket.settimeout(30.0)
|
||||||
|
|||||||
Reference in New Issue
Block a user