Exemplo n.º 1
0
    async def copy_key_values_to(self, to_node: int, interval: Interval,
                                 dict_to_transfer: Dict[str, str]):
        """
        Copy key_values in the `interval` from persistent_storage to `to_node`
        """
        keys_to_transfer = dict_to_transfer.keys()
        msg_to_transfer = self.message_sender.create_message(
            MessageType.TRANSFER_KEYS, to_node, {
                'interval': interval.to_string(),
                'data_dict': dict_to_transfer,
                'copy_node': self.chord_addr,
                'receiving_node': to_node
            })
        recieved_msg = await self.send_message_and_await_response(
            msg_to_transfer, MessageType.TRANSFER_KEYS_CONFIRMED)

        keys_transfered_successfully, interval_transfered_successfully = recieved_msg.get_args(
            ['keys_transfered', 'interval'])
        assert (set(keys_transfered_successfully) == set(
            dict_to_transfer.keys()))
        trace_log(colorama.Fore.MAGENTA + "KeyTransferer", self.chord_addr,
                  ": Copy Successfully completed from", self.chord_addr, "to",
                  to_node, "of interval", interval.to_string())
        if debug_log_key_transfers:
            debug_log("KeyTransferer", self.chord_addr,
                      ": Copy Successfully completed from", self.chord_addr,
                      "to", to_node, "of interval", interval.to_string())
Exemplo n.º 2
0
    async def handle_i_might_be_your_predecessor(self, msg: Message):
        assert ('addr' in msg.args)
        candidate_predecessor = msg.args['addr']

        # If we are the first node.
        if (self.successor == self.addr):
            self.successor = candidate_predecessor
            trace_log("SuccRouter addr:", self.addr, "Setting new successor ",
                      candidate_predecessor)
            # transfer the stuff from (self.addr, candidate_predecessor] because we are no longer responsible for that
            #   half of addr space
            # debug_log(self.addr, "Transfer because self.successor == self.addr, pred", self.predecessor, 'suc', self.successor)
            # await self.key_transferer.copy_to(candidate_predecessor, Interval(self.addr, False, candidate_predecessor, True))

        # We know there are at least two nodes in the system.
        if (self.predecessor == None):
            # From our point of view, we used to be responsible for (self.sucessor, self.addr]
            # Now, candidate_predecessor is coming in and taking (self.successor, candidate_predecessor] from us
            # So we transfer (self.successor, candidate_predecessor] to them.
            if (self.successor != candidate_predecessor):
                debug_log(self.addr,
                          "Transfer because self.prdecessor == None, pred",
                          self.predecessor, 'suc', self.successor)
                await self.key_transferer.copy_to(
                    candidate_predecessor,
                    Interval(self.successor, False, candidate_predecessor,
                             True))
            else:
                # There is only one other node, so we should transfer him the other side
                debug_log(self.addr,
                          "Transfer because self.prdecessor == None, pred",
                          self.predecessor, 'suc', self.successor)
                await self.key_transferer.copy_to(
                    candidate_predecessor,
                    Interval(self.addr, False, candidate_predecessor, True))

            # Normally, we don't need this. However, this fixes an edge cases involved with starting up with 1 node and having
            # 2 concurrent joins.

            self.predecessor = candidate_predecessor
            trace_log("SuccRouter addr:", self.addr,
                      "No current predecessor. Setting predecessor ",
                      candidate_predecessor)
        elif (in_interval(self.predecessor, self.addr, candidate_predecessor)):
            # From our point of view, we are responsible for (self.predecessor, self.addr]
            # However, now we are only responisble for (candidate_predecessor, self.addr]
            # So, we transfer out the difference: (self.predecessor, candidate_predecessor]
            debug_log(
                self.addr,
                "Transfer because candidate_predecessor \in (self.predecessor, self.addr), pred",
                self.predecessor, 'suc', self.successor)
            await self.key_transferer.copy_to(
                candidate_predecessor,
                Interval(self.predecessor, False, candidate_predecessor, True))
            self.predecessor = candidate_predecessor
            trace_log("SuccRouter addr:", self.addr,
                      "Setting new predecessor ", candidate_predecessor)
def main(argv):
    opts, args = getopt.getopt(argv, "cghilt:o", ["ip="])
    del args

    if len(opts) == 0:
        print("timing_listener.py -i <server IP address>")
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print("timing_listener.py -i <server IP address>")
            sys.exit()
        elif opt in ("-i", "--ip"):
            ip = arg
            print(ip)
        else:
            print("timing_listener.py -i <server IP address>")
            sys.exit(2)

    interval = Interval.Interval(2, send_server_info, args=[ip])
    print("Starting Interval, press CTRL+C to stop.")
    interval.start()

    while True:
        try:

            # Loads the incoming data into a json format
            raw_data, addr = listenSocket.recvfrom(1024)
            data = json.loads(raw_data)
            ip = addr[0]

            # Gets the computer clock for the moment the data sample is recived
            # and adds it to the raw .csv-file
            local_time = datetime.datetime.now()
            local_time = local_time.strftime("%H:%M:%S")

            #Plot real time graph here

            event_id = data['timetic']
            timestamp = data['drift']

            with open(file_name, 'a', newline='') as csvfile:
                fieldnames = ["Local_time", 'Event_ID', 'Node', 'Timestamp']
                writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
                thisdict = {
                    "Local_time": local_time,
                    "Event_ID": event_id,
                    "Node": ip,
                    "Timestamp": timestamp
                }
                print(thisdict)
                writer.writerow(thisdict)
                csvfile.close()

        except KeyboardInterrupt:
            print("Shutting down interval...")
            interval.stop()
            break
Exemplo n.º 4
0
 async def replicate_our_data_to(self, nodes):
     for new_successor in nodes:
         if new_successor != None:
             if self.router.predecessor == None:
                 debug_log('SuccessorList.Stabalize', self.addr,
                           ':new successor so Transferring keys',
                           new_successor)
                 await self.key_transferer.copy_to(
                     new_successor,
                     Interval(self.router.successor, False, self.addr,
                              True))
             else:
                 debug_log('SuccessorList.Stabalize', self.addr,
                           ':new successor so Transferring keys',
                           new_successor)
                 await self.key_transferer.copy_to(
                     new_successor,
                     Interval(self.router.predecessor, False, self.addr,
                              True))
