forked from benvandoren/utility-mon
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutility_meters.py
executable file
·124 lines (108 loc) · 5.85 KB
/
utility_meters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3.4
import pymysql
import time
import datetime
# object based on meter id
class UtilityMeter:
def __init__(self, mId, mType, mEpoch, mConsumption, dbCur):
self.mId = mId
self.mType = mType
self.mEpoch = mEpoch
self.mConsumption = mConsumption
#
self.time1 = mEpoch
self.consumption1 = mConsumption
self.time2 = mEpoch
self.consumption2 = mConsumption
#
self.dbCur = dbCur
class ElectricMeter(UtilityMeter):
def getCurrentWatts(self, currTime, currConsumption):
#time
self.time = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
self.watts = 0
self.time2 = currTime
self.consumption2 = currConsumption
self.timeDiff = self.time2 - self.time1
if(self.timeDiff < 0):
print("Error: Time Diff Negative. Customer: %s. %d - %d = %d" % (self.mId, self.time2, self.time1, self.timeDiff))
# min 5min granularity
if(self.timeDiff >= 300):
# figure out the power used in this time
self.powerDiff = self.consumption2 - self.consumption1
# if the power hasn't incremented then do nothing
if(self.powerDiff != 0):
# reset time1 and consumption1
self.time1 = currTime
self.consumption1 = currConsumption
# convert power diff from kwh to kws
self.watts = (self.powerDiff * 3600 /self.timeDiff)
# if numbers are way out of range throw error
if(self.watts > 10000 or self.watts < -10000):
print("Calculated use out of range! Got:")
print("[%s] Customer %s Using %f watts. %d Wh / %d s" % (self.time, self.mId, self.watts, self.powerDiff, self.timeDiff))
return -1
print("[%s] Customer %s Using %f watts. %d Wh / %d s" % (self.time, self.mId, self.watts, self.powerDiff, self.timeDiff))
# write to db
self.dbCur.execute("insert into UtilityMeter(mId, mType, mTime, mTotalConsumption, mConsumed) values (%s, %d, %d, %d, %f)" % (self.mId, int(self.mType), int(currTime), int(currConsumption), self.watts))
return self.watts
class GasMeter(UtilityMeter):
# cubic feet / sec ??
def getGasPerSec(self, currTime, currConsumption):
#time
self.time = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
self.gasPerSec = 0
self.time2 = currTime
self.consumption2 = currConsumption
self.timeDiff = self.time2 - self.time1
if(self.timeDiff < 0):
print("Error: Time Diff Negative. Customer: %s. %d - %d = %d" % (self.mId, self.time2, self.time1, self.timeDiff))
# min 5min granularity
if(self.timeDiff >= 300):
# calculate gas / sec
self.gasDiff = self.consumption2 - self.consumption1
# if it hasn't changed do nothing
if(self.gasDiff != 0):
# reset time1 and consumption1
self.time1 = currTime
self.consumption1 = currConsumption
self.gasPerSec = self.gasDiff / self.timeDiff
# if numbers are way out of range throw error
if(self.gasPerSec > 10000 or self.gasPerSec < -10000):
print("Calculated use out of range! Got:")
print("[%s] Customer %s Using %f cubic feet / sec. %d / %d s" % (self.time, self.mId, self.gasPerSec, self.gasDiff, self.timeDiff))
return -1
print("[%s] Customer %s Using %f cubic feet / sec. %d / %d s" % (self.time, self.mId, self.gasPerSec, self.gasDiff, self.timeDiff))
# write to db
self.dbCur.execute("insert into UtilityMeter(mId, mType, mTime, mTotalConsumption, mConsumed) values (%s, %d, %d, %d, %f)" % (int(self.mId), int(self.mType), int(currTime), int(currConsumption), self.gasPerSec))
return self.gasPerSec
class WaterMeter(UtilityMeter):
# cubic feet / sec ??
def getWaterPerSec(self, currTime, currConsumption):
#time
self.time = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
self.waterPerSec = 0
self.time2 = currTime
self.consumption2 = currConsumption
self.timeDiff = self.time2 - self.time1
if(self.timeDiff < 0):
print("Error: Time Diff Negative. Customer: %s. %d - %d = %d" % (self.mId, self.time2, self.time1, self.timeDiff))
# min 5min granularity
if(self.timeDiff >= 300):
# calculate water / sec
self.waterDiff = self.consumption2 - self.consumption1
# if it hasn't changed do nothing
if(self.waterDiff != 0):
# reset time1 and consumption1
self.time1 = currTime
self.consumption1 = currConsumption
self.waterPerSec = self.waterDiff / self.timeDiff
# if numbers are way out of range throw error
if(self.waterPerSec > 10000 or self.waterPerSec < -10000):
print("Calculated use out of range! Got:")
print("[%s] Customer %s Using %f cubic feet / sec. %d / %d s" % (self.time, self.mId, self.waterPerSec, self.waterDiff, self.timeDiff))
return -1
print("[%s] Customer %s Using %f cubic feet / sec. %d / %d s" % (self.time, self.mId, self.waterPerSec, self.waterDiff, self.timeDiff))
# write to db
self.dbCur.execute("insert into UtilityMeter(mId, mType, mTime, mTotalConsumption, mConsumed) values (%s, %d, %d, %d, %f)" % (int(self.mId), int(self.mType), int(currTime), int(currConsumption), self.waterPerSec))
return self.waterPerSec