-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataCollection.py
More file actions
85 lines (64 loc) · 2.42 KB
/
DataCollection.py
File metadata and controls
85 lines (64 loc) · 2.42 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
# Author: Chris Ward
# Date: 04/20/2015
# Version: 5/18/2015
# Description: Reads in data from our CR850 data logger and stores to files based on table name.
from datetime import datetime, timedelta
from pycampbellcr1000 import CR1000
from pycampbellcr1000 import utils
import platform
import os
# Check system platform, if windows, we need to open files in binary mode
platform = platform.system()
# Holds the device's mapped location
if platform == 'Linux':
location = "dev/ttyO4"
elif platform == 'Windows':
location = "COM1"
else:
location = "COM1"
# Holds the port on which we're communicating with the device
port = "115200"
# The device we're connecting to,
device = CR1000.from_url('serial:/' + location + ":" + port)
# Get all tables from device
tables = device.list_tables()
# Start date for data collection, should be fifteen minutes in the past
start_date_form = datetime.now() - timedelta(minutes=15)
# End date for data collection, should be now, to complete our 15 minute interval
end_date_form = datetime.now()
"""
" Function which takes in a table name, gathers its data and exports it as a CSV file for analysis.
" @:param table_name - name of table to collect data and export
"""
def collect_data(table_name):
exists = False
os.open('.filelock', os.O_WRONLY | os.O_CREAT)
if os.path.exists(table_name + '.csv'):
exists = True
if platform == 'Linux':
table_file = os.open(table_name + '.csv', os.O_WRONLY | os.O_APPEND | os.O_CREAT)
else:
table_file = os.open(table_name + '.csv', os.O_BINARY | os.O_WRONLY | os.O_APPEND | os.O_CREAT)
table_data = device.get_data(table_name, start_date_form, end_date_form)
if exists:
table_csv = utils.dict_to_csv(table_data, ",", header=False)
else:
table_csv = utils.dict_to_csv(table_data, ",", header=True)
os.write(table_file, table_csv.encode('UTF-8'))
os.close(table_file)
os.remove('.filelock')
"""
" "Main" function of the program. Creates a lock to enable prevention concurrent access when trying
" to upload data.
"""
def main():
# Create a lock file so that files can't be altered before we're finished.
os.open('.datalock', os.O_WRONLY | os.O_CREAT)
# Iterate through tables list, and collect data from each
for table in tables:
collect_data(table)
# Collection finished, remove lock
os.remove('.datalock')
return 0
# Call main, execute program
main()