from google.oauth2 import service_account import datetime from datetime import date from dateutil.relativedelta import relativedelta import traceback import requests import json import sys from utils import parse_order_date, get_phone_with_code from send_whatsapp2024 import ( send_whatsapp, generate_jeevnai_whatsapp_image, capture_full_page_screenshot, png_to_jpg, stop_server, ) from google.cloud import storage from upload_file import upload_file_to_storage from logger.main_logger2 import Logger companyDomainNameToCodeName = {"linxag.ng": "linxag", "sat.payagri1.com": "payagri"} SERVICE_KEY_PATH = "servicekey.json" PROJECT_NAME = "farmbase-b2f7e" def gen_report_new( uid, field_address, center_lat, center_long, imagedate, fieldid, field_area, lang, failed, whitelabel, rangeLatLng, points, polygonID=None, ndviAvgVal=0, ndwiAvgVal=0, fieldOrPolyData=None, ): """ :param whitelabel: the domain name of the company """ import pdfkit # import io # import codecs import report_html_filler as filler # from create_bargraph import create_bargraph from create_linegraph import create_linegraph import firebase_admin from firebase_admin import credentials from firebase_admin import db # from firebase_admin import messaging # import time # from get_weather_data import get_weather_data # from create_temperature_bargraph import create_temperature_bargraph from create_weather_line_graph import create_weather_line_graph # import cv2 # from PIL import Image # from create_index_piechart import create_index_piechart from get_weather_forecast import get_weather_forecast # from column_img import column_img # import csv import os.path # from pyvirtualdisplay import Display # from pdf_compressor import compress # from geopy.geocoders import Nominatim from create_instructions import ( getRedZones, createFieldDirImg, getInstructionsByZones, isCloudyImg, getSmsFullInstructionsByZones, createDirOnMapImg, ) from getMapByCoordinates import getFieldMapImg from gen_radar_hybrid import create_hybrid_image # from docx_to_pdf import docx_to_pdf # Fetch the service account key JSON file contents try: cred = credentials.Certificate("servicekey.json") try: firebase_admin.initialize_app( cred, {"databaseURL": "https://farmbase-b2f7e-31c0c.firebaseio.com/"} ) except: print("fire running") try: fireApp2 = firebase_admin.initialize_app( cred, {"databaseURL": "https://farmbase-b2f7e-60c5a.firebaseio.com/"}, "fireApp2", ) except: fireApp2 = firebase_admin.get_app("fireApp2") # whatsappSmsCompanies = { # "ITC": { # # sehore1/2, # "UIDs": ["fSRlyKzCxxMJHJqDzpGhQImyyxf2", "rF4qo7DGoxd3AeEC1Ux4vBGbsbl1"], # "defaultSmsLang": "hi", # "isDefaultLangCompulsory": True, # }, # "RohaFarmsLLP": { # # rohafarmsllp@gmail.com, # "UIDs": ["XSzwRsmzu4OeCm8UvllGn7xsnOk1"], # "defaultSmsLang": "en", # "isDefaultLangCompulsory": True, # "fieldIDs": [ # "1663590354626", # "1663868427256", # "1663868432667", # "1663868434413", # "1663868438991", # "1663868440996", # "1663868442297", # "1663868443515", # "1663868444633", # "1663868445670", # ], # }, # "Godrej":{ # #yaswanth.tippireddy@godrejagrovet.com # "UIDs": ["TCXcp5VIsfhHZrh0nm2VsgBtcGy2"], # "defaultSmsLang": "en", # "isDefaultLangCompulsory": True, # } # } # whatsappSmsCompany = None # for key, company in whatsappSmsCompanies.items(): # if uid in company["UIDs"]: # whatsappSmsCompany = company # break whatsappEnabled = fieldOrPolyData.get("isWhatsappEnabled", None) fSmsLang = fieldOrPolyData.get("smsLang", None) userData = db.reference(f"{uid}/MyProfile", fireApp2).get() if userData is not None: # user data present in DB # isGodrej = uid in whatsappSmsCompanies["Godrej"]["UIDs"] companyCode = ( userData["CompanyCode"] if "CompanyCode" in userData else "Farmonaut" ) else: # no user data present; check by whitelabel name whitelabelCodeName = companyDomainNameToCodeName.get(whitelabel) userData = {} companyCode = whitelabelCodeName or "Farmonaut" isGodrej = companyCode == "Godrej" orgReportData = db.reference(f"OrganisationData/{companyCode}/ReportData").get() addLogo = orgReportData.get("addLogo", True) # adds logo to map img dynamicLinkDomain = orgReportData.get("dynamicLinkDomain") expertNumber = orgReportData.get("expertNumber", "6366026267") isPolygon = polygonID != None # if isPolygon: # orgSmsData = { # "defaultSmsLang": "en", # } # geolocator = Nominatim(user_agent="geoapiExercises") # location = geolocator.reverse(str(center_lat)+"," + str(center_long)) # address_obj = location.raw['address'] # temp_field_address = address_obj.get('suburb','') + ", " + address_obj.get('city','') + ", " + address_obj.get('state','') + ", " + address_obj.get('country','') temp_field_address = field_address cred = service_account.Credentials.from_service_account_file(SERVICE_KEY_PATH) storage_client = storage.Client(PROJECT_NAME, cred) bucket_name = "farmbase-b2f7e.appspot.com" all_langs = lang.split(",") # hasDefaultSmsLang = None if orgSmsData == None else orgSmsData[ # "defaultSmsLang"] in all_langs if whatsappEnabled == None: whatsappEnabled = userData.get( "isWhatsappEnabled", False ) or orgReportData.get("isWhatsappEnabled", False) print("langg1s: ", fSmsLang, whatsappEnabled, userData, orgReportData) if whatsappEnabled: fSmsLang = ( fSmsLang or userData.get("defaultSmsLang", None) or orgReportData.get("defaultSmsLang", None) ) print("fSmsLang: ", fSmsLang) isSmsLangInReportLangs = fSmsLang in all_langs today = date.today() date_d = today.strftime("%Y%m%d") year = date_d[0:4] month = date_d[4:6] day = date_d[6:] report_date = year + "-" + month + "-" + day + " (in yyyy-mm-dd)" sensed_year = imagedate[0:4] sensed_month = imagedate[4:6] sensed_day = imagedate[6:] sensed_imagedate = ( sensed_year + "-" + sensed_month + "-" + sensed_day + " (in yyyy-mm-dd)" ) sensed_day = int(sensed_day) sensed_month = int(sensed_month) print("report_date", report_date, sensed_imagedate) if isPolygon: etci_image = "%s/%s/%s_TCI.png" % (uid, fieldid, polygonID) ndvi_image = "%s/%s/%s_NDVI.png" % (uid, fieldid, polygonID) ndwi_image = "%s/%s/%s_NDWI.png" % (uid, fieldid, polygonID) rvi_image = "%s/%s/%s_RVI.png" % (uid, fieldid, polygonID) rsm_image = "%s/%s/%s_RSM.png" % (uid, fieldid, polygonID) ndwi_image_cmap2 = "%s/%s/%s_NDWI_CMAP2.png" % (uid, fieldid, polygonID) ndvi_image_cmap2 = "%s/%s/%s_NDVI_CMAP2.png" % (uid, fieldid, polygonID) # rvi_image_cmap2 = "%s/%s/%s_RVI_CMAP2.png" % (uid, fieldid, polygonID) # rsm_image_cmap2 = "%s/%s/%s_RSM_CMAP2.png" % (uid, fieldid, polygonID) recl_image = "%s/%s/%s_RECL.png" % (uid, fieldid, polygonID) recl_image_cmap2 = "%s/%s/%s_RECL_CMAP2.png" % (uid, fieldid, polygonID) dir_img = "%s/%s/%s_dirImg.png" % (uid, fieldid, polygonID) map_img = "%s/%s/%s_mapImg.jpg" % (uid, fieldid, polygonID) bdy_img = "%s/%s/%s_bdyImg.jpg" % (uid, fieldid, polygonID) ndre_image = "%s/%s/%s_NDRE.png" % (uid, fieldid, polygonID) ndre_image_cmap2 = "%s/%s/%s_NDRE_CMAP2.png" % (uid, fieldid, polygonID) else: etci_image = "%s/%s/ETCI.png" % (uid, fieldid) ndvi_image = "%s/%s/ndvi.png" % (uid, fieldid) ndwi_image = "%s/%s/ndwi.png" % (uid, fieldid) rvi_image = "%s/%s/rvi.png" % (uid, fieldid) rsm_image = "%s/%s/rsm.png" % (uid, fieldid) ndwi_image_cmap2 = "%s/%s/ndwi_cmap2.png" % (uid, fieldid) ndvi_image_cmap2 = "%s/%s/ndvi_cmap2.png" % (uid, fieldid) # rvi_image_cmap2 = "%s/%s/rvi_cmap2.png" % (uid, fieldid) # rsm_image_cmap2 = "%s/%s/rsm_cmap2.png" % (uid, fieldid) dir_img = "%s/%s/dirImg.png" % (uid, fieldid) map_img = "%s/%s/mapImg.jpg" % (uid, fieldid) bdy_img = "%s/%s/bdyImg.jpg" % (uid, fieldid) recl_image = "%s/%s/recl.png" % (uid, fieldid) recl_image_cmap2 = "%s/%s/recl_cmap2.png" % (uid, fieldid) ndre_image = "%s/%s/ndre.png" % (uid, fieldid) ndre_image_cmap2 = "%s/%s/ndre_cmap2.png" % (uid, fieldid) tci_image = "%s/%s/TCI.png" % (uid, fieldid) evi_image = "%s/%s/evi.png" % (uid, fieldid) savi_image = "%s/%s/savi.png" % (uid, fieldid) ndmi_image = "%s/%s/ndmi.png" % (uid, fieldid) hybrid_image = "%s/%s/hybrid.png" % (uid, fieldid) hybrid_blind_image = "%s/%s/hybrid_blind.png" % (uid, fieldid) dem_image = "%s/%s/dem.png" % (uid, fieldid) soc_image = "%s/%s/soc.png" % (uid, fieldid) evapo_image = "%s/%s/evapo.png" % (uid, fieldid) evi_image_cmap2 = "%s/%s/evi_cmap2.png" % (uid, fieldid) savi_image_cmap2 = "%s/%s/savi_cmap2.png" % (uid, fieldid) soc_image_cmap2 = "%s/%s/soc_cmap2.png" % (uid, fieldid) ndmi_image_cmap2 = "%s/%s/ndmi_cmap2.png" % (uid, fieldid) fieldLatLngs = { "minLat": rangeLatLng[0], "minLng": rangeLatLng[1], "maxLat": rangeLatLng[2], "maxLng": rangeLatLng[3], "centerLat": center_lat, "centerLng": center_long, } print(["pp1", points, fieldLatLngs, map_img]) mapImgDimens = db.reference( f"PaidMonitoredFields/PMF/{uid}/{fieldid}/MapDimensions" ).get() mapImgPresentInStorage = checkIfStorageFilePresent( uid, fieldid, "default", "mapImg.jpg", storage_client ) createMapImg = mapImgDimens == None or (not mapImgPresentInStorage) if not createMapImg: # Field Map Img is already present outPath = downloadStorageFileToPath( uid, fieldid, "default", "mapImg.jpg", storage_client, map_img ) createMapImg = outPath == None if createMapImg: # Field Map Img is not present; create and upload mapImgDimens = getFieldMapImg(points, fieldLatLngs, map_img) db.reference(f"PaidMonitoredFields/PMF/{uid}/{fieldid}/MapDimensions").set( mapImgDimens ) upload_file_to_storage(uid, fieldid, "default", map_img, "mapImg.jpg") isNDVIPresent = os.path.exists(ndvi_image) try: isCloudy = isCloudyImg(tci_image, ndvi_image) if isNDVIPresent else True # create_hybrid_image(ndvi_image, ndwi_image, (uid + '/' + fieldid + '/hybrid.png')) except: isCloudy = True # create_hybrid_image(rvi_image, rsm_image, (uid + '/' + fieldid + '/hybrid.png')) if isCloudy == True: create_hybrid_image( rvi_image, rsm_image, (uid + "/" + fieldid + "/hybrid.png"), uid, fieldid, imagedate, ) else: create_hybrid_image( ndvi_image, ndwi_image, (uid + "/" + fieldid + "/hybrid.png"), uid, fieldid, imagedate, ) isSmallPlant = ndviAvgVal < 0.4 and ndwiAvgVal < 0.4 isRVIPresent = os.path.exists(rvi_image) if isRVIPresent: rviRedZones = getRedZones(rvi_image) rsmRedZones = getRedZones(rsm_image) # rvi2RedZones = getRedZones(rvi_image_cmap2) # rsm2RedZones = getRedZones(rsm_image_cmap2) if isNDVIPresent: ndviRedZones = getRedZones(ndvi_image) ndvi2RedZones = getRedZones(ndvi_image_cmap2, True) ndwiRedZones = getRedZones(ndwi_image) ndwi2RedZones = getRedZones(ndwi_image_cmap2, True) socRedZones = getRedZones(soc_image) soc2RedZones = getRedZones(soc_image_cmap2, True) # save data to DB irriZones = ( ( rsmRedZones # if not isSmallPlant else rsm2RedZones) if isRVIPresent else None ) if isCloudy else ( (ndwiRedZones if not isSmallPlant else ndwi2RedZones) if isNDVIPresent else None ) ) cropZones = ( ( rviRedZones # if not isSmallPlant else rvi2RedZones) if isRVIPresent else None ) if isCloudy else ( (ndviRedZones if not isSmallPlant else ndvi2RedZones) if isNDVIPresent else None ) ) print("isRVTPresent, irriZones, cropZones", isRVIPresent, irriZones, cropZones) soilZones = ( (socRedZones if not isSmallPlant else soc2RedZones) if isNDVIPresent else None ) # Initialize the app with a service account, granting admin privileges # whitelabel = "nothing" health = ( db.reference("PaidMonitoredFields") .child("PMF") .child(uid) .child(fieldid) .child("Health") .get() ) ndvi = try_index(health, "ndvi") evi = try_index(health, "evi") ndwi = try_index(health, "ndwi") ndre = try_index(health, "ndre") ndmi = try_index(health, "ndmi") savi = try_index(health, "savi") soc = try_index(health, "soc") # if lang == 'ar': # alignmentType = WD_ALIGN_PARAGRAPH.RIGHT # else: # alignmentType = WD_ALIGN_PARAGRAPH.LEFT tmp_header_img_url = "https://farmonaut.com/Images/" + uid + ".jpeg" if is_url_image(tmp_header_img_url): header_img_url = tmp_header_img_url else: tmp_header_img_url = "https://farmonaut.com/Images/" + whitelabel + ".jpg" if is_url_image(tmp_header_img_url): header_img_url = tmp_header_img_url else: header_img_url = orgReportData.get( "logoUrl", "https://farmonaut.com/Images/Farmonaut_Logo_1.jpg" ) f_lat = str(center_lat) f_lat = f_lat[0:6] f_long = str(center_long) f_long = f_long[0:6] cur_date = imagedate cur_weather_data = ( db.reference("PaidMonitoredFields") .child("PMF") .child(uid) .child(fieldid) .child("Weather") .child(cur_date) .get() ) try: min_temp = cur_weather_data["min_temp"] except: min_temp = None past_weather_url = "https://farmonaut.com/Images/data_not_found.jpg" # min_temp = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('min_temp').get() if min_temp is None: print("Weather data not available") min_temp = "not available" max_temp = "not available" humidity = "not available" pressure = "not available" station = "not available" wind_speed = "not available" wind_deg = "not available" cloud_cover = "not available" else: min_temp = float(min_temp) - 273 min_temp = str(min_temp) min_temp = min_temp[0:5] max_temp = cur_weather_data["max_temp"] # max_temp = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('max_temp').get() max_temp = float(max_temp) - 273 max_temp = str(max_temp) max_temp = max_temp[0:5] humidity = cur_weather_data["humidity"] # humidity = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('humidity').get() pressure = cur_weather_data["pressure"] # pressure = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('pressure').get() station = cur_weather_data["station"] # station = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('station').get() wind_speed = cur_weather_data["wind_speed"] # wind_speed = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('wind_speed').get() try: wind_deg = cur_weather_data["wind_deg"] except: wind_deg = "Not Available" # wind_deg = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('wind_deg').get() try: cloud_cover = cur_weather_data["cloud_cover"] except: cloud_cover = "Not Available" weather_data = ( db.reference("PaidMonitoredFields") .child("PMF") .child(uid) .child(fieldid) .child("Weather") .get() ) for langIndex in range(len(all_langs)): lang = all_langs[langIndex] lang = lang.replace(" ", "") lang_num = 0 allReportValues = { "addressValueH6": temp_field_address, "fieldAreaValue": field_area, } all_strings = [] row_num = 0 print("looping over langs", langIndex, lang) all_strings.append(lang) try: reportStrings = db.reference("ReportStrings2022").child(lang).get() except: reportStrings = db.reference("ReportStrings2022").child("en").get() # print(reportStrings) for i in reportStrings: all_strings.append(i) # print(all_strings) print("langStrings[5]", all_strings[5]) allReportValues["generationDateValueH6"] = year + "-" + month + "-" + day allReportValues["visitDateValueH6"] = ( str(sensed_year) + "-" + str(sensed_month) + "-" + str(sensed_day) ) allReportValues["rgbTCIImg"] = tci_image allReportValues["rgbETCIImg"] = etci_image allReportValues["colorBlindImg"] = hybrid_blind_image allReportValues["evapoImg"] = evapo_image allReportValues["decisionTreeBasicImg"] = hybrid_image allReportValues["ndviSmallHeightImg"] = ndvi_image_cmap2 allReportValues["eviGoodHeightImg"] = evi_image allReportValues["eviSmallHeightImg"] = evi_image_cmap2 allReportValues["ndwiGoodHeightImg"] = ndwi_image allReportValues["ndwiSmallHeightImg"] = ndwi_image_cmap2 allReportValues["ndreGoodHeightImg"] = ndre_image allReportValues["ndreSmallHeightImg"] = ndre_image_cmap2 allReportValues["saviGoodHeightImg"] = savi_image allReportValues["saviSmallHeightImg"] = savi_image_cmap2 allReportValues["ndmiGoodHeightImg"] = ndmi_image allReportValues["ndmiSmallHeightImg"] = ndmi_image_cmap2 if uid == "M53J9SImW9My4vghlCaaWLYxsc22": lang = "hi" # Create and save the field dir img in language 'lang' to storage createFieldDirImg(etci_image, dir_img, reportStrings, lang) dirImgStoragePath = "dir_img_%s.png" % lang if not checkIfStorageFilePresent( uid, fieldid, "default", dirImgStoragePath, storage_client ): upload_file_to_storage( uid, fieldid, "default", dir_img, dirImgStoragePath ) allReportValues["decisionTreeFieldDirImg"] = dir_img allReportValues["isPolygon"] = isPolygon allReportValues["isCloudy"] = isCloudy allReportValues["isSmallPlant"] = isSmallPlant if mapImgDimens != None: allReportValues["mapImgSize"] = ( mapImgDimens.get("width"), mapImgDimens.get("height"), ) allReportValues["fieldImgPaddings"] = ( mapImgDimens.get("horiPadPer"), mapImgDimens.get("verPadPer"), ) allReportValues["mapImg"] = map_img allReportValues["demGoodHeightImg"] = dem_image # allReportValues["demSmallHeightImg"] = dem_image_cmap2 allReportValues["socGoodHeightImg"] = soc_image allReportValues["socSmallHeightImg"] = soc_image_cmap2 if isRVIPresent: allReportValues["rviImg"] = rvi_image allReportValues["soilMoistImg"] = rsm_image allReportValues["decisionTreeCloudyIrriP"] = getInstructionsByZones( rsmRedZones, reportStrings, True, lang ) allReportValues["decisionTreeCloudyCropP"] = getInstructionsByZones( rviRedZones, reportStrings, False, lang ) if isNDVIPresent: allReportValues["ndviGoodHeightImg"] = ndvi_image allReportValues["decisionTreeLaterIrriP"] = getInstructionsByZones( ndwiRedZones, reportStrings, True, lang ) allReportValues["decisionTreeEarlyIrriP"] = getInstructionsByZones( ndwi2RedZones, reportStrings, True, lang ) allReportValues["decisionTreeLaterCropP"] = getInstructionsByZones( ndviRedZones, reportStrings, False, lang ) allReportValues["decisionTreeEarlyCropP"] = getInstructionsByZones( ndvi2RedZones, reportStrings, False, lang ) if isGodrej: isRECLPresent = os.path.exists(recl_image) if isRECLPresent: allReportValues["reclGoodHeightImg"] = recl_image allReportValues["reclSmallHeightImg"] = recl_image_cmap2 allReportValues["isOilPalm"] = True allReportValues["fieldBdyImg"] = bdy_img allReportValues["companyLogoImg"] = header_img_url allReportValues["latValue"] = f_lat allReportValues["longValue"] = f_long # weather allReportValues["weatherValueStn"] = station allReportValues["weatherValuePressure"] = pressure allReportValues["weatherValueCloud"] = cloud_cover allReportValues["weatherValueTempMin"] = min_temp allReportValues["weatherValueTempMax"] = max_temp allReportValues["weatherValueWind"] = wind_speed allReportValues["weatherValueWindDir"] = wind_deg allReportValues["weatherValueHumidity"] = humidity if weather_data is None: print("weather data not available yet") else: cloud_array = [] humidity_array = [] max_temp_array = [] min_temp_array = [] pressure_array = [] wind_deg_array = [] wind_speed_array = [] dates_array = [] for key, val in weather_data.items(): weather_date = key date_data = weather_data[weather_date] dates_array.append(weather_date) dates_array.sort() for key in dates_array: weather_date = key date_data = weather_data[weather_date] try: cloud_array.append(date_data["cloud_cover"]) except: cloud_array.append("not availabe") try: humidity_array.append(date_data["humidity"]) except: humidity_array.append("not available") try: t_max_temp = date_data["max_temp"] - 273 except: t_max_temp = "not available" try: t_min_temp = date_data["min_temp"] - 273 except: t_min_temp = "not available" max_temp_array.append(t_max_temp) min_temp_array.append(t_min_temp) try: pressure_array.append(date_data["pressure"]) except: pressure_array.append("not available") try: wind_deg_array.append(date_data["wind_deg"]) except: wind_deg_array.append("not available") try: wind_speed_array.append(date_data["wind_speed"]) except: wind_speed_array.append("not available") if len(dates_array) > 1: try: line_types = [ "min_temp", "max_temp", "cloud_cover", "humidity", "pressure", "wind_speed", ] weather_array = [ min_temp_array, max_temp_array, cloud_array, humidity_array, pressure_array, wind_speed_array, ] create_weather_line_graph( uid, line_types, weather_array, dates_array, fieldid ) allReportValues["weatherGraphImg"] = ( uid + "/" + fieldid + "/weather_linegraph.png" ) # past_weather_url = upload_file(uid, fieldid, imagedate,(uid+'_weather_linegraph.png'),'past_weather.png') except: past_weather_url = ( "https://farmonaut.com/Images/data_not_found.jpg" ) if len(ndvi) > 1: print("came to linegraph") create_linegraph(uid, "ndvi", ndvi, fieldid) create_linegraph(uid, "evi", evi, fieldid) create_linegraph(uid, "savi", savi, fieldid) create_linegraph(uid, "ndre", ndre, fieldid) create_linegraph(uid, "ndwi", ndwi, fieldid) create_linegraph(uid, "ndmi", ndmi, fieldid) create_linegraph(uid, "soc", soc, fieldid) allReportValues["eviGraphImg"] = ( uid + "/" + fieldid + "/evi_linegraph.png" ) allReportValues["ndviGraphImg"] = ( uid + "/" + fieldid + "/ndvi_linegraph.png" ) allReportValues["ndwiGraphImg"] = ( uid + "/" + fieldid + "/ndwi_linegraph.png" ) allReportValues["ndmiGraphImg"] = ( uid + "/" + fieldid + "/ndmi_linegraph.png" ) allReportValues["ndreGraphImg"] = ( uid + "/" + fieldid + "/ndre_linegraph.png" ) allReportValues["saviGraphImg"] = ( uid + "/" + fieldid + "/savi_linegraph.png" ) allReportValues["socGraphImg"] = ( uid + "/" + fieldid + "/soc_linegraph.png" ) allReportValues["eviIndexImg"] = uid + "/" + fieldid + "/evi_piechart.png" allReportValues["ndviIndexImg"] = uid + "/" + fieldid + "/ndvi_piechart.png" allReportValues["ndwiIndexImg"] = uid + "/" + fieldid + "/ndwi_piechart.png" allReportValues["ndmiIndexImg"] = uid + "/" + fieldid + "/ndmi_piechart.png" allReportValues["ndreIndexImg"] = uid + "/" + fieldid + "/ndre_piechart.png" allReportValues["saviIndexImg"] = uid + "/" + fieldid + "/savi_piechart.png" allReportValues["socIndexImg"] = uid + "/" + fieldid + "/soc_piechart.png" all_weather_data = get_weather_forecast(center_lat, center_long) all_days = [] min_temps = [] max_temps = [] rain_probs = [] max_preps = [] cloud_covers = [] final_days = [] weather_infos = [] for key, val in all_weather_data.items(): all_days.append(key) all_days.sort() all_days = all_days[1:] for key in all_days: # print('kk') # print(key) single_w_data = all_weather_data[key] w_date = datetime.datetime.fromtimestamp(int(key)) final_days.append(str(w_date)[0:10]) weather_infos.append(single_w_data["summary"]) cloud_covers.append(str(int(float(single_w_data["cloudcover"])))) try: temp_min_temp = str(single_w_data["templow"]) # temp_min_temp = ( # str(round(((float(single_w_data["templow"])-32)*(5/9)), 2))) # # temp_min_temp = temp_min_temp + \ # # ((' at '+str(datetime.datetime.fromtimestamp( # # int(single_w_data["templowtime"])))[11:16])) min_temps.append(temp_min_temp) # temp_max_temp = ( # str(round(((float(single_w_data["temphigh"])-32)*(5/9)), 2))) # temp_max_temp = temp_max_temp + \ # ((' at '+str(datetime.datetime.fromtimestamp( # int(single_w_data["temphightime"])))[11:16])) temp_max_temp = str(single_w_data["temphigh"]) max_temps.append(temp_max_temp) except: min_temps.append("not available") max_temps.append("not available") try: rain_probs.append(single_w_data["preciprob"]) temp_prep = str((single_w_data["precimax"])) # temp_prep = temp_prep + \ # ((' at '+str(datetime.datetime.fromtimestamp( # int(single_w_data["precimaxtime"])))[11:16])) max_preps.append(temp_prep) except: rain_probs.append("not available") max_preps.append("not available") allReportValues[ "weather" + str(len(max_preps)) + "MaxPrecipitationTd" ] = max_preps[-1] allReportValues["weather" + str(len(max_temps)) + "MaxTempTd"] = ( max_temps[-1] ) allReportValues["weather" + str(len(min_temps)) + "MinTempTd"] = ( min_temps[-1] ) allReportValues["weather" + str(len(weather_infos)) + "SummaryTd"] = ( weather_infos[-1] ) allReportValues["weather" + str(len(final_days)) + "DateTd"] = ( final_days[-1] ) allReportValues["weather" + str(len(rain_probs)) + "RainTd"] = ( rain_probs[-1] ) allReportValues["weather" + str(len(cloud_covers)) + "CloudCoverTd"] = ( cloud_covers[-1] ) # document.add_page_break() # try: # present_ndvi = float(ndvi[imagedate])/100 # except: # present_ndvi = 0 # try: # present_ndwi = float(ndwi[imagedate])/100 # except: # present_ndwi = 0 # try: # present_ndre = float(ndre[imagedate])/100 # except: # present_ndre = 0 # try: # present_savi = float(savi[imagedate])/100 # except: # present_savi = 0 # try: # present_evi = float(evi[imagedate])/100 # except: # present_evi = 0 # try: # present_ndmi = float(ndmi[imagedate])/100 # except: # present_ndmi = float(0) # try: # present_soc = float(soc[imagedate])/100 # except: # present_soc = 0 # print(html_message) for key, value in allReportValues.items(): allReportValues[key] = str(value) html_str = filler.populateData(lang, reportStrings, allReportValues) field_address = field_address.replace(" ", "_") field_address = field_address.replace(",", "_") pdf_name = ( uid + "/" + fieldid + "/" + whitelabel + (polygonID + "_" if isPolygon else "") + "_report_" + lang + "_" + str(imagedate) + "_" + removeSlash(field_address) + ".pdf" ) htmlfilename = uid + "_" + fieldid + "report.html" destination_blob_name = ( "PaidMonitoredFields/" + str(uid) + "/" + str(fieldid) + "/" + str(imagedate) + "/report.html" ) bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) blob.upload_from_string(html_str) saveToTmpHtml(html_str, htmlfilename) # blob = bucket.blob(destination_blob_name) # blob.download_to_filename(htmlfilename) # with codecs.open('marathi.txt', 'w', encoding='utf-8') as file: # file.write(all_strings[80]) # with codecs.open(htmlfilename, 'w', encoding='utf-8') as file: # file.write(html_message) # htmlfile = open(htmlfilename,"w") # htmlfile.write(html_message) # htmlfile.close() options = { "enable-local-file-access": "", "page-height": "192mm", "page-width": "136mm", "margin-top": "6mm", "margin-bottom": "6mm", "margin-left": "8mm", "margin-right": "8mm", # "font": "report_fonts/Noto_Sans/NotoSans-Medium.ttf", # 'page-size': 'A4', # 'margin-top': '0.75in', } # path_wkhtmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe' # config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf) # with Display(): # pdfkit.from_url(htmlfilename, pdf_name) pdfkit.from_file(htmlfilename, pdf_name, options=options) ## whatsapp sms part try: storageReportName = "%sreport%s.pdf" % ( polygonID + "_" if isPolygon else "", "" if lang == "en" else lang, ) reportUrl = upload_file_to_storage( uid, fieldid, imagedate, pdf_name, storageReportName, 9000 ) ## send whatsapp msg print("whatsapp vars", whatsappEnabled, isSmsLangInReportLangs) if whatsappEnabled and ( (not isSmsLangInReportLangs and langIndex == 0) or (lang == fSmsLang) ): print( "whatsapp started", isCloudy, isRVIPresent, isNDVIPresent, [rsmRedZones, rviRedZones] if isRVIPresent else [], [ndwiRedZones, ndviRedZones] if isNDVIPresent else [], ) ## it's expected that irriZones and cropZones are not None try: templateType = "%s%s" % ( 0 if len(irriZones) != 0 else 1, 0 if len(cropZones) != 0 else 1, ) except: templateType = "11" print(traceback.format_exc()) Logger().mlogger.error( "gen_report_new zone error", { "isCloudy": isCloudy, "isRVIPresent": isRVIPresent, "isNDVIPresent": isNDVIPresent, "irriZones": irriZones, "cropZones": cropZones, "rvi_image": rvi_image, "ndvi_image": ndvi_image, "line_no": sys.exc_info()[2].tb_lineno, "error": traceback.format_exc() }, user=uid ) # smsCropInstructions = getSmsInstructionsByZones( # cropZones, reportStrings, lang) # smsIrriInstructions = getSmsInstructionsByZones( # irriZones, reportStrings, lang) ## set smsLang & get instructions & dirImg smsLang = lang if isSmsLangInReportLangs else (fSmsLang or "en") if uid == "M53J9SImW9My4vghlCaaWLYxsc22": smsLang = "hi" if smsLang != lang: reportStrings = ( db.reference("ReportStrings2022").child(smsLang).get() ) smsInstructions = getSmsFullInstructionsByZones( templateType, irriZones, cropZones, reportStrings, smsLang ) ## check dirMapImg in storage & upload if not present storageDirMapImgName = "%sdir_on_map_%s.png" % ( polygonID + "_" if isPolygon else "", smsLang, ) doesDirMapImgExist = checkIfStorageFilePresent( uid, fieldid, "default", storageDirMapImgName, storage_client, ) if not doesDirMapImgExist: # dir map img not present if lang != smsLang: createFieldDirImg( etci_image, dir_img, reportStrings, smsLang ) dirImgStoragePath = "dir_img_%s.png" % smsLang if not checkIfStorageFilePresent( uid, fieldid, "default", dirImgStoragePath, storage_client, ): upload_file_to_storage( uid, fieldid, "default", dir_img, dirImgStoragePath ) dirMapPath = "%s/%s/%sdirMapImg%s.png" % ( uid, fieldid, polygonID + "_" if isPolygon else "", smsLang, ) createDirOnMapImg( dir_img, map_img, mapImgDimens, dirMapPath, addLogo=addLogo ) dirMapUrl = upload_file_to_storage( uid, fieldid, "default", dirMapPath, storageDirMapImgName ) else: # dir map img present dirMapUrl = getStorageFileUrl( uid, fieldid, "default", storageDirMapImgName, storage_client, ) ## create short URLs dirMapShortUrl = dirMapUrl # createShortLink(dirMapUrl, defaultUrl='https://farmonaut.com/wp-content/uploads/2024/01/thumbnail-Whatsapp-Advisory--1024x576.jpg') if dynamicLinkDomain: reportShortUrl = createShortDynamicLink( reportUrl, dynamicLinkDomain ) else: reportShortUrl = createShortLink(reportUrl) print(dirMapShortUrl, dirMapUrl, reportShortUrl, reportUrl) ## get name, phoneNo if isPolygon: polygonData = ( fieldOrPolyData or db.reference("PaidMonitoredFields") .child("PMF") .child(uid) .child(fieldid) .child("Polygons") .child(polygonID) .get() ) phoneNo = ( polygonData["PhoneNumber"] if "PhoneNumber" in polygonData else polygonData["FarmerInfoData"]["whatsAppNumber"] ) userName = polygonData["Name"] else: phoneNo = ( fieldOrPolyData.get("whatsAppNumber", None) or fieldOrPolyData.get("Phone", None) or fieldOrPolyData.get("FarmerPhoneNumber", None) or userData.get("Phone", None) ) userName = fieldOrPolyData.get("Name", " ") or userData.get( "Name", " " ) ## for DCM if uid == "M53J9SImW9My4vghlCaaWLYxsc22": orgData = fieldOrPolyData["OrgData"] if userName == None: userName = orgData.get("OrgRyotName", "Farmer") if phoneNo == None: phoneNo = orgData.get( "OrgWhatsappNo", orgData.get("OrgMobileNo", None) ) phoneNo = get_phone_with_code(phoneNo) # get no of days left of monitoring orderDateStr = fieldOrPolyData.get("OrderDate") orderDate = parse_order_date(orderDateStr) orderMonths = fieldOrPolyData.get("PaymentType", 0) if orderMonths != -2: expiryDate = orderDate + relativedelta( days=int(orderMonths * 30) ) totalDays = (expiryDate - orderDate).days now = datetime.datetime.now() noDaysDone = (now - orderDate).days noDaysLeft = totalDays - noDaysDone reportsLeft = int(noDaysLeft / 5) orderMsgStr = "{} {} {} {}".format( noDaysLeft, ( reportStrings.get("day") if noDaysLeft == 1 else reportStrings.get("days") ), reportsLeft, ( reportStrings.get("report") if reportsLeft == 1 else reportStrings.get("reports") ), ) # 8 days , 5 reports ## send msg orgCompanyData = orgReportData.get( "smsCompanyName", {"en": "Farmonaut"} ) orgName = orgCompanyData.get( smsLang, orgCompanyData.get("en", "Farmonaut") ) smsParams = [ userName, orgName, temp_field_address, smsInstructions, reportShortUrl, expertNumber, orgName, ] if orderMonths != -2: # non subscription smsParams.insert(6, orderMsgStr) send_whatsapp( dirMapShortUrl, smsParams, lang, uid, fieldid, phoneNo, whitelabel, imagedate, ) except: # whatsapp part error print(traceback.format_exc()) return { "isCloudy": isCloudy, "cropRedZones": ( "empty" if cropZones == None or len(cropZones) == 0 else cropZones ), "irrigationRedZones": ( "empty" if irriZones == None or len(irriZones) == 0 else irriZones ), "socRedZones": ( None if isNDVIPresent else ("empty" if len(soilZones) == 0 else soilZones) ), } except: print(traceback.format_exc()) print("end of gen_report_new") return "yes" def removeSlash(str): return str.replace("/", "") def checkIfStorageFilePresent(uid, fieldId, imgDate, fileName, storage_client): bucket_name = "farmbase-b2f7e.appspot.com" destination_blob_name = ( "PaidMonitoredFields/" + str(uid) + "/" + str(fieldId) + "/" + str(imgDate) + "/" + fileName ) bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) return blob.exists() def getStorageFileUrl(uid, fieldId, imgDate, fileName, storage_client): bucket_name = "farmbase-b2f7e.appspot.com" destination_blob_name = ( "PaidMonitoredFields/" + str(uid) + "/" + str(fieldId) + "/" + str(imgDate) + "/" + fileName ) bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) cred = service_account.Credentials.from_service_account_file("servicekey.json") try: url = blob.generate_signed_url( version="v4", expiration=datetime.timedelta(minutes=10000), method="GET", credentials=cred, ) return url except: return None def getStorageFileUrlGeneral(filePath, storage_client): bucket_name = "farmbase-b2f7e.appspot.com" destination_blob_name = filePath bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) cred = service_account.Credentials.from_service_account_file("servicekey.json") try: url = blob.generate_signed_url( version="v4", expiration=datetime.timedelta(minutes=10000), method="GET", credentials=cred, ) return url except Exception as e: print(e) return None def downloadStorageFileToPath(uid, fieldId, imgDate, fileName, storage_client, outPath): """Downloads field file and save to outPath RETURNS - outPath if successful""" bucket_name = "farmbase-b2f7e.appspot.com" destination_blob_name = ( "PaidMonitoredFields/" + str(uid) + "/" + str(fieldId) + "/" + str(imgDate) + "/" + fileName ) bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) cred = service_account.Credentials.from_service_account_file("servicekey.json") try: blob.download_to_filename(outPath) return outPath except: return None def createShortDynamicLink(longLink, domainPrefix="https://farmonaut.page.link"): apiKey = "AIzaSyCdVSM4xjufjH7v01en4fh6TdomsdurmC8" info = { "dynamicLinkInfo": { "domainUriPrefix": domainPrefix, "link": longLink, }, "suffix": {"option": "SHORT"}, } response = requests.post( "https://firebasedynamiclinks.googleapis.com/v1/shortLinks", params={"key": apiKey}, data=json.dumps(info), ) if response.status_code == 200: return json.loads(response.text)["shortLink"] else: print(response.status_code, response.text) return None def createShortLink(longLink, domainPrefix="https://farmonaut.com", defaultUrl=""): apiKey = "eyJhbGciOiJIUzI1NiIsInR5cGUiOiJKV1QifQ.eyJpYXQiOjE3MTkzMDUzMTgsImlkZW50aWZpZXIiOiJmYXJtb25hdXRfYWRtaW4ifQ.VdTMA95b0zzePCnnlMsxl7TiOYhW9PEQ1EszL7gRQnY" info = {"url": longLink, "domain": domainPrefix} response = requests.post( "https://us-central1-farmbase-b2f7e.cloudfunctions.net/create_short_url", headers={ "Authorization": "Bearer " + apiKey, "Content-Type": "application/json", }, data=json.dumps(info), ) if response.status_code == 200: return json.loads(response.text)["data"]["short_url"] else: return defaultUrl def saveToTmpHtml(htmlStr, htmlFileName): with open(htmlFileName, "w", encoding="utf8") as f: f.write(htmlStr) def try_index(health, index): new_index = {} try: new_index = health[index] except: new_index = {} return new_index def is_url_image(image_url): """ Returns True if the image pointed to by image_url is an image """ image_formats = ("image/png", "image/jpeg", "image/jpg") r = requests.head(image_url) if r.headers["content-type"] in image_formats: return True return False ######### Test Code ######### def test(): print("start") stop_server() # started by send_whatsapp file # x = ImageFont.LAYOUT_RAQM # return # PDF report creation field_obj = { "CenterLat": 0.5045593579570529, "CenterLatLarge": 0.5045593579570529, "CenterLong": 101.55170509050396, "CenterLongLarge": 101.55170509050396, "Coordinates": { "P_1": {"Latitude": 0.5065166216827659, "Longitude": 101.55161925981548}, "P_10": {"Latitude": 0.5044996790071132, "Longitude": 101.55318566988018}, "P_11": {"Latitude": 0.504252925338939, "Longitude": 101.55374356935528}, "P_12": {"Latitude": 0.5040276285032929, "Longitude": 101.55378648469951}, "P_13": {"Latitude": 0.5037272327103198, "Longitude": 101.55380794237163}, "P_14": {"Latitude": 0.502376797482014, "Longitude": 101.55233634027365}, "P_15": {"Latitude": 0.5036212945062717, "Longitude": 101.55134928735617}, "P_16": {"Latitude": 0.5044151976562541, "Longitude": 101.54966486009482}, "P_2": {"Latitude": 0.5067419184320918, "Longitude": 101.55227371881512}, "P_3": {"Latitude": 0.5063556954284676, "Longitude": 101.55245610902813}, "P_4": {"Latitude": 0.5059587439841081, "Longitude": 101.55290672014263}, "P_5": {"Latitude": 0.5055081504232448, "Longitude": 101.55305692384746}, "P_6": {"Latitude": 0.5049931863154092, "Longitude": 101.55292817781475}, "P_7": {"Latitude": 0.5049395442184976, "Longitude": 101.5525419397166}, "P_8": {"Latitude": 0.5048322600233386, "Longitude": 101.55255266855266}, "P_9": {"Latitude": 0.5048000747644512, "Longitude": 101.55290672014263}, "a": {"Latitude": 0.5044567653263703, "Longitude": 101.54960223863628}, }, "Country": "Indonesia", "CropCode": "107", "Email": "eddie@inovasi-iot.tech", "Expiring": "no", "FarmerName": "hidayat", "FarmerPhoneNumber": "eddie@inovasi-iot.tech", "FieldAddress": "Jalan Tanpa Nama, Bencah Lesung, Kec. Tenayan Raya, Kota Pekanbaru, Riau 28285, Indonesia", "FieldArea": 110046, "FieldDescription": "demo", "FieldID": "1739255114968", "FieldLatLen": 0.02574820791313115, "FieldLatLenLarge": 0.051496414526395085, "FieldLongLen": 0.025749206543011383, "FieldLongLenLarge": 0.0514984130859375, "FieldMaxLat": 0.5067419184320918, "FieldMaxLong": 101.55380794237163, "FieldMinLat": 0.502376797482014, "FieldMinLong": 101.54960223863628, "GenTif": "yes", "Health": { "avi": {"20250206": "10", "20250211": "10", "20250216": "10"}, "bsi": {"20250206": "98", "20250211": "88", "20250216": "100"}, "evi": {"20250206": "23", "20250211": "61", "20250216": "40"}, "lai": {"20250206": "41", "20250211": "50", "20250216": "50"}, "ndmi": {"20250206": "11", "20250211": "19", "20250216": "56"}, "ndre": {"20250206": "22", "20250211": "29", "20250216": "10"}, "ndvi": {"20250206": "23", "20250211": "31", "20250216": "10"}, "ndwi": {"20250206": "27", "20250211": "46", "20250216": "58"}, "rsm": {"20250206": "81", "20250211": "81", "20250216": "83"}, "rvi": {"20250206": "89", "20250211": "89", "20250216": "89"}, "savi": {"20250206": "32", "20250211": "45", "20250216": "10"}, "si": {"20250206": "100", "20250211": "100", "20250216": "100"}, "soc": {"20250206": "0", "20250211": "0", "20250216": "0"}, "vari": {"20250206": "11", "20250211": "29", "20250216": "10"}, "vssi": {"20250206": "10", "20250211": "10", "20250216": "10"}, }, "IndexBreakdown": { "20250206": { "avi": ["110046", "0", "0", "0", "0", "0", "0", "0", "0", "0"], "bsi": ["0", "0", "0", "0", "0", "0", "0", "0", "18916", "91130"], "evi": ["0", "82496", "27550", "0", "0", "0", "0", "0", "0", "0"], "lai": ["0", "0", "0", "96688", "13358", "0", "0", "0", "0", "0"], "ndmi": ["100445", "9601", "0", "0", "0", "0", "0", "0", "0", "0"], "ndre": ["44", "86187", "23815", "0", "0", "0", "0", "0", "0", "0"], "ndvi": ["0", "82496", "27550", "0", "0", "0", "0", "0", "0", "0"], "ndwi": ["44", "37392", "66019", "6591", "0", "0", "0", "0", "0", "0"], "rsm": [ "0", "0", "4218", "0", "10853", "0", "21223", "25595", "10480", "37678", ], "rvi": [ "0", "0", "2351", "0", "3581", "0", "12676", "12940", "19861", "58637", ], "savi": ["0", "6415", "78805", "24562", "264", "0", "0", "0", "0", "0"], "si": ["0", "0", "0", "0", "0", "0", "0", "0", "0", "110046"], "vari": ["101258", "8590", "198", "0", "0", "0", "0", "0", "0", "0"], "vssi": ["110046", "0", "0", "0", "0", "0", "0", "0", "0", "0"], }, "20250211": { "avi": ["110046", "0", "0", "0", "0", "0", "0", "0", "0", "0"], "bsi": ["0", "0", "0", "0", "0", "0", "132", "27330", "76586", "5998"], "evi": ["0", "0", "0", "0", "1538", "90691", "17686", "132", "0", "0"], "lai": ["0", "0", "0", "4723", "100665", "0", "4658", "0", "0", "0"], "ndmi": ["15423", "85001", "9623", "0", "0", "0", "0", "0", "0", "0"], "ndre": ["0", "15027", "92251", "2768", "0", "0", "0", "0", "0", "0"], "ndvi": ["0", "1538", "90691", "17686", "132", "0", "0", "0", "0", "0"], "ndwi": [ "0", "0", "4899", "32339", "71797", "1011", "0", "0", "0", "0", ], "rsm": [ "0", "0", "4218", "0", "10853", "0", "21223", "25595", "10480", "37678", ], "rvi": [ "0", "0", "2351", "0", "3581", "0", "12676", "12940", "19861", "58637", ], "savi": [ "0", "0", "2329", "53276", "50376", "3933", "132", "0", "0", "0", ], "si": ["0", "0", "0", "0", "0", "0", "0", "0", "0", "110046"], "vari": [ "11095", "17092", "52200", "29571", "88", "0", "0", "0", "0", "0", ], "vssi": ["110046", "0", "0", "0", "0", "0", "0", "0", "0", "0"], }, "20250216": { "avi": ["110046", "0", "0", "0", "0", "0", "0", "0", "0", "0"], "bsi": ["0", "0", "0", "0", "0", "0", "0", "0", "0", "110046"], "evi": ["0", "0", "0", "110046", "0", "0", "0", "0", "0", "0"], "lai": ["0", "0", "0", "0", "110046", "0", "0", "0", "0", "0"], "ndmi": ["0", "0", "0", "0", "46312", "63734", "0", "0", "0", "0"], "ndre": ["110046", "0", "0", "0", "0", "0", "0", "0", "0", "0"], "ndvi": ["110046", "0", "0", "0", "0", "0", "0", "0", "0", "0"], "ndwi": ["0", "0", "0", "0", "22233", "87813", "0", "0", "0", "0"], "rsm": [ "0", "0", "4372", "0", "8656", "0", "20454", "22541", "11336", "42687", ], "rvi": [ "0", "0", "3120", "0", "6898", "0", "10260", "9623", "19180", "60966", ], "savi": ["110046", "0", "0", "0", "0", "0", "0", "0", "0", "0"], "si": ["0", "0", "0", "0", "0", "0", "0", "0", "0", "110046"], "vari": ["110046", "0", "0", "0", "0", "0", "0", "0", "0", "0"], "vssi": ["110046", "0", "0", "0", "0", "0", "0", "0", "0", "0"], }, }, "Language": "id", "LatestDay": "20250216", "MapDimensions": { "height": 484.9814586867499, "horiPadPer": 0.10000000000000002, "verPadPer": 0.10000000000000002, "width": 470.4002802673561, "zoom": 17, }, "OrderDate": "11-February-2025", "Paid": "yes", "PaymentType": 3, "Phone": "eddie@inovasi-iot.tech", "PlantDistance": "8", "SARDays": {"20250203": "yes", "20250214": "yes", "20250215": "yes"}, "SensedDays": { "20250206": { "cropRedZones": "empty", "irrigationRedZones": [3, 4, 5, 6], "isCloudy": True, }, "20250211": "yes", "20250216": "yes", }, "SowingDate": "1739232000", "UID": "tTiPWW2nikbpxKcvnEHDLPHGo1d2", "URI": "MediaAssets2/user_dp.png", "Weather": { "20250211": { "cloud_cover": 60, "humidity": 84, "max_temp": 305.8, "min_temp": 295.5, "pressure": 1012, "station": "Pekanbaru", "wind_deg": 54, "wind_speed": 2.9, }, "20250216": { "cloud_cover": 79, "humidity": 97, "max_temp": 302.1, "min_temp": 295.6, "pressure": 1010, "station": "Pekanbaru", "wind_deg": 4, "wind_speed": 2.2, }, "20250217": { "cloud_cover": 83, "humidity": 98, "max_temp": 302.1, "min_temp": 296, "pressure": 1011, "station": "Pekanbaru", "wind_deg": 279, "wind_speed": 2.5, }, "20250218": { "cloud_cover": 72, "humidity": 96, "max_temp": 301.5, "min_temp": 295.9, "pressure": 1012, "station": "Pekanbaru", "wind_deg": 309, "wind_speed": 1.6, }, "20250221": { "cloud_cover": 69, "humidity": 99, "max_temp": 305.4, "min_temp": 295.2, "pressure": 1012, "station": "Pekanbaru", "wind_deg": 221, "wind_speed": 0.9, }, }, "Whitelabel": "inovasi.web.app", } res = gen_report_new( "tTiPWW2nikbpxKcvnEHDLPHGo1d2", "demo", "0.5045593579570529", "101.55170509050396", "20250216", "1739255114968", 110046, "en", 0, whitelabel="farmonaut", rangeLatLng=[ 0.502376797482014, 101.54960223863628, 0.5067419184320918, 101.55380794237163, ], # minLat, minLong, maxLat, maxLong points={ "P_1": {"Latitude": 0.5065166216827659, "Longitude": 101.55161925981548}, "P_10": {"Latitude": 0.5044996790071132, "Longitude": 101.55318566988018}, "P_11": {"Latitude": 0.504252925338939, "Longitude": 101.55374356935528}, "P_12": {"Latitude": 0.5040276285032929, "Longitude": 101.55378648469951}, "P_13": {"Latitude": 0.5037272327103198, "Longitude": 101.55380794237163}, "P_14": {"Latitude": 0.502376797482014, "Longitude": 101.55233634027365}, "P_15": {"Latitude": 0.5036212945062717, "Longitude": 101.55134928735617}, "P_16": {"Latitude": 0.5044151976562541, "Longitude": 101.54966486009482}, "P_2": {"Latitude": 0.5067419184320918, "Longitude": 101.55227371881512}, "P_3": {"Latitude": 0.5063556954284676, "Longitude": 101.55245610902813}, "P_4": {"Latitude": 0.5059587439841081, "Longitude": 101.55290672014263}, "P_5": {"Latitude": 0.5055081504232448, "Longitude": 101.55305692384746}, "P_6": {"Latitude": 0.5049931863154092, "Longitude": 101.55292817781475}, "P_7": {"Latitude": 0.5049395442184976, "Longitude": 101.5525419397166}, "P_8": {"Latitude": 0.5048322600233386, "Longitude": 101.55255266855266}, "P_9": {"Latitude": 0.5048000747644512, "Longitude": 101.55290672014263}, "a": {"Latitude": 0.5044567653263703, "Longitude": 101.54960223863628}, }, ndviAvgVal=10, ndwiAvgVal=58, fieldOrPolyData=field_obj, ) print("res", res) return # Storage File Link creation & Short link creation # init_firebase() cred = service_account.Credentials.from_service_account_file(SERVICE_KEY_PATH) storage_client = storage.Client(PROJECT_NAME, cred) # print(storage_client) # img_url = f"localhost:8000/{uid}/{fieldid}/interactive_report.html" # file_path = ( # "PaidMonitoredFields/" # + str(uid) # + "/" # + "payagri black gram.png" # ) # media_url = getStorageFileUrlGeneral(file_path, storage_client) # png_to_jpg(media_url) # img_url = generate_jeevnai_whatsapp_image("test", "2k7JiD9RyISioSMvA4Vf5xuOkcs1", "1728976554116", "en", "20241213") # print("img_url", media_url) # return # getStorageFileUrl(uid, fieldid, "20241213", "ETCI.png", storage_client) dirMapShortUrl = "https://picsum.photos/200/300.jpg" # REDUNDANT media_url = createShortDynamicLink( "https://farmonaut.com/wp-content/uploads/2020/01/Farmonaut_Logo_Black.png", domainPrefix="https://precisiongrow.page.link", ) print("media_url", media_url) return # Sending Whatsapp userName = "PayAgri" orgName = "Farmonaut" temp_field_address = "Payagri primary processing Black gram" smsInstructions = "irrigation inspection is required in north, north east, center directions of your field and crop is in good condition" reportShortUrl = "https://farmonaut.com/app_redirect" expertNumber = "6366026267" expiryTxt = "51 days 10 reports" smsParams = [ userName, orgName, temp_field_address, smsInstructions, reportShortUrl, expertNumber, expiryTxt, orgName, ] lang = "en" phoneNo = "+919980971974" # phoneNo = "+919958983308" whitelabel = None send_whatsapp( dirMapShortUrl, smsParams, lang, uid, fieldid, phoneNo, whitelabel, imagedate, ) print("done") # test()