]> www.average.org Git - loctrkd.git/blob - README.md
opencellid: use temp table rather than memory db
[loctrkd.git] / README.md
1 # A server to collect data from GPS Locators
2
3 Supported:
4
5 * zx303 ZhongXun Topin Locator
6   zx303 GPS+GPRS module is a cheap and featureful GPS tracker for pets,
7   children, elderly family members, and, of course, illegal tracking
8   of people and objects, though the latter absolutely must not be done.
9 * Some watches-locators, sometimes identified as D99 or similar
10
11 ## Introduction
12
13 This work is inspired by [this project](https://github.com/tobadia/petGPS),
14 but it is more of a complete re-implementation than a derived work.
15 There also exists an
16 [industrial strength open source server](https://www.traccar.org/)
17 that supports multiple types of trackers.
18
19 When powered up, the module makes TCP connection to the configured
20 (via SMS) server, identifies itself (with IMEI) in the first message,
21 and continues to send periodic messages with location and other status
22 updates. Some of these messages require a response from the server.
23 In particular, when the module has no GPS coverage, it sends information
24 about nearby GSM+ cell towers and WiFi access points, to which the server
25 is expected to respond with a message containing approximate location
26 derived from this data. To do that, the server may need to consult with
27 some external service.
28
29 Because we would usually want location information reach consumer
30 instantly upon arrival, and _also_ to be stored, it makes sense to
31 design the system in "microservices" way, using a message bus in
32 "publish-subscribe" model. And then, since we already have a
33 message-passing infrastructure anyway, it makes sense to decouple
34 the server process that maintains TCP connections with the the tracker
35 terminals from the processes that analyses messages and prepares responses.
36
37 This leads us to this implementation, that has consists of five daemons
38 that talk to each other over Zeromq:
39
40 - **collector** that keeps open TCP connections with the terminals
41   and publishes received messages _and_ sent responses on the message
42   bus,
43 - **storage** that subscribes to the messages and responses from the
44   collector and stores them in a database,
45 - **termconfig** that subscribes to messages that need nontrivial
46   response (most of them are about configuring various settings in
47   the terminal, hence the name), and sends responses to the collector
48   for relaying to the terminal,
49 - **lookaside** that subscribes to "rough" location messages, queries
50   an external source (in our implementation, either google maps "API",
51   or a local opencellid database), and prepares responses with
52   approximated location, and
53 - **wsgateway** that is a websockets server that translates messages
54   between our internal zeromq bus and websocket clients, i.e. web
55   pages. This daemon is also capable of responding to http with
56   a single html file. This functionality is mainly for debugging.
57   Users of the package are expected to implement their own web
58   application that communicates with this server.
59
60 There is also a command-line tool to send messages to the terminal.
61 A number of useful actions can be initiated in this way.
62
63 ## Configuring the Terminal
64
65 Send SMS to the telephone number of the SIM card plugged in the terminal,
66 with the text
67
68 ```
69 server#<your_server_address>#<port>#
70 ```
71
72 Server address may be FQDN or a literal IP address. Port is a number;
73 by default, this application listens on the port 4303. A different
74 port can be configured in the config file.
75
76 It is recommended to always keep the service running while the terminal
77 is powered up: it is possible that the terminal is programmed to reset
78 itself to the default configuration if it cannot connect to the server
79 for prolonged time.
80
81 ## Websocket messages
82
83 Websockets server communicates with the web page using json encoded
84 text messages. The only supported message from the web page to the
85 server is subscription message. Recognised elements are:
86
87 - **type** - a string that must be "subscribe"
88 - **backlog** - an integer specifying how many previous locations to
89   send for the start. Limit is per-imei.
90 - **imei** - a list of 16-character strings with IMEIs of the
91   tracker terminals to watch.
92
93 Each subscription request nullifies preexisting list of IMEIs
94 associated with the web client, and replaces it with the list supplied
95 in the message.
96
97 Example of a subscription request:
98
99 ```
100 {"imei":["8354369077195199"],
101  "type":"subscribe",
102  "timestamp":1652134234657,
103  "backlog":5}
104 ```
105
106 Server sends to the client a backlog of last locations of the
107 terminals, that it fetches from the database maintained by the
108 storage service, one location per websocket message. It then
109 continues to send further messages when they are received from
110 the module, in real time, including gps location, responses with
111 approximated location, and status with the precentage of battery
112 charge.
113
114 Example of a location message:
115
116 ```
117 {"type": "location",
118  "imei": "8354369077195199",
119  "timestamp": "2022-05-09 21:52:34.643277+00:00",
120  "longitude": 17.465816,
121  "latitude": 47.52013,
122  "accuracy": "gps"} // or "approximate"
123 ```
124
125 Example of a status message
126
127 ```
128 {"type": "status",
129  "imei": "8354369077195199",
130  "timestamp": "2022-05-09 21:52:34.643277+00:00",
131  "battery": 46}
132 ```
133
134 ## Lookaside service
135
136 When the terminal has no gps reception, it uses secondary sources of
137 location hints: list of nearby cell towers, and list of MAC addresses
138 of nearby WiFi access point, with signal strength. It expects a
139 response from the server with approximated location. In order to get
140 such approximation, the server system needs a source of information
141 about cell towers and/or WiFi access points in the area. We support
142 two ways to get approximated location: querying Google geolocation
143 service, and using locally installed database filled with data
144 downloaded from opencellid crowdsourced source. For both options,
145 you will need an access token. Google service is "online", you are
146 making a request for each approximation (and thus reveal location of
147 your users to Google). Opencellid service is "offline": you download
148 the file with locations of all cell towers in the country (or worldwide)
149 once, or refresh it at relatively long intervals, such as a week or a
150 month, and then all queries are fulfilled locally. Note that opencellid
151 data does not contain WiFi access points, so the approximation will
152 less accurate.
153
154 Lookaside service can be configured to use either of the options by
155 assigning `backend = opencellid` or `backend = googlemaps` in the
156 configuration file (`/etc/loctrkd.conf` by default). Then, the path to
157 the file with the auth token needs to be specified in the `[opencellid]`
158 section or `[googlemaps]` section of the configuration file respectively.
159
160 Note that in both cases, the value in the configuration file needs
161 to _point to the file_ that contains the token, rather than contain
162 the token itself. The file needs to be readable for the user under which
163 services are executed. That is the user `loctrkd` if this software was
164 installed as the Debian package.
165
166 This part of setup cannot be automated, because each user needs to
167 obtain their own access token for one of the above services.
168
169 ## Termconfig Service
170
171 To configure terminal settings, such as SOS numbers, update intervals etc.,
172 "termconfig" service consults the configuration file. It should contain
173 the section `[termconfig]`, and optionally sections named after the IMEIs
174 of individual terminals. `[termconfig]` values are used when the individual
175 section is not present.
176
177 For a bigger multi-client setup the user will want to re-implement this
178 service to use some kind of a database, with the data configurable by the
179 owners of the terminals.
180
181 ## Homepage and source
182
183 Home page is [http://www.average.org/loctrkd/](http://www.average.org/loctrkd/)
184 Get the source from the origin `git://git.average.org/loctrkd.git`
185 or from [Github mirror](https://github.com/crosser/loctrkd).