#!/usr/bin/env python3.5 import paho.mqtt.client as mqtt import time import sys import http.client, urllib import json import threading import os import shutil import uuid import hashlib import serial import array import base64 import urllib.request import datetime import requests import logging """ Device Information - the information about this device These device information is used for the MQTT topic. This program will subscribe and publish to the MQTT topic. MQTT topic to subscribe to: AISKY/// MQTT topic to publish to : AISKY////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. The commands 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 AppleFarm Setup the 'model' variable: The following command sets the 'model' variable to MK-G. root@mylinkit:~# fw_setenv model MK-G Then, the following command can be used to display the u-boot environment variables. root@mylinkit:~# fw_printenv """ """ MQTT Server If you don't have your own MQTT server, you can use the public MQTT server 'iot.eclipse.org'. But with 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 = 1883 mqtt_alive = 60 # camera API command camera_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 path ndvi_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_id mqtt_pub_topic = mqtt_sub_topic + "/Log" ##nr nr = "GTW009001001" ##angle Tilt_old = 0 Pan_old = 0 Zoom_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 = '001' url = camera_zoomin r = requests.post(url, headers=headers, data=data) print (r.content) def Zoom_1(): headers = {'Content-Type': 'text/xml'} data = '001' 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 messages mqtt_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 server mqtt_subscribe_thread = threading.Thread(target=thread_job) mqtt_subscribe_thread.start() mqtt_client.loop_forever()