Skip to content

Commit

Permalink
Merge pull request #47 from tybtab/memory-#13
Browse files Browse the repository at this point in the history
Closes #13, #42, #43
  • Loading branch information
bplimley authored Jun 28, 2016
2 parents 12cc6a9 + fa2b0d5 commit db9d357
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 53 deletions.
23 changes: 18 additions & 5 deletions auxiliaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from time import sleep
import os
import traceback
import time

from globalvalues import RPI
if RPI:
Expand Down Expand Up @@ -214,7 +215,8 @@ def __init__(self,
self.down_interval_s = down_interval_s
self.led = network_led
self.blink_period_s = 1.5

self.last_try_time = None

self.logfile = logfile
self.v = verbosity
set_verbosity(self, logfile=logfile)
Expand Down Expand Up @@ -251,12 +253,15 @@ def update(self, up_state=None):
up_state is the shared memory object for the pinging process.
If calling update() manually, leave it as None (default).
"""

if not self.last_try_time:
self.last_try_time = time.time()

if up_state is None:
up_state = self.up_state

response = self._ping()
if response == 0:
self.last_try_time = time.time()
up_state.value = 'U'
if self.led:
if self.led.blinker:
Expand All @@ -265,10 +270,16 @@ def update(self, up_state=None):
self.vprint(2, ' {} is UP'.format(self.hostname))
else:
up_state.value = 'D'
self.vprint(1, ' {} is DOWN!'.format(self.hostname))
self.vprint(3, 'Network down for {} seconds'.format(time.time() - self.last_try_time))
if self.led:
self.led.start_blink(interval=self.blink_period_s)
self.vprint(1, ' {} is DOWN!'.format(self.hostname))

if time.time() - self.last_try_time >= 1800:
self.vprint(1, 'Making network go back up')
os.system("sudo ifdown wlan1")
os.system("sudo ifup wlan1")
self.last_try_time = time.time()

def _do_pings(self, up_state):
"""Runs forever - only call as a subprocess"""
try:
Expand Down Expand Up @@ -305,7 +316,9 @@ def __nonzero__(self):

def cleanup(self):
GPIO.cleanup()

if self._p:
self._p.terminate()
self.pinging = False

class Config(object):
"""
Expand Down
155 changes: 155 additions & 0 deletions data_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
from auxiliaries import datetime_from_epoch
from sender import ServerSender
from sensor import Sensor
from auxiliaries import set_verbosity
from globalvalues import ANSI_RESET, ANSI_YEL, ANSI_GR, ANSI_RED
from globalvalues import DEFAULT_DATA_BACKLOG_FILE
from collections import deque
import socket
import time
import ast
import os

CPM_DISPLAY_TEXT = (
'{{time}}: {yellow} {{counts}} cts{reset}' +
' --- {green}{{cpm:.2f}} +/- {{cpm_err:.2f}} cpm{reset}' +
' ({{start_time}} to {{end_time}})').format(
yellow=ANSI_YEL, reset=ANSI_RESET, green=ANSI_GR)
strf = '%H:%M:%S'

class Data_Handler(object):
"""
Object for sending data to server.
Also handles writing to datalog and
storing to memory.
"""

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, cpm, cpm_err):
"""
Test Mode
"""
self.vprint(
1, ANSI_RED + " * Test mode, not sending to server * " +
ANSI_RESET)

def no_config_send(self, cpm, cpm_err):
"""
Configuration file not present
"""
self.vprint(1, "Missing config file, not sending to server")

def no_publickey_send(self, cpm, cpm_err):
"""
Publickey not present
"""
self.vprint(1, "Missing public key, not sending to server")

def no_network_send(self, cpm, cpm_err):
"""
Network is not up
"""
if self.manager.protocol == 'new':
self.send_to_queue(cpm, cpm_err)
self.vprint(1, "Network down, saving to queue in memory")
else:
self.vprint(1, "Network down, not sending to server")

