aboutsummaryrefslogtreecommitdiff
path: root/mongodb.py
blob: 4344b8887bd1502c88eac401452f4761e22b52b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#! /usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.1 $"
__date__ = "$Date: 2012/01/06 $"
__date__ = "$Date: 2012/01/09 $"
__copyright__ = "Copyright (c) 2012 Public Research Centre Henri Tudor"
__license__ = ""

# http://127.0.0.1:28017/wisafecar/users
# http://www.mongodb.org/display/DOCS/Http+Interface#HttpInterface-SimpleRESTInterface


# https://github.com/kchodorow/sleepy.mongoose/wiki/
# Insertion of a message:
#     curl --data 'docs=[{"message":"traffic jam","position":"tampere"}]' 'http://10.13.1.157:27080/wisafecar/messages/_insert'
#
# Connecting:
#     https://github.com/kchodorow/sleepy.mongoose/wiki/Connecting
# Examples:
#     curl --data 'name=wsc' http://localhost:27080/_connect
#     GET http://localhost:27080/wisafecar/users/_find?name=wsc
# Filtering:
#     If we want to get all results for the client with id=d39fc988-376b-11e1-aa3b-0022680cb7e4:
#         urllib.quote('{"id":"d39fc988-376b-11e1-aa3b-0022680cb7e4"}')
#         http://127.0.0.1:27080/wisafecar/users/_find?criteria=%7B%22id%22%3A%22d39fc988-376b-11e1-aa3b-0022680cb7e4%22%7D
#           (urllib.quote skip special caracters.)
#    If we want to find all the traffic jams:
#         urllib.quote('{"message":"traffic_jam"}')
#         http://127.0.0.1:27080/wisafecar/messages/_find?criteria=%7B%22message%22%3A%22traffic_jam%22%7D

import time

from pymongo.connection import Connection

class MongoWSC(object):
    """
    """
    def __init__(self, url='localhost', port=27017):
        """
        Instantiates the connection.
        """
        self.connection = Connection(url, port)
        self.db = self.connection.wisafecar

    #
    # Collection: users
    #
    def register_user(self, sender_uuid, user):
        """
        Insert a new user in the collection of users.
        """
        user_dic = {"uuid":sender_uuid, "name":user, \
                    "time-registration":time.time()}

        collection = self.db.users
        cursor = collection.find({"uuid":sender_uuid})
        if cursor.count() == 0:
            collection.insert(user_dic)

    def get_users(self):
        """
        Return the list of users.
        """
        collection = self.db.users
        return collection

    def print_users(self):
        """
        List and print the users.
        """
        collection = self.db.users
        cursor = collection.find()
        for d in cursor:
            print d

    def nb_users(self):
        """
        Return the number of users.
        """
        collection = self.db.users
        collection.count()

    #
    # Collection: messages
    #
    def log_message(self, sender_uuid, message):
        """
        Create a new message in the collection of messages.
        """
        message_dic = {"reporter":sender_uuid, "time":message[0], "lat":message[2].split(",")[1][:-1],
                        "long": message[2].split(",")[0][1:], "message":message[3]}

        collection = self.db.messages
        collection.insert(message_dic)

    def print_messages(self):
        """
        List and print the messages received by all the users.
        """
        collection = self.db.messages
        cursor = collection.find()
        for d in cursor:
            print d

    #
    # Collection: positions
    #
    def log_positions(self, sender_uuid, position):
        """
        Log a new position.
        """
        collection_dic = {"uuid":sender_uuid, "time":position[0], "lat":position[2].split(",")[1][:-1],
                        "long": position[2].split(",")[0][1:]}

        collection = self.db.positions
        collection.insert(collection_dic)

    def print_positions(self):
        """
        List and print the position updates of all the users.
        """
        collection = self.db.positions
        cursor = collection.find()
        for d in cursor:
            print d

    # Functions on database
    def drop_wsc_database(self):
        self.connection.drop_database('wisafecar')

if __name__ == "__main__":
    # Point of entry in execution mode.
    wsc_database = MongoWSC()
    print "Registered users:"
    wsc_database.print_users()

    print "\nPosition updates received:"
    wsc_database.print_positions()

    print "\nMessage received:"
    wsc_database.print_messages()

    print "\nDropping database..."
    wsc_database.drop_wsc_database()


    wsc_database.register_user("1d91f6fafe85", "Cedric")

    wsc_database.log_positions("1d91f6fafe85", [time.time(), "position", "(49.6229,6.18687)"])
    wsc_database.log_positions("1d91f6fafe85", [time.time(), "position", "(49.6228,6.18692)"])

    wsc_database.log_message("1d91f6fafe85", [time.time(), "alert", "(49.628,6.176)", "accident"])
    wsc_database.log_message("1d91f6fafe85", [time.time(), "alert", "(49.612,6.18689708)", "traffic_jam"])
bgstack15