Exemplo n.º 5
0
 def getIntervals(self):
     """
     :rtype: List[Interval]
     """
     r = []
     for x in sorted(self.start_points):
         if x == self._father(self.starts, x):
             r.append(Interval(x, self._father(self.ends, x)))
         else:
             self.start_points.remove(x)
     return r
Exemplo n.º 6
0
 def send_confirm_transfer(self, copy_node: int, interval: Interval,
                           data_dict: Dict[str, str]):
     """
     Send a message that confirms a transfer was completed.
     """
     confirm_msg = self.message_sender.create_message(
         MessageType.TRANSFER_KEYS_CONFIRMED, copy_node, {
             'interval': interval.to_string(),
             'keys_transfered': list(data_dict.keys())
         })
     self.message_sender.send_msg(confirm_msg)
Exemplo n.º 7
0
 async def copy_to(self, to_node: int, interval: Interval):
     """
     Copy the keys of an interval from our persistant storage to the `to_node`.
     """
     trace_log("KeyTransferer ", self.chord_addr,
               " : Starting copy of keys from myself to ", to_node)
     all_keys = await self.persistant_storage.get_keys()
     keys_to_transfer = [
         key for key in all_keys if interval.in_interval(hash_key(key))
     ]
     dict_to_transfer = {}
     for key in keys_to_transfer:
         dict_to_transfer[key] = await self.persistant_storage.get(key)
     await self.copy_key_values_to(to_node, interval, dict_to_transfer)
Exemplo n.º 8
0
def main(argv):
    ip = None

    opts, _ = getopt.getopt(argv,"cdfghil:o",["channel=", "database=", "filter=", "graph=", "ip=", "label="])
    
    for opt, arg in opts:
        if opt == '-h':
            print("A label must be set for the test to start: filter_test.py --label '<label>'")
            sys.exit()
        elif opt in ("-i", "-a", "--ip", "--address"):
            ip = arg
            print(ip)

    if not ip:
        print("Please provide IP argument: setup.py --ip <IP address>")
        sys.exit()
    
    interval = Interval.Interval(2, sendServerInfo, args=[ip,])
    print("Starting Interval, press CTRL+C to stop.")
    interval.start() 
