-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient1.py
175 lines (141 loc) · 6.07 KB
/
Client1.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import socket
import time
import inspect
import sys
import datetime
import MySQLdb
CONSUMER_ID = 2000000000000
RETAILER_ID = 123456789012
host = '127.0.0.1'
port = 5560
use_weight = 0
weight_used = 0
weight_at_recharge = 0
retry_idx = 4
def lineno():
"""Returns the current line number in our program."""
return inspect.currentframe().f_back.f_lineno
valve_state = 0
#import RPi.GPIO as GPIO
#for i in range(0, 10):
#GPIO.setmode(GPIO.BCM)
#GPIO.setup(17, GPIO.OUT)
#GPIO.output(17, GPIO.LOW)
#time.sleep(0.5)
def send_command(iter):
#i = 0
#weight = i # function included in weight.py
#read_time = time.time()
#read_date = datetime.datetime.fromtimestamp(read_time).strftime('%Y/%m/%d')
#read_time = datetime.datetime.fromtimestamp(read_time).strftime('%H:%M:%S')
#command = "SEND " + str(read_date) + " " + str(read_time) + " " + str(weight)
#print(command)
#time.sleep(2)
#i += 1
# gas = read_gas()
# valve_state
# if gas > gas_threshold:
# valve_state = 0
# print("##....Gas Leak Detected....##")
# print("##....Gas Flow Stopped....##")
#
print("Iteration : " + str(iter))
global use_weight
global weight_at_recharge
i = 0
weight = i # function included in weight.py
read_time = time.time()
read_date = datetime.datetime.fromtimestamp(read_time).strftime('%Y/%m/%d')
read_time = datetime.datetime.fromtimestamp(read_time).strftime('%H:%M:%S')
command = "SEND " + str(CONSUMER_ID) + " " + str(read_date) + " " + str(read_time) + " " + str(weight)
print(command)
time.sleep(5)
i += 1
host="sql143.main-hosting.eu"
user="u402156879_lpg1"
passwd="!prajjwala2k18"
db="u402156879_lpg1"
#Connecting to database
print("\nGot the Credentials")
try:
print("Connecting to the Database")
db = MySQLdb.connect(host, user, passwd, db, connect_timeout = 2 )
print("##....Connected to the Database....##")
cur = db.cursor()
sql_query = "INSERT INTO `CONSUMER_REALTIME_GAS_USAGE`(`CONSUMER_ID`,`DATE`,`TIME`,`GAS_LEFT`) VALUES('%s','%s','%s','%s')" %(CONSUMER_ID, read_date, read_time, weight)
try:
print("##....Executing the commit....##")
cur.execute(sql_query)
print("##....Commiting the Query....##\n")
db.commit()
reply = "Query Executed Succesfully"
except Exception as e:
print(e)
reply = "Error1 " + str(e) + "##....Error Executing the Command....##\n"
db.rollback()
if(use_weight == 0 and weight_used <= 0):
sql_query1 = "SELECT * FROM CONSUMER_GAS_USAGE WHERE CONSUMER_ID = '%i' AND STATUS = '%i'" %(CONSUMER_ID, 1)
try:
cur.execute(sql_query1)
results = cur.fetchall()
for row in results:
print("##....Query Executed....##")
print("##....Fetching Data....##")
RECHARGE_DATE = row[1]
RECHARGE_TIME = row[2]
RECHARGE_AMOUNT = row[3]
RECHARGE_GAS_EQUIVALENT = row[4]
use_weight = RECHARGE_GAS_EQUIVALENT
# Now print fetched result
print("RECHARGE_DATE='%s'\nRECHARGE_TIME='%s'\nRECHARGE_AMOUNT='%s'\nRECHARGE_GAS_EQUIVALENT='%s'" %(RECHARGE_DATE,RECHARGE_TIME,RECHARGE_AMOUNT,RECHARGE_GAS_EQUIVALENT))
update_query = "UPDATE CONSUMER_GAS_USAGE SET STATUS = '%i' WHERE CONSUMER_ID = '%s' AND RECHARGE_TIME = '%s'" %(0,CONSUMER_ID, RECHARGE_TIME)
print(update_query)
cur.execute(update_query)
db.commit()
print("\n##....Status Updated....##\n")
if(use_weight > 0):
#weight_at_recharge = read_weight()
print("\nSystem Recharged for Rs. " + str(use_weight) + "\n")
break;
except Exception as e:
print(e)
reply = "Error1 " + str(e) + "##....Error Executing the Command....##\n"
db.rollback()
db.close()
except Exception as e:
print(e)
reply = "Error1 " + str(e) + "We got some Error Connecting to database. Please Try again"
for i in range(1000):
# gas = read_gas()
# valve_state
# if gas > gas_threshold:
# valve_state = 0
# print("##....Gas Leak Detected....##")
# print("##....Gas Flow Stopped....##")
# else:
print("Use Weight : " + str(use_weight) + " g")
#current_weight = read_weight()
#weight_used = weight_at_recharge - current_weight
#if(use_weight > 0 and weight_used < use_weight):
# use_weight = use_weight - weight_used
# #######..........SHUT ON VALVE...........######
# GPIO.output(relay_pin,GPIO.HIGH)
# valve_state = 1:
#if(use_weight > 0 and weight_used <= use_weight):
# use_weight = 0
# #######..........SHUT OFF VALVE...........######
# GPIO.output(relay_pin, GPIO.LOW)
# valve_state = 0:
#if(use_weight == 0):
# #######..........SHUT OFF VALVE...........######
# GPIO.output(relay_pin,GPIO.LOW)
# valve_state = 0:
send_command(i)
#send_command(i,current_weight)
time.sleep(10)
# if valve_state = 0:
#### "Switch off Valve" ####
# GPIO.output(relay_pin,GPIO.LOW)
# else:
#### "Switch on Valve" ####
# GPIO.output(relay_pin,GPIO.HIGH)