Ejemplo n.º 1
0
def send_log_message(mode=DEFAULT_SENDER_MODE,
                     config=DEFAULT_CONFIG,
                     publickey=DEFAULT_PUBLICKEY,
                     address=DEFAULT_HOSTNAME,
                     port=None,
                     msgcode=0,
                     message='ServerSender test'):
    """
    Send a log message to the server.
    """

    try:
        config_obj = Config(config)
    except IOError:
        # file doesn't exist
        config_obj = None

    try:
        key_obj = PublicKey(publickey)
    except IOError:
        # file doesn't exist
        key_obj = None

    sender = ServerSender(mode=mode,
                          address=address,
                          port=port,
                          config=config_obj,
                          publickey=key_obj,
                          verbosity=3)

    if key_obj is None:
        # no encryption
        if config is None:
            # no ID
            packet = ','.join(msgcode, message)
            sender.send_data(packet)
        else:
            # has ID
            packet = ','.join(sender.config.hash, sender.config.ID, msgcode,
                              message)
            sender.send_data(packet)
    else:
        # encryption
        if config is None:
            # no ID
            packet = ','.join(msgcode, message)
            encrypted = sender.encrypt_packet(packet)
            sender.send_data(encrypted)
        else:
            # standard, full functionality
            sender.send_log(msgcode, message)
Ejemplo n.º 2
0
def send_test_d3s_packet(config=DEFAULT_CONFIG,
                         publickey=DEFAULT_PUBLICKEY,
                         aeskey=DEFAULT_AESKEY,
                         port=None,
                         encrypt=True):
    """
    Send a test packet in the format of the D3S data.
    """

    try:
        config_obj = Config(config)
    except IOError:
        config_obj = None
    try:
        key_obj = PublicKey(publickey)
    except IOError:
        key_obj = None
        if encrypt:
            print("no publickey, can't encrypt")
        encrypt = False
    try:
        with open(aeskey, 'r') as aesfile:
            aeskey = aesfile.read()
            aes = AES.new(aeskey, mode=AES.MODE_ECB)
    except IOError:
        aes = None
        if encrypt:
            print("no AES key, can't encrypt")
        encrypt = False

    sender = ServerSender(port=port,
                          config=config_obj,
                          publickey=key_obj,
                          aes=aes,
                          verbosity=3)

    spectrum = [int(np.random.random() * 3) for _ in xrange(4096)]
    raw_packet = sender.construct_packet_new_D3S(time.time(), spectrum)

    if encrypt:
        packet_to_send = sender.encrypt_packet_aes(raw_packet)
    else:
        packet_to_send = raw_packet

    try:
        sender.send_data(packet_to_send)
    except socket.timeout:
        print('timeout!')

    return packet_to_send
Ejemplo n.º 3
0
def send_test_packets(mode=DEFAULT_SENDER_MODE,
                      config=DEFAULT_CONFIG,
                      publickey=DEFAULT_PUBLICKEY,
                      address=DEFAULT_HOSTNAME,
                      port=None,
                      n=3,
                      encrypt=True,
                      raw_packet=None):
    """
    Send n (default 3) test packets to the DoseNet server.
    """

    sleep_time = 2  # seconds

    try:
        config_obj = Config(config)
    except IOError:
        # file doesn't exist
        config_obj = None
    key_obj = PublicKey(publickey)

    sender = ServerSender(mode=mode,
                          address=address,
                          port=port,
                          config=config_obj,
                          publickey=key_obj,
                          verbosity=3)

    try:
        station_id = config_obj.ID
    except AttributeError:
        station_id = '?'
    if raw_packet is None:
        raw_packet = 'Test packet from station {} by mode {}'.format(
            station_id, mode)

    if encrypt:
        packet_to_send = sender.encrypt_packet(raw_packet)
    else:
        packet_to_send = raw_packet

    for _ in xrange(n):
        sender.send_data(packet_to_send)
        time.sleep(sleep_time)