Exemplo n.º 9
0
        :rtype: List[int]
        """
        a = list(enumerate(intervals))
        a.sort(key=lambda (i, x): x.start)
        b = list(enumerate(intervals))
        b.sort(key=lambda (i, x): x.end)

        n = len(intervals)
        k = 0
        r = [-1 for i in xrange(n)]
        for i, x in b:
            if k < n and x.end <= a[k][1].start:
                r[i] = a[k][0]
            else:
                while k < n and x.end > a[k][1].start:
                    k += 1
                if k < n:
                    r[i] = a[k][0]
                else:
                    r[i] = -1
        return r


if __name__ == '__main__':
    from utils import Interval

    f = Solution().findRightInterval
    assert f(Interval.make_list([ [1,2] ])) == [-1]
    assert f(Interval.make_list([ [3,4], [2,3], [1,2] ])) == [-1, 0, 1]
    assert f(Interval.make_list([ [1,4], [2,3], [3,4] ])) == [-1, 2, -1]
Exemplo n.º 10
0
def main(argv):
    global GRAPH_ENABLED
    global DB_ENABLED
    chosenChannel = None
    distance = None
    label = None
    sampleNumber = 0
    rawRssiList = list()
    filteredRssiList = list()
    xAxis = list()

    kalman = KalmanFilter(dim_x=1, dim_z=1)
    kalman.x = np.array([[-30.]])
    kalman.F = np.array([[1.]])
    kalman.H = np.array([[1.]])
    kalman.P = np.array([[0.]])
    kalman.R = 3.19
    kalman.Q = 0.065

    opts, args = getopt.getopt(
        argv, "cdfghil:o",
        ["channel=", "distance=", "filter=", "graph=", "ip=", "label="])
    del (args)

    if len(opts) == 0:
        print(
            "A label must be set for the test to start: filter_test.py --label '<label>'"
        )
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print(
                "A label must be set for the test to start: filter_test.py --label '<label>'"
            )
            sys.exit()
        elif opt in ("-i", "--ip"):
            ip = arg
            print(ip)
        elif opt in ("-d", "--distance"):
            distance = int(arg)
            print("Test for distance: ", distance)
        elif opt in ("-l", "--label"):
            label = arg
            print("Label for test: ", label)
        elif opt in ("-c", "--channel"):
            chosenChannel = int(arg)
        elif opt in ("-g", "--graph"):
            GRAPH_ENABLED = True
        else:
            print(
                "A label must be set for the test to start: filter_test.py --label '<label>'"
            )
            sys.exit(2)

    if not distance or not label:
        print(
            "A label must be set for the test to start: filter_test.py --label '<label>'"
        )
        sys.exit(2)

    if GRAPH_ENABLED:
        import matplotlib.pyplot as plt
        #plt.ion()
        #plt.axis([0, distance * 2, -80, -20])

        #plt.legend(loc="lower right")

        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.grid(color='#cccccc', linestyle='-', linewidth=1)
        ax.legend(loc="lower right")

    interval = Interval.Interval(2, sendServerInfo, args=[
        ip,
    ])
    print("Starting filter test... Press CTRL + C to stop.")
    interval.start()

    while True:
        try:
            rawData, addr = listenSocket.recvfrom(1024)
            data = json.loads(rawData)

            ip = addr[0]
            nodeID = data['nodeID']
            timestamp = data['timestamp']
            address = data['address']
            rssi = data['RSSI']
            crc = data['CRC']
            lpe = data['LPE']
            counter = data['counter']
            syncController = data['syncController']
            channel = data['channel']

            if chosenChannel == channel and crc == 1 and lpe == 0:

                kalman.predict()
                kalman.update(rssi)
                filteredRssi = kalman.x[0]
                filteredRssiList.append(filteredRssi)
                xAxis.append(round(sampleNumber / 2))
                rawRssiList.append(data['RSSI'])
                plt.axis([0, xAxis[-1] + 20, -50, -20])
                if sampleNumber == 1:
                    plt.legend(loc="lower right")

                if DB_ENABLED:
                    sql = "INSERT INTO rssi_data VALUES(NULL, NULL, '%s', '%s', %d, '%s', %d, %d, %d, %f, %d, NULL, %d, %d, %d, '%s')" % (
                        nodeID, ip, timestamp, address, channel, counter, rssi,
                        filteredRssi, distance, crc, lpe, syncController,
                        label)

                    cursor.execute(sql)
                    db.commit()

                if GRAPH_ENABLED and (sampleNumber %
                                      SAMPLES_FOR_EACH_UPDATE) == 0:
                    color = 'k'
                    name = "Unknown channel"
                    if channel == 37:
                        color = 'r'
                        name = "Channel 37"
                    elif channel == 38:
                        color = 'b'
                        name = "Channel 38"
                    elif channel == 39:
                        color = 'g'
                        name = "Channel 39"

                    if PLOT_DISTANCE:
                        plt.scatter(distance,
                                    filteredRssi,
                                    color=color,
                                    alpha=0.1,
                                    label=name)
                        plt.scatter(distance,
                                    rssi,
                                    color='k',
                                    alpha=0.1,
                                    label=name)
                    elif PLOT_TIME:
                        ax.plot(xAxis,
                                rawRssiList,
                                'r-',
                                label="Raw",
                                alpha=0.7)
                        ax.plot(xAxis,
                                filteredRssiList,
                                'b-',
                                label="Kalman",
                                linewidth=3)
                    plt.pause(0.0001)

                sampleNumber += 1
                print(sampleNumber, "\tFrom", ip, "\tTimestamp: ", timestamp,
                      "\tCounter: ", counter, "\tAddr.: ", address,
                      "\tChannel: ", channel, "\tRSSI: ", rssi, "\tCRC: ", crc,
                      "\tLPE: ", data['LPE'])

        except KeyboardInterrupt:
            print("Shutting down interval...")
            interval.stop()
            break
Exemplo n.º 11
0
class Solution(object):
    def eraseOverlapIntervals(self, intervals):
        """
        :type intervals: List[Interval]
        :rtype: int
        """
        if not intervals:
            return 0
        intervals.sort(key=lambda x: x.start)
        it = iter(intervals)
        end, cnt = next(it).end, 0
        for x in it:
            if x.start < end:  # overlap
                cnt += 1
                end = min(end, x.end)  # erase interval with larger end
            else:
                end = x.end
        return cnt


if __name__ == '__main__':
    from utils import Interval
    f = Solution().eraseOverlapIntervals
    assert f(Interval.make_list([[0,2],[1,3],[1,3],[2,4],[3,5],[3,5],[4,6]])) == 4
    assert f(Interval.make_list([[0,2],[1,3],[2,4],[3,5],[4,6]])) == 2
    assert f(Interval.make_list([[1,100],[11,22],[1,11],[2,12]])) == 2
    assert f(Interval.make_list([ [1,2], [2,3], [3,4], [1,3] ])) == 1
    assert f(Interval.make_list([ [1,2], [1,2], [1,2] ])) == 2
    assert f(Interval.make_list([ [1,2], [2,3] ])) == 0
                        everoverlap = False
                        if chromosome_name_ref in chromosome_gene_list_ref:
                            for g in chromosome_gene_list_ref[
                                    chromosome_name_ref]:
                                if g.start < end and g.start > start:
                                    everoverlap = True
                                if start < g.end and start > g.start:
                                    everoverlap = True

                        if (not chromosome_name_ref
                                in ref_black_list_interval):
                            ref_black_list_interval[
                                chromosome_name_ref] = np.empty([0, 1],
                                                                Interval)
                        gRef = Interval(start, end)
                        ref_black_list_interval[
                            chromosome_name_ref] = np.append(
                                ref_black_list_interval[chromosome_name_ref],
                                [gRef])
                        if (not everoverlap):
                            seq = getSubSequence(ref_genome,
                                                 chromosome_name_ref, start,
                                                 end)
                            if len(seq) > args.seq_length:
                                distance = len(seq) - args.seq_length
                                s_distance = int(distance / 2)
                                seq = seq[s_distance:(s_distance +
                                                      args.seq_length)]
                            outputString = seq
                            if outputString not in outputsequences:
Exemplo n.º 13
0
    async def handle_transfer_keys_to_me(self, msg: Message):
        """
        Handle a request to transfer keys to me.
        """
        if self.router == None:
            return
        interval, data_dict, copy_node, receiving_node = msg.get_args(
            ['interval', 'data_dict', 'copy_node', 'receiving_node'])
        interval = parse_interval_str(interval)
        receiving_node = int(receiving_node)
        copy_node = int(copy_node)
        if debug_log_key_transfers:
            debug_log("KeyTransferer", self.chord_addr,
                      ": Handling transfer keys from",
                      copy_node, "to", self.chord_addr, "of interval",
                      interval.to_string(), "current-pred",
                      self.router.predecessor, " succ ", self.router.successor)

        data_to_store = None
        # Check that we should recieve these and that this is in fact intended for us.
        if (self.chord_addr == self.router.successor
                or self.router.predecessor == None):
            # These are intended for us, because we control everything
            # Store the data on disk
            data_to_store = data_dict
        else:
            # assert(interval.top == self.chord_addr and interval.top_closed)
            print("interval is", interval, "predecessor",
                  self.router.predecessor)
            if (interval.in_interval(self.router.predecessor)):
                # This means the whole interval isn't for us.
                # Take out what's ours and foward what ever else there is
                our_key_range = Interval(self.chord_addr, True,
                                         self.router.successor, False)
                our_keys = set(key for key in data_dict.keys()
                               if our_key_range.in_interval(hash_key(key)))
                dict_that_is_ours = dict(
                    {key: data_dict[key]
                     for key in our_keys})

                other_keys = set(data_dict.keys()).difference(our_keys)
                dict_that_isnt_ours = {
                    key: data_dict[key]
                    for key in other_keys
                }
                # Assume we are the end of the interval, then we need to pass it back wards.
                other_range = Interval(interval.bottom, interval.bottom_closed,
                                       self.router.predecessor, True)
                # Pass along the rest of the interval
                await self.copy_key_values_to(self.router.predecessor,
                                              other_range, dict_that_isnt_ours)
                data_to_store = dict_that_is_ours
            else:
                # This means we are replicating
                data_to_store = data_dict

        # Store the data on disk
        debug_log("KeyTransferer", self.chord_addr,
                  ": Handling transfer keys from",
                  copy_node, "to", self.chord_addr, "of interval",
                  interval.to_string(), "current-pred",
                  self.router.predecessor, " succ ", self.router.successor,
                  "data", data_to_store)
        await self.persistant_storage.add_dict(data_to_store)

        # Confirm what we stored.
        self.send_confirm_transfer(copy_node, interval, data_dict)
Exemplo n.º 14
0
def main(argv):
    global GRAPH_ENABLED
    global DB_ENABLED
    label = None
    totalCounter = 0
    allPositions = dict()
    setupEnable = True
    truePosition = Position(0, 0, 0)
    tagColor = ["b", "y", "c"]

    opts, args = getopt.getopt(argv, "cdfghilsxyz:o", [
        "channel=", "database=", "filter=", "graph=", "ip=", "label=",
        "setup=", "x=", "y=", "z="
    ])
    del (args)

    if len(opts) == 0:
        print(
            "A label must be set for the test to start: filter_test.py --label '<label>'"
        )
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print(
                "A label must be set for the test to start: filter_test.py --label '<label>'"
            )
            sys.exit()
        elif opt in ("-i", "--ip"):
            ip = arg
            print(ip)
        elif opt in ("-l", "--label"):
            label = arg
            print("Label for test: ", label)
        elif opt in ("-g", "--graph"):
            GRAPH_ENABLED = True
        elif opt in ("-d", "--database"):
            DB_ENABLED = True
        elif opt in ("-s", "--setup"):
            if str(arg) == "true":
                setupEnable = True
            else:
                configFile = str(arg)
        elif opt in ("-x", "--trueX"):
            print("x: ", arg)
            truePosition.x = float(arg)
        elif opt in ("-y", "--trueY"):
            truePosition.y = float(arg)
        elif opt in ("-z", "--trueZ"):
            truePosition.z = float(arg)
        else:
            print(
                "A label must be set for the test to start: filter_test.py --label '<label>'"
            )
            sys.exit(2)
    if truePosition.x == 0 and truePosition.y == 0 and truePosition.z == 0:
        truePosition = "NULL"
    else:
        truePosition = (truePosition.x, truePosition.y, truePosition.z)
        print(truePosition)
    if not label:
        print(
            "A label must be set for the test to start: filter_test.py --label '<label>'"
        )
        sys.exit(2)

    interval = Interval.Interval(2, sendServerInfo, args=[
        ip,
    ])
    print("Starting positioning test... Press CTRL + C to stop.")
    interval.start()

    if setupEnable:
        # Run configuration of node setup
        setupConfig = dict()
        setupConfig["listenSocket"] = listenSocket
        setupConfig["broadcastSocket"] = broadcastSocket

        nodes = setup.setupNodes(setupConfig, fromFile=False)
        configFile = "nodeConfig.txt"
        print(nodes)
    else:
        nodes = setup.setupNodes(None, fromFile=True, fileName=configFile)

    tags = dict()

    if GRAPH_ENABLED:
        import matplotlib.pyplot as plt
        plt.ion()
        fig = plt.figure()
        ax = fig.add_subplot(111)
        plt.axis([-3, 10, -3, 10])

        ax.set_aspect(1)
        ax.grid(color='#cccccc', linestyle='-', linewidth=1)

        for _, node in nodes.items():
            node.setActiveStatus(False)

    while True:
        try:
            sql = ''
            rawData, addr = listenSocket.recvfrom(1024)
            data = json.loads(rawData)

            if not "nodeID" in data:
                continue

            ip = addr[0]
            nodeID = data['nodeID']
            timestamp = data['timestamp']
            address = data['address']
            if "txPower" in data:
                txPower = data['txPower']
            else:
                txPower = defaultTxPower
            rssi = data['RSSI']
            crc = data['CRC']
            lpe = data['LPE']
            counter = data['counter']
            syncController = data['syncController']
            channel = data['channel']

            # Get true node position from config file or setup
            nodePosition = (nodes[nodeID].position.x, nodes[nodeID].position.y,
                            nodes[nodeID].position.z)

            sql = "INSERT INTO `%s` VALUES(NULL, NULL, '%s', '%s', %d, '%s', %d, %d, %d, %d, NULL, %d, NULL, %d, %d, %d, '%s', '%s', NULL, NULL, '%s', '%s')" % (
                DB_TABLE, nodeID, ip, timestamp, address, channel, counter,
                txPower, rssi, 0, crc, lpe, syncController, label, configFile,
                nodePosition, settings)

            # If CRC or LPE fails, store to database and move on to next iteration
            if crc == 0 or lpe == 1:
                if DB_ENABLED:
                    sql = "INSERT INTO `%s` VALUES(NULL, NULL, '%s', '%s', %d, '%s', %d, %d, %d, %d, NULL, %d, NULL, %d, %d, %d, '%s', '%s', NULL, NULL, '%s', '%s')" % (
                        DB_TABLE, nodeID, ip, timestamp, address, channel,
                        counter, txPower, rssi, 0, crc, lpe, syncController,
                        label, configFile, nodePosition, settings)
                    cursor.execute(sql)
                    db.commit()
                continue

            if nodeID not in nodes:
                continue

            if address not in nodes[nodeID].tags:
                nodes[nodeID].tags[address] = Tag(address)
                nodes[nodeID].tags[address].currentCounter = counter
                nodes[nodeID].tags[address].currentCounterAdvCount = 0
                if address not in tags:
                    tags[address] = (1, 1, 1)
                    allPositions[address] = list()

            nodes[nodeID].setActiveStatus(True)
            nodes[nodeID].tags[address].rssi.append(rssi)

            if nodes[nodeID].tags[address].currentCounter == counter:
                nodes[nodeID].tags[address].currentCounterAdvCount += 1
                if DB_ENABLED:
                    sql = "INSERT INTO `%s` VALUES(NULL, NULL, '%s', '%s', %d, '%s', %d, %d, %d, %d, NULL, %d, NULL, %d, %d, %d, '%s', '%s', NULL, '%s', '%s', '%s')" % (
                        DB_TABLE, nodeID, ip, timestamp, address, channel,
                        counter, txPower, rssi, 0, crc, lpe, syncController,
                        label, configFile, truePosition, nodePosition,
                        settings)
            else:
                nodes[nodeID].tags[address].currentCounter = counter
                nodes[nodeID].tags[address].currentCounterAdvCount = 1

                selectedChannelRssi = max(nodes[nodeID].tags[address].rssi)

                nodes[nodeID].tags[address].kalman.predict()
                nodes[nodeID].tags[address].kalman.update(selectedChannelRssi)
                filteredRssi = nodes[nodeID].tags[address].kalman.x[0]
                nodes[nodeID].tags[address].filteredRssi = filteredRssi
                nodes[nodeID].tags[address].rssi = list()

                if DB_ENABLED:
                    sql = "INSERT INTO `%s` VALUES(NULL, NULL, '%s', '%s', %d, '%s', %d, %d, %d, %d, %f, %d, NULL, %d, %d, %d, '%s', '%s', NULL, '%s', '%s', '%s')" % (
                        DB_TABLE, nodeID, ip, timestamp, address, channel,
                        counter, txPower, rssi, filteredRssi, 0, crc, lpe,
                        syncController, label, configFile, truePosition,
                        nodePosition, settings)

            if totalCounter > 0 and totalCounter % 6 == 0:
                if GRAPH_ENABLED:
                    ax.cla()
                    ax.set_xlim((-5, 10))
                    ax.set_ylim((-5, 15))
                    plt.grid()

                tagKey = -1

                for tagAddress in tags:
                    tagKey += 1
                    positions = list()
                    color = 'k'

                    for _, node in nodes.items():
                        if node.getActiveStatus(
                        ) == False or tagAddress not in node.tags:
                            continue
                        x = node.position.x
                        y = node.position.y
                        z = node.position.z

                        # Log-distance path loss model
                        filteredRssi = node.tags[tagAddress].filteredRssi
                        node.tags[
                            tagAddress].distance = distance.logDistancePathLoss(
                                filteredRssi,
                                rssi_d0=LOG_DISTANCE_RSSI_D0,
                                d0=LOG_DISTANCE_D0,
                                n=LOG_DISTANCE_N,
                                stDev=LOG_DISTANCE_ST_DEV)

                        radius = node.tags[tagAddress].distance
                        positions.append((x, y, z, radius))
                        if DB_ENABLED:
                            sql = "INSERT INTO `%s` VALUES(NULL, NULL, '%s', '%s', %d, '%s', %d, %d, %d, %d, %f, NULL, %f, %d, %d, %d, '%s', '%s', NULL, '%s', '%s', '%s')" % (
                                DB_TABLE, nodeID, ip, timestamp, address,
                                channel, counter, txPower, rssi, filteredRssi,
                                radius, crc, lpe, syncController, label,
                                configFile, truePosition, nodePosition,
                                settings)

                        if GRAPH_ENABLED:
                            circle = plt.Circle((x, y),
                                                radius=radius,
                                                color=color,
                                                alpha=0.1)
                            center = plt.Circle((x, y),
                                                radius=0.1,
                                                color='r',
                                                alpha=1)

                            ax.add_patch(circle)
                            ax.add_patch(center)

                    if len(positions) >= NUMBER_OF_NODES_TO_USE:
                        sortedPositions = sorted(positions, key=lambda x: x[3])
                        estimatedPosition = multi.multilateration(
                            sortedPositions[:NUMBER_OF_NODES_TO_USE],
                            dimensions=POSITION_DIMENSIONS)
                        allPositions[tagAddress].append(estimatedPosition)
                        tags[tagAddress] = estimatedPosition

                        if truePosition != "NULL":
                            error_3d = np.sqrt(
                                np.square(estimatedPosition[0] -
                                          truePosition[0]) +
                                np.square(estimatedPosition[1] -
                                          truePosition[1]) +
                                np.square(estimatedPosition[2] -
                                          truePosition[2]))

                            error_2d = np.sqrt(
                                np.square(estimatedPosition[0] -
                                          truePosition[0]) +
                                np.square(estimatedPosition[1] -
                                          truePosition[1]))

                            ax.text(
                                11, 3, ''.join(
                                    ("Error 2D: ", str(round(error_2d, 2)))))
                            ax.text(
                                11, 1, ''.join(
                                    ("Error 3D: ", str(round(error_3d, 2)))))
                            print("x: ", round(estimatedPosition[0],
                                               2), "\ty: ",
                                  round(estimatedPosition[1], 2), "\tz: ",
                                  round(estimatedPosition[2], 2),
                                  "\t Error 3D: ", round(error_3d,
                                                         2), "\tError 2D: ",
                                  round(error_2d, 2))

                        if DB_ENABLED:
                            sql = "INSERT INTO `%s` VALUES(NULL, NULL, '%s', '%s', %d, '%s', %d, %d, %d, %d, %f, NULL, %f, %d, %d, %d, '%s', '%s', '%s', '%s', '%s', '%s')" % (
                                DB_TABLE, nodeID, ip, timestamp, address,
                                channel, counter, txPower, rssi, filteredRssi,
                                radius, crc, lpe, syncController, label,
                                configFile, estimatedPosition, truePosition,
                                nodePosition, settings)

                        if GRAPH_ENABLED:
                            positionIndicator = plt.Circle(
                                estimatedPosition,
                                radius=0.20,
                                color=tagColor[tagKey],
                                alpha=1)
                            ax.add_patch(positionIndicator)

                            if truePosition != "NULL":
                                truePositionIndicator = plt.Circle(
                                    truePosition,
                                    radius=0.20,
                                    color="g",
                                    alpha=1)
                                ax.add_patch(truePositionIndicator)

                if GRAPH_ENABLED:
                    plt.draw()
                    plt.pause(0.0001)

            if DB_ENABLED:
                cursor.execute(sql)
                db.commit()
            totalCounter += 1

        except KeyboardInterrupt:
            print("Shutting down interval...")
            interval.stop()
            if GRAPH_ENABLED:

                ax.cla()
                ax.set_xlim((-5, 10))
                ax.set_ylim((-5, 15))
                plt.grid()
                for _, node in nodes.items():
                    x = node.position.x
                    y = node.position.y
                    z = node.position.z
                    center = plt.Circle((x, y), radius=0.1, color='r', alpha=1)
                    ax.add_patch(center)

                tagKey = -1
                for tagAddress in tags:
                    x = list()
                    y = list()
                    tagKey += 1
                    for position in allPositions[tagAddress][30:]:
                        x.append(position[0])
                        y.append(position[1])

                        plt.plot(x, y, '-', color=tagColor[tagKey], alpha=0.1)

                    if truePosition != "NULL":
                        truePositionIndicator = plt.Circle(truePosition,
                                                           radius=0.20,
                                                           color="g",
                                                           alpha=1)
                        ax.add_patch(truePositionIndicator)

                plt.draw()
                plt.pause(0.0001)
                input("Press enter to exit")
            break
Exemplo n.º 15
0
        :rtype: List[int]
        """
        a = list(enumerate(intervals))
        a.sort(key=lambda (i, x): x.start)
        b = list(enumerate(intervals))
        b.sort(key=lambda (i, x): x.end)

        n = len(intervals)
        k = 0
        r = [-1 for i in xrange(n)]
        for i, x in b:
            if k < n and x.end <= a[k][1].start:
                r[i] = a[k][0]
            else:
                while k < n and x.end > a[k][1].start:
                    k += 1
                if k < n:
                    r[i] = a[k][0]
                else:
                    r[i] = -1
        return r


