<commit_before>"""
SensorSafe Data Transmitter
Connects socket to tcp://localhost:5556
Collects data from Zwave interface using Zeromq and
   sends the data in JSON to SensorSafe

Usage: python sendToSensorSafe.py [api-key] [Options]"

    [api-key] is the API key given when registering for a SensorSafe account.

    Options:

        -f [frequency]
            This is the number of seconds between each http request
            to SensorSafe."

            If [frequency] is not specified or is set to 0, the requests
                are made every time the data is received by the zeromq socket.

        -e This option specifies that all event-driven measurements should be
           sent right away (usually because they are more time-critical)
           instead of sending based on the frequency. Note that the none
           event-driven measurements will still be governed by the frequency
           and are not affected. Currently, the event-driven measurements
           are Motion and Door Status (Open/Closed).

        -v Verbose option that shows more information about the upload to
           SensorSafe.
"""

import sys              # Used for commandline arguments
import httplib          # Used for http requests
import urllib           # Used for http requests
import json             # Used for json formatted data
import time             # Used for timestamps
import threading        # Used for creating separate thread for sending to
                            # SensorSafe
import getopt           # Used for command line option handling
import zmq              # Used for receiving data sent over zeromq socket
import signal           # Used for properly cancelling timer thread when Ctrl-c
                            # is pressed

SERVER_ADDRESS = "172.16.17.32"
SERVER_PREFIX = ""
HTTP_REQUEST_TIMEOUT = 60

# Colors
BLUE = "\033[94m"
GREEN = "\033[92m"
PURPLE = "\033[95m"
ENDCOLOR = "\033[0m"


class SensorVariableTracker:
    """ This class keeps track of all variables received from LabSenseZwave
        over the Zeromq socket and delivers the data to the
        SensorSafe server every [frequency] seconds. """

    def __init__(self, key, event_driven, verbose, frequency=0):
        """ Initialize the Zeromq socket to receive data
            and initialize timer thread to send data at given
            frequency. """

        #  Socket to talk to server
        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.SUB)

        self.socket.connect("tcp://localhost:5556")
        self.socket.connect("tcp://localhost:5557")

        # Subscribe to all zeromq messages
        self.socket.setsockopt(zmq.SUBSCRIBE, "")

        # This is the frequency at which the data will be sent to SensorSafe in
        # seconds
        self.frequency = frequency

        # sensorData keeps track of all the data entries that need to be sent
        # to SensorSafe
        self.sensorData = []

        # event_driven specifies if the event driven measurements should be
        # sent right away because they are more time
        # critical. Currently event driven measurements are Motion and Door.
        # Note: the none-event-driven variables are still controlled by the
        # frequency
        self.event_driven = event_driven

        # if verbose is true, more information about the upload process will be
        # printed
        self.verbose = verbose

        self.key = key

        # Start separate thread that runs sendSensorData every [frequency]
        # seconds
        if frequency > 0:
            print "Starting Timer with frequency " + str(frequency)
            self.timer_thread = threading.Timer(self.frequency,
                    self.sendSensorData)
            self.timer_thread.start()

            # Set up Ctrl-C signal handler
            def signal_handler(signal, frame):
                self.timer_thread.cancel()
                sys.exit(0)

            signal.signal(signal.SIGINT, signal_handler)

    def registerValue(self, measurement, value):
        """ Register a data entry to the sensorData list """

        if "Door" in measurement:
            color = BLUE
        elif "Energy" in measurement or "Power" in measurement:
            color = GREEN
        else:
            color = PURPLE
        cur_time = int(round(time.time() * 1000))

        print "%sRegistering %s: %s at %d %s" % (color, measurement, value,
                cur_time, ENDCOLOR)

        data_entry = {
                "sampling_interval": 1,
                "timestamp": cur_time,
                "location": {"latitude": 34.068839550018311,
                             "longitude": -118.44383955001831},
                "data_channel": [measurement],
                "data": [[value]]
        }
        # If option for sending event driven measurements is specified, send
        # the data immediately
        if self.event_driven == True and measurement in ("Motion", "Door"):
            success = self.sendToSensorSafe(data_entry)
            if not success:
                self.sensorData.append(data_entry)
        else:
            self.sensorData.append(data_entry)

        # When Frequency is 0, this is a special mode where values are sent at
        # the rate they are received
        if self.frequency == 0:
            self.sendSensorData()

    def registerBatchedValues(self, measurement, value):
        """ Register several data entries to the sensorData list """

        cur_time = int(round(time.time() * 1000))

        print "Registering "+ measurement + " " + str(value)

        data_entry = {
                "sampling_interval": 1,
                "timestamp": cur_time,
                "location": {"latitude": 34.068839550018311,
                             "longitude": -118.44383955001831},
                "data_channel": [measurement],
                "data": [value]
        }
        # If option for sending event driven measurements is specified, send
        # the data immediately

        self.sensorData.append(data_entry)

    def receiveFromSocket(self):
        """ Continually receive data from zwave and send data to SensorSafe """

        print "Collecting data from Zwave..."
        while(1):
            string = self.socket.recv()

            string_list = string.split()
            measurement = string_list[0]
            if len(string_list) == 2:
                str_value = string_list[1]<commit_msg>Made all data send to separate power and current values. Need to find mapping from those values to the actual wall outlets of the lab<commit_after>"""
SensorSafe Data Transmitter
Connects socket to tcp://localhost:5556
Collects data from Zwave interface using Zeromq and
   sends the data in JSON to SensorSafe

Usage: python sendToSensorSafe.py [api-key] [Options]"

    [api-key] is the API key given when registering for a SensorSafe account.

    Options:

        -f [frequency]
            This is the number of seconds between each http request
            to SensorSafe."

            If [frequency] is not specified or is set to 0, the requests
                are made every time the data is received by the zeromq socket.

        -e This option specifies that all event-driven measurements should be
           sent right away (usually because they are more time-critical)
           instead of sending based on the frequency. Note that the none
           event-driven measurements will still be governed by the frequency
           and are not affected. Currently, the event-driven measurements
           are Motion and Door Status (Open/Closed).

        -v Verbose option that shows more information about the upload to
           SensorSafe.
"""

