Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added skipidentical support for ATC mode #74

Merged
merged 4 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
venv
.vscode
.vscode
__pycache__/
*.py[cod]
29 changes: 16 additions & 13 deletions LYWSD03MMC.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ class Measurement:
sensorname: str = ""
rssi: int = 0

def __eq__(self, other): #rssi may be different
if self.temperature == other.temperature and self.humidity == other.humidity and self.calibratedHumidity == other.calibratedHumidity and self.battery == other.battery and self.voltage == other.voltage and self.sensorname == other.sensorname:
return True
def __eq__(self, other): #rssi may be different, so exclude it from comparison
if self.temperature == other.temperature and self.humidity == other.humidity and self.calibratedHumidity == other.calibratedHumidity and self.battery == other.battery and self.sensorname == other.sensorname:
#in atc mode also exclude voltage as it changes often due to frequent measurements
return True if args.atc else (self.voltage == other.voltage)
else:
return False

measurements=deque()
#globalBatteryLevel=0
previousMeasurement=Measurement(0,0,0,0,0,0,0,0)
identicalCounter=0
previousMeasurements={}
identicalCounters={}

def signal_handler(sig, frame):
if args.atc:
Expand Down Expand Up @@ -66,17 +67,18 @@ def watchDog_Thread():


def thread_SendingData():
global previousMeasurement
global previousMeasurements
global measurements
path = os.path.dirname(os.path.abspath(__file__))
while True:
try:
mea = measurements.popleft()
if (mea == previousMeasurement and identicalCounter < args.skipidentical): #only send data when it has changed or X identical data has been skipped, ~10 pakets per minute, 50 pakets --> writing at least every 5 minutes
print("Measurements are identical don't send data\n")
identicalCounter+=1
continue
identicalCounter=0
if mea.sensorname in previousMeasurements:
prev = previousMeasurements[mea.sensorname]
if (mea == prev and identicalCounters[mea.sensorname] < args.skipidentical): #only send data when it has changed or X identical data has been skipped, ~10 packets per minute, 50 packets --> writing at least every 5 minutes
print("Measurements for " + mea.sensorname + " are identical; don't send data\n")
identicalCounters[mea.sensorname]+=1
continue
fmt = "sensorname,temperature,humidity,voltage" #don't try to seperate by semicolon ';' os.system will use that as command seperator
if ' ' in mea.sensorname:
sensorname = '"' + mea.sensorname + '"'
Expand All @@ -102,10 +104,11 @@ def thread_SendingData():
print ("Data couln't be send to Callback, retrying...")
time.sleep(5) #wait before trying again
else: #data was sent
previousMeasurement=Measurement(mea.temperature,mea.humidity,mea.voltage,mea.calibratedHumidity,mea.battery,0) #using copy or deepcopy requires implementation in the class definition
previousMeasurements[mea.sensorname]=Measurement(mea.temperature,mea.humidity,mea.voltage,mea.calibratedHumidity,mea.battery,mea.timestamp,mea.sensorname) #using copy or deepcopy requires implementation in the class definition
identicalCounters[mea.sensorname]=0

except IndexError:
#print("Keine Daten")
#print("No Data")
time.sleep(1)
except Exception as e:
print(e)
Expand Down