if __name__ == '__main__':
    from utils import Interval

    f = Solution().findRightInterval
    assert f(Interval.make_list([[1, 2]])) == [-1]
    assert f(Interval.make_list([[3, 4], [2, 3], [1, 2]])) == [-1, 0, 1]
    assert f(Interval.make_list([[1, 4], [2, 3], [3, 4]])) == [-1, 2, -1]
Exemplo n.º 16
0
def main(argv):
    global GRAPH_ENABLED
    global DB_ENABLED
    channel = False
    distance = None
    label = None
    sampleNumber = 0
    samples = dict()
    samples["channel_37"] = list()
    samples["channel_38"] = list()
    samples["channel_39"] = list()

    opts, args = getopt.getopt(argv,"cdghil:o",["channel=", "distance=", "graph=", "ip=", "label="])
    del(args)

    if len(opts) == 0:
        print("A distance and label must be set for the test to start: general.py --distance <distance> --label '<label>'")
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print("A distance and label must be set for the test to start: general.py --distance <distance> --label '<label>'")
            sys.exit()
        elif opt in ("-i", "--ip"):
            ip = arg
            print(ip)
        elif opt in ("-d", "--distance"):
            distance = float(arg)
            print("Test for distance: ", distance)
        elif opt in ("-l", "--label"):
            label = arg
            print("Label for test: ", label)
        elif opt in ("-c", "--channel"):
            channel = int(arg)
        elif opt in ("-g", "--graph"):
            GRAPH_ENABLED = True
        else:
            print("A distance and label must be set for the test to start: general.py --distance <distance> --label '<label>'")
            sys.exit(2)

    if not distance or not label:
        print("A distance and label must be set for the test to start: general.py --distance <distance> --label '<label>'")
        sys.exit(2)
 
    if GRAPH_ENABLED:
        import matplotlib.pyplot as plt
        plt.ion()
        plt.axis([36, 40, -80, -20])
        
        plt.legend(loc="lower right")

    interval = Interval.Interval(2, sendServerInfo, args=[ip,])
    print("Starting... Press CTRL + C to stop.")
    interval.start() 


    while True:
        try:
            rawData, addr = listenSocket.recvfrom(1024)
            data = json.loads(rawData)


            ip = addr[0]
            nodeID = data['nodeID']
            timestamp = data['timestamp']
            address = data['address']
            rssi = data['RSSI']
            crc = data['CRC']
            lpe = data['LPE']
            counter = data['counter']
            syncController = data['syncController']
            channel = data['channel']

            if DB_ENABLED:
                sql = "INSERT INTO rssi_data VALUES(NULL, NULL, '%s', '%s', %d, '%s', %d, %d, %d, NULL, %f, NULL, %d, %d, %d, '%s')" % (nodeID, ip, timestamp , address, channel, counter, rssi, distance, crc, lpe, syncController, label)

                cursor.execute(sql)
                db.commit()

            if GRAPH_ENABLED:
                color = 'k'
                name = "Unknown channel"
                if channel == 37:
                    color = 'r'
                    name = "Channel 37"
                elif channel == 38:
                    color = 'b'
                    name = "Channel 38"
                elif channel == 39:
                    color = 'g'
                    name = "Channel 39"
                
                plt.scatter(channel, rssi, color=color, alpha=0.1, label=name)
                plt.pause(0.0001)

            sampleNumber += 1

            if crc == 0:
                colorCode = "\u001b[31m"
            else:
                colorCode = ""

            print(colorCode, sampleNumber , "\tFrom", ip, "\tTimestamp: ", timestamp, "\tCounter: ", counter, "\tAddr.: ", address, "\tChannel: ", channel, "\tRSSI: ", rssi, "\tCRC: ", crc, "\tLPE: ", lpe, "\033[0m")

            if channel == 37:
                samples["channel_37"].append(rssi)
            elif channel == 38:
                samples["channel_38"].append(rssi)
            elif channel == 39:
                samples["channel_39"].append(rssi)

        except KeyboardInterrupt:
            print("")
            print("Average for channel 37 after ", len(samples["channel_37"]), " samples: ", np.average(samples["channel_37"]), "Standard deviation",  np.std(samples["channel_37"], ddof=1,))
            print("Average for channel 38 after ", len(samples["channel_38"]), " samples: ", np.average(samples["channel_38"]), "Standard deviation",  np.std(samples["channel_38"], ddof=1,))
            print("Average for channel 39 after ", len(samples["channel_39"]), " samples: ", np.average(samples["channel_39"]), "Standard deviation",  np.std(samples["channel_39"], ddof=1,))
            print("")
            print("Shutting down...")
            interval.stop()
            break
                        everoverlap = False
                        if chromosome_name_ref in chromosome_gene_list_ref:
                            for g in chromosome_gene_list_ref[
                                    chromosome_name_ref]:
                                if g.start < end and g.start > start:
                                    everoverlap = True
                                if start < g.end and start > g.start:
                                    everoverlap = True

                        if (not chromosome_name_ref
                                in ref_black_list_interval):
                            ref_black_list_interval[
                                chromosome_name_ref] = np.empty([0, 1],
                                                                Interval)
                        gRef = Interval(start, end)
                        ref_black_list_interval[
                            chromosome_name_ref] = np.append(
                                ref_black_list_interval[chromosome_name_ref],
                                [gRef])

                        ms2 = re.search('^(\d+)[SH]', m.group(6))
                        if ms2 != None:
                            query_start = int(ms2.group(1))
                            query_end = query_start + len(m.group(10)) - 1
                            query_chr = m.group(1)
                            everoverlap_query = False
                            if query_chr in chromosome_gene_list_query:
                                for g in chromosome_gene_list_query[query_chr]:
                                    if g.start < query_end and g.start > query_start:
                                        everoverlap_query = True
