-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_sv.py
526 lines (410 loc) · 15.7 KB
/
data_sv.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# -*-coding:utf-8 -*
"""
data services(web api) for Prject
"""
from __future__ import absolute_import
import json
import time
import traceback
from logging import getLogger
import requests
from nameko.constants import DEFAULT_HEARTBEAT
from nameko.dependency_providers import Config
from nameko.rpc import RpcProxy, rpc
from nameko.timer import timer
from nameko.web.handlers import http
from nameko_redis import Redis
from werkzeug.wrappers import Response
import constants
from lib.utils import parse_ip
_version = "0.2"
_author = "mabotech"
log = getLogger("data_sv")
class HttpService:
""" web api """
name = "http_service"
conf = Config()
db = RpcProxy("oracle_service")
### redis = RpcProxy("redis_service")
redis = Redis("development")
@timer(interval=constants.INTERVAL_HEARTBEAT)
def heartbeat(self):
"""set heartbeat time to redis[key: heartbeat] """
now = time.strftime("%Y-%m-%d %H:%M:%S")
## timestamp = 1000 * time.time()
log.debug("heartbeat: {}".format(now))
try:
self.redis.set("heartbeat", now)
except Exception as ex:
log.error(ex)
#################################################################
## 用户
#################################################################
@http("GET,POST", "/api/autologin")
def auto_login(self, request):
""""""
callback = request.args.get("callback", None)
try:
redirect_to = ""
if "redirect_to" in request:
redirect_to = request["redirect_to"]
res = {"is_login": 1, "redirect_to": redirect_to}
return "{}({})".format(callback, res)
except Exception as e:
log.error(e)
## 获得登陆账户
@http("GET", "/api/user/list")
def get_user_list(self, request):
""" get operators for this(IP) workstation """
##['pic_url', 'employeeno', 'name', 'status', 'zpq']
IP = request.args.get("IP", request.remote_addr)
callback = request.args.get("callback", None)
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
result = self.db.call_proc(
"sp_ws_emp",
["wip_order", "order_type", "operation", "step", "<CURSOR>"],
"array",
)
empinfo = []
tmp = result[4]
for item in tmp:
employeeno = item["employeeno"]
login_ts = self.redis.hget("login:" + employeeno, "login_ts")
if login_ts == None:
item["login"] = False
elif timestamp - float(login_ts) < 60 * 60 * 8 * 1000:
item["login"] = True
else:
item["login"] = True
empinfo.append(item)
data = json.dumps({"jsonrpc": "2.0", "result": empinfo, "id": 1})
if callback == None:
return data
else:
return "{}({});".format(callback, data)
@http("GET", "/api/user/login")
def get_user_login(self, request):
""""""
## Apriso ->
callback = request.args.get("callback", None)
id_ = request.args.get("id", 1)
### log.debug("{},{}".format(callback,id))
IP = request.args.get("IP", request.remote_addr)
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
### group = "ws:{}".format(wskey)
payload = self.redis.call_lua(
method="lua_user_get", keys=[group], args=[id_, timestamp])
if callback == None:
return Response(payload, status=200, mimetype="application/json")
else:
return "{}({});".format(callback, payload)
@http("GET", "/api/user/scan")
def scan_user(self, request):
""""""
## IC scanner ->
## /api/user/scan?ic=4253955683
IP = request.args.get("IP", request.remote_addr)
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
ic = request.args.get("ic", 1)
id_ = request.args.get("id", 1)
employeeNo = self.redis.call_lua(
method="lua_user_scan", keys=[group], args=[ic, id_, timestamp])
if employeeNo == "":
return "user_not_found"
else:
## submit order in db
# result = self.db.call_proc("AP_3DXML.completeWipOrder", [employeeNo, '<NUMBER>'])
# return json.dumps( {"jsonrpc": "2.0", "result": result, "id": id})
return employeeNo
## 添加员工IC、EmployeeNo到Redis
@http("POST", "/api/user/add")
def add_user(self, request):
## add user ic & id to Redis
## /api/user/add?ic=4253955683&id=ADMIN
"""
{"jsonrpc":"2.0", id=1, method="c1", params=[]}
"""
json_text = request.get_data(as_text=True)
id_ = request.args.get("id", 1)
ic = request.args.get("ic")
userid = request.args.get("userid")
IP = request.args.get("IP", request.remote_addr)
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
result = self.redis.call_lua(
method="lua_user_add", keys=[group], args=[ic, userid, timestamp])
return "OK"
#################################################################
## 零件
#################################################################
@http("GET", "/api/part/list")
def get_part_list(self, request):
""" parts as build """
info = {"零件号": "", "零件名": "", "序列号": "", "装配人": "", "专配时间": ""}
IP = request.args.get("IP", request.remote_addr)
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
### q =self.conf.get("ORACLE")["QUERY"]
### ## rpc in ora_sv
### data1 = self.db.query(q["operators"], ['a'])
result = self.db.call_proc("sp_get_parts", ["order1", "<CURSOR>"],
"array")
return json.dumps({"jsonrpc": "2.0", "result": result, "id": 1})
@http("GET", "/api/part/<string:step>")
def get_wi_part(self, request, step):
"""
step: curr, next
"""
info = {"零件号": "", "零件名": "", "序列号": "", "装配人": "", "专配时间": ""}
IP = request.args.get("IP", request.remote_addr)
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
part = self.redis.hget(group, "part")
changed = self.redis.hget(group, "changed")
if changed == "1":
self.redis.hset(group, "changed", 0)
return json.dumps({"step": step, "part": part, "changed": changed})
@http("GET", "/api/part/set")
def set_part(self, request):
"""
Apriso -> 设定零件
"""
IP = request.args.get("IP", request.remote_addr)
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
part = request.args.get("part")
next = request.args.get("next")
id_ = request.args.get("id", 1)
callback = request.args.get("callback", None)
result = self.redis.call_lua(
method="lua_part_set",
keys=[group],
args=[part, next, id_, timestamp])
if callback == None:
return "{}({});".format(callback, result)
else:
return result
@http("GET", "/api/part/get/<string:frame>")
def get_part(self, request, frame):
"""part_current & part_next
e.g. /api/part/get?IP=<ip>&callback=<callback>
"""
IP = request.args.get("IP", request.remote_addr)
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
frame = frame.lower()
id_ = request.args.get("id", 1)
seq = request.args.get("seq", 1)
callback = request.args.get("callback", None)
data = []
error = ""
try:
# get part_curr
rtn = self.redis.call_lua(
method="lua_part_get",
keys=[group],
args=[IP, frame, seq, id_, timestamp],
)
x = json.loads(rtn)
I_PRODUCTNSP = x["part"]
# get part_next
if I_PRODUCTNSP == "":
data = []
else:
data = self.db.call_proc(
"AP_3DXML.ProcTdXml_GetGenelogy",
[
I_PRODUCTNSP,
"part:<STRING>",
"<STRING>",
"<STRING>",
"<STRING>",
"<STRING>",
"<STRING>",
"<STRING>",
"<STRING>",
"<STRING>",
"<STRING>",
],
"array",
)
except Exception as err:
log.error(err)
error = repr(err)
result = json.dumps({
"jsonrpc": "2.0",
"result": data,
"x": x,
"id": id_,
"error": error
})
if callback == None:
return result
else:
return "{}({});".format(callback, result)
#################################################################
## 作业指导
#################################################################
@http("GET", "/api/wi/get")
def get_wi(self, request):
"""work Instructions
e.g. http://localhost:8000/api/wi/get?IP=<IP>
"""
IP = request.args.get("IP", request.remote_addr)
seq = request.args.get("seq", 1)
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
callback = request.args.get("callback", None)
result = self.redis.call_lua(
method="lua_wi_get", keys=[group], args=[seq, timestamp])
if callback == None:
return result
else:
return "{}({});".format(callback, result)
#################################################################
## 工单
#################################################################
@http("GET", "/api/order/set")
def set_order(self, request):
"""
Apriso -> Redis, 设定工位工单
"""
## workstation, WI path, 3D model path, wip_order, operation, step
params = set(["ws", "path_wi", "path_3d", "wip_order", "op", "step"])
IP = request.args.get("IP", request.remote_addr)
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
ws = request.args.get("ws", "no_ws")
path_wi = request.args.get("path_wi", "")
path_3d = request.args.get("path_3d", "no_path")
wip_order = request.args.get("wip_order", "")
order_type = request.args.get("order_type", "")
operation = request.args.get("operation", "")
step = request.args.get("step", "")
callback = request.args.get("callback", None)
## now = time.strftime("%Y-%m-%d %H:%M:%S")
result = self.redis.call_lua(
method="lua_order_set",
keys=[group],
args=[
ws,
path_wi,
path_3d,
wip_order,
order_type,
operation,
step,
timestamp,
IP,
],
)
if callback == None:
return result
else:
return "{}({});".format(callback, result)
@http("GET", "/api/order/get")
def get_order(self, request):
"""
Apriso -> Redis, 设定工位工单
"""
## workstation, WI path, 3D model path, wip_order, operation, step
result = None
IP = request.args.get("IP", request.remote_addr)
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
try:
id_ = request.args.get("id", 1)
callback = request.args.get("callback", None)
## now = time.strftime("%Y-%m-%d %H:%M:%S")
result = self.redis.call_lua(
method="lua_order_get", keys=[group], args=[id_, timestamp])
if callback == None:
return result
else:
return "{}({});".format(callback, result)
except Exception as ex:
log.error(ex)
#################################################################
## 安灯
#################################################################
@http("GET", "/api/andon/set")
def set_andon(self, request):
"""
Apriso -> Andon
"""
### signals = set(['material', 'quality', 'equipment', 'start', 'beep'])
IP = request.args.get("IP", request.remote_addr)
id_ = request.args.get("id", 1)
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
log.debug("{}:{}".format(addr, ip))
signal = request.args.get("signal")
status = request.args.get("status")
callback = request.args.get("JSONCALLBACK", "ng_jsonp_callback")
## 7 for andon
ip_4 = 10 * (int(addr[3]) // 10) + 7
addrs = addr[:3]
addrs.append(str(ip_4))
IP_Andon = ".".join(addrs)
# IP_Andon = "172.31.27.57"
port = 8000
url = "http://{}:{}/andon/change/status?title={}&status={}&JSONCALLBACK={}&IP={}".format(
IP_Andon, port, signal, status, callback, IP)
try:
r = requests.get(url)
log.debug(r.text)
result = self.redis.call_lua(
method="lua_andon_set",
keys=[group],
args=[signal, status, id_, timestamp],
)
log.debug(result)
return r.text
except Exception as ex:
log.error(ex)
return "{}({});".format(callback, json.dumps({"error": repr(ex)}))
@http("GET", "/api/andon/get")
def get_andon_status(self, request):
"""
Redis ->
"""
IP = request.args.get("IP", request.remote_addr)
id_ = request.args.get("id", 1)
callback = request.args.get("JSONCALLBACK", "ng_jsonp_callback")
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
ip_4 = 10 * (int(addr[3]) // 10) + 7
addrs = addr[:3]
addrs.append(str(ip_4))
IP_Andon = ".".join(addrs)
port = 8000
url = "http://{}:{}/andon/status?JSONCALLBACK={}&IP={}".format(
IP_Andon, port, callback, IP)
try:
r = requests.get(url)
return r.text
except Exception as ex:
log.error(ex)
return "{}({});".format(callback, json.dumps({"error": repr(ex)}))
@http("GET", "/api/andon/check")
def check_andon(self, request):
"""
Redis ->
"""
IP = request.args.get("IP", request.remote_addr)
id_ = request.args.get("id", 1)
# callback = request.args.get("JSONCALLBACK", "ng_jsonp_callback")
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
result = self.redis.call_lua(
method="lua_andon_get", keys=[group], args=[id_, timestamp])
return result
@http("GET", "/ip")
def ip_get(self, request):
""""""
return request.remote_addr
#################################################################
## 现场数据采集
#################################################################
## 游标卡尺
@http("GET,POST", "/api/measure/set")
def set_measure(self, request):
""""""
IP = request.args.get("IP", request.remote_addr)
val = request.args.get("val", None)
type_, wskey, addr, ip_, group, timestamp = parse_ip(IP)
### print(json.loads(request.get_data(as_text=True)))
### return u"received: {}".format(request.get_data(as_text=True))
if val != None:
self.db.call_proc("AP_3DXML.ProcTDXML_SetSerialRuler", ["SN", val],
"array")
return "OK"