Ejemplo n.º 4
0
    def handle_input(self, log, logfile, verbosity,
                         test, interval, config, publickey, aeskey):

        if verbosity is None:
            if test:
                verbosity = 3
            else:
                verbosity = 1
            if self.cirtest:
                verbosity = 0
        self.v = verbosity

        if log:
            if logfile is None:
                if self.sensor_type > 5 or self.sensor_type < 1:
                    print("No sensor running, try running through one of the subclasses to get a proper setup.")
                    self.takedown()
                for i in range(len(DEFAULT_LOGFILES)):
                    if self.sensor_type == i+1:
                        logfile = DEFAULT_LOGFILES[i]
                        break
            self.logfile = logfile
        else:
            self.logfile = None

        set_verbosity(self, logfile=logfile)

        if log:
            self.vprint(1, '')
            self.vprint(1, 'Writing to logfile at {}'.format(self.logfile))
        self.running = True

        if self.test:
            if interval is None:
                for i in range(len(DEFAULT_TEST_INTERVALS)):
                    if self.sensor_type == i+1:
                        self.vprint(
                            2, "No interval given, using default for {}".format(TEST_INTERVAL_NAMES[i]))
                        interval = DEFAULT_TEST_INTERVALS[i]
                        break

        if interval is None:
            for i in range(len(DEFAULT_INTERVALS)):
                if self.sensor_type == i+1:
                    self.vprint(
                        2, "No interval given, using interval at 5 minutes")
                    interval = DEFAULT_INTERVALS[i]
                    break

        if config is None:
            self.vprint(2, "No config file given, " +
                        "attempting to use default config path")
            config = DEFAULT_CONFIG
        if publickey is None:
            self.vprint(2, "No publickey file given, " +
                        "attempting to use default publickey path")
            publickey = DEFAULT_PUBLICKEY
        if aeskey is None and self.sensor_type == 2:
            #Only set the AES key when the D3S is being used
            self.vprint(2, "No AES key file given, " +
                        "attempting to use default AES key path")
            aeskey = DEFAULT_AESKEY

        self.interval = interval

        if config:
            try:
                self.config = Config(config,
                                     verbosity=self.v, logfile=self.logfile)
                if self.new_setup == None:
                    self.int_ID = int(self.config.ID)
                    if self.int_ID == 5 or self.int_ID == 29 or self.int_ID == 32 or \
                        self.int_ID == 33 or self.int_ID >= 39:
                        self.new_setup = True
                    else:
                        self.new_setup = False
            except IOError:
                raise IOError(
                    'Unable to open config file {}!'.format(config))
        else:
            self.vprint(
                1, 'WARNING: no config file given. Not posting to server')
            self.config = None

        if publickey:
            try:
                self.publickey = PublicKey(
                    publickey, verbosity=self.v, logfile=self.logfile)
            except IOError:
                raise IOError(
                    'Unable to load publickey file {}!'.format(publickey))
        else:
            self.vprint(
                1, 'WARNING: no public key given. Not posting to server')
            self.publickey = None

        if aeskey:
            try:
                with open(aeskey, 'r') as aesfile:
                    key = aesfile.read()
                    self.aes = AES.new(key, mode=AES.MODE_ECB)
            except IOError:
                raise IOError('Unable to load AES key file {}!'.format(
                    aeskey))
        if not aeskey and self.sensor_type == 2:
            self.vprint(
                1, 'WARNING: no AES key given. Not posting to server')
            self.aes = None
        if self.sensor_type != 2:
            self.aes = None
    def handle_input(self, log, logfile, verbosity, test, interval, config,
                     publickey):

        if log and logfile is None:
            logfile = DEFAULT_LOGFILE_AQ

        if logfile and not log:
            log = True

        if log:
            self.logfile = logfile
        else:
            self.logfile = None

        if verbosity is None:
            if test:
                verbosity = 2
            else:
                verbosity = 1
        self.v = verbosity
        set_verbosity(self, logfile=logfile)

        if log:
            self.vprint(1, '')
            self.vprint(1, 'Writing to logfile at {}'.format(self.logfile))
        self.running = False

        if self.test:
            if interval is None:
                self.vprint(2,
                            "No interval given, using default for TEST MODE")
                interval = DEFAULT_INTERVAL_TEST_AQ

        if interval is None:
            self.vprint(2, "No interval given, using interval at 5 minutes")
            interval = DEFAULT_INTERVAL_NORMAL_AQ
        if config is None:
            self.vprint(
                2, "No config file given, " +
                "attempting to use default config path")
            config = DEFAULT_CONFIG
        if publickey is None:
            self.vprint(
                2, "No publickey file given, " +
                "attempting to use default publickey path")
            publickey = DEFAULT_PUBLICKEY

        self.interval = interval

        if config:
            try:
                self.config = Config(config,
                                     verbosity=self.v,
                                     logfile=self.logfile)
            except IOError:
                raise IOError('Unable to open config file {}!'.format(config))
        else:
            self.vprint(
                1, 'WARNING: no config file given. Not posting to server')
            self.config = None

        if publickey:
            try:
                self.publickey = PublicKey(publickey,
                                           verbosity=self.v,
                                           logfile=self.logfile)
            except IOError:
                raise IOError(
                    'Unable to load publickey file {}!'.format(publickey))
        else:
            self.vprint(1,
                        'WARNING: no public key given. Not posting to server')
            self.publickey = None

        self.aes = None  #checked in sender, used for the manager_d3s
