-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestFileGenerator.py
79 lines (62 loc) · 2.04 KB
/
TestFileGenerator.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
#!/usr/bin/env python3
"""
Test File Generator
"""
from datetime import datetime
from datetime import timedelta
import time
import Standard_Settings as SS
import json
import random
def write_data_to_file(data_to_write):
"""
Takes the given data and creates a new file with the contents of it
Format of the filename is based on standard settings
RECORDFILE_NAME+tiemstamp+RECORDFILE_EXT
Stored in sub folder
RECORDFILE_LOCATION
Returns true if the record is written, or false if it isn't
Disk space management is handled by the calling program
"""
status = False
file_time = datetime.now().strftime("%y%m%d%H%M%S-%f")
data_record_name = SS.RECORDFILE_LOCATION + '/' + SS.RECORDFILE_NAME + file_time + SS.RECORDFILE_EXT
#TODO: Need to handle a failure to open the file
with open(data_record_name, mode='w') as f:
json.dump(data_to_write, f)
status = True
return status
def generate_timestamp():
"""
Generate a timestamp of the correct format
"""
now = str(datetime.now())
return str(now[:23])
def generate_data():
"""
Generate the data required and return it
[[type, value, units, timestamp],[type, value, units, timestamp]]
"""
sensor_type = 1
value = random.randint(0,100)
units = 'Lux'
timestamp = generate_timestamp()
return [[sensor_type, value, units, timestamp]]
def main():
"""
Generate data and write it
"""
while True:
#Repeatably write a file
status = False
file_time = datetime.now().strftime("%y%m%d%H%M%S-%f")
#TODO: If the datafile directoryu doesn't exist, an error is thrown!
data_record_name = SS.RECORDFILE_LOCATION + '/' + SS.RECORDFILE_NAME + file_time + SS.RECORDFILE_EXT
#TODO: Need to handle a failure to open the file
with open(data_record_name, mode='w') as f:
json.dump(generate_data(), f)
status = True
time.sleep(random.randint(0,100) /10)
return
if __name__ == "__main__":
main()