added wolf's code
[swamp-bikeopera:code.git] / src / swamp
1 #!/usr/bin/lua
2
3 package.path = package.path..";/home/startx/projects/swamp-machine/trunk/src/?.lua;/home/startx/projects/swamp-machine/trunk/lib/?.lua"
4
5 --require 'swamp'
6 require 'gps'
7 require 'posix'
8 require 'poly'
9 require 'map'
10 require 'direction'
11 require 'random'
12 require 'audioc'
13 require 'socket'
14
15 dofile '/home/startx/projects/swamp-machine/trunk/config/swamp.conf'
16 dofile '/home/startx/projects/swamp-machine/trunk/config/random.conf'
17
18 os.execute("touch /tmp/fake_gps > /dev/null")
19
20 function gpslog(lat, lon)
21    local  file = io.open(CONFIG.install_path.."log/gps.log", "a")
22    local logmessage = os.date().." "..lat.." "..lon.."\n"
23    file:write(logmessage)
24    file:close()
25 end
26
27 function swamplog(logmessage)
28    local  file = io.open(CONFIG.install_path.."log/swamp.log", "a")
29    file:write(os.date().." "..logmessage.."\n")
30    file:close()
31    if CONFIG.debug then
32      io.write(os.date().." "..logmessage.."\n")
33    end
34
35 end
36
37
38 -- load the mapfile
39 ZONES = map.load()
40
41 swamplog("mapfile loaded sucessfully")
42
43 -- bootstrap the system
44 -- start the player
45 os.execute("/home/startx/projects/swamp-machine/trunk/src/swamp_playd &") --  > /dev/null &")
46
47 swamplog("swamp_playd started")
48
49 print("waiting for playd")
50 posix.sleep(600)
51
52 -- start the gps daemon
53 os.execute("/home/startx/projects/swamp-machine/trunk/src/swamp_gpsd &")
54
55 swamplog("swamp_gpsd started")
56 print("swamp_gpsd started")
57
58 -- we give the gps some time to catch up
59 posix.sleep(4)
60
61 --- connect to the gps pipe
62 local file = io.open("/tmp/swamp_gps","r")
63 --- this is the main loop
64
65 local last_lat, last_lon
66 local last_time = os.time()
67
68 local last_zone = 0
69
70 print("STARTING LOOP")
71 while true do
72     math.randomseed(socket.gettime())
73     local ani = random.animal()
74     local chan = random.channel()                      
75
76     local line = file:read()
77         if line then
78
79     ----- random events
80
81     if random.by_probability(2) then
82        print("choosing ..")
83            print("PLAYING RANDOM: "..ani.." "..chan)
84            audioc.stop(ani)
85            audioc.play(ani, chan)
86     end
87
88
89
90           --- THIS IS JUST A FAKE FOR LOCAL TESTING
91           if CONFIG.fake_gps then
92             local fakezone = gps.fake_zone()
93               if fakezone ~= 0 then
94                if fakezone ~= last_zone then
95                       audioc.stop(last_zone)
96                print("NEW FAKEZONE: "..fakezone)
97                audioc.play(fakezone)
98                last_zone = fakezone
99                end
100             end
101           end
102
103
104            local lat, lon = string.match(line, "^(.-)%s(.-)$") 
105            if tonumber(lat) and tonumber(lon) then
106               gpslog(lat, lon)
107
108               if not last_lat then
109                  last_lat = lat
110                  last_lon = lon
111               end
112
113               -- caculate where we are              
114               for i, polygon in ipairs(ZONES) do
115                   location = poly.is_in_polygon(polygon, lat , lon)
116                   if location then
117                      --- trigger static sound
118                      swamplog("INSIDE "..i)
119
120              
121                      if i ~= last_zone then 
122                         audioc.stop(last_zone)
123                         swamplog("stopping "..last_zone)
124                         swamplog("playing "..i)
125                         audioc.play(i)
126                         last_zone = i
127                      end
128
129                      local area_centre_lat , area_centre_lon = poly.centroid(ZONES[i])
130                      swamplog("distance from centre: "..std.round(poly.distance(area_centre_lat , area_centre_lon, tonumber(lat), tonumber(lon)), 7)) 
131                   end
132               end
133                
134
135                -- calculate distance since last time
136                local time_diff = os.time() - last_time
137  
138                if time_diff > CONFIG.direction_track_time then
139                   -- print("time", last_lat, last_lon)
140                   last_time = os.time()
141                if tonumber(last_lat) and tonumber(last_lon) then
142                   swamplog("In last 10 seconds: "..std.round(poly.distance(tonumber(lat), tonumber(lon), tonumber(last_lat), tonumber(last_lon)), 7))
143                   local direction_resolved = direction.resolve(poly.direction(tonumber(lat), tonumber(lon), tonumber(last_lat), tonumber(last_lon)))
144                   --print(direction_resolved) 
145                   swamplog("direction: "..direction_resolved)
146                   last_lat = lat
147                   last_lon = lon
148                end
149                end
150
151            end
152
153
154            posix.sleep(1)
155         end
156 end
157
158