-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmwords_test.py
More file actions
265 lines (229 loc) · 7.6 KB
/
Copy pathmwords_test.py
File metadata and controls
265 lines (229 loc) · 7.6 KB
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
257
258
259
260
261
262
263
264
265
from flask import Flask, request
import os, webbrowser, threading, datetime, signal, json, psutil, re, glob, sys, win32event, win32api, winerror
from snap7 import Area
import snap7
from snap7.util import set_int,get_bool, set_bool, get_int
from apscheduler.schedulers.background import BackgroundScheduler
app = Flask(__name__)
scheduler = BackgroundScheduler()
scheduler.start()
# Establishing PLC Connection
client = snap7.client.Client()
try:
client.connect('192.168.0.1', 0, 1)
if client:
print("Connected ")
except Exception as e:
print("Error Occured Connecting to PLC: ", e)
read_type = []
color = "ON / OFF"
def execute_python_function():
print("Executed")
db_number = 0
db_byteindex = 0
db_bitindex = 0
data_address = {'db_number':1, 'db_byteindex':1, 'db_bitindex':1}
def read_memory_byte(plc, byte_index):
data = plc.read_area(Area.MK, 0, byte_index,2)
# bitvalue = get_bool(data, 0)
bytevalue= get_int(data, 0)
print(f"MW{byte_index} value: ", bytevalue)
return bytevalue
def write_memory_byte(plc, byte_index, value):
data = bytearray(2)
set_int(data, 0, value)
plc.write_area(Area.MK, 0, byte_index, data)
print(f"MW{byte_index} value set to: ", value)
return str(value)
def render_page(statusColor):
# print('The color is ', statusColor)
html_page = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PLC and Python Snap7 Demo</title>
<style>
body {{
font-family: Arial, sans-serif;
background-color: #f5f7fa;
margin: 0;
padding: 0;
}}
.navbar {{
background-color: #114b8b;
padding: 15px 30px;
color: white;
font-size: 20px;
text-align: left;
font-weight: bold;
position: fixed;
width: 2000px;
}}
.container {{
padding: 40px;
max-width: 1000px;
margin: auto;
}}
h1 {{
text-align: center;
color: #114b8b;
margin-bottom: 30px;
}}
.form-section, .status-section {{
background: white;
padding: 20px 30px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
margin-bottom: 20px;
}}
label {{
display: block;
margin-top: 10px;
}}
select, input[type="time"], input[type="number"], input[type="text"] {{
padding: 8px;
margin-top: 5px;
width: 100%;
max-width: 300px;
}}
input[type="text"] {{
padding: 8px;
margin-top: 5px;
width: 100%;
max-width: 700px;
}}
.form-grid {{
display: flex;
gap: 30px;
flex-wrap: wrap;
}}
.half {{
flex: 1;
min-width: 300px;
}}
button {{
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
border: none;
background-color: #007bff;
color: white;
border-radius: 5px;
cursor: pointer;
}}
.stop-btn {{
background-color: #d9534f;
}}
.file-upload {{
margin-top: 20px;
}}
.current-time {{
margin-top: 10px;
}}
.status-box {{
background-color: #e9f2ff;
padding: 15px;
border-radius: 5px;
}}
#logContent {{
white-space: pre-wrap;
background: #222;
color: #0f0;
padding: 15px;
border-radius: 5px;
max-height: 500px;
overflow-y: auto;
}}
</style>
</head>
<body>
<div class="navbar"> Python Dashboard for PLC</div>
<div class="container">
<h1>Read and Write PLC Memory Words from Website with Python</h1>
<div class="form-section">
<form method="POST" action="/trigger">
<div class="form-grid">
<div class="half">
<label for="db_byteindex">Memory Word Index </label><input id="db_byteindex" name="db_byteindex" value=1 required>
<label for="db_value">Memory Word Value </label><input id="db_value" name="db_value" value=1 required>
<button type="submit">Trigger</button>
</div>
</div>
</form>
</div>
<div class="form-section">
<form id="getstatus" method="POST" action="/get-status">
<div class="form-grid">
<div class="half">
<p><b>MW{data_address["db_byteindex"]}</b> Status: </p><h1 id="db_bit_status">{statusColor}</h1>
<button type="submit">Get Status</button>
</div>
</div>
</form>
</div>
<script>
function loadvalue(){{
value = document.getElementById('db_bit_status').textContent
console.log(value)
if (value=='OFF'){{
document.getElementById('db_bit_status').style.backgroundColor="Red";
console.log(value)
}}
if (value=='ON'){{
document.getElementById('db_bit_status').style.backgroundColor="Green";
}}
}}
function autosubmittingform(){{
document.getElementById('getstatus').submit()
}}
setInterval(loadvalue, 1000)
</script>
</body>
</html>
"""
return html_page
@app.route("/", methods=["GET"])
def home():
return render_page(statusColor=color)
@app.route("/status", methods=["POST"])
def schedule():
return render_page()
@app.route("/trigger", methods=["POST"])
def trigger():
data_address["db_byteindex"] = request.form.get("db_byteindex")
data_address["db_bitindex"] = request.form.get("db_bitindex")
db_value = request.form.get("db_value")
# print(db_number, db_byteindex, db_bitindex, db_value)
try:
write_memory_byte(plc=client, byte_index=int(data_address["db_byteindex"] ), value=int(db_value))
except Exception as e:
print("Error Occured while writing Values in DB: ", e)
return render_page(statusColor=color)
@app.route("/get-status", methods=["POST"])
def get_log():
try:
status = read_memory_byte(plc=client, byte_index=int(data_address["db_byteindex"]))
print("Current Value: ", status)
return render_page(statusColor=status)
except Exception as e:
error = "Error Occured while reading logs. Error: ", e
print(error)
# return status, 404, {'Content-Type': 'text/plain'}
return render_page(statusColor="")
@app.route("/shutdown")
def shutdown():
func = request.environ.get("werkzeug.server.shutdown")
if func is None:
os.kill(os.getpid(), signal.SIGINT)
# sys.exit(0)
else:
func()
return "Server shutting down..."
def open_browser():
webbrowser.open("http://127.0.0.1:5000")
if __name__ == "__main__":
threading.Timer(1.0, open_browser).start()
# if mac_address_check:
scheduler.add_job(get_log, 'interval', seconds=5)
app.run(debug=False)