import sys              # Used for commandline arguments
import httplib          # Used for http requests
import urllib           # Used for http requests
import json             # Used for json formatted data
import time             # Used for timestamps
import threading        # Used for creating separate thread for sending to
                            # SensorSafe
import getopt           # Used for command line option handling
import zmq              # Used for receiving data sent over zeromq socket
import signal           # Used for properly cancelling timer thread when Ctrl-c
                            # is pressed

SERVER_ADDRESS = "172.16.17.32"
SERVER_PREFIX = ""
HTTP_REQUEST_TIMEOUT = 60

# Colors
BLUE = "\033[94m"
GREEN = "\033[92m"
PURPLE = "\033[95m"
ENDCOLOR = "\033[0m"


class SensorVariableTracker:
    """ This class keeps track of all variables received from LabSenseZwave
        over the Zeromq socket and delivers the data to the
        SensorSafe server every [frequency] seconds. """

    def __init__(self, key, event_driven, verbose, frequency=0):
        """ Initialize the Zeromq socket to receive data
            and initialize timer thread to send data at given
            frequency. """

        #  Socket to talk to server
        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.SUB)

        self.socket.connect("tcp://localhost:5556")
        self.socket.connect("tcp://localhost:5557")

        # Subscribe to all zeromq messages
        self.socket.setsockopt(zmq.SUBSCRIBE, "")

        # This is the frequency at which the data will be sent to SensorSafe in
        # seconds
        self.frequency = frequency

        # sensorData keeps track of all the data entries that need to be sent
        # to SensorSafe
        self.sensorData = []

        # event_driven specifies if the event driven measurements should be
        # sent right away because they are more time
        # critical. Currently event driven measurements are Motion and Door.
        # Note: the none-event-driven variables are still controlled by the
        # frequency
        self.event_driven = event_driven

        # if verbose is true, more information about the upload process will be
        # printed
        self.verbose = verbose

        self.key = key

        # Start separate thread that runs sendSensorData every [frequency]
        # seconds
        if frequency > 0:
            print "Starting Timer with frequency " + str(frequency)
            self.timer_thread = threading.Timer(self.frequency,
                    self.sendSensorData)
            self.timer_thread.start()

            # Set up Ctrl-C signal handler
            def signal_handler(signal, frame):
                self.timer_thread.cancel()
                sys.exit(0)

            signal.signal(signal.SIGINT, signal_handler)

    def registerValue(self, measurement, value):
        """ Register a data entry to the sensorData list """

        if "Door" in measurement:
            color = BLUE
        elif "Energy" in measurement or "Power" in measurement:
            color = GREEN
        else:
            color = PURPLE
        cur_time = int(round(time.time() * 1000))

        print "%sRegistering %s: %s at %d %s" % (color, measurement, value,
                cur_time, ENDCOLOR)

        data_entry = {
                "sampling_interval": 1,
                "timestamp": cur_time,
                "location": {"latitude": 34.068839550018311,
                             "longitude": -118.44383955001831},
                "data_channel": [measurement],
                "data": [[value]]
        }
        # If option for sending event driven measurements is specified, send
        # the data immediately
        if self.event_driven == True and measurement in ("Motion", "Door"):
            success = self.sendToSensorSafe(data_entry)
            if not success:
                self.sensorData.append(data_entry)
        else:
            self.sensorData.append(data_entry)

        # When Frequency is 0, this is a special mode where values are sent at
        # the rate they are received
        if self.frequency == 0:
            self.sendSensorData()

    def registerBatchedValues(self, measurement, values):
        """ Register several data entries to the sensorData list """

        cur_time = int(round(time.time() * 1000))

        print "Registering "+ measurement + " " + str(values)
        measurements = []
        for i in range(len(values)):
            measurements.append(measurement + "_" + str(i+1))

        print measurements
        data_entry = {
                "sampling_interval": 1,
                "timestamp": cur_time,
                "location": {"latitude": 34.068839550018311,
                             "longitude": -118.44383955001831},
                "data_channel": measurements,
                "data": [values]
        }
        # If option for sending event driven measurements is specified, send
        # the data immediately

        self.sensorData.append(data_entry)

    def receiveFromSocket(self):
        """ Continually receive data from zwave and send data to SensorSafe """

        print "Collecting data from Zwave..."
        while(1):
            string = self.socket.recv()

            string_list = string.split()
            measurement = string_list[0]
            if len(string_list) == 2:
                str_value = string_list[1]
{'fraction_non_alphanumeric': 0.07213288834951456, 'fraction_numerical': 0.019796723300970875, 'mean_word_length': 2.0275545350172215, 'pattern_counts': {'":': 28, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3846677', 'n_tokens_mistral': 3525, 'n_tokens_neox': 3234, 'n_words': 1244}