Ejemplo n.º 6
0
    def handle_input(self,
                     log, logfile, verbosity,
                     test, interval, config, publickey):

        # resolve logging defaults
        if log and logfile is None:
            # use default file if logging is enabled
            logfile = DEFAULT_LOGFILE
        if logfile and not log:
            # enable logging if logfile is specified
            #   (this overrides a log=False input which wouldn't make sense)
            log = True
        if log:
            self.logfile = logfile
        else:
            self.logfile = None

        # set up verbosity
        if verbosity is None:
            if test:
                verbosity = 2
            else:
                verbosity = 1
        self.v = verbosity
        set_verbosity(self, logfile=logfile)

        if log:
            self.vprint(1, '')
            self.vprint(1, 'Writing to logfile at {}'.format(self.logfile))
        self.test = test
        self.running = False

        # resolve defaults that depend on test mode
        if self.test:
            if interval is None:
                self.vprint(
                    2, "No interval given, using default for TEST MODE")
                interval = DEFAULT_INTERVAL_TEST

        else:
            if interval is None:
                self.vprint(
                    2, "No interval given, using default for NORMAL MODE")
                interval = DEFAULT_INTERVAL_NORMAL
            if config is None:
                self.vprint(2, "No config file given, " +
                            "attempting to use default config path")
                config = DEFAULT_CONFIG
            if publickey is None:
                self.vprint(2, "No publickey file given, " +
                            "attempting to use default publickey path")
                publickey = DEFAULT_PUBLICKEY

        self.interval = interval

        if self.datalogflag:
            self.vprint(
                1, 'Writing CPM to data log at {}'.format(self.datalog))

        if config:
            try:
                self.config = Config(config,
                                     verbosity=self.v, logfile=self.logfile)
            except IOError:
                raise IOError(
                    'Unable to open config file {}!'.format(config))
        else:
            self.vprint(
                1, 'WARNING: no config file given. Not posting to server')
            self.config = None

        if publickey:
            try:
                self.publickey = PublicKey(
                    publickey, verbosity=self.v, logfile=self.logfile)
            except IOError:
                raise IOError(
                    'Unable to load publickey file {}!'.format(publickey))
        else:
            self.vprint(
                1, 'WARNING: no public key given. Not posting to server')
            self.publickey = None

        self.aes = None     # gets checked in sender. feature in manager_d3s
Ejemplo n.º 7
0
    def handle_input(self, log, logfile, verbosity, interval, config,
                     publickey):
        """
        Sets up logging, verbosity, interval, config, and publickey
        """

        # resolve logging defaults
        if log and logfile is None:
            # use default file if logging is enabled
            logfile = DEFAULT_LOGFILE_D3S
        if logfile and not log:
            # enable logging if logfile is specified
            #   (this overrides a log=False input which wouldn't make sense)
            log = True
        if log:
            self.logfile = logfile
        else:
            self.logfile = None

        if verbosity is None:
            verbosity = 1
        self.v = verbosity
        set_verbosity(self, logfile=logfile)

        if log:
            self.vprint(1, '')
            self.vprint(1, 'Writing to logfile at {}'.format(self.logfile))
        self.running = True

        if interval is None:
            self.vprint(2, "No interval given, using interval at 30 seconds")
            interval = DEFAULT_INTERVAL_NORMAL_D3S
        if config is None:
            self.vprint(
                2, "No config file given, " +
                "attempting to use default config path")
            config = DEFAULT_CONFIG
        if publickey is None:
            self.vprint(
                2, "No publickey file given, " +
                "attempting to use default publickey path")
            publickey = DEFAULT_PUBLICKEY

        self.interval = interval

        if config:
            try:
                self.config = Config(config,
                                     verbosity=self.v,
                                     logfile=self.logfile)
            except IOError:
                raise IOError('Unable to open config file {}!'.format(config))
        else:
            self.vprint(
                1, 'WARNING: no config file given. Not posting to server')
            self.config = None

        if publickey:
            try:
                self.publickey = PublicKey(publickey,
                                           verbosity=self.v,
                                           logfile=self.logfile)
            except IOError:
                raise IOError(
                    'Unable to load publickey file {}!'.format(publickey))
        else:
            self.vprint(1,
                        'WARNING: no public key given. Not posting to server')
            self.publickey = None