-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
230 lines (197 loc) · 8.82 KB
/
logger.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
# imports
from pylogix import PLC
import argparse
import json
import time
from datetime import datetime
from time import localtime, strftime
import csv
# construct argument parser, parse arguments
ap = argparse.ArgumentParser()
ap.add_argument("-c", "--conf", required=True, help="path to JSON config file")
ap.add_argument("-f", "--file", required=True, help="path to output CSV file")
args = vars(ap.parse_args())
# load configuration
cfg = json.load(open(args["conf"]))
# get output filename
try:
c_output_filename = args["file"]
if c_output_filename[-3:] != 'csv':
raise
except:
print("output file must be csv - exiting...")
exit()
# validate config
try:
c_ip = cfg["ip"]
c_tags = cfg["tags"]
c_headers = cfg["headers"]
c_trigger_type = cfg["trigger_type"]
c_trigger_tag = cfg["trigger_tag"]
c_period_time = cfg["period_time"]
c_compare_condition = cfg["compare_condition"]
c_compare_cutoff = cfg["compare_cutoff"]
c_print_timestamp = cfg["print_timestamp"]
c_live_update = cfg["live_update"]
if not(isinstance(c_ip, str)) or not(isinstance(c_period_time, (int, float))) or not(isinstance(c_print_timestamp, int)) or not(isinstance(c_compare_condition, str)) or not(isinstance(c_compare_cutoff, (int, float))):
raise
except:
print("invalid config - exiting...")
exit()
# custom exception for tag data not present
class TagError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return self.value
# utility function - return timestamp string
def GetTimestamp():
return (datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
# utility function - parse string from PLC string tag
def ReadString(tag, comm):
str_len = comm.Read(f'{tag}.LEN').Value
value = ''
if(str_len) > 0:
data = comm.Read(f'{tag}.DATA[0]', int(str_len)).Value
value = ''.join([chr(d) for d in data])
return value
# utility function - get row of data from PLC
def GetData(comm):
results = []
if(c_print_timestamp):
results.append(GetTimestamp())
tagdata = comm.Read(c_tags)
for t in range(len(c_tags)):
if tagdata[t].Status != 'Success':
raise TagError(f"failed to read tag: {c_tags[t]}")
if isinstance(tagdata[t].Value, (bytes, bytearray, str)):
results.append(ReadString(c_tags[t], comm))
else:
results.append(tagdata[t].Value)
return results
# utility function - update CSV by appending provided row. Only used with real-time updates; slower performance
def LiveUpdate(row, filename):
with open(filename, 'a', newline='') as csv_file:
realtime_writer = csv.writer(csv_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
realtime_writer.writerow(row)
# main function - open comms to PLC, record data until exit requested or error encountered
try:
if not c_live_update:
csv_file = open(c_output_filename, 'a', newline='')
writer = csv.writer(csv_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
with PLC(c_ip) as comm:
if c_print_timestamp:
c_headers = ['Timestamp'] + c_headers
# start csv with header row
if c_live_update:
LiveUpdate(c_headers, c_output_filename)
else:
writer.writerow(c_headers)
# define behavior based on trigger type chosen
match c_trigger_type:
# periodic trigger:
case "periodic":
print("starting with periodic trigger - press ctrl + c to quit logging")
previous_time = 0
while True:
current_time = time.time()
if current_time - previous_time > c_period_time:
data = GetData(comm)
if c_live_update:
LiveUpdate(data, c_output_filename)
else:
writer.writerow(data)
print('.', end='', flush=True)
previous_time = current_time
# change trigger - update whenever trigger tag is changed
case "change":
print("starting with change trigger - press ctrl + c to quit logging")
previous_triggerdata = comm.Read(c_trigger_tag)
if previous_triggerdata.Status != 'Success':
raise TagError(f"failed to read tag: {c_trigger_tag}")
previous_val = previous_triggerdata.Value
while True:
current_triggerdata = comm.Read(c_trigger_tag)
if current_triggerdata.Status != 'Success':
raise TagError(f"failed to read tag: {c_trigger_tag}")
current_val = current_triggerdata.Value
if current_val != previous_val:
data = GetData(comm)
if c_live_update:
LiveUpdate(data, c_output_filename)
else:
writer.writerow(data)
print('.', end='', flush=True)
previous_val = current_val
# compare trigger - update whenever trigger tag fulfills specified conditions
case "compare":
print("starting with comparison trigger - press ctrl + c to quit logging")
previous_triggerdata = comm.Read(c_trigger_tag)
if previous_triggerdata.Status != 'Success':
raise TagError(f"failed to read tag: {c_trigger_tag}")
while True:
current_triggerdata = comm.Read(c_trigger_tag).Value
match c_compare_condition:
case "grt":
if current_triggerdata > c_compare_cutoff:
data = GetData(comm)
if c_live_update:
LiveUpdate(data, c_output_filename)
else:
writer.writerow(data)
print('.', end='', flush=True)
case "geq":
if current_triggerdata >= c_compare_cutoff:
data = GetData(comm)
if c_live_update:
LiveUpdate(data, c_output_filename)
else:
writer.writerow(data)
print('.', end='', flush=True)
case "les":
if current_triggerdata < c_compare_cutoff:
data = GetData(comm)
if c_live_update:
LiveUpdate(data, c_output_filename)
else:
writer.writerow(data)
print('.', end='', flush=True)
case "leq":
if current_triggerdata <= c_compare_cutoff:
data = GetData(comm)
if c_live_update:
LiveUpdate(data, c_output_filename)
else:
writer.writerow(data)
print('.', end='', flush=True)
case "neq":
if current_triggerdata != c_compare_cutoff:
data = GetData(comm)
if c_live_update:
LiveUpdate(data, c_output_filename)
else:
writer.writerow(data)
print('.', end='', flush=True)
case "equ":
if current_triggerdata == c_compare_cutoff:
data = GetData(comm)
if c_live_update:
LiveUpdate(data, c_output_filename)
else:
writer.writerow(data)
print('.', end='', flush=True)
case _:
print("unknown comparison condition")
break
# unknown trigger
case _:
print("unknown trigger type")
print('\nexiting...')
except KeyboardInterrupt:
print("\nexiting...")
except Exception as e:
print(f"\nerror encountered: {type(e).__name__} - {e}. exiting...")
# cleanup
print(f"output stored here: {c_output_filename}")
if not c_live_update:
csv_file.close()