| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979 | #!/usr/bin/env python3.5import paho.mqtt.client as mqttimport timeimport sysimport http.client, urllibimport jsonimport threadingimport osimport shutilimport uuidimport hashlibimport serialimport arrayimport base64import urllib.requestimport datetimeimport requestsimport logging""" Device Information - the information about this deviceThese device information is used for the MQTT topic. This program will subscribe and publish tothe MQTT topic.MQTT topic to subscribe to: AISKY/<project_name>/<model_name>/<device_id>MQTT topic to publish to : AISKY/<project_name>/<model_name>/<device_id>/Log"""# @var project_name  The project name comes from the u-boot environment variable 'project'.# @var model_name    The model name comes from the u-boot environment variable 'model'.# @var device_id     The device id comes from the mac address of eth0.project_name = os.popen('cat /etc/aisky.conf | grep project').readline().split('=')[1].strip()model_name = os.popen('cat /etc/aisky.conf | grep model').readline().split('=')[1].strip()device_id = open('/sys/class/net/eth0/address').readline().strip()""" NOTE: Remember to setup the u-boot environment variables before executing this program. Thecommands to setup the u-boot environment variables are as follows.Setup the 'project' variable: The following command sets the 'project' variable to AppleFarm.    root@mylinkit:~# fw_setenv project AppleFarmSetup the 'model' variable: The following command sets the 'model' variable to MK-G.    root@mylinkit:~# fw_setenv model MK-GThen, the following command can be used to display the u-boot environment variables.    root@mylinkit:~# fw_printenv"""""" MQTT ServerIf you don't have your own MQTT server, you can use the public MQTT server 'iot.eclipse.org'. Butwith the public MQTT server, you can only publish and subscribe without a user name and password.Sometimes the public MQTT server is unstable."""# @var mqtt_server  The URL or IP address of the MQTT server to connect to.# @var mqtt_port    The port of the MQTT server to connect to.# @var mqtt_alive   Maximum period in seconds allowed between communications with the broker. If#                   no other messages are being exchanged, this controls the rate at which the #                   client will send ping messages to the broker.mqtt_server = "60.250.156.234"mqtt_port = 1883mqtt_alive = 60# camera API commandcamera_ircut_high = 'http://192.168.51.48/cgi-bin/camerasetting_cgi?action=set&channel=0&user=admin&pwd=abcd1234&TRCutLevel=high'camera_ircut_low = 'http://192.168.51.48/cgi-bin/camerasetting_cgi?action=set&channel=0&user=admin&pwd=abcd1234&TRCutLevel=low'camera_image = 'http://192.168.51.48/cgi-bin/images_cgi?channel=0&user=admin&pwd=abcd1234'camera_zoomin = "http://192.168.51.48/cgi-bin/ptz_cgi?action=ZoomAdd&user=admin&pwd=abcd1234"camera_zoomout = "http://192.168.51.48/cgi-bin/ptz_cgi?action=ZoomSub&user=admin&pwd=abcd1234"#php pathndvi_path = 'http://60.250.156.230/cust/c1.php'ppv_path = 'http://60.250.156.230/cust/p1.php'impv_path = 'http://60.250.156.230/cust/im1.php'log_path = 'http://54.248.68.32/MQTT/BBtoMQTT.php'log_angle_path = 'http://54.248.68.32/MQTT/BBtoMQTT_angle.php'reboot_path ="http://54.248.68.32/MQTT/BBtoCaveat.php"#log# @var mqtt_sub_topic  The MQTT topic to subscribe to.# @var mqtt_pub_topic  The MQTT topic to publish to.mqtt_sub_topic = "AISKY/" + project_name + "/" + model_name + "/" + device_idmqtt_pub_topic = mqtt_sub_topic + "/Log"##nrnr = "GTW009001001"##angleTilt_old = 0Pan_old = 0Zoom_old = 0## Calculate the SHA256 checksum of the file.# @param file  [in] The file path for which you want to calculate the checksum.def get_sha256sum(file):    with open(file, "rb") as f:        bytes = f.read()        return hashlib.sha256(bytes).hexdigest()## Send logs to the server.# @param command   [in] The command received from the server.# @param response  [in] The response message to the command.def server_log(command, rqnn):    localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())    # message to be sent in JSON format    payload = {        # let server know which device the message came from        'device_id': device_id,        # let server know when the message was sent from the device        'localtime': localtime,        'node_id': nr,        'command': command,        'rqnn': rqnn    }    jsonobj = json.dumps(payload, sort_keys=True, indent=4)    mqtt_client.publish(mqtt_pub_topic, jsonobj, qos=2)    print('Sent:')    print(jsonobj)def NDVI_log(command, bytes):    localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())    # message to be sent in JSON format    payload = {        # let server know which device the message came from        'device_id': device_id,        # let server know when the message was sent from the device        'localtime': localtime,        'node_id': nr,        'command': command,        'bytes': bytes    }    jsonobj = json.dumps(payload, sort_keys=True, indent=4)    mqtt_client.publish(mqtt_pub_topic, jsonobj, qos=2)    print('Sent:')    print(jsonobj)def get_ip_address(ifname):    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)    try:        return socket.inet_ntoa(fcntl.ioctl(            s.fileno(),            0x8915,  # SIOCGIFADDR            struct.pack('256s', ifname[:15])        )[20:24])    except:        return ""## Reset this device.def system_reboot():    server_log('reboot', 'Restarting system')    time.sleep(5)    os.system('sudo reboot')    time.sleep(10)def system_update_code():    server_log('update', 'Update system')    os.system('git clone http://60.250.156.230:3000/fatwolf/Edamame.git')    time.sleep(5)    os.system('sudo su')    os.system('cp /home/pi/Edamame/server.py /usr/sbin/')    time.sleep(10)    os.system('rm -rf Edamame')    server_log('reboot', 'Restarting system')    time.sleep(5)    os.system('sudo reboot')    time.sleep(10)## Reset this device to factory default.def factory_reset():    server_log('factory_reset', 'Resetting to factory default')    time.sleep(5)    os.system('firstboot -y')    os.system('reboot -f')    time.sleep(10)## Receive the system action  to this device .def call(msg):    server_log('a001', '1')    data = {'nr': nr, 'command': 'A', 'position': msg['position'], 'time': msg['time'], 'date': msg['date'],'response': 'YES', 'tilt_angle': 0, 'pan_angle': 0,            'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("call  ok")def ndvi_1(msg):    server_log('ndvi_1', 'start')    print(msg['orPan'])    print(msg['orTilt'])    print(msg['orZoom'])    print(msg['position'])    orpan(msg)    ortilt(msg)    orzoom(msg)    time.sleep(2)    data = {'nr': nr, 'command':'B' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 1 angle  ok")    data = {'nr': nr,'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_angle_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 1 angle  log")def ndvi_2(msg):    server_log('ndvi_2', 'start')    print(msg['orPan'])    print(msg['orTilt'])    print(msg['orZoom'])    print(msg['position'])    orpan(msg)    ortilt(msg)    orzoom(msg)    time.sleep(2)    data = {'nr': nr,'command':'B' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES','tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 2 angle  ok")    data = {'nr': nr,'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_angle_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 2 angle  log")def ndvi_3(msg):    server_log('ndvi_3', 'start')    print(msg['orPan'])    print(msg['orTilt'])    print(msg['orZoom'])    print(msg['position'])    orpan(msg)    ortilt(msg)    orzoom(msg)    time.sleep(2)    data = {'nr': nr, 'command':'B' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 3 angle  ok")    data = {'nr': nr,'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_angle_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 3 angle  log")def ndvi_4(msg):    server_log('ndvi_4', 'start')    print(msg['orPan'])    print(msg['orTilt'])    print(msg['orZoom'])    print(msg['position'])    orpan(msg)    ortilt(msg)    orzoom(msg)    time.sleep(2)    data = {'nr': nr, 'command':'B' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 4 angle  ok")    data = {'nr': nr,'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_angle_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 4 angle  log")def ndvi_5(msg):    server_log('ndvi_5', 'start')    print(msg['orPan'])    print(msg['orTilt'])    print(msg['orZoom'])    print(msg['position'])    orpan(msg)    ortilt(msg)    orzoom(msg)    time.sleep(2)    data = {'nr': nr, 'command':'B' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 5 angle  ok")    data = {'nr': nr, 'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']), 'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_angle_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 5 angle  log")def ndvi_6(msg):    server_log('ndvi_6', 'start')    print(msg['orPan'])    print(msg['orTilt'])    print(msg['orZoom'])    print(msg['position'])    orpan(msg)    ortilt(msg)    orzoom(msg)    time.sleep(2)    data = {'nr': nr, 'command':'B' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 6 angle  ok")    data = {'nr': nr,'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_angle_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 6 angle  log")def ndvi_7(msg):    server_log('ndvi_7', 'start')    print(msg['orPan'])    print(msg['orTilt'])    print(msg['orZoom'])    print(msg['position'])    orpan(msg)    ortilt(msg)    orzoom(msg)    time.sleep(2)    data = {'nr': nr, 'command':'B' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES','tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 7 angle  ok")    data = {'nr': nr,'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_angle_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 7 angle  log")def ndvi_8(msg):    server_log('ndvi_8', 'start')    print(msg['orPan'])    print(msg['orTilt'])    print(msg['orZoom'])    print(msg['position'])    orpan(msg)    ortilt(msg)    orzoom(msg)    time.sleep(2)    data = {'nr': nr, 'command':'B' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 8 angle  ok")    data = {'nr': nr, 'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']), 'zoom': int(msg['orZoom'])}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_angle_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 8 angle  log")def manual_ndvi(msg):    server_log('manual_ndvi',  'start')    ndvi(msg)    # -----------------------log---------------------------------------------------    data = {'nr': nr,'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("MANUAL NDVI  ok")def p1_preview_photo(msg):    server_log('p1_preview_photo', 'start')    req = requests.get(camera_ircut_low)    fileName = datetime.datetime.now().strftime("/home/pi/p1/1.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    dress = '/home/pi/p1/'    for root, dirs, files in os.walk(dress):        print('files: {}'.format(len(files)))        if len(files) >= 1:            files.sort()            for f in files:                with open(os.path.join(root, f), "rb") as imageFile:                    str = base64.b64encode(imageFile.read())                url = ppv_path                values = {'p22_1': str, 'p22_name_1': f}                p22_1 = urllib.parse.urlencode(values)                p22_1 = p22_1.encode('utf-8')                req = urllib.request.Request(url, p22_1)                req.add_header('User-Agent', 'Magic Browser')                resp = urllib.request.urlopen(req)                respData = resp.read()                print(os.path.join(root, f))                #print(respdata)                os.remove(os.path.join(root, f))                print("ok")    data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("p1_preview_photo ok")def p2_preview_photo(msg):    server_log('p2_preview_photo', 'start')    req = requests.get(camera_ircut_low)    fileName = datetime.datetime.now().strftime("/home/pi/p2/2.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    dress = '/home/pi/p2/'    for root, dirs, files in os.walk(dress):        print('files: {}'.format(len(files)))        if len(files) >= 1:            files.sort()            for f in files:                with open(os.path.join(root, f), "rb") as imageFile:                    str = base64.b64encode(imageFile.read())                url = ppv_path                values = {'p22_2': str, 'p22_name_2': f}                p22_2 = urllib.parse.urlencode(values)                p22_2 = p22_2.encode('utf-8')                req = urllib.request.Request(url, p22_2)                req.add_header('User-Agent', 'Magic Browser')                resp = urllib.request.urlopen(req)                respData = resp.read()                print(os.path.join(root, f))                #print(respdata)                os.remove(os.path.join(root, f))                print("ok")    data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("p2_preview_photo ok")def p3_preview_photo(msg):    server_log('p3_preview_photo', 'start')    req = requests.get(camera_ircut_low)    fileName = datetime.datetime.now().strftime("/home/pi/p3/3.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    dress = '/home/pi/p3/'    for root, dirs, files in os.walk(dress):        print('files: {}'.format(len(files)))        if len(files) >= 1:            files.sort()            for f in files:                with open(os.path.join(root, f), "rb") as imageFile:                    str = base64.b64encode(imageFile.read())                url = ppv_path                values = {'p22_3': str, 'p22_name_3': f}                p22_3 = urllib.parse.urlencode(values)                p22_3 = p22_3.encode('utf-8')                req = urllib.request.Request(url, p22_3)                req.add_header('User-Agent', 'Magic Browser')                resp = urllib.request.urlopen(req)                respData = resp.read()                print(os.path.join(root, f))                #print(respdata)                os.remove(os.path.join(root, f))                print("ok")    data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("p3_preview_photo ok")def p4_preview_photo(msg):    server_log('p4_preview_photo', 'start')    req = requests.get(camera_ircut_low)    fileName = datetime.datetime.now().strftime("/home/pi/p4/4.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    dress = '/home/pi/p4/'    for root, dirs, files in os.walk(dress):        print('files: {}'.format(len(files)))        if len(files) >= 1:            files.sort()            for f in files:                with open(os.path.join(root, f), "rb") as imageFile:                    str = base64.b64encode(imageFile.read())                url = ppv_path                values = {'p22_4': str, 'p22_name_4': f}                p22_4 = urllib.parse.urlencode(values)                p22_4 = p22_4.encode('utf-8')                req = urllib.request.Request(url, p22_4)                req.add_header('User-Agent', 'Magic Browser')                resp = urllib.request.urlopen(req)                respData = resp.read()                print(os.path.join(root, f))                #print(respdata)                os.remove(os.path.join(root, f))                print("ok")    data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("p4_preview_photo ok")def p5_preview_photo(msg):    server_log('p5_preview_photo', 'start')    req = requests.get(camera_ircut_low)    fileName = datetime.datetime.now().strftime("/home/pi/p5/5.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    dress = '/home/pi/p5/'    for root, dirs, files in os.walk(dress):        print('files: {}'.format(len(files)))        if len(files) >= 1:            files.sort()            for f in files:                with open(os.path.join(root, f), "rb") as imageFile:                    str = base64.b64encode(imageFile.read())                url = ppv_path                values = {'p22_5': str, 'p22_name_5': f}                p22_5 = urllib.parse.urlencode(values)                p22_5 = p22_5.encode('utf-8')                req = urllib.request.Request(url, p22_5)                req.add_header('User-Agent', 'Magic Browser')                resp = urllib.request.urlopen(req)                respData = resp.read()                print(os.path.join(root, f))                #print(respdata)                os.remove(os.path.join(root, f))                print("ok")    data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("p5_preview_photo ok")def p6_preview_photo(msg):    server_log('p6_preview_photo', 'start')    req = requests.get(camera_ircut_low)    fileName = datetime.datetime.now().strftime("/home/pi/p6/6.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    dress = '/home/pi/p6/'    for root, dirs, files in os.walk(dress):        print('files: {}'.format(len(files)))        if len(files) >= 1:            files.sort()            for f in files:                with open(os.path.join(root, f), "rb") as imageFile:                    str = base64.b64encode(imageFile.read())                url = ppv_path                values = {'p22_6': str, 'p22_name_6': f}                p22_6 = urllib.parse.urlencode(values)                p22_6 = p22_6.encode('utf-8')                req = urllib.request.Request(url, p22_6)                req.add_header('User-Agent', 'Magic Browser')                resp = urllib.request.urlopen(req)                respData = resp.read()                print(os.path.join(root, f))                #print(respdata)                os.remove(os.path.join(root, f))                print("ok")    data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("p6_preview_photo ok")def p7_preview_photo(msg):    server_log('p7_preview_photo', 'start')    req = requests.get(camera_ircut_low)    fileName = datetime.datetime.now().strftime("/home/pi/p7/7.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    dress = '/home/pi/p7/'    for root, dirs, files in os.walk(dress):        print('files: {}'.format(len(files)))        if len(files) >= 1:            files.sort()            for f in files:                with open(os.path.join(root, f), "rb") as imageFile:                    str = base64.b64encode(imageFile.read())                url = ppv_path                values = {'p22_7': str, 'p22_name_7': f}                p22_7 = urllib.parse.urlencode(values)                p22_7 = p22_7.encode('utf-8')                req = urllib.request.Request(url, p22_7)                req.add_header('User-Agent', 'Magic Browser')                resp = urllib.request.urlopen(req)                respData = resp.read()                print(os.path.join(root, f))               # print(respdata)                os.remove(os.path.join(root, f))                print("ok")    data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("p7_preview_photo ok")def p8_preview_photo(msg):    server_log('p8_preview_photo', 'start')    req = requests.get(camera_ircut_low)    fileName = datetime.datetime.now().strftime("/home/pi/p8/8.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    dress = '/home/pi/p8/'    for root, dirs, files in os.walk(dress):        print('files: {}'.format(len(files)))        if len(files) >= 1:            files.sort()            for f in files:                with open(os.path.join(root, f), "rb") as imageFile:                    str = base64.b64encode(imageFile.read())                url = ppv_path                values = {'p22_8': str, 'p22_name_8': f}                p22_8 = urllib.parse.urlencode(values)                p22_8 = p22_8.encode('utf-8')                req = urllib.request.Request(url, p22_8)                req.add_header('User-Agent', 'Magic Browser')                resp = urllib.request.urlopen(req)                respData = resp.read()                print(os.path.join(root, f))                #print(respdata)                os.remove(os.path.join(root, f))                print("ok")    data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("p8_preview_photo ok")def tilt(msg):    global Tilt_old    Tilt_old = int(msg['Tilt']) + Tilt_old    if (Tilt_old > 90):        Tilt_old = 90        Tilt(90)    elif (Tilt_old < -25):        Tilt_old = -25        Tilt(-25)    else:        Tilt(Tilt_old)    data = {'nr': nr, 'command': 'T', 'position': "R", 'time':"0", 'date': msg['date'],            'response': 'YES','tilt_angle': Tilt_old,'pan_angle':Pan_old,'zoom':  Zoom_old}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("Tilt ok")    data = {'nr': nr, 'tilt_angle':Tilt_old, 'pan_angle': Pan_old, 'zoom': Zoom_old}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_angle_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("T angle  log")def pan(msg):    global Pan_old    Pan_old = int(msg['Pan']) + Pan_old    if (Pan_old > 360):        Pan_old = Pan_old - 360        Pan(Pan_old)    elif (Pan_old < 0):        Pan_old = 360 + Pan_old        Pan(Pan_old)    else:        Pan(Pan_old)    data = {'nr': nr, 'command': 'P', 'position': "R", 'time': "0", 'date': msg['date'],'response': 'YES','tilt_angle':Tilt_old, 'pan_angle': Pan_old,'zoom': Zoom_old}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("Pan ok")    data = {'nr': nr, 'tilt_angle':Tilt_old, 'pan_angle': Pan_old, 'zoom': Zoom_old}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_angle_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("P angle  log")def zoom(msg):    global Zoom_old    Zoom_old =int(msg['Zoom'])+ Zoom_old    print(Zoom_old)    if (Zoom_old > 5):        Zoom_old = 5        Zoom(0)    elif (Zoom_old < 0):        Zoom_old = 0        Zoom(0)    else:        Zoom(int(msg['Zoom']))    data = {'nr': nr, 'command': 'Z', 'position': "R", 'time':"0", 'date': msg['date'],            'response': 'YES','tilt_angle':Tilt_old, 'pan_angle':Pan_old,'zoom': Zoom_old}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("Zoom ok")    data = {'nr': nr, 'tilt_angle':Tilt_old, 'pan_angle': Pan_old, 'zoom': Zoom_old}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_angle_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("Z angle  log")def zoom0(msg):    global Zoom_old    Zoom(-(Zoom_old))    Zoom_old = 0    data = {'nr': nr, 'command': 'Z', 'position': "R", 'time':"0", 'date': msg['date'],            'response': 'YES','tilt_angle':Tilt_old, 'pan_angle':Pan_old,'zoom': -(Zoom_old)}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("Zoom ok")    data = {'nr': nr, 'tilt_angle':Tilt_old, 'pan_angle': Pan_old, 'zoom': -(Zoom_old)}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_angle_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("Z angle  log")def orpan(msg):    global Pan_old    Pan_old = int(msg['orPan'])    if (Pan_old>360):        Pan_old = Pan_old - 360        Pan(Pan_old)    elif(Pan_old<0):        Pan_old = 360 + Pan_old        Pan(Pan_old)    else:        Pan(Pan_old)def ortilt(msg):    global Tilt_old    Tilt_old = int(msg['orTilt'])    if (Tilt_old > 90):        Tilt_old = 90        Tilt(90)    elif (Tilt_old < -25):        Tilt_old = -25        Tilt(-25)    else:        Tilt(Tilt_old)def orzoom(msg):    global Zoom_old    Zoom_old = int(msg['orZoom']) - Zoom_old    print(Zoom_old)    Zoom(Zoom_old)    Zoom_old = int(msg['orZoom'])##  device action.def take_preview_photo(msg):    im = datetime.datetime.now().strftime('/home/pi/immediate/PV.jpg')    save_photo = datetime.datetime.now().strftime(im)    fileName = datetime.datetime.now().strftime(save_photo)    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    dress = '/home/pi/immediate/'    for root, dirs, files in os.walk(dress):        print('files: {}'.format(len(files)))        if len(files) >= 1:            files.sort()            for f in files:                with open(os.path.join(root, f), "rb") as imageFile:                    str = base64.b64encode(imageFile.read())                url = impv_path                values = {'data': str, 'name': f}                data = urllib.parse.urlencode(values)                data = data.encode('utf-8')                req = urllib.request.Request(url, data)                req.add_header('User-Agent', 'Magic Browser')                resp = urllib.request.urlopen(req)                respData = resp.read()                #print(respData)                #os.remove(os.path.join(root, f))                print("ok")    data = {'nr': nr, 'command': 'S', 'position': msg['position'], 'time': msg['time'], 'date': msg['date'],            'response': 'YES', 'tilt_angle': 0, 'pan_angle': 0, 'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("take_preview_photo ok")def ndvi(msg):    req = requests.get(camera_ircut_high)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/ir_image1/"+msg['date']+"a.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    req = requests.get(camera_ircut_low)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/n_image1/"+msg['date']+"b.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    def main_n():        dress = '/home/pi/ir_image1/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}                    data = urllib.parse.urlencode(values)                    data = data.encode('utf-8')                    req = urllib.request.Request(url, data)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                   # print(respData)                    os.remove(os.path.join(root, f))                    print("ok")    def main_ir():        dress = '/home/pi/n_image1/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'c_na21': str, 'name_na21': f, 'nr21': nr}                    c = urllib.parse.urlencode(values)                    c = c.encode('utf-8')                    req = urllib.request.Request(url, c)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")        # ---------------------------------------------------------------------------------------    main_n()    main_ir()def Pan(pan):    a = pan    if a == 360:        w = a - 1        a1 = w * 100        print(a1)        k = '{0:x}'.format(a1)        h = k.zfill(4)        print(h)        p3 = (h[0:2])        p4 = (h[2:])        str_10 = int(p3, 16)        str_10_2 = int(p4, 16)        sum_10 = (str_10 + str_10_2 + 76)        str_16 = hex(sum_10)        sum = (str_16[-2:])        str_10_sum = int(sum, 16)        SERIAL_PORT = '/dev/ttyS0'        ser = serial.Serial(SERIAL_PORT, baudrate=9600, timeout=5)        Pan = [0xFF, 0x01, 0x00, 0x4B, str_10, str_10_2, str_10_sum]        ser.write(array.array('B', Pan).tostring())        print('left360')    elif 0 < a <= 359:        a1 = a * 100        print(a1)        k = '{0:x}'.format(a1)        h = k.zfill(4)        print(h)        p3 = (h[0:2])        p4 = (h[2:])        str_10 = int(p3, 16)        str_10_2 = int(p4, 16)        sum_10 = (str_10 + str_10_2 + 76)        str_16 = hex(sum_10)        sum = (str_16[-2:])        str_10_sum = int(sum, 16)        SERIAL_PORT = '/dev/ttyS0'        ser = serial.Serial(SERIAL_PORT, baudrate=9600, timeout=5)        Pan = [0xFF, 0x01, 0x00, 0x4B, str_10, str_10_2, str_10_sum]        ser.write(array.array('B', Pan).tostring())        print('left')    elif a == 0:        SERIAL_PORT = '/dev/ttyS0'        ser = serial.Serial(SERIAL_PORT, baudrate=9600, timeout=5)        Pan = [0xFF, 0x01, 0x00, 0x4B, 0x00, 0x00, 0x4C]        ser.write(array.array('B', Pan).tostring())        print('leftzore')def Tilt(tilt):    a = tilt    def up():        q = int(360)        a1 = (q - a) * 100        print(a1)        k = '{0:x}'.format(a1)        h = k.zfill(4)        print(h)        p3 = (h[0:2])        ##print(p3)        p4 = (h[2:])        ##print(p4)        str_10 = int(p3, 16)        str_10_2 = int(p4, 16)        sum_10 = (str_10 + str_10_2 + 78)        str_16 = hex(sum_10)        ##print(str_16)        sum = (str_16[-2:])        str_10_sum = int(sum, 16)        SERIAL_PORT = '/dev/ttyS0'        ser = serial.Serial(SERIAL_PORT, baudrate=9600, timeout=5)        Pan = [0xFF, 0x01, 0x00, 0x4D, str_10, str_10_2, str_10_sum]        ser.write(array.array('B', Pan).tostring())        print('up')    def down():        k = abs(a)        a1 = k * 100        print (a1)        k = '{0:x}'.format(a1)        h = k.zfill(4)        print(h)        p3 = (h[0:2])        ##print(p3)        p4 = (h[2:])        ##print(p4)        str_10 = int(p3, 16)        str_10_2 = int(p4, 16)        sum_10 = (str_10 + str_10_2 + 78)        str_16 = hex(sum_10)        ##print(str_16)        sum = (str_16[-2:])        str_10_sum = int(sum, 16)        SERIAL_PORT = '/dev/ttyS0'        ser = serial.Serial(SERIAL_PORT, baudrate=9600, timeout=5)        Pan = [0xFF, 0x01, 0x00, 0x4D, str_10, str_10_2, str_10_sum]        ser.write(array.array('B', Pan).tostring())        print('down')    if -1 < a <= 90:        up()    elif 0 > a >= -25:        down()def Zoom(zoom):    def Zoom1():        headers = {'Content-Type': 'text/xml'}        data = '<?xml version="1.0" encoding="utf-8"?><request><ptzcmd><protocol>0</protocol><cmd>0</cmd><addr>1</addr></</request>'        url = camera_zoomin        r = requests.post(url, headers=headers, data=data)        print (r.content)    def Zoom_1():        headers = {'Content-Type': 'text/xml'}        data = '<?xml version="1.0" encoding="utf-8"?><request><ptzcmd><protocol>0</protocol><cmd>0</cmd><addr>1</addr></</request>'        url = camera_zoomout        r = requests.post(url, headers=headers, data=data)        print (r.content)        print ('you')    a = zoom    print(a)    if (a == 0):        print("NO")    elif (a>0):        for i in range(a):            time.sleep(1)            for j in range(10):                   Zoom1()    else:        a=-1*a        for i in range(a):            time.sleep(1)            for j in range(10):                   Zoom_1()def ndvi1(msg):    server_log('NDVI1', 'start')    req = requests.get(camera_ircut_high)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/a1/2020-07-20_15.05a1.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    path1 = '/home/pi/a1/2020-07-20_15.05a1.tif'    size1 = os.path.getsize(path1)    NDVI_log('NDVI1', size1)    req = requests.get(camera_ircut_low)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/b1/2020-07-20_15.05b1.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    path2 = '/home/pi/b1/2020-07-20_15.05b1.tif'    size2 = os.path.getsize(path2)    NDVI_log('NDVI1', size2)    print("ok")    def main_n():        dress = '/home/pi/a1/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '1'                    values = {'data_ira1': str, 'name_ira1': f, 'nr1': nr}                    data = urllib.parse.urlencode(values)                    data = data.encode('utf-8')                    req = urllib.request.Request(url, data)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")    def main_ir():        dress = '/home/pi/b1/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '1'                    values = {'c_na1': str, 'name_na1': f, 'nr1': nr}                    c = urllib.parse.urlencode(values)                    c = c.encode('utf-8')                    req = urllib.request.Request(url, c)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")        # --------------------------------------------------------------------------------------    main_n()    main_ir()    print("NDVI 1 ok")def ndvi2(msg):    server_log('NDVI2', 'start')    print(msg['date'])    req = requests.get(camera_ircut_high)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/a2/"+msg['date']+"a2.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    req = requests.get(camera_ircut_low)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/b2/"+msg['date']+"b2.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    def main_n():        dress = '/home/pi/a2/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}                    data = urllib.parse.urlencode(values)                    data = data.encode('utf-8')                    req = urllib.request.Request(url, data)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")    def main_ir():        dress = '/home/pi/b2/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'c_na21': str, 'name_na21': f, 'nr21': nr}                    c = urllib.parse.urlencode(values)                    c = c.encode('utf-8')                    req = urllib.request.Request(url, c)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")        # ---------------------------------------------------------------------------------------    main_n()    main_ir()    data = {'nr': nr, 'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle':0, 'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 2 ok")def ndvi3(msg):    server_log('NDVI3', 'start')    print(msg['date'])    req = requests.get(camera_ircut_high)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/a3/"+msg['date']+"a3.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    req = requests.get(camera_ircut_low)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/b3/"+msg['date']+"b3.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    def main_n():        dress = '/home/pi/a3/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}                    data = urllib.parse.urlencode(values)                    data = data.encode('utf-8')                    req = urllib.request.Request(url, data)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                   # print(respData)                    os.remove(os.path.join(root, f))                    print("ok")    def main_ir():        dress = '/home/pi/b3/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'c_na21': str, 'name_na21': f, 'nr21': nr}                    c = urllib.parse.urlencode(values)                    c = c.encode('utf-8')                    req = urllib.request.Request(url, c)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")        # ---------------------------------------------------------------------------------------    main_n()    main_ir()    data = {'nr': nr, 'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle':0, 'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 3 ok")def ndvi4(msg):    server_log('NDVI4', 'start')    print(msg['date'])    req = requests.get(camera_ircut_high)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/a4/"+msg['date']+"a4.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    req = requests.get(camera_ircut_low)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/b4/"+msg['date']+"b4.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    def main_n():        dress = '/home/pi/a4/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}                    data = urllib.parse.urlencode(values)                    data = data.encode('utf-8')                    req = urllib.request.Request(url, data)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")    def main_ir():        dress = '/home/pi/b4/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'c_na21': str, 'name_na21': f, 'nr21': nr}                    c = urllib.parse.urlencode(values)                    c = c.encode('utf-8')                    req = urllib.request.Request(url, c)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")        # ---------------------------------------------------------------------------------------    main_n()    main_ir()    data = {'nr': nr, 'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle':0, 'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 4 ok")def ndvi5(msg):    server_log('NDVI5', 'start')    print(msg['date'])    req = requests.get(camera_ircut_high)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/a5/"+msg['date']+"a5.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    req = requests.get(camera_ircut_low)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/b5/"+msg['date']+"b5.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    def main_n():        dress = '/home/pi/a5/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}                    data = urllib.parse.urlencode(values)                    data = data.encode('utf-8')                    req = urllib.request.Request(url, data)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                   # print(respData)                    os.remove(os.path.join(root, f))                    print("ok")    def main_ir():        dress = '/home/pi/b5/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'c_na21': str, 'name_na21': f, 'nr21': nr}                    c = urllib.parse.urlencode(values)                    c = c.encode('utf-8')                    req = urllib.request.Request(url, c)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")        # ---------------------------------------------------------------------------------------    main_n()    main_ir()    data = {'nr': nr, 'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle':0, 'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 5 ok")def ndvi6(msg):    server_log('NDVI6', 'start')    print(msg['date'])    req = requests.get(camera_ircut_high)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/a6/"+msg['date']+"a6.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    req = requests.get(camera_ircut_low)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/b6/"+msg['date']+"b6.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    def main_n():        dress = '/home/pi/a6/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}                    data = urllib.parse.urlencode(values)                    data = data.encode('utf-8')                    req = urllib.request.Request(url, data)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")    def main_ir():        dress = '/home/pi/b6/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'c_na21': str, 'name_na21': f, 'nr21': nr}                    c = urllib.parse.urlencode(values)                    c = c.encode('utf-8')                    req = urllib.request.Request(url, c)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")        # ---------------------------------------------------------------------------------------    main_n()    main_ir()    data = {'nr': nr, 'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle':0, 'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 6 ok")def ndvi7(msg):    server_log('NDVI7', 'start')    print(msg['date'])    req = requests.get(camera_ircut_high)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/a7/"+msg['date']+"a7.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    req = requests.get(camera_ircut_low)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/b7/"+msg['date']+"b7.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    def main_n():        dress = '/home/pi/a7/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}                    data = urllib.parse.urlencode(values)                    data = data.encode('utf-8')                    req = urllib.request.Request(url, data)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")    def main_ir():        dress = '/home/pi/b7/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'c_na21': str, 'name_na21': f, 'nr21': nr}                    c = urllib.parse.urlencode(values)                    c = c.encode('utf-8')                    req = urllib.request.Request(url, c)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")        # ---------------------------------------------------------------------------------------    main_n()    main_ir()    data = {'nr': nr, 'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle':0, 'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 7 ok")def ndvi8(msg):    #server_log('NDVI8', 'start')    print(msg['date'])    req = requests.get(camera_ircut_high)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/a8/"+msg['date']+"a8.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    req = requests.get(camera_ircut_low)    time.sleep(2)    fileName = datetime.datetime.now().strftime("/home/pi/b8/"+msg['date']+"b8.tif")    req = requests.get(camera_image)    file = open(fileName, 'wb')    for chunk in req.iter_content(100000):        file.write(chunk)    file.close()    def main_n():        dress = '/home/pi/a8/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}                    data = urllib.parse.urlencode(values)                    data = data.encode('utf-8')                    req = urllib.request.Request(url, data)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")    def main_ir():        dress = '/home/pi/b8/'        for root, dirs, files in os.walk(dress):            print('files: {}'.format(len(files)))            if len(files) >= 1:                files.sort()                for f in files:                    with open(os.path.join(root, f), "rb") as imageFile:                        str = base64.b64encode(imageFile.read())                    url = ndvi_path                    nr = '21'                    values = {'c_na21': str, 'name_na21': f, 'nr21': nr}                    c = urllib.parse.urlencode(values)                    c =  c.encode('utf-8')                    req = urllib.request.Request(url, c)                    req.add_header('User-Agent', 'Magic Browser')                    resp = urllib.request.urlopen(req)                    respData = resp.read()                    print(os.path.join(root, f))                    #print(respData)                    os.remove(os.path.join(root, f))                    print("ok")        # ---------------------------------------------------------------------------------------    main_n()    main_ir()    data = {'nr': nr, 'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle':0, 'pan_angle': 0,'zoom': 0}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(log_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("NDVI 8 ok")## Report the system information about this device to the server.def system_info():    # get firmware version from the file    fw_version = open('/etc/fw_version').readline().strip()    localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())    vpn_ip = get_ip_address('tun0')    # message to be sent in JSON format    payload = {        'device_id': device_id,        'localtime': localtime,        'command': 'system_info',        'project': project_name,        'model': model_name,        'fw_version': fw_version,        'mqtt_sub_topic': mqtt_sub_topic,        'mqtt_pub_topic': mqtt_pub_topic    }    if vpn_ip:        payload['vpn_connection'] = vpn_ip    else:        payload['vpn_connection'] = 'disconnected'    jsonobj = json.dumps(payload, sort_keys=True, indent=4)    mqtt_client.publish(mqtt_pub_topic, jsonobj, qos=2)    print('Sent:')    print(jsonobj)## Update system firmware.# @param msg  [in] The message from the server in JSON format.def system_update(msg):    if (msg['filetype'] == 'img'):        # create a directory to store the firmware image        dirname = '/tmp/' + str(uuid.uuid1()) + '/'        filepath = dirname + 'update.img'        os.makedirs(dirname)        # start to download the firmware image        server_log('system_update', 'Downloading system firmware')        urllib.urlretrieve(msg['url'], filepath)        # check the SHA256 checksum of the firmware image        if (msg['sha256sum'] == get_sha256sum(filepath)):            server_log('system_update', 'Updating system firmware')            # start to upgrade the system firmware            os.system('sysupgrade -n ' + filepath)        else:            server_log('system_update', 'ERROR: SHA256 checksum is wrong')            shutil.rmtree(dirname, ignore_errors=True)## The callback function for connecting.# @param client    [in] The client instance for this callback.# @param userdata  [in] The private user data as set in Client() or user_data_set().# @param flags     [in] Response flags sent by the broker.# @param rc        [in] The connection result.def on_connect(client, userdata, flags, rc):    # subscribe MQTT topic on connection    client.subscribe(mqtt_sub_topic, qos=2)    server_log('reboot | factory_reset', 'Boot completed')    logging.info('system running')    data = {'nr': nr, 'caveat': 'reboot'}    data = urllib.parse.urlencode(data)    data = data.encode('utf-8')    req = urllib.request.Request(reboot_path, data)    req.add_header('User-Agent', 'Magic Browser')    resp = urllib.request.urlopen(req)    respData = resp.read()    print("reboot check  ok")## The callback function for processing messages from the server.# @param client    [in] The client instance for this callback.# @param userdata  [in] The private user data as set in Client() or user_data_set().# @param msg       [in] An instance of MQTT message.def on_message(client, userdata, msg):    # print(msg.payload)    msg.payload = msg.payload.decode('utf-8')    jsonmsg = json.loads(msg.payload)    #print(jsonmsg)    print('Received:')    print(json.dumps(jsonmsg, sort_keys=True, indent=4, separators=(',', ':')))    # processing the command from the server    if (jsonmsg['command'] == 'a035'):        system_reboot()    elif (jsonmsg['command'] == 'a039'):        factory_reset()    elif (jsonmsg['command'] == 'system_update'):        system_update(jsonmsg)    elif (jsonmsg['command'] == 'a033'):        system_update_code()    elif (jsonmsg['command'] == 'vpn_connect'):        vpn_connect()    elif (jsonmsg['command'] == 'vpn_disconnect'):        vpn_disconnect()    elif (jsonmsg['node_id'] == "GTW009001001"):        if (jsonmsg['command'] == 'a000'):            call(jsonmsg)        elif (jsonmsg['command'] == 'a050'):            if (jsonmsg['position'] == '1'):                ndvi_1(jsonmsg)            elif (jsonmsg['position'] == '2'):                ndvi_2(jsonmsg)            elif (jsonmsg['position'] == '3'):                ndvi_3(jsonmsg)            elif (jsonmsg['position'] == '4'):                ndvi_4(jsonmsg)            elif (jsonmsg['position'] == '5'):                ndvi_5(jsonmsg)            elif (jsonmsg['position'] == '6'):                ndvi_6(jsonmsg)            elif (jsonmsg['position'] == '7'):                ndvi_7(jsonmsg)            elif (jsonmsg['position'] == '8'):                ndvi_8(jsonmsg)        elif (jsonmsg['command'] == 'a051'):            if (jsonmsg['position'] == '1'):                ndvi1(jsonmsg)            elif (jsonmsg['position'] == '2'):                ndvi2(jsonmsg)            elif (jsonmsg['position'] == '3'):                ndvi3(jsonmsg)            elif (jsonmsg['position'] == '4'):                ndvi4(jsonmsg)            elif (jsonmsg['position'] == '5'):                ndvi5(jsonmsg)            elif (jsonmsg['position'] == '6'):                ndvi6(jsonmsg)            elif (jsonmsg['position'] == '7'):                ndvi7(jsonmsg)            elif (jsonmsg['position'] == '8'):                ndvi8(jsonmsg)            elif (jsonmsg['position'] == '0'):                manual_ndvi(jsonmsg)        elif (jsonmsg['command'] == 'a014'):            tilt(jsonmsg)        elif (jsonmsg['command'] == 'a012'):            pan(jsonmsg)        elif (jsonmsg['command'] == 'a016'):            zoom(jsonmsg)        elif (jsonmsg['command'] == 'a052'):            zoom0(jsonmsg)        elif (jsonmsg['command'] == 'a018'):            if (jsonmsg['position'] == '1'):                p1_preview_photo(jsonmsg)            elif (jsonmsg['position'] == '2'):                p2_preview_photo(jsonmsg)            elif (jsonmsg['position'] == '3'):                p3_preview_photo(jsonmsg)            elif (jsonmsg['position'] == '4'):                p4_preview_photo(jsonmsg)            elif (jsonmsg['position'] == '5'):                p5_preview_photo(jsonmsg)            elif (jsonmsg['position'] == '6'):                p6_preview_photo(jsonmsg)            elif (jsonmsg['position'] == '7'):                p7_preview_photo(jsonmsg)            elif (jsonmsg['position'] == '8'):                p8_preview_photo(jsonmsg)            elif (jsonmsg['position'] == '0'):                take_preview_photo(jsonmsg)    else:        server_log(jsonmsg['command'], 'ERROR: Unknown command')## A thread used to subscribe to and wait for messages from the server.def thread_job():    # create a MQTT client with a user name and password to subscribe to the messages    mqtt_thread_client = mqtt.Client()    mqtt_thread_client.on_connect = on_connect    mqtt_thread_client.on_message = on_message    mqtt_thread_client.username_pw_set(username='aisky-client', password='aiskyc')    mqtt_thread_client.connect(mqtt_server, mqtt_port, mqtt_alive)    mqtt_thread_client.loop_forever()# create a MQTT client with a user name and password to publish messagesmqtt_client = mqtt.Client()mqtt_client.username_pw_set(username='aisky-client', password='aiskyc')mqtt_client.connect(mqtt_server, mqtt_port, mqtt_alive)# create a thread to subscribe to and wait for messages from the servermqtt_subscribe_thread = threading.Thread(target=thread_job)mqtt_subscribe_thread.start()mqtt_client.loop_forever()
 |