Exemplo n.º 18
0
def main(argv):
    channel = False
    global GRAPH_ENABLED
    opts, args = getopt.getopt(argv, "cghilt:o", [
        "channel=", "graph=", "ip=", "addr=", "address=", "label=", "txpower="
    ])
    del (args)

    startTime = time.time()
    label = ''
    txPower = DEFAULT_TX_POWER

    counter_37 = {"total": 0, "CRC_error": 0, "LPE_error": 0}
    counter_38 = {"total": 0, "CRC_error": 0, "LPE_error": 0}
    counter_39 = {"total": 0, "CRC_error": 0, "LPE_error": 0}

    if len(opts) == 0:
        print("node_listener.py -i <server IP address>")
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print("node_listener.py -i <server IP address>")
            sys.exit()
        elif opt in ("-i", "-a", "--ip", "--address"):
            ip = arg
            print(ip)
        elif opt in ("-c", "--channel"):
            channel = int(arg)
        elif opt in ("-g", "--graph"):
            GRAPH_ENABLED = True
        elif opt in ("-l", "--label"):
            label = arg
        elif opt in ("-t", "--txpower"):
            txPower = arg
        else:
            print("node_listener.py -i <server IP address>")
            sys.exit(2)

    if GRAPH_ENABLED:
        import matplotlib.pyplot as plt
        plt.ion()
        plt.axis([0, 3, -100, 0])

    interval = Interval.Interval(2, sendServerInfo, args=[
        ip,
    ])
    print("Starting Interval, press CTRL+C to stop.")
    interval.start()

    while True:
        try:
            rawData, addr = listenSocket.recvfrom(1024)
            data = json.loads(rawData)
            ip = addr[0]
            nodeID = data['nodeID']
            timestamp = data['timestamp']
            address = data['address']
            channel = data['channel']
            counter = data['counter']
            rssi = data['RSSI']
            crc = data['CRC']
            lpe = data['LPE']
            syncController = data['syncController']

            printing = False

            if "MAC" in data:
                print("WHO AM I message from MAC", data["MAC"], ", IP is ",
                      data["IP"])
                continue

            #delta = data['timestamp'] - times[addr[0]]
            times[addr[0]] = data['timestamp']

            if DB_ENABLED:
                sql = "INSERT INTO %s VALUES(NULL, NULL, '%s', '%s', %d, '%s', %d, %d, %d, NULL, NULL, NULL, %d, %d, %d, '%s', %d)" % (
                    DB_TABLE_NAME, nodeID, ip, timestamp, address, channel,
                    counter, rssi, crc, lpe, syncController, label, txPower)
                cursor.execute(sql)
                db.commit()

            #dist = distance.empiricalDistance(data['RSSI'], 4, -45.0)

            if data['channel'] == 37:
                counter_37["total"] += 1
                color = 'r'
                if data["CRC"] == 0:
                    counter_37["CRC_error"] += 1
                if data["LPE"] == 1:
                    counter_37["LPE_error"] += 1
            elif data['channel'] == 38:
                counter_38["total"] += 1
                color = 'r'
                if data["CRC"] == 0:
                    counter_38["CRC_error"] += 1
                if data["LPE"] == 1:
                    counter_38["LPE_error"] += 1
                color = 'g'
            if data['channel'] == 39:
                counter_39["total"] += 1
                color = 'r'
                if data["CRC"] == 0:
                    counter_39["CRC_error"] += 1
                if data["LPE"] == 1:
                    counter_39["LPE_error"] += 1
                color = 'b'

            if GRAPH_ENABLED and data['channel'] == 37:
                if addr[0] == "10.0.0.11":
                    color = 'r'
                    dist = distance.empiricalDistance(data['RSSI'], 4, -45.0)
                    plt.scatter(dist, data['RSSI'], color=color, alpha=0.1)
                elif addr[0] == "10.0.0.13":
                    color = 'g'
                    dist = distance.empiricalDistance(data['RSSI'], 4, -45.0)
                    plt.scatter(dist, data['RSSI'], color=color, alpha=0.1)
                '''
                color = 'g'
                dist = distance.ituDistance(data['RSSI'], distance.bleChannelToFrequency(data['channel']), 30, 14.1, 1)
                plt.scatter(dist, data['RSSI'], color=color, alpha=0.1)

                color = 'b'
                dist = distance.logDistancePathLoss(data['RSSI'], -42.0, 1.0, 4, 0)
                plt.scatter(dist, data['RSSI'], color=color, alpha=0.1)
                #plt.show()
                '''

                plt.pause(0.0001)

            if channel != False:
                if int(data['channel']) == channel:
                    printing = True
            else:
                printing = True

            if printing:
                if data['CRC'] == 0:
                    colorCode = "\u001b[31m"
                else:
                    colorCode = ""
                print(colorCode, " From", addr[0], "\tTimestamp: ",
                      times[addr[0]], "\tCounter: ", data['counter'],
                      "\tAddr.: ", data['address'], "\tChannel: ",
                      data['channel'], "\tRSSI: ", data['RSSI'], "\tCRC: ",
                      data['CRC'], "\tLPE: ", data['LPE'], "\tSyncer: ",
                      data['syncController'], "\033[0m")

        except KeyboardInterrupt:
            print("Shutting down interval...")
            interval.stop()
            print("")
            print("Recording time: ", time.time() - startTime)
            print("Channel 37 - total: ", counter_37["total"],
                  "\t CRC errors: ", counter_37["CRC_error"],
                  "\t LPE errors: ", counter_37["LPE_error"])
            print("Channel 38 - total: ", counter_38["total"],
                  "\t CRC errors: ", counter_38["CRC_error"],
                  "\t LPE errors: ", counter_38["LPE_error"])
            print("Channel 39 - total: ", counter_39["total"],
                  "\t CRC errors: ", counter_39["CRC_error"],
                  "\t LPE errors: ", counter_39["LPE_error"])
            break
