def BusNowDtl(vehicleList): conf = ReadConf.ReadConf().Nostra_map() url = conf['apiURL'] + '/busnow' headers = {'Content-Type': 'application/json'} body = {"token": conf['token'], "vehicleList": vehicleList} r = requests.post(url, data=json.dumps(body), headers=headers) vehicles = json.loads(r.text)['bus'] ora = ora_conf().ora() try: dsn_tns = cx_Oracle.makedsn(ora['server'], ora['port'], ora['service']) ora_conn = cx_Oracle.connect(ora['user'], ora['password'], dsn_tns, encoding="UTF-8") ora_cursor = ora_conn.cursor() ora_qryStr = ora_conf().qryBusNowDtl()['Query'] data = [] for vehicle in vehicles: truck_position = collections.OrderedDict() truck_addr = json.loads( Here_map.ReverseGeoCoder(str(vehicle['latitude']), str(vehicle['longitude']), '2').decode('utf-8')) parameter = {'truck_no': vehicle['vehicleName']} # print(vehicle) ora_cursor.execute(ora_qryStr, parameter) for row in ora_cursor: addr = truck_addr['Address'][0]['label'] truck_position['vehicle'] = vehicle['vehicleName'] truck_position['datetime'] = ConvertTool.tzToStr( vehicle['dateTime']) truck_position['speed'] = vehicle['speed'] truck_position['heading'] = vehicle['direction'] truck_position['latitude'] = vehicle['latitude'] truck_position['longitude'] = vehicle['longitude'] truck_position['address'] = addr truck_position['engine'] = vehicle['evEngine'] truck_position['dn_no'] = row[0] truck_position['emp_no'] = row[1] truck_position['emp_name'] = row[2] truck_position['tel'] = row[3] data.append(truck_position) ora_cursor.close() return json.dumps(data, indent=" ", ensure_ascii=False).encode('utf-8') except cx_Oracle.DatabaseError as e: data['error'] = e.args[0].message return json.dumps(data, indent=" ", ensure_ascii=False).encode('utf-8') finally: if ora_conn is not None: ora_conn.close()
def on_get(self, req, resp): username, password = getUserPass(req) if (username is None) or (password is None): response(resp, 'GET, OPTIONS', falcon.HTTP_404, 'error: user password') else: params = dict({}) for key, value in req.params.items(): params.update({key: value}) if ('origin' in params) and ('destination' in params) and ('transportMode' in params): origin = params['origin'] destination = params['destination'] transportMode = params['transportMode'] data = Here_map.RouteSummary(origin=origin, destination=destination,transportMode=transportMode).decode('utf-8') response(resp, 'GET, OPTIONS', falcon.HTTP_200, data) else: response(resp, 'GET, OPTIONS', falcon.HTTP_404, 'error: Require latitude longitude acc parameters.')
def on_get(self, req, resp): username, password = getUserPass(req) if (username is None) or (password is None): response(resp, 'GET, OPTIONS', falcon.HTTP_404, 'error: user password') else: params = dict({}) for key, value in req.params.items(): params.update({key: value}) if ('latitude' in params) and ('longitude' in params) and ('acc' in params): latitude = params['latitude'] longitude = params['longitude'] acc = params['acc'] data = Here_map.ReverseGeoCoder(latitude, longitude, acc).decode('utf-8') response(resp, 'GET, OPTIONS', falcon.HTTP_200, data) else: response(resp, 'GET, OPTIONS', falcon.HTTP_404, 'error: Require latitude longitude acc parameters.')
def BusNow(vehicleList): conf = ReadConf.ReadConf().Nostra_map() url = conf['apiURL']+'/busnow' headers={'Content-Type': 'application/json'} body = {"token": conf['token'], "vehicleList": vehicleList} r = requests.post(url, data=json.dumps(body), headers=headers) vehicles = json.loads(r.text)['bus'] data = [] for vehicle in vehicles: truck_position = collections.OrderedDict() truck_addr = json.loads(Here_map.ReverseGeoCoder(str(vehicle['latitude']), str(vehicle['longitude']), '2').decode('utf-8')) addr = truck_addr['Address'][0]['label'] truck_position['vehicle'] = vehicle['vehicleName'] # truck_position['datetime'] = ConvertTool.tzToStr(vehicle['dateTime']) truck_position['datetime'] = ConvertUtil.tzToStr(vehicle['dateTime']) truck_position['speed'] = vehicle['speed'] truck_position['heading'] = vehicle['direction'] truck_position['latitude'] = vehicle['latitude'] truck_position['longitude'] = vehicle['longitude'] truck_position['address'] = addr truck_position['engine'] = vehicle['evEngine'] data.append(truck_position) return json.dumps(data, indent=" ", ensure_ascii=False).encode('utf-8')
def put_data(self, user, password, body): source_latlng = body['LAT_LNG'] mode = 'fastest;truck;traffic:enabled' data = collections.OrderedDict(body) # ------------- TRUCK_POSITION -------------------------------------- truck_no = ConvertUtil.convert_truck(body['TRUCK_NO']) truck_position = json.loads(Nostra.BusNow(truck_no).decode('utf-8')) # print('truck_no {} position {} '.format(truck_no,truck_position)) truck_latlng = str(truck_position[0]['latitude']) + ',' + str( truck_position[0]['longitude']) # print('source {} dest {}'.format(source_latlng,truck_latlng)) try: distance = json.loads( Here_map.CalculateWayPoint(truck_latlng, source_latlng, mode).decode('utf-8')) far_from = { 'distance': distance['distance'], 'travel_time': distance['travel_time'] } except: # far_from = {'distance': 0, 'travel_time': 0} t = collections.OrderedDict() t = truck_position[0] t['far_from'] = far_from data['TRUCK_POSITION'] = t # ----------------- MOBILE POSITION --------------------------------------------- old_position = data['POSITION'] source_latlng = data['LAT_LNG'] dt = arrow.now position_latlng = str(old_position['latitude']) + ',' + str( old_position['longitude']) distance = json.loads( Here_map.CalculateWayPoint(position_latlng, source_latlng, mode).decode('utf-8')) position_far_from = { 'distance': distance['distance'], 'travel_time': distance['travel_time'] } old_position['timestamp'] = time.time() old_position['far_from'] = position_far_from mobile_addr = json.loads( Here_map.ReverseGeoCoder(str(old_position['latitude']), str(old_position['longitude']), '1').decode('utf-8')) ts = arrow.now().format('YYYY-MM-DD HH:mm:ss') old_position['address'] = mobile_addr['Address'][0]['label'] old_position['timestamp'] = ts data['POSITION'] = old_position # -----------------------INSERT TO POSTGRES----------------------------------- login = Login_Postgres(user=user, password=password) is_login = json.loads(login.login().decode('utf-8')) if is_login['login'] == 'True' and ('|csdplan|hrconnect|hr|'.find(user) > 0): #-- pg = ReadConf().postgres12() server ใหม่ทำงานผ่าน ทดสอบเป็น server เดิมด้วย pg = ReadConf().postgres() conn = psycopg2.connect(host=pg['server'], port=pg['port'], database=pg['database'], user=user, password=password) conn.autocommit = False try: cursor = conn.cursor() json_data = json.dumps( data, ensure_ascii=False, indent=None).encode('utf-8').decode('utf-8') qryStr = ReadConf().ins_dn_timestamp()['Query'] qryStr = qryStr.replace('{{data}}', json_data) cursor.execute(qryStr) # print(qryStr) return json.dumps('{"insert": "successful"}', ensure_ascii=False).encode(encoding='utf-8') except (psycopg2.Error) as e: return json.dumps( { 'login': e.pgerror, 'company': '', 'status': '' }, indent=" ", ensure_ascii=False).encode(encoding='utf-8') finally: if conn is not None: conn.commit() conn.close() else: return json.dumps({ 'login': '******' }).encode('utf-8')
def updateDB(): pg = ReadConf().postgres() user = pg['pg_user'] password = pg['pg_pass'] try: conn = psycopg2.connect(host=pg['server'], port=pg['port'], database=pg['database'], user=user, password=password) conn.autocommit = False cursor = conn.cursor() # 1. read dn_timestamp data->>'STATUS = 'N' ----------------------- qryStr = "select id,data from dn_timestamp where data->>'STATUS'='N'" cursor.execute(qryStr) data = cursor.fetchall() # ----------------------------------------------------------------- # 2. for row update data->>'STATUS = 'P' for lock -------------- qryUpdatStatus = ReadConf().upd_dn_timestamp_status()['Query'] for row in data: print(row[0], row[1]) print('update GPS') mode = 'fastest;truck;traffic:enabled' for row in data: source_latlng = row[1]['LAT_LNG'] # -------------------------- มือถือ -------------------------------------------- position_latlng = row[1]['POSITION']['latitude'] + ',' + row[1][ 'POSITION']['longitude'] mobile_addr = json.loads( Here_map.ReverseGeoCoder(row[1]['POSITION']['latitude'], row[1]['POSITION']['longitude'], '1').decode('utf-8')) distance = json.loads( Here_map.CalculateWayPoint(position_latlng, source_latlng, mode).decode('utf-8')) position_far_from = { 'distance': distance['distance'], 'travel_time': distance['travel_time'] } print(position_far_from, mobile_addr) # --------------------------- รถบรรทุก ------------------------------------------ truck_latlng = '{},{}'.format( row[1]['TRUCK_POSITION']['latitude'], row[1]['TRUCK_POSITION']['longitude']) truck_addr = json.loads( Here_map.ReverseGeoCoder( str(row[1]['TRUCK_POSITION']['latitude']), str(row[1]['TRUCK_POSITION']['longitude']), '1').decode('utf-8')) distance = json.loads( Here_map.CalculateWayPoint(truck_latlng, source_latlng, mode).decode('utf-8')) truck_far_from = { 'distance': distance['distance'], 'travel_time': distance['travel_time'] } print(truck_far_from, truck_addr) # ----------------------------------------------------------------- cursor.close() # ----------------------------------------------------------------- except (psycopg2.Error) as e: print(e.pgerror) finally: if conn is not None: conn.commit() conn.close()
def put_data(self, user, password, body): source_latlng = body['LAT_LNG'] mode = 'fastest;truck;traffic:enabled' data = collections.OrderedDict(body) # ------------- TRUCK_POSITION -------------------------------------- truck_no = convert_truck(body['TRUCK_NO']) truck_position = json.loads(Nostra.BusNow(truck_no).decode('utf-8')) truck_latlng = str(truck_position[0]['latitude']) + ',' + str( truck_position[0]['longitude']) distance = json.loads( Here_map.CalculateWayPoint(truck_latlng, source_latlng, mode).decode('utf-8')) far_from = { 'distance': distance['distance'], 'travel_time': distance['travel_time'] } t = collections.OrderedDict() t = truck_position[0] t['far_from'] = far_from data['TRUCK_POSITION'] = t # ----------------- POSITION --------------------------------------------- position_latlng = str(body['POSITION']['latitude']) + ',' + str( body['POSITION']['longitude']) routeSummary = json.loads( Here_map.RouteSummary(origin=position_latlng, destination=source_latlng, transportMode='truck').decode('utf-8')) position_far_from = { 'distance': routeSummary['distance'], 'travel_time': routeSummary['duration'] } old_position = data['POSITION'] old_position['far_from'] = position_far_from data['POSITION'] = old_position # -----------------------INSERT TO POSTGRES----------------------------------- login = Login_Postgres(user=user, password=password) is_login = json.loads(login.login().decode('utf-8')) if is_login['login'] == 'True' and ( '|csdplan|hrconnect|line|hr|'.find(user) > 0): #-- pg = ReadConf().postgres12() server ใหม่ทำงานผ่าน ทดสอบเป็น server เดิมด้วย pg = ReadConf().postgres() conn = psycopg2.connect(host=pg['server'], port=pg['port'], database=pg['database'], user=user, password=password) conn.autocommit = False try: cursor = conn.cursor() json_data = json.dumps( data, ensure_ascii=False, indent=None).encode('utf-8').decode('utf-8') qryStr = ReadConf().ins_dn_timestamp()['Query'] qryStr = qryStr.replace('{{data}}', json_data) cursor.execute(qryStr) return json.dumps('{"insert": "successful"}', ensure_ascii=False).encode(encoding='utf-8') except psycopg2.Error as e: return json.dumps( { 'login': e.pgerror, 'company': '', 'status': '' }, indent=" ", ensure_ascii=False).encode(encoding='utf-8') finally: if conn is not None: conn.commit() conn.close() else: return json.dumps({ 'login': '******' }).encode('utf-8')