venkataakhil
1 year ago
7 changed files with 1721 additions and 372 deletions
@ -0,0 +1,439 @@ |
|||
import frappe |
|||
from frappe.model.document import Document |
|||
import json |
|||
import os |
|||
from frappe.utils import cstr |
|||
|
|||
from smart_service.apis.app_user_login import input_validation |
|||
|
|||
""" Constants """ |
|||
|
|||
JSON_EXT = ".json" |
|||
JSON_INT_PATH = "/files/json_files/internal/" |
|||
JSON_FULL_INT_PATH = "/files/json_files/full_update/internal/" |
|||
JSON_GLOABL_PATH = "/files/json_files/global/" |
|||
JSON_FULL_GLOBAL_PATH = "/files/json_files/full_update/" |
|||
STATUS = "status" |
|||
ERROR = "error" |
|||
UPDATE_AVAILABLE = 'IsUpdateAvailable' |
|||
UPDATE_FAILED = "Update checking Failed" |
|||
PARAM_MISSING = "Parameter missing :" |
|||
VERSION = 'Version' |
|||
CUR_VERSION = 'CurrentVersion' |
|||
DIS = 'Discription' |
|||
FILE_ERROR = 'File failed to load' |
|||
LANGUAGE = 'Language' |
|||
FILE_SIZE = 'FileSize' |
|||
JSON_URL = 'JsonURL' |
|||
|
|||
site_name = cstr(frappe.local.site) |
|||
base_url = os.path.expanduser( |
|||
"~") + "/frappe-bench/sites/" + site_name + "/public" |
|||
|
|||
|
|||
@frappe.whitelist(allow_guest=1) |
|||
def check_vehicle_update(vehicle_list=None): |
|||
""" Rate Limiting """ |
|||
# rate_res = custom_rate_limit(limit=5, seconds=15) |
|||
# if rate_res != 1: |
|||
# return rate_res |
|||
|
|||
""" Validate Inputs """ |
|||
val = input_validation(vehicle_list=vehicle_list) |
|||
|
|||
""" Validate Input response """ |
|||
if val != '': |
|||
return {"Vehicle": None, |
|||
"VehicleReqList": [], |
|||
STATUS: 0, ERROR: PARAM_MISSING+val} |
|||
|
|||
response = {} |
|||
""" Iterate Vehicle List """ |
|||
if vehicle_list: |
|||
try: |
|||
""" Read Request Body """ |
|||
req_list = json.loads(vehicle_list) |
|||
response['Vehicle'] = req_list['Vehicle'] |
|||
vehicle = req_list['Vehicle'] |
|||
iid = req_list['InstallationId'] |
|||
vehicle_data = req_list['VehicleReqList'] |
|||
|
|||
""" Get Publish Type based on IID """ |
|||
publish_type = frappe.db.get_list( |
|||
'App Device', filters={'name': iid}, fields='publish_type') |
|||
|
|||
if publish_type: |
|||
publish_type = publish_type[0]['publish_type'] |
|||
VehicleReqList = [] |
|||
for v in vehicle_data: |
|||
l_id = v['LanguageID'] |
|||
current_version = float(v[CUR_VERSION]) |
|||
|
|||
""" New function to validate publish version and get file path """ |
|||
pulish_response = get_vehicle_publish_data(vehicle=vehicle, |
|||
l_id=l_id, current_version=current_version, publish_type=publish_type) |
|||
|
|||
""" Append to Main Vehicle Request Response List """ |
|||
VehicleReqList.append(pulish_response) |
|||
|
|||
return { |
|||
"Vehicle": vehicle, "VehicleReqList": VehicleReqList, STATUS: 1, ERROR: "No error"} |
|||
except Exception as e: |
|||
return {"Vehicle": None, "VehicleReqList": [], STATUS: 0, ERROR: str(e)} |
|||
else: |
|||
return {"Vehicle": None, "VehicleReqList": [], STATUS: 0, ERROR: "Check Parameter: vehicle list"} |
|||
|
|||
|
|||
def get_file_size(file_name): |
|||
return os.path.getsize(base_url + file_name) |
|||
|
|||
|
|||
def get_vehicle_publish_data(vehicle, l_id, current_version, publish_type): |
|||
""" Response Structure """ |
|||
vehicle_response_data = { |
|||
"Name": None, |
|||
"Version": 0, |
|||
"Vehicle": None, |
|||
"Language": l_id, |
|||
"Description": None, |
|||
"JsonURL": None, |
|||
"FileSize": None, |
|||
"Error": None, |
|||
"IsUpdateAvailable": False, |
|||
"CurrentVersion": current_version |
|||
} |
|||
latest_global_version = frappe.db.sql('''SELECT name as Name,format(version,2) as version,vehicle_id,language,release_description as Description |
|||
FROM `tabPublish` where vehicle='{0}' and language='{1}' and publish_status='Published' and vehicle_status='Active' |
|||
and publish_module='Automotive System' and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' and vehicle='{0}' and publish_type='Global' and language='{1}'); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
|
|||
latest_publish_version = frappe.db.sql('''SELECT name as Name,format(version,2) as version,vehicle_id,language,release_description as Description |
|||
FROM `tabPublish` where vehicle='{0}' and language='{1}' and publish_status='Published' and vehicle_status='Active' |
|||
and publish_module='Automotive System' and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' and vehicle='{0}' and language='{1}' and version >= {2}); |
|||
'''.format(vehicle, l_id, current_version), as_dict=1) |
|||
|
|||
if not latest_publish_version: |
|||
""" Latest File already downloaded Global/Internal """ |
|||
|
|||
vehicle_response_data['IsUpdateAvailable'] = False |
|||
vehicle_response_data['Remarks'] = "Latest Version" |
|||
|
|||
else: |
|||
if not latest_global_version or (current_version >= float(latest_global_version[0]['version'])): |
|||
""" Only First Internal available """ |
|||
|
|||
if publish_type == 'Internal': |
|||
value = round( |
|||
float(latest_publish_version[0]['version']) - current_version, 2) |
|||
|
|||
if value == 0.01: |
|||
# frappe.log_error('Inside 1 Internal') |
|||
""" 1 Internal File """ |
|||
data = frappe.db.sql('''SELECT name as Name,format(version,2) as Version,vehicle_id as Vehicle,language as Language, |
|||
release_description as Description FROM `tabPublish` where vehicle='{0}' and language='{1}' and |
|||
publish_status='Published' and vehicle_status='Active' and publish_module='Automotive System' |
|||
and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' and publish_type='Internal' |
|||
and vehicle='{0}' and language='{1}'); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
# frappe.log_error('data', str(data)) |
|||
# data = latest_publish_version |
|||
# frappe.log_error("data",str(data)) |
|||
if data: |
|||
file_name = JSON_INT_PATH+vehicle+"/"+vehicle + \ |
|||
"-" + l_id + "_v" + \ |
|||
str(data[0][VERSION]) + JSON_EXT |
|||
if os.path.exists(base_url + file_name): |
|||
vehicle_response_data['Name'] = data[0]['Name'] |
|||
vehicle_response_data['Vehicle'] = data[0]['Vehicle'] |
|||
vehicle_response_data['Version'] = data[0]['Version'] |
|||
vehicle_response_data['Language'] = data[0]['Language'] |
|||
vehicle_response_data['Description'] = data[0]['Description'] |
|||
vehicle_response_data['FileSize'] = get_file_size( |
|||
file_name) |
|||
vehicle_response_data['JsonURL'] = file_name |
|||
vehicle_response_data['IsUpdateAvailable'] = True |
|||
vehicle_response_data['Remarks'] = "Internal available" |
|||
|
|||
else: |
|||
vehicle_response_data['IsUpdateAvailable'] = False |
|||
vehicle_response_data['Remarks'] = "Internal available" |
|||
vehicle_response_data['Error'] = "Failed to read file" |
|||
else: |
|||
vehicle_response_data['IsUpdateAvailable'] = False |
|||
vehicle_response_data['Remarks'] = "Internal available" |
|||
vehicle_response_data['Error'] = "Failed to get Publish version" |
|||
|
|||
else: |
|||
""" Full Internal File """ |
|||
# same as latest_publish_version |
|||
data = frappe.db.sql('''SELECT name as Name,format(version,2) as Version,vehicle_id as Vehicle,language as Language, |
|||
release_description as Description FROM `tabPublish` where vehicle='{0}' and language='{1}' and |
|||
publish_status='Published' and vehicle_status='Active' and publish_module='Automotive System' |
|||
and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' and publish_type='Internal' |
|||
and vehicle='{0}' and language='{1}' ); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
# data = latest_publish_version |
|||
if data: |
|||
file_name = JSON_FULL_INT_PATH+vehicle+"/"+vehicle + \ |
|||
"-" + l_id + "-"+"full_v" + \ |
|||
str(data[0][VERSION] |
|||
) + JSON_EXT |
|||
if os.path.exists(base_url + file_name): |
|||
vehicle_response_data['Name'] = data[0]['Name'] |
|||
vehicle_response_data['Vehicle'] = data[0]['Vehicle'] |
|||
vehicle_response_data['Version'] = data[0]['Version'] |
|||
vehicle_response_data['Language'] = data[0]['Language'] |
|||
vehicle_response_data['Description'] = data[0]['Description'] |
|||
vehicle_response_data['FileSize'] = get_file_size( |
|||
file_name) |
|||
vehicle_response_data['JsonURL'] = file_name |
|||
|
|||
vehicle_response_data['IsUpdateAvailable'] = True |
|||
vehicle_response_data['Remarks'] = "Internal Full Update available" |
|||
|
|||
else: |
|||
vehicle_response_data['IsUpdateAvailable'] = False |
|||
vehicle_response_data['Remarks'] = "Internal Full Update available" |
|||
vehicle_response_data['Error'] = "Failed to read file" |
|||
else: |
|||
vehicle_response_data['IsUpdateAvailable'] = False |
|||
vehicle_response_data['Remarks'] = "Internal Full Update available" |
|||
vehicle_response_data['Error'] = "Failed to get Publish version" |
|||
else: |
|||
vehicle_response_data['IsUpdateAvailable'] = False |
|||
vehicle_response_data['Remarks'] = "No Update available" |
|||
|
|||
elif current_version < float(latest_global_version[0]['version']): |
|||
""" Only Global available """ |
|||
|
|||
if (float(latest_global_version[0]['version']) - int(current_version)) > 1: |
|||
""" Full Global File """ |
|||
data = frappe.db.sql('''SELECT name as Name,format(version,2) as Version,vehicle_id as Vehicle,language as Language, |
|||
release_description as Description FROM `tabPublish` where vehicle='{0}' and language='{1}' and |
|||
publish_status='Published' and vehicle_status='Active' and publish_module='Automotive System' |
|||
and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' |
|||
and vehicle='{0}' and language='{1}' and publish_type ='Global'); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
if data: |
|||
file_name = JSON_FULL_GLOBAL_PATH + vehicle + "/" + vehicle + \ |
|||
"-" + l_id + "-"+"full_v" + \ |
|||
str(data[0][VERSION] |
|||
) + JSON_EXT |
|||
|
|||
if os.path.exists(base_url + file_name): |
|||
|
|||
vehicle_response_data['Name'] = data[0]['Name'] |
|||
vehicle_response_data['Vehicle'] = data[0]['Vehicle'] |
|||
vehicle_response_data['Version'] = data[0]['Version'] |
|||
vehicle_response_data['Language'] = data[0]['Language'] |
|||
vehicle_response_data['Description'] = data[0]['Description'] |
|||
|
|||
vehicle_response_data['FileSize'] = get_file_size( |
|||
file_name) |
|||
vehicle_response_data['JsonURL'] = file_name |
|||
|
|||
vehicle_response_data['IsUpdateAvailable'] = True |
|||
vehicle_response_data['Remarks'] = "Global Full Update available" |
|||
|
|||
else: |
|||
vehicle_response_data['IsUpdateAvailable'] = False |
|||
vehicle_response_data['Remarks'] = "Global Full Update available" |
|||
vehicle_response_data['Error'] = "Failed to read file" |
|||
else: |
|||
vehicle_response_data['IsUpdateAvailable'] = False |
|||
vehicle_response_data['Remarks'] = "Global Full Update available" |
|||
vehicle_response_data['Error'] = "Failed to get Publish version" |
|||
|
|||
# vehicle_response_data['IsUpdateAvailable'] = True |
|||
# vehicle_response_data['Remarks'] = "Full Update Global available" |
|||
else: |
|||
|
|||
""" Single Global File """ |
|||
# vehicle_response_data['IsUpdateAvailable'] = True |
|||
# vehicle_response_data['Remarks'] = "Global available" |
|||
|
|||
data = frappe.db.sql('''SELECT name as Name,format(version,2) as Version,vehicle_id as Vehicle,language as Language, |
|||
release_description as Description FROM `tabPublish` where vehicle='{0}' and language='{1}' and |
|||
publish_status='Published' and vehicle_status='Active' and publish_module='Automotive System' |
|||
and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' |
|||
and vehicle='{0}' and language='{1}' and publish_type ='Global'); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
|
|||
if data: |
|||
file_name = JSON_GLOABL_PATH+vehicle+"/"+vehicle + \ |
|||
"-" + l_id + "_v" + \ |
|||
str(data[0][VERSION]) + JSON_EXT |
|||
|
|||
if os.path.exists(base_url + file_name): |
|||
|
|||
vehicle_response_data['Name'] = data[0]['Name'] |
|||
vehicle_response_data['Vehicle'] = data[0]['Vehicle'] |
|||
vehicle_response_data['Version'] = data[0]['Version'] |
|||
vehicle_response_data['Language'] = data[0]['Language'] |
|||
vehicle_response_data['Description'] = data[0]['Description'] |
|||
|
|||
vehicle_response_data['FileSize'] = get_file_size( |
|||
file_name) |
|||
vehicle_response_data['JsonURL'] = file_name |
|||
|
|||
vehicle_response_data['IsUpdateAvailable'] = True |
|||
vehicle_response_data['Remarks'] = "Global available" |
|||
|
|||
else: |
|||
vehicle_response_data['IsUpdateAvailable'] = False |
|||
vehicle_response_data['Remarks'] = "Global available" |
|||
vehicle_response_data['Error'] = "Failed to read file" |
|||
else: |
|||
vehicle_response_data['IsUpdateAvailable'] = False |
|||
vehicle_response_data['Remarks'] = "Global available" |
|||
vehicle_response_data['Error'] = "Failed to get Publish version" |
|||
|
|||
else: |
|||
""" Unknow condition Dont Know Yet """ |
|||
value = {'latest_publish_version': latest_publish_version[0]['version'], |
|||
" latest_global_version": latest_global_version[0]['version']} |
|||
frappe.log_error('New check Update', str( |
|||
f'Unknow Condition{value}')) |
|||
vehicle_response_data['IsUpdateAvailable'] = True |
|||
vehicle_response_data['Remarks'] = "New Condition" |
|||
|
|||
return vehicle_response_data |
|||
|
|||
|
|||
@frappe.whitelist(allow_guest=1) |
|||
def check_all_vehicle_updates(vehicle_list=None): |
|||
|
|||
val = input_validation(vehicle_list=vehicle_list) |
|||
if val != '': |
|||
return {STATUS: 0, ERROR: PARAM_MISSING+val} |
|||
|
|||
if vehicle_list: |
|||
try: |
|||
vehicle_req_list = [] |
|||
response = {} |
|||
v_list = json.loads(vehicle_list) |
|||
lang = v_list['LanguageID'] |
|||
iid = v_list['InstallationId'] |
|||
vehicle_data = v_list['VehicleReqList'] |
|||
publish_type = frappe.db.sql( |
|||
'''SELECT publish_type FROM `tabApp Device` where name='{}';'''.format(iid), as_list=1) |
|||
|
|||
if publish_type[0][0] is not None: |
|||
if vehicle_data: |
|||
for v in vehicle_data: |
|||
v_id = v['Vehicle'] |
|||
current_version = float(v[CUR_VERSION]) |
|||
data1_global = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description |
|||
FROM `tabPublish` where vehicle='{}' and language='{}' and publish_status='Published' |
|||
and publish_type='Global' |
|||
and vehicle_status='Active' and publish_module='Automotive System' and version = (select max(version) as version |
|||
from `tabPublish` where publish_module='Automotive System' |
|||
and vehicle='{}' and publish_type='Global' and language='{}');'''.format(v_id, lang, v_id, lang), as_dict=1) |
|||
|
|||
data1 = frappe.db.sql('''SELECT name,format(max(version),2) as version,vehicle_id,language,release_description as description |
|||
FROM `tabPublish` where vehicle='{}' and language='{}' |
|||
and publish_status='Published' and vehicle_status='Active' and publish_module='Automotive System' |
|||
and vehicle='{}' and language='{}' |
|||
;'''.format(v_id, lang, v_id, lang), as_dict=1) |
|||
|
|||
if data1[0]['version'] is not None: |
|||
if current_version == float(data1[0]['version']): |
|||
|
|||
data1[0][CUR_VERSION] = current_version |
|||
data1[0][UPDATE_AVAILABLE] = "false" |
|||
vehicle_req_list.append(data1) |
|||
elif len(data1_global) == 0: |
|||
|
|||
if current_version < float(data1[0]['version']): |
|||
data = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description |
|||
FROM `tabPublish` where vehicle='{}' and language='{}' and publish_status='Published' and |
|||
publish_type='{}' and vehicle_status='Active' and publish_module='Automotive System' and |
|||
version = (select max(version) as version from `tabPublish` where publish_module='Automotive System' |
|||
and vehicle='{}' and language='{}')'''.format(v_id, lang, publish_type[0][0], v_id, lang), as_dict=1) |
|||
data_append = [] |
|||
rd = [] |
|||
try: |
|||
for d in data: |
|||
d[UPDATE_AVAILABLE] = 'true' |
|||
|
|||
d[CUR_VERSION] = current_version |
|||
data_append.append(d) |
|||
vehicle_req_list.append(data_append) |
|||
|
|||
except: |
|||
pass |
|||
else: |
|||
if current_version < float(data1_global[0]['version']): |
|||
data = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description |
|||
FROM `tabPublish` where vehicle='{}' and language='{}' |
|||
and publish_status='Published' and publish_type='Global' and vehicle_status='Active' |
|||
and publish_module='Automotive System' |
|||
and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' |
|||
and vehicle='{}' and language='{}')'''.format(v_id, lang, v_id, lang), as_dict=1) |
|||
|
|||
elif current_version == float(data1_global[0]['version']): |
|||
|
|||
if current_version < float(data1[0]['version']): |
|||
data = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description |
|||
FROM `tabPublish` where vehicle='{}' and language='{}' and publish_status='Published' and publish_type='{}' |
|||
and vehicle_status='Active' and publish_module='Automotive System' |
|||
and version = (select max(version) as version from `tabPublish` where publish_module='Automotive System' |
|||
and vehicle='{}' and language='{}')'''.format(v_id, lang, publish_type[0][0], v_id, lang), as_dict=1) |
|||
|
|||
elif current_version > float(data1_global[0]['version']): |
|||
|
|||
if current_version < float(data1[0]['version']): |
|||
data = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description |
|||
FROM `tabPublish` where vehicle='{}' and language='{}' and publish_status='Published' and |
|||
publish_type='{}' and vehicle_status='Active' and publish_module='Automotive System' and |
|||
version = (select max(version) as version from `tabPublish` where publish_module='Automotive System' |
|||
and vehicle='{}' and language='{}')'''.format(v_id, lang, publish_type[0][0], v_id, lang), as_dict=1) |
|||
|
|||
# return data |
|||
data_append = [] |
|||
rd = [] |
|||
try: |
|||
for d in data: |
|||
d[UPDATE_AVAILABLE] = 'true' |
|||
|
|||
d[CUR_VERSION] = current_version |
|||
data_append.append(d) |
|||
vehicle_req_list.append(data_append) |
|||
|
|||
# for note in publish_dec: |
|||
# rd.append(note) |
|||
# vehicle_req_list.append(rd) |
|||
# for n in publish_dec: |
|||
# rd.append(n['release_description']) |
|||
# vehicle_req_list.append(rd) |
|||
except: |
|||
pass |
|||
|
|||
response['LanguageID'] = lang |
|||
|
|||
if len(vehicle_req_list) != 0: |
|||
data = [vu[0] for vu in vehicle_req_list] |
|||
|
|||
# publish_dec_txt = [pd[0] for pd in publish_dec] |
|||
response['VehicleReqList'] = data |
|||
# response['release_description'] = publish_dec_txt['release_description'] |
|||
return response |
|||
else: |
|||
return {STATUS: 0, ERROR: "No Vehicles in criteria"} |
|||
else: |
|||
return {STATUS: 0, ERROR: "Invalid Publish Details"} |
|||
else: |
|||
return {STATUS: 0, ERROR: "Invalid Publish Details"} |
|||
|
|||
except Exception as e: |
|||
|
|||
return {STATUS: 0, ERROR: str(e)} |
|||
else: |
|||
return {STATUS: 0, ERROR: "Check argument: vehicle list"} |
@ -0,0 +1,321 @@ |
|||
import frappe |
|||
from frappe.model.document import Document |
|||
import json |
|||
import os |
|||
from frappe.utils import cstr |
|||
|
|||
from smart_service.apis.app_user_login import input_validation |
|||
|
|||
JSON_EXT = ".json" |
|||
JSON_INT_PATH = "/files/json_files/internal/" |
|||
JSON_FULL_INT_PATH = "/files/json_files/full_update/internal/" |
|||
JSON_GLOABL_PATH = "/files/json_files/global/" |
|||
JSON_FULL_GLOBAL_PATH = "/files/json_files/full_update/" |
|||
STATUS = "status" |
|||
ERROR = "error" |
|||
UPDATE_AVAILABLE = 'IsUpdateAvailable' |
|||
UPDATE_FAILED = "Update checking Failed" |
|||
PARAM_MISSING = "Parameter missing :" |
|||
VERSION = 'version' |
|||
CUR_VERSION = 'CurrentVersion' |
|||
DIS = 'Discription' |
|||
FILE_ERROR = 'File failed to load' |
|||
LANGUAGE = 'language' |
|||
FILE_SIZE = 'FileSize' |
|||
JSON_URL = 'JsonURL' |
|||
REMARKS = 'remarks' |
|||
|
|||
|
|||
site_name = cstr(frappe.local.site) |
|||
base_url = os.path.expanduser( |
|||
"~") + "/frappe-bench/sites/" + site_name + "/public" |
|||
|
|||
|
|||
def custom_rate_limit(limit, seconds): |
|||
ip_based = True |
|||
ip = frappe.local.request_ip if ip_based is True else None |
|||
identity = None |
|||
|
|||
identity = ip |
|||
cache_key = f"rl:{frappe.form_dict.cmd}:{identity}" |
|||
value = frappe.cache().get(cache_key) or 0 |
|||
if not value: |
|||
frappe.cache().setex(cache_key, seconds, 0) |
|||
|
|||
value = frappe.cache().incrby(cache_key, 1) |
|||
if value > limit: |
|||
frappe.local.response["http_status_code"] = 429 |
|||
return "You hit the rate limit because of too many requests. Please try after sometime." |
|||
return 1 |
|||
|
|||
|
|||
@frappe.whitelist(allow_guest=1) |
|||
def check_all_vehicle_updates(vehicle_list=None): |
|||
""" Validate Input's """ |
|||
val = input_validation(vehicle_list=vehicle_list) |
|||
|
|||
""" Error response based on missing Input arguements """ |
|||
if val != '': |
|||
return {"LanguageID": None, |
|||
"VehicleReqList": [], STATUS: 0, ERROR: PARAM_MISSING+val} |
|||
|
|||
if vehicle_list: |
|||
""" Validate Input keys """ |
|||
try: |
|||
vehicle_req_list = [] |
|||
v_list = json.loads(vehicle_list) |
|||
language = v_list['LanguageID'] |
|||
iid = v_list['InstallationId'] |
|||
vehicle_data = v_list['VehicleReqList'] |
|||
publish_type = frappe.db.sql( |
|||
'''SELECT publish_type FROM `tabApp Device` where name='{}';'''.format(iid), as_list=1) |
|||
|
|||
if not publish_type: |
|||
""" Error Response for Invalid Publish Type """ |
|||
return {"LanguageID": None, |
|||
"VehicleReqList": [], STATUS: 0, ERROR: "Publish Type not set "} |
|||
|
|||
for v in vehicle_data: |
|||
vehicle_id = get_vehicle_id(v['Vehicle']) |
|||
publish_response = get_all_vehicle_publish_data( |
|||
v['Vehicle'], language, float(v['CurrentVersion']), publish_type[0][0], vehicle_id) |
|||
|
|||
vehicle_req_list.append(publish_response) |
|||
|
|||
""" Vehicle List Response """ |
|||
return {"LanguageID": language, |
|||
"VehicleReqList": vehicle_req_list, STATUS: 1, ERROR: 'No Error'} |
|||
|
|||
except Exception as e: |
|||
""" Error Response for Input key's validation """ |
|||
return {"LanguageID": None, |
|||
"VehicleReqList": [], STATUS: 0, ERROR: str(e)} |
|||
|
|||
else: |
|||
""" Error Response for Empty Vehicle List """ |
|||
return {"LanguageID": None, |
|||
"VehicleReqList": [], STATUS: 0, ERROR: "Check Arugument: vehicle list"} |
|||
|
|||
|
|||
def get_vehicle_id(vehicle): |
|||
vehicle_id = frappe.db.sql( |
|||
'''select myid as vehicle_id from `tabVehicle` where vehicle ='{0}'; '''.format(vehicle), as_dict=1) |
|||
if vehicle_id: |
|||
return vehicle_id[0]['vehicle_id'] |
|||
else: |
|||
False |
|||
|
|||
|
|||
def get_all_vehicle_publish_data(vehicle, l_id, current_version, publish_type, vehicle_id): |
|||
""" Response Structure """ |
|||
vehicle_response_data = { |
|||
"name": None, |
|||
"version": 0, |
|||
"vehicle_id": vehicle_id, |
|||
"language": l_id, |
|||
"description": None, |
|||
ERROR: None, |
|||
UPDATE_AVAILABLE: False, |
|||
"CurrentVersion": current_version |
|||
} |
|||
latest_global_version = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description |
|||
FROM `tabPublish` where vehicle='{0}' and language='{1}' and publish_status='Published' and vehicle_status='Active' |
|||
and publish_module='Automotive System' and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' and vehicle='{0}' and publish_type='Global' and language='{1}'); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
|
|||
latest_publish_version = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description |
|||
FROM `tabPublish` where vehicle='{0}' and language='{1}' and publish_status='Published' and vehicle_status='Active' |
|||
and publish_module='Automotive System' and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' and vehicle='{0}' and language='{1}' and version > {2}); |
|||
'''.format(vehicle, l_id, current_version), as_dict=1) |
|||
|
|||
if not latest_publish_version: |
|||
""" Latest File already downloaded Global/Internal """ |
|||
|
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Latest Version" |
|||
|
|||
else: |
|||
if not latest_global_version or (current_version >= float(latest_global_version[0]['version'])): |
|||
|
|||
""" Only Internal available """ |
|||
|
|||
if publish_type == 'Internal': |
|||
value = round( |
|||
float(latest_publish_version[0]['version']) - current_version, 2) |
|||
|
|||
if value == 0.01: |
|||
""" 1 Internal File """ |
|||
data = frappe.db.sql('''SELECT name, format(version,2) as version,vehicle_id, language, |
|||
release_description as description FROM `tabPublish` where vehicle='{0}' and language='{1}' and |
|||
publish_status='Published' and vehicle_status='Active' and publish_module='Automotive System' |
|||
and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' and publish_type='Internal' |
|||
and vehicle='{0}' and language='{1}'); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
|
|||
if data: |
|||
file_name = JSON_INT_PATH+vehicle+"/"+vehicle + \ |
|||
"-" + l_id + "_v" + \ |
|||
str(data[0][VERSION]) + JSON_EXT |
|||
|
|||
if os.path.exists(base_url + file_name): |
|||
vehicle_response_data['name'] = data[0]['name'] |
|||
|
|||
vehicle_response_data['version'] = data[0]['version'] |
|||
# vehicle_response_data['vehicle_id'] = data[0]['vehicle_id'] |
|||
vehicle_response_data['language'] = data[0]['language'] |
|||
vehicle_response_data['description'] = data[0]['description'] |
|||
# vehicle_response_data['FileSize'] = get_file_size( |
|||
# file_name) |
|||
# vehicle_response_data['JsonURL'] = file_name |
|||
vehicle_response_data[UPDATE_AVAILABLE] = True |
|||
vehicle_response_data[REMARKS] = "Internal available" |
|||
|
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Internal available" |
|||
vehicle_response_data[ERROR] = "Failed to read file" |
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Internal available" |
|||
vehicle_response_data[ERROR] = "Failed to get Publish version" |
|||
|
|||
else: |
|||
""" Full Internal File """ |
|||
data = frappe.db.sql('''SELECT name, format(version,2) as version,vehicle_id, language, |
|||
release_description as description FROM `tabPublish` where vehicle='{0}' and language='{1}' and |
|||
publish_status='Published' and vehicle_status='Active' and publish_module='Automotive System' |
|||
and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' and publish_type='Internal' |
|||
and vehicle='{0}' and language='{1}' ); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
|
|||
if data: |
|||
file_name = JSON_FULL_INT_PATH+vehicle+"/"+vehicle + \ |
|||
"-" + l_id + "-"+"full_v" + \ |
|||
str(data[0][VERSION] |
|||
) + JSON_EXT |
|||
if os.path.exists(base_url + file_name): |
|||
vehicle_response_data['name'] = data[0]['name'] |
|||
vehicle_response_data['version'] = data[0]['version'] |
|||
# vehicle_response_data['vehicle_id'] = data[0]['vehicle_id'] |
|||
vehicle_response_data['language'] = data[0]['language'] |
|||
vehicle_response_data['description'] = data[0]['description'] |
|||
# vehicle_response_data['FileSize'] = get_file_size( |
|||
# file_name) |
|||
# vehicle_response_data['JsonURL'] = file_name |
|||
|
|||
vehicle_response_data[UPDATE_AVAILABLE] = True |
|||
vehicle_response_data[REMARKS] = "Internal Full Update available" |
|||
|
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Internal Full Update available" |
|||
vehicle_response_data[ERROR] = "Failed to read file" |
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Internal Full Update available" |
|||
vehicle_response_data[ERROR] = "Failed to get Publish version" |
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "No Update available" |
|||
|
|||
elif current_version < float(latest_global_version[0]['version']): |
|||
""" Only Global available """ |
|||
|
|||
if (float(latest_global_version[0]['version']) - int(current_version)) > 1: |
|||
""" Full Global File """ |
|||
data = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id, language, |
|||
release_description as description FROM `tabPublish` where vehicle='{0}' and language='{1}' and |
|||
publish_status='Published' and vehicle_status='Active' and publish_module='Automotive System' |
|||
and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' |
|||
and vehicle='{0}' and language='{1}' and publish_type ='Global'); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
if data: |
|||
file_name = JSON_FULL_GLOBAL_PATH + vehicle + "/" + vehicle + \ |
|||
"-" + l_id + "-"+"full_v" + \ |
|||
str(data[0][VERSION] |
|||
) + JSON_EXT |
|||
|
|||
if os.path.exists(base_url + file_name): |
|||
|
|||
vehicle_response_data['name'] = data[0]['name'] |
|||
vehicle_response_data['version'] = data[0]['version'] |
|||
# vehicle_response_data['vehicle_id'] = data[0]['vehicle_id'] |
|||
vehicle_response_data['language'] = data[0]['language'] |
|||
vehicle_response_data['description'] = data[0]['description'] |
|||
|
|||
# vehicle_response_data['FileSize'] = get_file_size( |
|||
# file_name) |
|||
# vehicle_response_data['JsonURL'] = file_name |
|||
|
|||
vehicle_response_data[UPDATE_AVAILABLE] = True |
|||
vehicle_response_data[REMARKS] = "Global Full Update available" |
|||
|
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Global Full Update available" |
|||
vehicle_response_data[ERROR] = "Failed to read file" |
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Global Full Update available" |
|||
vehicle_response_data[ERROR] = "Failed to get Publish version" |
|||
|
|||
# vehicle_response_data[UPDATE_AVAILABLE] = True |
|||
# vehicle_response_data[REMARKS] = "Full Update Global available" |
|||
else: |
|||
|
|||
""" Single Global File """ |
|||
# vehicle_response_data[UPDATE_AVAILABLE] = True |
|||
# vehicle_response_data[REMARKS] = "Global available" |
|||
|
|||
data = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id, language, |
|||
release_description as description FROM `tabPublish` where vehicle='{0}' and language='{1}' and |
|||
publish_status='Published' and vehicle_status='Active' and publish_module='Automotive System' |
|||
and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' |
|||
and vehicle='{0}' and language='{1}' and publish_type ='Global'); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
|
|||
if data: |
|||
file_name = JSON_GLOABL_PATH+vehicle+"/"+vehicle + \ |
|||
"-" + l_id + "_v" + \ |
|||
str(data[0][VERSION]) + JSON_EXT |
|||
|
|||
if os.path.exists(base_url + file_name): |
|||
|
|||
vehicle_response_data['name'] = data[0]['name'] |
|||
vehicle_response_data['version'] = data[0]['version'] |
|||
# vehicle_response_data['vehicle_id'] = data[0]['vehicle_id'] |
|||
vehicle_response_data['language'] = data[0]['language'] |
|||
vehicle_response_data['description'] = data[0]['description'] |
|||
|
|||
# vehicle_response_data['FileSize'] = get_file_size( |
|||
# file_name) |
|||
# vehicle_response_data['JsonURL'] = file_name |
|||
|
|||
vehicle_response_data[UPDATE_AVAILABLE] = True |
|||
vehicle_response_data[REMARKS] = "Global available" |
|||
|
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Global available" |
|||
vehicle_response_data[ERROR] = "Failed to read file" |
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Global available" |
|||
vehicle_response_data[ERROR] = "Failed to get Publish version" |
|||
|
|||
else: |
|||
""" Unknow condition Dont Know Yet """ |
|||
value = {'latest_publish_version': latest_publish_version[0]['version'], |
|||
" latest_global_version": latest_global_version[0]['version']} |
|||
frappe.log_error('New check Update', str( |
|||
f'Unknow Condition{value}')) |
|||
vehicle_response_data[UPDATE_AVAILABLE] = True |
|||
vehicle_response_data[REMARKS] = "New Condition" |
|||
|
|||
return vehicle_response_data |
File diff suppressed because it is too large
Loading…
Reference in new issue