1--
2-- This file is part of Cardpeek, the smartcard reader utility.
3--
4-- Copyright 2009-2013 by 'L1L1'
5--
6-- Cardpeek is free software: you can redistribute it and/or modify
7-- it under the terms of the GNU General Public License as published by
8-- the Free Software Foundation, either version 3 of the License, or
9-- (at your option) any later version.
10--
11-- Cardpeek is distributed in the hope that it will be useful,
12-- but WITHOUT ANY WARRANTY; without even the implied warranty of
13-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14-- GNU General Public License for more details.
15--
16-- You should have received a copy of the GNU General Public License
17-- along with Cardpeek.  If not, see <http://www.gnu.org/licenses/>.
18--
19
20---------------------------------------------------------------------
21-- Most of the data and coding ideas in this file
22-- was contributed by 'Pascal Terjan', based on location
23-- data from 'Aurélien Baro'.
24---------------------------------------------------------------------
25
26require('lib.strict')
27require('etc.paris-metro')
28require('etc.paris-rer')
29
30SERVICE_PROVIDERS = {
31  [2] = "SNCF",
32  [3] = "RATP"
33}
34
35TRANSPORT_LIST = {
36  [1] = "Urban Bus",
37  [2] = "Interurban Bus",
38  [3] = "Metro",
39  [4] = "Tram",
40  [5] = "Train",
41  [8] = "Parking"
42}
43
44TRANSITION_LIST = {
45  [1] = "Entry",
46  [2] = "Exit",
47  [4] = "Inspection",
48  [6] = "Interchange (entry)",
49  [7] = "Interchange (exit)"
50}
51
52function navigo_process_events(cardenv,node_label)
53	local event_node
54	local record_node
55	local ref_node
56
57	local code_value
58	local code_transport
59	local code_transition
60	local code_transport_string
61	local code_transition_string
62	local code_string
63	local service_provider_value
64	local location_id_value
65	local sector_id
66	local station_id
67	local location_string
68
69	event_node = cardenv:find_first({label=node_label, parsed="true"})
70
71	if event_node==nil then
72	   log.print(log.WARNING,"No " .. node_label .. " found in card")
73	   return 0
74	end
75
76	for record_node in event_node:find({label="record"}) do
77
78	    -- is it RATP or SNCF ?
79	    ref_node = record_node:find_first({label="EventServiceProvider"})
80	    service_provider_value = bytes.tonumber(ref_node:get_attribute("val"))
81	    ref_node:set_attribute("alt",SERVICE_PROVIDERS[service_provider_value])
82
83	    ref_node = record_node:find_first({label="EventCode"})
84	    code_value = bytes.tonumber(ref_node:get_attribute("val"))
85
86	    -- is it a bus, a tram, a metro, ... ?
87	    code_transport = bit.SHR(code_value,4)
88	    code_transport_string  = TRANSPORT_LIST[code_transport]
89	    if code_transport_string==nil then code_transport_string = code_transport end
90
91	    -- is it an entry, an exit, ... ?
92	    code_transition = bit.AND(code_value,0xF)
93	    code_transition_string = TRANSITION_LIST[code_transition]
94	    if (code_transition_string==nil) then code_transition_string = code_transition end
95
96	    ref_node:set_attribute("alt",code_transport_string.." - "..code_transition_string)
97
98	    -- service_provider_value == RATP and code_transport in { metro, tram, train } ?
99	    if (service_provider_value==3 or service_provider_value==2) and code_transport>=3 and code_transport<=5 then
100	       ref_node = record_node:find_first({label="EventLocationId"})
101	       location_id_value = bytes.tonumber(ref_node:get_attribute("val"))
102	       sector_id = bit.SHR(location_id_value,9)
103	       station_id = bit.AND(bit.SHR(location_id_value,4),0x1F)
104
105	       if code_transport==5 and BANLIEUE_LIST[sector_id] and BANLIEUE_LIST[sector_id][station_id] then
106		     location_string = "secteur "..sector_id.." - station "..BANLIEUE_LIST[sector_id][station_id]
107               else
108	             if METRO_LIST[sector_id]~=nil then
109		        location_string = "secteur "..METRO_LIST[sector_id]['name'].." - station "
110			if METRO_LIST[sector_id][station_id]==nil then
111                           location_string = location_string .. station_id
112	                else
113                           location_string = location_string .. METRO_LIST[sector_id][station_id]
114                        end
115		     else
116		        location_string = "secteur "..sector_id.." - station "..station_id
117		     end
118	       end
119
120	       ref_node:set_attribute("alt",location_string)
121
122	    end
123	end
124end
125
126navigo_process_events(CARD,"Event logs")
127navigo_process_events(CARD,"Special events")
128
129
130