-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_opc.py
256 lines (197 loc) · 7.12 KB
/
client_opc.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/python
# -*- coding: utf-8 -*-
import OpenOPC
from Tkinter import *
import sqlite3
import os
import MySQLdb
import mysql.connector
import time
def connect(serveurName, taglist):
"""
Connexion serveur OPC
serveurName: name of the OPC server
taglist: list with name of item
return list of item information
"""
try:
# Connexion serveur
opc = OpenOPC.client()
u= opc.connect(serveurName)
# Read item list
v = opc.read(taglist)
# Close socket
opc.close()
except:
print "Error - No connection to OPC server !!!"
return v
def printItems(taglist,liste):
"""
Extract values of all items
taglist: list with name of all items
liste: list of item value
return values of all items in a list
"""
v = connect('***',taglist)
# *** is the name of the OPC Server
# Stock item value in liste
for i in range(len(v)):
(name, val, qual, time) = v[i]
if val == '':
val = 0
liste.append(val)
return liste
def printItem(taglist,liste,position):
"""
Extract a value of an item
taglist: list with name of all items
liste: list of item value
position: position of the item in liste
return value of an item
"""
v = connect('***',taglist)
# Stockage des Items dans la liste
for i in range(len(v)):
(name, val, qual, time) = v[i]
if val == '':
val = 0
liste.append(val)
resultat = liste[position]
return resultat
def demandValue(taglist,liste,phrase1,mot):
"""
put values of all items in string to insert in database
taglist: list with name of all items
liste: list of item value
phrase1: string with values of all items
mot: value of an item
return a string with values of all items separated with ','
"""
for i in range(len(liste)):
mot = str(liste[i])
phrase1 += "'"
phrase1 += mot
phrase1 += "'"
if not i == len(liste)-1:
phrase1 += ','
return phrase1
def demandNameItem(listDb,phrase2,mot):
"""
put database name of all items in string to insert in database
listDb: list with datbase name of all items
phrase2: string with database name of all items
mot: database name of an item
return a string with database name of all items separated with ','
"""
for i in range(len(listDb)):
mot = str(listDb[i])
phrase2 += mot
if not i == len(listDb)-1:
phrase2 += ','
return phrase2
def preparRequete(phrase1,phrase2):
"""
put database request in a string
phrase1: string with values of all items
phrase1: string with database name of all items
return final request to insert values of all item in the database
"""
requete = ''
requeteDebut = 'INSERT INTO tablename('
requeteFin = ') VALUES('
requete = requeteDebut + phrase2 + requeteFin + phrase1
requete += ')'
return requete
print("")
print("------------- CLIENT OPC PYTHON -------------\n")
print("")
print("Menu:\n")
print("1. Start ")
print("2. Stop ")
print("")
# ... is the name of item
taglist =["....","...."]
# datebase name of item
listDb =["time"
,"..."
,"..."]
liste = []
phrase1 = ''
phrase2 = ''
mot = ''
i = 0
positionItem = ** # replace ** with a position
resultat = ''
Quit = 0
test_BDD = 0
while Quit != 1 :
try:
print "Menu:"
nb = input()
# to start program
if nb == 1:
print "Connexion to OPC server"
# Value of an item at time = t
resultat1 = printItem(taglist,liste,positionItem)
# infite loop to test if there a value of a item change
while 1:
test_BDD = 0
time.sleep(20) # Pause of 20 sec
temps = time.strftime("%H:%M:%S") # Real time
# Value of an item at time = t+1
resultat2 = printItem(taglist,liste,positionItem)
# if value change, it will save the new value in the database
if(resultat1 != resultat2):
print "Receive OPC data at ", temps
# Erase the list
phrase1 = ''
phrase2 = ''
del liste[:]
# Preparation of the database request
phrase1 = demandValue(taglist,liste,phrase1,mot)
phrase2 = demandNameItem(listDb,phrase2,mot)
query = preparRequete(phrase1,phrase2)
i = 0
# Try max 3 times to insert data in the database
while test_BDD != 1 and i <= 3:
if i == 3:
i = 3
print "Connexion to BDD impossible at ", temps, "\n"
test_BDD = 0
# print "Boucle 2 at ", temps
try:
if i <= 2:
print "Send data to database at ", temps
# Connexion to the database don't forget to enter personal information
cnx = mysql.connector.connect(user='...',
password='',
host='127.0.0.1',
database='...')
# Send data in database
cur = cnx.cursor()
cur.execute(query)
cnx.commit()
cnx.close()
# to exit the loop
test_BDD = 1
except:
time.sleep(5)
if i <= 2:
print " -------------------------- "
print " Error - connexion database"
print " -------------------------- "
i += 1
# Erase the list
phrase1 = ''
phrase2 = ''
del liste[:]
resultat1 = printItem(taglist,liste,positionRoll)
if i == 4:
break
resultat2 = ''
del liste[:]
elif nb == 2:
# to stop program
Quit = 1
except:
print "Error - Client is disconnected "