Exemplo n.º 19
0
def main(argv):
    global nodes
    opts, args = getopt.getopt(
        argv, "cghi:o", ["channel=", "graph=", "ip=", "addr=", "address="])
    del (args)
    totalCounter = 0

    if len(opts) == 0:
        print("udp_listener.py -i <server IP address>")
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print("udp_listener.py -i <server IP address>")
            sys.exit()
        elif opt in ("-i", "-a", "--ip", "--address"):
            ip = arg
            print(ip)
        else:
            print("udp_listener.py -i <server IP address>")
            sys.exit(2)

    interval = Interval.Interval(2, sendServerInfo, args=[
        ip,
    ])
    print("Starting Interval, press CTRL+C to stop.")
    interval.start()

    # Initiate figure
    fig = plt.figure()
    plt.ion()
    ax = fig.add_subplot(1, 1, 1)
    ax.set_xlim((-5, 5))
    ax.set_ylim((-5, 5))
    x0, x1 = ax.get_xlim()
    y0, y1 = ax.get_ylim()
    ax.set_aspect(abs(x1 - x0) / abs(y1 - y0))

    while True:
        try:
            rawData, addr = listenSocket.recvfrom(1024)
            data = json.loads(rawData)
            ip = addr[0]
            nodeID = data['nodeID']
            timestamp = data['timestamp']
            address = data['address']
            rssi = data['RSSI']
            crc = data['CRC']
            lpe = data['LPE']
            counter = data['counter']
            times[ip] = timestamp

            if crc == 1 and lpe == 0:
                if nodeID not in nodes:
                    nodes[nodeID] = Node(nodeID)

                if address not in nodes[nodeID].tags:
                    nodes[nodeID].tags[address] = Tag(address)
                    nodes[nodeID].tags[address].currentCounter = counter
                    nodes[nodeID].tags[address].currentCounterAdvCount = 0
                    if address not in tags:
                        tags[address] = 1

                if nodes[nodeID].tags[address].currentCounter == counter:
                    nodes[nodeID].tags[address].currentCounterAdvCount += 1
                else:
                    nodes[nodeID].tags[address].currentCounter = counter
                    nodes[nodeID].tags[address].currentCounterAdvCount = 1

                if nodes[nodeID].tags[address].currentCounterAdvCount == 3:
                    nodes[nodeID].tags[address].rssi.append(rssi)
                    selectedChannelRssi = max(nodes[nodeID].tags[address].rssi)

                    nodes[nodeID].tags[address].kalman.predict()
                    nodes[nodeID].tags[address].kalman.update(
                        selectedChannelRssi)
                    filteredRssi = nodes[nodeID].tags[address].kalman.x[0]
                    nodes[nodeID].tags[address].filteredRssi = filteredRssi

                    # Log-distance path loss model
                    nodes[nodeID].tags[address].distance = round(
                        distance.logDistancePathLoss(filteredRssi,
                                                     rssi_d0=-40.0,
                                                     d0=1.0,
                                                     n=2.6,
                                                     xo=1.1),
                        ndigits=2)

                    print("node ID: ", nodeID, "\tIP: ", ip, "\tAddress: ",
                          address, "\tFiltered RSSI: ",
                          nodes[nodeID].tags[address].filteredRssi,
                          "\tDistance: ", nodes[nodeID].tags[address].distance)

                    nodes[nodeID].tags[address].rssi = list()
                    totalCounter += 1

                if totalCounter > 0 and totalCounter % 20 == 0:
                    ax.cla()
                    ax.set_xlim((-3, 5))
                    ax.set_ylim((-3, 5))
                    plt.grid()
                    color = "b"

                    for tagAddress in tags:
                        positions = list()
                        if color == "b":
                            color = "k"
                        else:
                            color = "b"

                        for _, node in nodes.items():
                            x = node.position.x
                            y = node.position.y
                            radius = node.tags[tagAddress].distance
                            positions.append((x, y, radius))

                            circle = plt.Circle((x, y),
                                                radius=radius,
                                                color=color,
                                                alpha=0.1)
                            center = plt.Circle((x, y),
                                                radius=0.1,
                                                color='r',
                                                alpha=1)
                            ax.add_patch(circle)
                            ax.add_patch(center)

                        if len(positions) > 2:
                            position = multi.multilateration(positions)
                            tags[tagAddress] = position

                        positionIndicator = plt.Circle(tags[tagAddress],
                                                       radius=0.15,
                                                       color=color,
                                                       alpha=1)
                        ax.add_patch(positionIndicator)

                    plt.draw()
                    plt.pause(0.0001)

        except KeyboardInterrupt:
            print("Shutting down interval...")
            interval.stop()
            break