def regular_send(self, this_end, cpm, cpm_err):
"""
Normal send
"""
try:
if self.manager.protocol == 'new':
self.manager.sender.send_cpm_new(this_end, cpm, cpm_err)
if self.queue:
self.vprint(1, "Flushing memory queue to server")
while self.queue:
trash = self.queue.popleft()
self.manager.sender.send_cpm_new(trash[0], trash[1], trash[2])
else:
self.manager.sender.send_cpm(cpm, cpm_err)
except socket.error:
if self.manager.protocol == 'new':
self.send_to_queue(cpm, cpm_err)
self.vprint(1, "Socket error: saving to queue in memory")
else:
self.vprint(1, "Socket error: data not sent")

def send_all_to_backlog(self, path=DEFAULT_DATA_BACKLOG_FILE):
if self.manager.protocol == 'new':
if self.queue:
self.vprint(1, "Flushing memory queue to backlog file")
with open(path, 'a') as f:
while self.queue:
f.write('{0}, '.format(self.queue.popleft()))

def send_to_queue(self, cpm, cpm_err):
"""
Adds the time, cpm, and cpm_err to the deque object.
"""
if self.manager.protocol == 'new':
time_string = time.time()
self.queue.append([time_string, cpm, cpm_err])

def backlog_to_queue(self, path=DEFAULT_DATA_BACKLOG_FILE):
"""
Sends data in backlog to queue and deletes the backlog
"""
if self.manager.protocol == 'new':
if os.path.isfile(path):
self.vprint(2, "Flushing backlog file to memory queue")
with open(path, 'r') as f:
data = f.read()
data = ast.literal_eval(data)
for i in data:
self.queue.append([i[0], i[1], i[2]])
print(self.queue)
os.remove(path)

def main(self, datalog, cpm, cpm_err, this_start, this_end, counts):
"""
Determines how to handle the cpm data.
"""
start_text = datetime_from_epoch(this_start).strftime(strf)
end_text = datetime_from_epoch(this_end).strftime(strf)

self.vprint(1, CPM_DISPLAY_TEXT.format(
time=datetime_from_epoch(time.time()),
counts=counts,
cpm=cpm,
cpm_err=cpm_err,
start_time=start_text,
end_time=end_text,
))

self.manager.data_log(datalog, cpm, cpm_err)

if self.manager.test:
self.test_send(cpm, cpm_err)
elif not self.manager.config:
self.no_config_send(cpm, cpm_err)
elif not self.manager.publickey:
self.no_publickey_send(cpm, cpm_err)
elif not self.manager.network_up:
self.no_network_send(cpm, cpm_err)
else:
self.regular_send(this_end, cpm, cpm_err)


7 changes: 6 additions & 1 deletion dosenet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# setup paths and check config files
HOME=/home/pi
DOSENET=$HOME/dosenet-raspberrypi
DOSENET=$HOME/dev/memory/dosenet-raspberrypi
CONFIGDIR=$HOME/config
LOGTAG=dosenet

Expand All @@ -22,6 +22,8 @@ PUBLICKEY=$CONFIGDIR/id_rsa_lbl.pub

case "$1" in
start)
logger --stderr --id --tag $LOGTAG "Waiting for NTP to be synced..."
ntp-wait -n 10 -s 30
logger --stderr --id --tag $LOGTAG "Starting DoseNet script"
# -dm runs screen in background. doesn't work without it on Raspbian Jesse.
sudo screen -dm python $DOSENET/manager.py
Expand All @@ -30,6 +32,9 @@ case "$1" in
logger --stderr --id --tag $LOGTAG "Stopping DoseNet script"
sudo killall python &
;;
finish)
sudo kill -s SIGQUIT
;;
test)
logger --stderr --id --tag $LOGTAG "Testing DoseNet script"
# allow testing without configfile and/or publickey
Expand Down
2 changes: 2 additions & 0 deletions globalvalues.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
DEFAULT_TCP_PORT = 5100
DEFAULT_SENDER_MODE = 'udp'
DEFAULT_DATALOG = '/home/pi/data-log.txt'
DEFAULT_DATA_BACKLOG_FILE = '/home/pi/data_backlog_file.txt'
DEFAULT_PROTOCOL = 'old'

DEFAULT_INTERVAL_NORMAL = 300
DEFAULT_INTERVAL_TEST = 30
Expand Down
Loading

0 comments on commit db9d357

Please sign in to comment.