"""
This runs continuously and serves our sensor readings when requested.

Base script from:
http://ilab.cs.byu.edu/python/socket/echoserver.html
"""

import socket
import json


class SensorServer:
    def __init__(self, host='', port=8888, size=1024, backlog=5):
        self.host = host
        self.port = port
        self.size = size
        self.backlog = backlog

        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s.bind((host, port))
        self.s.listen(backlog)

    def serve_readings(self):
        with open('readings.json') as f:
            try:
                data = json.load(f)
            except:
                print("Got bad JSON data from file. Not sending.")
                return False

        client, address = self.s.accept()
        print("Sending: %s" % str(data))
        b = json.dumps(data).encode('utf-8')
        client.sendall(b)
        client.close()
        return True


if __name__ == '__main__':
    input("Start sensor_run.py then hit enter to start server.")
    ss = SensorServer()
    while 1:
        ss.serve_readings()

"""
This runs continuously and serves our sensor readings when requested.

Base script from:
http://ilab.cs.byu.edu/python/socket/echoserver.html
"""
import redis
import socket
import json


class SensorServer:
    def __init__(self, host='', port=8888, size=1024, backlog=5):
        self.host = host
        self.port = port
        self.size = size
        self.backlog = backlog

        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s.bind((host, port))
        self.s.listen(backlog)

        self.r_server = redis.Redis('localhost')

    def serve_readings(self):
        ir_s = self.r_server.get('ir_s')
        ir_l = self.r_server.get('ir_l')
        ir_r = self.r_server.get('ir_r')
        ir_s = self.r_server.get('s_m')

        readings = {
            'ir_s': ir_s,
            'ir_l': ir_l,
            'ir_r': ir_r,
            'ir_s': ir_s
        }

        client, address = self.s.accept()
        print("Sending: %s" % str(readings))
        b = json.dumps(readings).encode('utf-8')
        client.sendall(b)
        client.close()


if __name__ == '__main__':
    input("Start sensor_run.py then hit enter to start server.")
    ss = SensorServer()
    while 1:
        ss.serve_readings()

Update sensor server.