-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.py
313 lines (242 loc) · 11.9 KB
/
UI.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
from math import ceil
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.filechooser import FileChooserListView
from kivy.uix.popup import Popup
from kivy.uix.image import Image
from functools import partial
from BlockChainAsset import BlockChainAsset
from MyWeb3 import MyWeb3
secret = '0x546de93a45c8df31e63b0bea9534a7ca03e9eb0e817f0d436d9b324b43d0a123'
address = '0xdce2dd4bB3A9E29714dD317d05869fcD72F20Cbe'
logisticAddress = '0xfb1e3d935da5fc952bee28d600b4fef481bc8eb7'
consumerAddress = '0x07f69985895fb70953f74f473f20e353136cf91f'
deployerAddress = '0xb39237807bcb2d32a26578d289b61cb53b6ae321'
myweb3 = MyWeb3(logisticAddress, consumerAddress, deployerAddress, 'AmBros.sol')
BALANCE = 'Balance'
ORDER = 'Order'
SENSOR = 'Sensor'
RECEIVE = 'Receive'
screenTexts = [BALANCE, ORDER, SENSOR, RECEIVE]
ORDER_NUMBER = 'Order Number'
CREATE_TIME = 'Creation Time'
PICKUP_TIME = 'Pickup Time'
DELIVERY_TIME = 'Delivery Time'
SHIPPING_PRICE = 'Shipping Price'
QUANTITY = 'Quantity'
MAX_TEMPERATURE = 'Max Temperature'
MIN_TEMPERATURE = 'Min Temperature'
TOLERANCE_TIME = 'Tolerance Time'
MEASURE_INTERVAL = 'Measure Interval'
PENALTY_PERDAY = 'Penalty Per Day'
MAX_PENALTY = 'Max Penalty'
BC_SYNC_INTERVAL = 'Block Chain Sync Interval'
INCOTERM = 'Incoterm'
assetTexts = [ORDER_NUMBER, CREATE_TIME, PICKUP_TIME, DELIVERY_TIME, SHIPPING_PRICE, QUANTITY, MAX_TEMPERATURE, MIN_TEMPERATURE, TOLERANCE_TIME, MEASURE_INTERVAL, PENALTY_PERDAY, MAX_PENALTY, BC_SYNC_INTERVAL, INCOTERM]
bcAsset = BlockChainAsset(secret, address)
orderData = dict()
payBack = 0
payToDelivery = 0
def submit(btn):
global orderData
data = dict(map(lambda t: (t, btn.father.entries[t].input.text), assetTexts))
orderData = data
orderData[ORDER_NUMBER] = int(orderData[ORDER_NUMBER])
orderData[CREATE_TIME] = int(orderData[CREATE_TIME])
orderData[PICKUP_TIME] = int(orderData[PICKUP_TIME])
orderData[DELIVERY_TIME] = int(orderData[DELIVERY_TIME])
orderData[SHIPPING_PRICE] = int(orderData[SHIPPING_PRICE])
orderData[QUANTITY] = int(orderData[QUANTITY])
orderData[MAX_TEMPERATURE] = int(orderData[MAX_TEMPERATURE])
orderData[MIN_TEMPERATURE] = int(orderData[MIN_TEMPERATURE])
orderData[TOLERANCE_TIME] = int(orderData[TOLERANCE_TIME])
orderData[MEASURE_INTERVAL] = int(orderData[MEASURE_INTERVAL])
orderData[PENALTY_PERDAY] = float(orderData[PENALTY_PERDAY])
orderData[MAX_PENALTY] = float(orderData[MAX_PENALTY])
orderData[BC_SYNC_INTERVAL] = int(orderData[BC_SYNC_INTERVAL])
bcAsset.buildAsset(orderData[ORDER_NUMBER], orderData[CREATE_TIME], orderData[PICKUP_TIME], orderData[DELIVERY_TIME], orderData[SHIPPING_PRICE], orderData[QUANTITY], orderData[MAX_TEMPERATURE], orderData[MIN_TEMPERATURE], orderData[TOLERANCE_TIME], orderData[MEASURE_INTERVAL], orderData[PENALTY_PERDAY], orderData[MAX_PENALTY], orderData[BC_SYNC_INTERVAL], orderData[INCOTERM])
myweb3.createOrder(orderData[ORDER_NUMBER], orderData[SHIPPING_PRICE])
popup = Popup(title='Info', content=Label(text='Submitted', font_size = 50), size_hint=(0.3, 0.3))
popup.open()
def getBalance(instance):
consumerBalance = int(round(myweb3.getConsumerBalance() / 1e18))
logisticBalance = int(round(myweb3.getLogisticBalance() / 1e18))
contractBalance = int(round(myweb3.getContractBalance() / 1e18))
popup = Popup(title='Info', content=Label(text='Consumer Balance: {} ether\nLogistic Balance: {} ether\nContract Balance: {} ether'.format(consumerBalance, logisticBalance, contractBalance), font_size = 50), size_hint=(0.3, 0.3))
popup.open()
class BalanceScreen(Screen):
def __init__(self, *args, **kwargs):
super(BalanceScreen, self).__init__(*args, **kwargs)
btn = Button(text='Get Balance', size_hint=(0.2, 0.2), pos_hint={'center_x': 0.5, 'center_y': 0.5}, font_size = 50)
self.add_widget(btn)
btn.bind(on_press=getBalance)
class FormEntry(GridLayout):
def __init__(self, name=None, *args, **kwargs):
super(FormEntry, self).__init__(*args, **kwargs)
self.cols = 2
self.add_widget(Label(text=name, font_size = 50))
self.input = TextInput(multiline=False, font_size = 50)
self.add_widget(self.input)
class SubmitButton(Button):
def __init__(self, father=None, *args, **kwargs):
super(Button, self).__init__(*args, **kwargs)
self.font_size = 50
self.text = 'Submit'
self.father = father
class OrderLayout(GridLayout):
def __init__(self, *args, **kwargs):
super(OrderLayout, self).__init__(*args, **kwargs)
self.cols = 1
self.size_hint = (0.5, 0.95)
self.pos_hint = {'center_x': 0.5, 'center_y': 0.5}
self.spacing = 5
self.entries = dict()
for text in assetTexts:
entry = FormEntry(name=text)
self.entries[text] = entry
self.add_widget(entry)
self.entries[ORDER_NUMBER].input.text = '0'
self.entries[CREATE_TIME].input.text = '1518652801'
self.entries[PICKUP_TIME].input.text = '1518652801'
self.entries[DELIVERY_TIME].input.text = '1518825601'
self.entries[SHIPPING_PRICE].input.text = '10000000000000000000'
self.entries[QUANTITY].input.text = '24'
self.entries[MAX_TEMPERATURE].input.text = '40'
self.entries[MIN_TEMPERATURE].input.text = '5'
self.entries[TOLERANCE_TIME].input.text = str(45 * 60)
self.entries[MEASURE_INTERVAL].input.text = '60'
self.entries[PENALTY_PERDAY].input.text = '0.1'
self.entries[MAX_PENALTY].input.text = '0.5'
self.entries[BC_SYNC_INTERVAL].input.text = '1800'
self.entries[INCOTERM].input.text = 'CPT'
btn = SubmitButton(text='Submit!', father=self)
self.add_widget(btn)
btn.bind(on_press=submit)
class OrderScreen(Screen):
def __init__(self, *args, **kwargs):
super(OrderScreen, self).__init__(*args, **kwargs)
self.add_widget(OrderLayout())
class SensorFileChooser(FileChooserListView):
def __init__(self, *args, **kwargs):
super(SensorFileChooser, self).__init__(*args, **kwargs)
self.path = '/Users/lostbenjamin/Desktop/AmBros/cases/'
def on_submit(*args):
filePath = args[1][0]
f = open(filePath)
lines = f.readlines()
for line in lines:
index, tempSensor1, tempSensor2, tempSensor3, timestamp = list(map(lambda item: int(item), line.strip('\r\n').split(',')))
bcAsset.buildEvent(tempSensor1, tempSensor2, tempSensor3, timestamp)
f.close()
popup = Popup(title='Info', content=Label(text='Arrived!', font_size = 50), size_hint=(0.3, 0.3))
popup.open()
class SensorScreen(Screen):
def __init__(self, *args, **kwargs):
super(SensorScreen, self).__init__(*args, **kwargs)
self.fileChooser = SensorFileChooser()
self.add_widget(self.fileChooser)
def check(instance):
global payBack, payToDelivery
minTemperature = orderData[MIN_TEMPERATURE]
maxTemperature = orderData[MAX_TEMPERATURE]
events = bcAsset.getEvents()
lastEvent = None
events = sorted(events, key = lambda e : e['content']['data']['created_at'])
for event in events:
if lastEvent != None:
lastTempSensor1 = lastEvent['content']['data']['custom']['tempSensor1']
lastTempSensor2 = lastEvent['content']['data']['custom']['tempSensor2']
lastTempSensor3 = lastEvent['content']['data']['custom']['tempSensor3']
nowTempSensor1 = event['content']['data']['custom']['tempSensor1']
nowTempSensor2 = event['content']['data']['custom']['tempSensor2']
nowTempSensor3 = event['content']['data']['custom']['tempSensor3']
if (not (minTemperature<=lastTempSensor1<=maxTemperature) and not (minTemperature<=nowTempSensor1<=maxTemperature)) or (not (minTemperature<=lastTempSensor2<=maxTemperature) and not (minTemperature<=nowTempSensor2<=maxTemperature)) or (not (minTemperature<=lastTempSensor3<=maxTemperature) and not (minTemperature<=nowTempSensor3<=maxTemperature)):
payBack = orderData[SHIPPING_PRICE]
payToDelivery = 0
print('1')
print('payBack {}'.format(payBack))
print('payToDelivery {}'.format(payToDelivery))
break
lastEvent = event
else:
startEvent = events[0]
endEvent = events[-1]
timeSpentMore = endEvent['content']['data']['created_at'] - orderData[DELIVERY_TIME]
daysSpentMore = ceil(timeSpentMore / (60 * 60 * 24))
penalty = min(orderData[PENALTY_PERDAY] * daysSpentMore, orderData[MAX_PENALTY])
payBack = int(orderData[SHIPPING_PRICE] * penalty)
payToDelivery = orderData[SHIPPING_PRICE] - payBack
print('2')
print('daySpentMore {}'.format(daysSpentMore))
print('payBack {}'.format(payBack))
print('payToDelivery {}'.format(payToDelivery))
popup = Popup(title='Info', content=Label(text='Pay Back: {} ether\nPay For Delivery: {} ether'.format(int(round(payBack/1e18)), int(round(payToDelivery/1e18))), font_size = 50), size_hint=(0.3, 0.3))
popup.open()
def confirm(instance):
myweb3.confirm(orderData[ORDER_NUMBER], payToDelivery)
popup = Popup(title='Info', content=Label(text='Confirmed!'.format(payBack, payToDelivery), font_size = 50), size_hint=(0.3, 0.3))
popup.open()
class ReceiveButtonsLayout(BoxLayout):
def __init__(self, father=None, *args, **kwargs):
super(ReceiveButtonsLayout, self).__init__(*args, **kwargs)
self.orientation = 'horizontal'
self.spacing = 100
checkBtn = Button(text='Check', font_size = 50)
checkBtn.bind(on_press=check)
self.add_widget(checkBtn)
confirmBtn = Button(text='Confirm', font_size = 50)
confirmBtn.bind(on_press=confirm)
self.add_widget(confirmBtn)
self.father = father
class ReceiveLayout(BoxLayout):
def __init__(self, *args, **kwargs):
super(ReceiveLayout, self).__init__(*args, **kwargs)
self.orientation = 'vertical'
self.size_hint = (0.5, 0.1)
self.pos_hint = {'center_x': 0.5, 'center_y': 0.5}
self.spacing = 200
self.buttonLayout = ReceiveButtonsLayout(father=self)
self.add_widget(self.buttonLayout)
class ReceiveScreen(Screen):
def __init__(self, *args, **kwargs):
super(ReceiveScreen, self).__init__(*args, **kwargs)
self.add_widget(ReceiveLayout())
class Manager(ScreenManager):
def __init__(self, *args, **kwargs):
super(Manager, self).__init__(*args, **kwargs)
balanceScreen = BalanceScreen(name=BALANCE)
self.add_widget(balanceScreen)
orderScreen = OrderScreen(name=ORDER)
self.add_widget(orderScreen)
sensorScreen = SensorScreen(name=SENSOR)
self.add_widget(sensorScreen)
receiveScreen = ReceiveScreen(name=RECEIVE)
self.add_widget(receiveScreen)
class Navigation(GridLayout):
def __init__(self, manager=None, *args, **kwargs):
super(Navigation, self).__init__(*args, **kwargs)
self.manager = manager
self.cols = 4
self.size_hint = (1, .1)
for text in screenTexts:
self.add_widget(Button(text=text, on_release=self.change, font_size = 50))
def change(self, btn):
self.manager.current = btn.text
class Root(BoxLayout):
def __init__(self, *args, **kwargs):
super(Root, self).__init__(*args, **kwargs)
self.orientation = 'vertical'
manager = Manager()
self.add_widget(Navigation(manager))
self.add_widget(manager)
class AmBrosApp(App):
def build(App):
return Root()
if __name__ == '__main__':
AmBrosApp().run()