forked from raghavendra-nataraj/ParkItEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
97 lines (69 loc) · 2.69 KB
/
test.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
__author__ = 'Shwetha'
from cloudant.client import Cloudant
import sys
import time
import serial
import re
ser = serial.Serial('/dev/ttyACM0', 9600)
masterdict={}
def commonCreation(key):
return masterdict[key]
client = Cloudant('06867603-146b-43d1-b8d9-454f272fdc02-bluemix','cacca3cc252cb10645410c6e42a7ebb0c4a6382bb0dfa940b029d1de3285fa64',url='https://06867603-146b-43d1-b8d9-454f272fdc02-bluemix:cacca3cc252cb10645410c6e42a7ebb0c4a6382bb0dfa940b029d1de3285fa64@06867603-146b-43d1-b8d9-454f272fdc02-bluemix.cloudant.com')
# Connect to the server
client.connect()
# Perform client tasks...
session = client.session()
def update(key):
if 'parking' in client.all_dbs():
my_database = client['parking']
selector = {'zipcode': zipcode,'pno':rpino,'_id':id}
docs = my_database.get_query_result(selector,raw_result=True, limit=100)
# print len(docs['docs'])
if len(docs['docs']) == 0:
print "no docs"
data = commonCreation(key)
# Create a document using the Database API
my_document = my_database.create_document(data)
else:
for doc in docs['docs']:
# print "insidd docs"
my_doc=my_database[doc['_id']]
print my_doc['status'], statuss
if my_doc['status'] != statuss:
my_doc['status'] = statuss
my_doc.save()
else:
print "there"
my_database = client.create_database('parking')
if my_database.exists():
print 'Table creation SUCCESS!!'
# Create document content data
data = commonCreation(key)
# Create a document using the Database API
my_document = my_database.create_document(data)
# def checkForUpdate:
if 'parking' in client.all_dbs():
#Reading data into local dictionary
my_daata = client['parking']
for doc in my_daata:
pin= str(doc['zipcode']) + str(doc['pno']) + str(doc['_id'])
masterdict[pin] = doc['status']
while True:
serInp = ser.readline()
serInp = serInp.strip()
print serInp
inps = serInp.split("_")
if len(inps) == 4:
id = inps[2]
zipcode = inps[3]
rpino = inps[1]
statuss = inps[0]
key = id + zipcode + rpino
if key in masterdict:
if masterdict[key]['status'] != statuss:
masterdict[key]['status'] = statuss
update(key);
else:
masterdict[key] = {'_id':id,'zipcode':zipcode,'pno':rpino,'status':statuss}
update(key);
client.disconnect()