-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileUpdater.py
140 lines (94 loc) · 3.37 KB
/
FileUpdater.py
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
from threading import Thread
from threading import Event
from model import EliteModel
import simplejson
import sys
class FileUpdater(Thread):
def __init__(self):
Thread.__init__(self)
self.interval = 10
self.model = EliteModel()
self.stopEvent = Event()
def run(self):
print ("Running FileUpdater")
while not self.stopEvent.wait(self.interval):
#break # temporary disabled
self.checkUpdate()
pass
def checkUpdate(self):
try:
print "checking if file exists"
self.update_system_from_file()
except Exception as e:
print "error "+str(e)
def update_system_from_file(self):
#parse data from the file
data = []
with open("/tmp/systems.json") as data_file:
data = simplejson.load(data_file)
print str(len(data))+" systems to import"
outputEvery=10
count =0
countOut = 0
for system in data:
#print system
system_id = self.model.get_system(system["name"]);
self.model.update_system(system_id,system)
count=count +1
countOut= countOut+1
if countOut >= outputEvery:
countOut=0
print "imported "+str(count)+" systems"
sys.stdout.flush()
self.model.commit()
if self.stopEvent.is_set():
return
self.model.commit()
data = None
class StationUpdater(Thread):
def __init__(self):
Thread.__init__(self)
self.interval = 10
self.model = EliteModel()
self.stopEvent = Event()
def run(self):
print ("Running StationUpdater")
while not self.stopEvent.wait(self.interval):
#break # temporary disabled
self.checkUpdate()
pass
def checkUpdate(self):
try:
print "checking if file exists"
self.update_system_from_file()
except Exception as e:
print "error "+str(e)
def update_system_from_file(self):
#parse data from the file
data = []
with open("/tmp/stations.json") as data_file:
data = simplejson.load(data_file)
print str(len(data))+" stations to import"
outputEvery=10
count =0
countOut = 0
for station in data:
#print station["system_id"]
system_id = self.model.get_system_by_imported_id(station["system_id"]);
#print system_id
if system_id != 0:
#print station["system_id"]
station_id = self.model.get_station(station["name"],system_id)
#print station["system_id"]
self.model.update_station(station_id,station)
count=count +1
countOut= countOut+1
if countOut >= outputEvery:
countOut=0
print "imported "+str(count)+" stations"
sys.stdout.flush()
self.model.commit()
if self.stopEvent.is_set():
return
self.model.commit()
data = None