forked from bearing/dosenet-raspberrypi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_handler_d3s.py
175 lines (156 loc) · 6.11 KB
/
data_handler_d3s.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
from auxiliaries import datetime_from_epoch
from auxiliaries import set_verbosity
from globalvalues import ANSI_RESET, ANSI_YEL, ANSI_GR, ANSI_RED
from globalvalues import DEFAULT_DATA_BACKLOG_FILE_D3S
from collections import deque
import socket
import time
import ast
import os
import errno
import csv
SPECTRA_DISPLAY_TEXT = (
'{{time}}: {yellow} {{total_counts}} {reset}' +
'{green} total counts from {reset}' +
' ({{start_time}} to {{end_time}})').format(
yellow=ANSI_YEL, reset=ANSI_RESET, green=ANSI_GR)
strf = '%H:%M:%S'
class Data_Handler_D3S(object):
def __init__(self,
manager=None,
verbosity=1,
logfile=None,
):
self.v = verbosity
if manager and logfile is None:
set_verbosity(self, logfile=manager.logfile)
else:
set_verbosity(self, logfile=logfile)
self.manager = manager
self.queue = deque('')
def test_send(self, spectra):
"""
Test Mode
"""
self.vprint(
1, ANSI_RED + " * Test mode, not sending to server * " +
ANSI_RESET)
def no_config_send(self, spectra):
"""
Configuration file not present
"""
self.vprint(1, "Missing config file, not sending to server")
def no_publickey_send(self, spectra):
"""
Publickey not present
"""
self.vprint(1, "Missing public key, not sending to server")
def send_to_memory(self, spectra):
"""
Network is not up
"""
self.send_to_queue(spectra)
self.vprint(1, "Network down, saving to queue in memory")
def regular_send(self, this_end, spectra):
"""
Normal send
"""
self.manager.sender.send_spectra_new_D3S(this_end, spectra)
#print(self.queue)
if self.queue:
self.vprint(1, "Flushing memory queue to server")
while self.queue:
#print(len(self.queue))
trash = self.queue.popleft()
self.manager.sender.send_spectra_new_D3S(
trash[0], trash[1])
def send_all_to_backlog(self, path=DEFAULT_DATA_BACKLOG_FILE_D3S):
if self.queue:
self.vprint(1, "Flushing memory queue to backlog file")
temp = []
while self.queue:
temp.append(self.queue.popleft())
with open(path, "ab") as f: # might only work for python 3?
writer = csv.writer(f)
writer.writerows(temp)
def send_to_queue(self, spectra):
"""
Adds the time and spectra to the deque object.
"""
time_string = time.time()
self.queue.append([time_string, spectra])
def backlog_to_queue(self, path=DEFAULT_DATA_BACKLOG_FILE_D3S):
"""
Sends data in backlog to queue and deletes the backlog
"""
if os.path.isfile(path):
self.vprint(2, "Flushing backlog file to memory queue")
with open(path, 'rb') as f:
reader = csv.reader(f)
lst = list(reader)
for i in lst:
#print(i)
timestring = i[0]
spectra = i[1]
timestring = ast.literal_eval(timestring)
spectra = ast.literal_eval(spectra)
self.queue.append([timestring, spectra])
os.remove(path)
def main(self, datalog, calibrationlog, spectra, this_start, this_end):
"""
Determines how to handle the spectra data.
"""
start_text = datetime_from_epoch(this_start).strftime(strf)
end_text = datetime_from_epoch(this_end).strftime(strf)
self.vprint(
1, SPECTRA_DISPLAY_TEXT.format(
time=datetime_from_epoch(time.time()),
total_counts=sum(spectra),
start_time=start_text,
end_time=end_text))
self.manager.data_log(datalog, spectra)
self.manager.calibration_log(calibrationlog, spectra)
if self.manager.test:
# for testing the memory queue
self.send_to_memory(spectra)
elif not self.manager.config:
self.no_config_send(spectra)
elif not self.manager.publickey:
self.no_publickey_send(spectra)
else:
try:
self.regular_send(this_end, spectra)
except (socket.gaierror, socket.error, socket.timeout) as e:
if e == socket.gaierror:
if e[0] == socket.EAI_AGAIN:
# TCP and UDP
# network is down, but NetworkStatus didn't notice yet
# (resolving DNS like dosenet.dhcp.lbl.gov)
self.vprint(
1,
'Failed to send packet! Address resolution error')
else:
self.vprint(
1, 'Failed to send packet! Address error: ' +
'{}: {}'.format(*e))
elif e == socket.error:
if e[0] == errno.ECONNREFUSED:
# TCP
# server is not accepting connections
self.vprint(
1, 'Failed to send packet! Connection refused')
elif e[0] == errno.ENETUNREACH:
# TCP and UDP
# network is down, but NetworkStatus didn't notice yet
# (IP like 131.243.51.241)
self.vprint(
1, 'Failed to send packet! Network is unreachable')
else:
# consider handling errno.ECONNABORTED errno.ECONNRESET
self.vprint(
1, 'Failed to send packet! Socket error: ' +
'{}: {}'.format(*e))
elif e == socket.timeout:
# TCP
self.vprint(1, 'Failed to send packet! Socket timeout')
self.send_to_memory(spectra)