aisky-mqttd 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  1. #!/usr/bin/env python3.5
  2. import paho.mqtt.client as mqtt
  3. import time
  4. import sys
  5. import http.client, urllib
  6. import json
  7. import threading
  8. import os
  9. import shutil
  10. import uuid
  11. import hashlib
  12. import serial
  13. import array
  14. import base64
  15. import urllib.request
  16. import datetime
  17. import requests
  18. import logging
  19. """ Device Information - the information about this device
  20. These device information is used for the MQTT topic. This program will subscribe and publish to
  21. the MQTT topic.
  22. MQTT topic to subscribe to: AISKY/<project_name>/<model_name>/<device_id>
  23. MQTT topic to publish to : AISKY/<project_name>/<model_name>/<device_id>/Log
  24. """
  25. # @var project_name The project name comes from the u-boot environment variable 'project'.
  26. # @var model_name The model name comes from the u-boot environment variable 'model'.
  27. # @var device_id The device id comes from the mac address of eth0.
  28. project_name = os.popen('cat /etc/aisky.conf | grep project').readline().split('=')[1].strip()
  29. model_name = os.popen('cat /etc/aisky.conf | grep model').readline().split('=')[1].strip()
  30. device_id = open('/sys/class/net/eth0/address').readline().strip()
  31. """ NOTE: Remember to setup the u-boot environment variables before executing this program. The
  32. commands to setup the u-boot environment variables are as follows.
  33. Setup the 'project' variable: The following command sets the 'project' variable to AppleFarm.
  34. root@mylinkit:~# fw_setenv project AppleFarm
  35. Setup the 'model' variable: The following command sets the 'model' variable to MK-G.
  36. root@mylinkit:~# fw_setenv model MK-G
  37. Then, the following command can be used to display the u-boot environment variables.
  38. root@mylinkit:~# fw_printenv
  39. """
  40. """ MQTT Server
  41. If you don't have your own MQTT server, you can use the public MQTT server 'iot.eclipse.org'. But
  42. with the public MQTT server, you can only publish and subscribe without a user name and password.
  43. Sometimes the public MQTT server is unstable.
  44. """
  45. # @var mqtt_server The URL or IP address of the MQTT server to connect to.
  46. # @var mqtt_port The port of the MQTT server to connect to.
  47. # @var mqtt_alive Maximum period in seconds allowed between communications with the broker. If
  48. # no other messages are being exchanged, this controls the rate at which the
  49. # client will send ping messages to the broker.
  50. mqtt_server = "60.250.156.234"
  51. mqtt_port = 1883
  52. mqtt_alive = 60
  53. # camera API command
  54. camera_ircut_high = 'http://192.168.51.48/cgi-bin/camerasetting_cgi?action=set&channel=0&user=admin&pwd=abcd1234&TRCutLevel=high'
  55. camera_ircut_low = 'http://192.168.51.48/cgi-bin/camerasetting_cgi?action=set&channel=0&user=admin&pwd=abcd1234&TRCutLevel=low'
  56. camera_image = 'http://192.168.51.48/cgi-bin/images_cgi?channel=0&user=admin&pwd=abcd1234'
  57. camera_zoomin = "http://192.168.51.48/cgi-bin/ptz_cgi?action=ZoomAdd&user=admin&pwd=abcd1234"
  58. camera_zoomout = "http://192.168.51.48/cgi-bin/ptz_cgi?action=ZoomSub&user=admin&pwd=abcd1234"
  59. #php path
  60. ndvi_path = 'http://60.250.156.230/cust/c1.php'
  61. ppv_path = 'http://60.250.156.230/cust/p1.php'
  62. impv_path = 'http://60.250.156.230/cust/im1.php'
  63. log_path = 'http://54.248.68.32/MQTT/BBtoMQTT.php'
  64. log_angle_path = 'http://54.248.68.32/MQTT/BBtoMQTT_angle.php'
  65. reboot_path ="http://54.248.68.32/MQTT/BBtoCaveat.php"
  66. #log
  67. # @var mqtt_sub_topic The MQTT topic to subscribe to.
  68. # @var mqtt_pub_topic The MQTT topic to publish to.
  69. mqtt_sub_topic = "AISKY/" + project_name + "/" + model_name + "/" + device_id
  70. mqtt_pub_topic = mqtt_sub_topic + "/Log"
  71. ##nr
  72. nr = "GTW009001001"
  73. ##angle
  74. Tilt_old = 0
  75. Pan_old = 0
  76. Zoom_old = 0
  77. ## Calculate the SHA256 checksum of the file.
  78. # @param file [in] The file path for which you want to calculate the checksum.
  79. def get_sha256sum(file):
  80. with open(file, "rb") as f:
  81. bytes = f.read()
  82. return hashlib.sha256(bytes).hexdigest()
  83. ## Send logs to the server.
  84. # @param command [in] The command received from the server.
  85. # @param response [in] The response message to the command.
  86. def server_log(command, rqnn):
  87. localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  88. # message to be sent in JSON format
  89. payload = {
  90. # let server know which device the message came from
  91. 'device_id': device_id,
  92. # let server know when the message was sent from the device
  93. 'localtime': localtime,
  94. 'node_id': nr,
  95. 'command': command,
  96. 'rqnn': rqnn
  97. }
  98. jsonobj = json.dumps(payload, sort_keys=True, indent=4)
  99. mqtt_client.publish(mqtt_pub_topic, jsonobj, qos=2)
  100. print('Sent:')
  101. print(jsonobj)
  102. def NDVI_log(command, bytes):
  103. localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  104. # message to be sent in JSON format
  105. payload = {
  106. # let server know which device the message came from
  107. 'device_id': device_id,
  108. # let server know when the message was sent from the device
  109. 'localtime': localtime,
  110. 'node_id': nr,
  111. 'command': command,
  112. 'bytes': bytes
  113. }
  114. jsonobj = json.dumps(payload, sort_keys=True, indent=4)
  115. mqtt_client.publish(mqtt_pub_topic, jsonobj, qos=2)
  116. print('Sent:')
  117. print(jsonobj)
  118. def get_ip_address(ifname):
  119. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  120. try:
  121. return socket.inet_ntoa(fcntl.ioctl(
  122. s.fileno(),
  123. 0x8915, # SIOCGIFADDR
  124. struct.pack('256s', ifname[:15])
  125. )[20:24])
  126. except:
  127. return ""
  128. ## Reset this device.
  129. def system_reboot():
  130. server_log('reboot', 'Restarting system')
  131. time.sleep(5)
  132. os.system('sudo reboot')
  133. time.sleep(10)
  134. def system_update_code():
  135. server_log('update', 'Update system')
  136. os.system('git clone http://60.250.156.230:3000/fatwolf/Edamame.git')
  137. time.sleep(5)
  138. os.system('sudo su')
  139. os.system('cp /home/pi/Edamame/server.py /usr/sbin/')
  140. time.sleep(10)
  141. os.system('rm -rf Edamame')
  142. server_log('reboot', 'Restarting system')
  143. time.sleep(5)
  144. os.system('sudo reboot')
  145. time.sleep(10)
  146. ## Reset this device to factory default.
  147. def factory_reset():
  148. server_log('factory_reset', 'Resetting to factory default')
  149. time.sleep(5)
  150. os.system('firstboot -y')
  151. os.system('reboot -f')
  152. time.sleep(10)
  153. ## Receive the system action to this device .
  154. def call(msg):
  155. server_log('a001', '1')
  156. data = {'nr': nr, 'command': 'A', 'position': msg['position'], 'time': msg['time'], 'date': msg['date'],'response': 'YES', 'tilt_angle': 0, 'pan_angle': 0,
  157. 'zoom': 0}
  158. data = urllib.parse.urlencode(data)
  159. data = data.encode('utf-8')
  160. req = urllib.request.Request(log_path, data)
  161. req.add_header('User-Agent', 'Magic Browser')
  162. resp = urllib.request.urlopen(req)
  163. respData = resp.read()
  164. print("call ok")
  165. def ndvi_1(msg):
  166. server_log('ndvi_1', 'start')
  167. print(msg['orPan'])
  168. print(msg['orTilt'])
  169. print(msg['orZoom'])
  170. print(msg['position'])
  171. orpan(msg)
  172. ortilt(msg)
  173. orzoom(msg)
  174. time.sleep(2)
  175. 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'])}
  176. data = urllib.parse.urlencode(data)
  177. data = data.encode('utf-8')
  178. req = urllib.request.Request(log_path, data)
  179. req.add_header('User-Agent', 'Magic Browser')
  180. resp = urllib.request.urlopen(req)
  181. respData = resp.read()
  182. print("NDVI 1 angle ok")
  183. data = {'nr': nr,'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}
  184. data = urllib.parse.urlencode(data)
  185. data = data.encode('utf-8')
  186. req = urllib.request.Request(log_angle_path, data)
  187. req.add_header('User-Agent', 'Magic Browser')
  188. resp = urllib.request.urlopen(req)
  189. respData = resp.read()
  190. print("NDVI 1 angle log")
  191. def ndvi_2(msg):
  192. server_log('ndvi_2', 'start')
  193. print(msg['orPan'])
  194. print(msg['orTilt'])
  195. print(msg['orZoom'])
  196. print(msg['position'])
  197. orpan(msg)
  198. ortilt(msg)
  199. orzoom(msg)
  200. time.sleep(2)
  201. 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'])}
  202. data = urllib.parse.urlencode(data)
  203. data = data.encode('utf-8')
  204. req = urllib.request.Request(log_path, data)
  205. req.add_header('User-Agent', 'Magic Browser')
  206. resp = urllib.request.urlopen(req)
  207. respData = resp.read()
  208. print("NDVI 2 angle ok")
  209. data = {'nr': nr,'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}
  210. data = urllib.parse.urlencode(data)
  211. data = data.encode('utf-8')
  212. req = urllib.request.Request(log_angle_path, data)
  213. req.add_header('User-Agent', 'Magic Browser')
  214. resp = urllib.request.urlopen(req)
  215. respData = resp.read()
  216. print("NDVI 2 angle log")
  217. def ndvi_3(msg):
  218. server_log('ndvi_3', 'start')
  219. print(msg['orPan'])
  220. print(msg['orTilt'])
  221. print(msg['orZoom'])
  222. print(msg['position'])
  223. orpan(msg)
  224. ortilt(msg)
  225. orzoom(msg)
  226. time.sleep(2)
  227. 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'])}
  228. data = urllib.parse.urlencode(data)
  229. data = data.encode('utf-8')
  230. req = urllib.request.Request(log_path, data)
  231. req.add_header('User-Agent', 'Magic Browser')
  232. resp = urllib.request.urlopen(req)
  233. respData = resp.read()
  234. print("NDVI 3 angle ok")
  235. data = {'nr': nr,'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}
  236. data = urllib.parse.urlencode(data)
  237. data = data.encode('utf-8')
  238. req = urllib.request.Request(log_angle_path, data)
  239. req.add_header('User-Agent', 'Magic Browser')
  240. resp = urllib.request.urlopen(req)
  241. respData = resp.read()
  242. print("NDVI 3 angle log")
  243. def ndvi_4(msg):
  244. server_log('ndvi_4', 'start')
  245. print(msg['orPan'])
  246. print(msg['orTilt'])
  247. print(msg['orZoom'])
  248. print(msg['position'])
  249. orpan(msg)
  250. ortilt(msg)
  251. orzoom(msg)
  252. time.sleep(2)
  253. 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'])}
  254. data = urllib.parse.urlencode(data)
  255. data = data.encode('utf-8')
  256. req = urllib.request.Request(log_path, data)
  257. req.add_header('User-Agent', 'Magic Browser')
  258. resp = urllib.request.urlopen(req)
  259. respData = resp.read()
  260. print("NDVI 4 angle ok")
  261. data = {'nr': nr,'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}
  262. data = urllib.parse.urlencode(data)
  263. data = data.encode('utf-8')
  264. req = urllib.request.Request(log_angle_path, data)
  265. req.add_header('User-Agent', 'Magic Browser')
  266. resp = urllib.request.urlopen(req)
  267. respData = resp.read()
  268. print("NDVI 4 angle log")
  269. def ndvi_5(msg):
  270. server_log('ndvi_5', 'start')
  271. print(msg['orPan'])
  272. print(msg['orTilt'])
  273. print(msg['orZoom'])
  274. print(msg['position'])
  275. orpan(msg)
  276. ortilt(msg)
  277. orzoom(msg)
  278. time.sleep(2)
  279. 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'])}
  280. data = urllib.parse.urlencode(data)
  281. data = data.encode('utf-8')
  282. req = urllib.request.Request(log_path, data)
  283. req.add_header('User-Agent', 'Magic Browser')
  284. resp = urllib.request.urlopen(req)
  285. respData = resp.read()
  286. print("NDVI 5 angle ok")
  287. data = {'nr': nr, 'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']), 'zoom': int(msg['orZoom'])}
  288. data = urllib.parse.urlencode(data)
  289. data = data.encode('utf-8')
  290. req = urllib.request.Request(log_angle_path, data)
  291. req.add_header('User-Agent', 'Magic Browser')
  292. resp = urllib.request.urlopen(req)
  293. respData = resp.read()
  294. print("NDVI 5 angle log")
  295. def ndvi_6(msg):
  296. server_log('ndvi_6', 'start')
  297. print(msg['orPan'])
  298. print(msg['orTilt'])
  299. print(msg['orZoom'])
  300. print(msg['position'])
  301. orpan(msg)
  302. ortilt(msg)
  303. orzoom(msg)
  304. time.sleep(2)
  305. 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'])}
  306. data = urllib.parse.urlencode(data)
  307. data = data.encode('utf-8')
  308. req = urllib.request.Request(log_path, data)
  309. req.add_header('User-Agent', 'Magic Browser')
  310. resp = urllib.request.urlopen(req)
  311. respData = resp.read()
  312. print("NDVI 6 angle ok")
  313. data = {'nr': nr,'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}
  314. data = urllib.parse.urlencode(data)
  315. data = data.encode('utf-8')
  316. req = urllib.request.Request(log_angle_path, data)
  317. req.add_header('User-Agent', 'Magic Browser')
  318. resp = urllib.request.urlopen(req)
  319. respData = resp.read()
  320. print("NDVI 6 angle log")
  321. def ndvi_7(msg):
  322. server_log('ndvi_7', 'start')
  323. print(msg['orPan'])
  324. print(msg['orTilt'])
  325. print(msg['orZoom'])
  326. print(msg['position'])
  327. orpan(msg)
  328. ortilt(msg)
  329. orzoom(msg)
  330. time.sleep(2)
  331. 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'])}
  332. data = urllib.parse.urlencode(data)
  333. data = data.encode('utf-8')
  334. req = urllib.request.Request(log_path, data)
  335. req.add_header('User-Agent', 'Magic Browser')
  336. resp = urllib.request.urlopen(req)
  337. respData = resp.read()
  338. print("NDVI 7 angle ok")
  339. data = {'nr': nr,'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']),'zoom': int(msg['orZoom'])}
  340. data = urllib.parse.urlencode(data)
  341. data = data.encode('utf-8')
  342. req = urllib.request.Request(log_angle_path, data)
  343. req.add_header('User-Agent', 'Magic Browser')
  344. resp = urllib.request.urlopen(req)
  345. respData = resp.read()
  346. print("NDVI 7 angle log")
  347. def ndvi_8(msg):
  348. server_log('ndvi_8', 'start')
  349. print(msg['orPan'])
  350. print(msg['orTilt'])
  351. print(msg['orZoom'])
  352. print(msg['position'])
  353. orpan(msg)
  354. ortilt(msg)
  355. orzoom(msg)
  356. time.sleep(2)
  357. 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'])}
  358. data = urllib.parse.urlencode(data)
  359. data = data.encode('utf-8')
  360. req = urllib.request.Request(log_path, data)
  361. req.add_header('User-Agent', 'Magic Browser')
  362. resp = urllib.request.urlopen(req)
  363. respData = resp.read()
  364. print("NDVI 8 angle ok")
  365. data = {'nr': nr, 'tilt_angle': int(msg['orTilt']), 'pan_angle': int(msg['orPan']), 'zoom': int(msg['orZoom'])}
  366. data = urllib.parse.urlencode(data)
  367. data = data.encode('utf-8')
  368. req = urllib.request.Request(log_angle_path, data)
  369. req.add_header('User-Agent', 'Magic Browser')
  370. resp = urllib.request.urlopen(req)
  371. respData = resp.read()
  372. print("NDVI 8 angle log")
  373. def manual_ndvi(msg):
  374. server_log('manual_ndvi', 'start')
  375. ndvi(msg)
  376. # -----------------------log---------------------------------------------------
  377. data = {'nr': nr,'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}
  378. data = urllib.parse.urlencode(data)
  379. data = data.encode('utf-8')
  380. req = urllib.request.Request(log_path, data)
  381. req.add_header('User-Agent', 'Magic Browser')
  382. resp = urllib.request.urlopen(req)
  383. respData = resp.read()
  384. print("MANUAL NDVI ok")
  385. def p1_preview_photo(msg):
  386. server_log('p1_preview_photo', 'start')
  387. req = requests.get(camera_ircut_low)
  388. fileName = datetime.datetime.now().strftime("/home/pi/p1/1.tif")
  389. req = requests.get(camera_image)
  390. file = open(fileName, 'wb')
  391. for chunk in req.iter_content(100000):
  392. file.write(chunk)
  393. file.close()
  394. dress = '/home/pi/p1/'
  395. for root, dirs, files in os.walk(dress):
  396. print('files: {}'.format(len(files)))
  397. if len(files) >= 1:
  398. files.sort()
  399. for f in files:
  400. with open(os.path.join(root, f), "rb") as imageFile:
  401. str = base64.b64encode(imageFile.read())
  402. url = ppv_path
  403. values = {'p22_1': str, 'p22_name_1': f}
  404. p22_1 = urllib.parse.urlencode(values)
  405. p22_1 = p22_1.encode('utf-8')
  406. req = urllib.request.Request(url, p22_1)
  407. req.add_header('User-Agent', 'Magic Browser')
  408. resp = urllib.request.urlopen(req)
  409. respData = resp.read()
  410. print(os.path.join(root, f))
  411. #print(respdata)
  412. os.remove(os.path.join(root, f))
  413. print("ok")
  414. data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}
  415. data = urllib.parse.urlencode(data)
  416. data = data.encode('utf-8')
  417. req = urllib.request.Request(log_path, data)
  418. req.add_header('User-Agent', 'Magic Browser')
  419. resp = urllib.request.urlopen(req)
  420. respData = resp.read()
  421. print("p1_preview_photo ok")
  422. def p2_preview_photo(msg):
  423. server_log('p2_preview_photo', 'start')
  424. req = requests.get(camera_ircut_low)
  425. fileName = datetime.datetime.now().strftime("/home/pi/p2/2.tif")
  426. req = requests.get(camera_image)
  427. file = open(fileName, 'wb')
  428. for chunk in req.iter_content(100000):
  429. file.write(chunk)
  430. file.close()
  431. dress = '/home/pi/p2/'
  432. for root, dirs, files in os.walk(dress):
  433. print('files: {}'.format(len(files)))
  434. if len(files) >= 1:
  435. files.sort()
  436. for f in files:
  437. with open(os.path.join(root, f), "rb") as imageFile:
  438. str = base64.b64encode(imageFile.read())
  439. url = ppv_path
  440. values = {'p22_2': str, 'p22_name_2': f}
  441. p22_2 = urllib.parse.urlencode(values)
  442. p22_2 = p22_2.encode('utf-8')
  443. req = urllib.request.Request(url, p22_2)
  444. req.add_header('User-Agent', 'Magic Browser')
  445. resp = urllib.request.urlopen(req)
  446. respData = resp.read()
  447. print(os.path.join(root, f))
  448. #print(respdata)
  449. os.remove(os.path.join(root, f))
  450. print("ok")
  451. data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}
  452. data = urllib.parse.urlencode(data)
  453. data = data.encode('utf-8')
  454. req = urllib.request.Request(log_path, data)
  455. req.add_header('User-Agent', 'Magic Browser')
  456. resp = urllib.request.urlopen(req)
  457. respData = resp.read()
  458. print("p2_preview_photo ok")
  459. def p3_preview_photo(msg):
  460. server_log('p3_preview_photo', 'start')
  461. req = requests.get(camera_ircut_low)
  462. fileName = datetime.datetime.now().strftime("/home/pi/p3/3.tif")
  463. req = requests.get(camera_image)
  464. file = open(fileName, 'wb')
  465. for chunk in req.iter_content(100000):
  466. file.write(chunk)
  467. file.close()
  468. dress = '/home/pi/p3/'
  469. for root, dirs, files in os.walk(dress):
  470. print('files: {}'.format(len(files)))
  471. if len(files) >= 1:
  472. files.sort()
  473. for f in files:
  474. with open(os.path.join(root, f), "rb") as imageFile:
  475. str = base64.b64encode(imageFile.read())
  476. url = ppv_path
  477. values = {'p22_3': str, 'p22_name_3': f}
  478. p22_3 = urllib.parse.urlencode(values)
  479. p22_3 = p22_3.encode('utf-8')
  480. req = urllib.request.Request(url, p22_3)
  481. req.add_header('User-Agent', 'Magic Browser')
  482. resp = urllib.request.urlopen(req)
  483. respData = resp.read()
  484. print(os.path.join(root, f))
  485. #print(respdata)
  486. os.remove(os.path.join(root, f))
  487. print("ok")
  488. data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}
  489. data = urllib.parse.urlencode(data)
  490. data = data.encode('utf-8')
  491. req = urllib.request.Request(log_path, data)
  492. req.add_header('User-Agent', 'Magic Browser')
  493. resp = urllib.request.urlopen(req)
  494. respData = resp.read()
  495. print("p3_preview_photo ok")
  496. def p4_preview_photo(msg):
  497. server_log('p4_preview_photo', 'start')
  498. req = requests.get(camera_ircut_low)
  499. fileName = datetime.datetime.now().strftime("/home/pi/p4/4.tif")
  500. req = requests.get(camera_image)
  501. file = open(fileName, 'wb')
  502. for chunk in req.iter_content(100000):
  503. file.write(chunk)
  504. file.close()
  505. dress = '/home/pi/p4/'
  506. for root, dirs, files in os.walk(dress):
  507. print('files: {}'.format(len(files)))
  508. if len(files) >= 1:
  509. files.sort()
  510. for f in files:
  511. with open(os.path.join(root, f), "rb") as imageFile:
  512. str = base64.b64encode(imageFile.read())
  513. url = ppv_path
  514. values = {'p22_4': str, 'p22_name_4': f}
  515. p22_4 = urllib.parse.urlencode(values)
  516. p22_4 = p22_4.encode('utf-8')
  517. req = urllib.request.Request(url, p22_4)
  518. req.add_header('User-Agent', 'Magic Browser')
  519. resp = urllib.request.urlopen(req)
  520. respData = resp.read()
  521. print(os.path.join(root, f))
  522. #print(respdata)
  523. os.remove(os.path.join(root, f))
  524. print("ok")
  525. data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}
  526. data = urllib.parse.urlencode(data)
  527. data = data.encode('utf-8')
  528. req = urllib.request.Request(log_path, data)
  529. req.add_header('User-Agent', 'Magic Browser')
  530. resp = urllib.request.urlopen(req)
  531. respData = resp.read()
  532. print("p4_preview_photo ok")
  533. def p5_preview_photo(msg):
  534. server_log('p5_preview_photo', 'start')
  535. req = requests.get(camera_ircut_low)
  536. fileName = datetime.datetime.now().strftime("/home/pi/p5/5.tif")
  537. req = requests.get(camera_image)
  538. file = open(fileName, 'wb')
  539. for chunk in req.iter_content(100000):
  540. file.write(chunk)
  541. file.close()
  542. dress = '/home/pi/p5/'
  543. for root, dirs, files in os.walk(dress):
  544. print('files: {}'.format(len(files)))
  545. if len(files) >= 1:
  546. files.sort()
  547. for f in files:
  548. with open(os.path.join(root, f), "rb") as imageFile:
  549. str = base64.b64encode(imageFile.read())
  550. url = ppv_path
  551. values = {'p22_5': str, 'p22_name_5': f}
  552. p22_5 = urllib.parse.urlencode(values)
  553. p22_5 = p22_5.encode('utf-8')
  554. req = urllib.request.Request(url, p22_5)
  555. req.add_header('User-Agent', 'Magic Browser')
  556. resp = urllib.request.urlopen(req)
  557. respData = resp.read()
  558. print(os.path.join(root, f))
  559. #print(respdata)
  560. os.remove(os.path.join(root, f))
  561. print("ok")
  562. data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}
  563. data = urllib.parse.urlencode(data)
  564. data = data.encode('utf-8')
  565. req = urllib.request.Request(log_path, data)
  566. req.add_header('User-Agent', 'Magic Browser')
  567. resp = urllib.request.urlopen(req)
  568. respData = resp.read()
  569. print("p5_preview_photo ok")
  570. def p6_preview_photo(msg):
  571. server_log('p6_preview_photo', 'start')
  572. req = requests.get(camera_ircut_low)
  573. fileName = datetime.datetime.now().strftime("/home/pi/p6/6.tif")
  574. req = requests.get(camera_image)
  575. file = open(fileName, 'wb')
  576. for chunk in req.iter_content(100000):
  577. file.write(chunk)
  578. file.close()
  579. dress = '/home/pi/p6/'
  580. for root, dirs, files in os.walk(dress):
  581. print('files: {}'.format(len(files)))
  582. if len(files) >= 1:
  583. files.sort()
  584. for f in files:
  585. with open(os.path.join(root, f), "rb") as imageFile:
  586. str = base64.b64encode(imageFile.read())
  587. url = ppv_path
  588. values = {'p22_6': str, 'p22_name_6': f}
  589. p22_6 = urllib.parse.urlencode(values)
  590. p22_6 = p22_6.encode('utf-8')
  591. req = urllib.request.Request(url, p22_6)
  592. req.add_header('User-Agent', 'Magic Browser')
  593. resp = urllib.request.urlopen(req)
  594. respData = resp.read()
  595. print(os.path.join(root, f))
  596. #print(respdata)
  597. os.remove(os.path.join(root, f))
  598. print("ok")
  599. data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}
  600. data = urllib.parse.urlencode(data)
  601. data = data.encode('utf-8')
  602. req = urllib.request.Request(log_path, data)
  603. req.add_header('User-Agent', 'Magic Browser')
  604. resp = urllib.request.urlopen(req)
  605. respData = resp.read()
  606. print("p6_preview_photo ok")
  607. def p7_preview_photo(msg):
  608. server_log('p7_preview_photo', 'start')
  609. req = requests.get(camera_ircut_low)
  610. fileName = datetime.datetime.now().strftime("/home/pi/p7/7.tif")
  611. req = requests.get(camera_image)
  612. file = open(fileName, 'wb')
  613. for chunk in req.iter_content(100000):
  614. file.write(chunk)
  615. file.close()
  616. dress = '/home/pi/p7/'
  617. for root, dirs, files in os.walk(dress):
  618. print('files: {}'.format(len(files)))
  619. if len(files) >= 1:
  620. files.sort()
  621. for f in files:
  622. with open(os.path.join(root, f), "rb") as imageFile:
  623. str = base64.b64encode(imageFile.read())
  624. url = ppv_path
  625. values = {'p22_7': str, 'p22_name_7': f}
  626. p22_7 = urllib.parse.urlencode(values)
  627. p22_7 = p22_7.encode('utf-8')
  628. req = urllib.request.Request(url, p22_7)
  629. req.add_header('User-Agent', 'Magic Browser')
  630. resp = urllib.request.urlopen(req)
  631. respData = resp.read()
  632. print(os.path.join(root, f))
  633. # print(respdata)
  634. os.remove(os.path.join(root, f))
  635. print("ok")
  636. data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}
  637. data = urllib.parse.urlencode(data)
  638. data = data.encode('utf-8')
  639. req = urllib.request.Request(log_path, data)
  640. req.add_header('User-Agent', 'Magic Browser')
  641. resp = urllib.request.urlopen(req)
  642. respData = resp.read()
  643. print("p7_preview_photo ok")
  644. def p8_preview_photo(msg):
  645. server_log('p8_preview_photo', 'start')
  646. req = requests.get(camera_ircut_low)
  647. fileName = datetime.datetime.now().strftime("/home/pi/p8/8.tif")
  648. req = requests.get(camera_image)
  649. file = open(fileName, 'wb')
  650. for chunk in req.iter_content(100000):
  651. file.write(chunk)
  652. file.close()
  653. dress = '/home/pi/p8/'
  654. for root, dirs, files in os.walk(dress):
  655. print('files: {}'.format(len(files)))
  656. if len(files) >= 1:
  657. files.sort()
  658. for f in files:
  659. with open(os.path.join(root, f), "rb") as imageFile:
  660. str = base64.b64encode(imageFile.read())
  661. url = ppv_path
  662. values = {'p22_8': str, 'p22_name_8': f}
  663. p22_8 = urllib.parse.urlencode(values)
  664. p22_8 = p22_8.encode('utf-8')
  665. req = urllib.request.Request(url, p22_8)
  666. req.add_header('User-Agent', 'Magic Browser')
  667. resp = urllib.request.urlopen(req)
  668. respData = resp.read()
  669. print(os.path.join(root, f))
  670. #print(respdata)
  671. os.remove(os.path.join(root, f))
  672. print("ok")
  673. data = {'nr': nr,'command':'S' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle': 0,'pan_angle': 0,'zoom': 0}
  674. data = urllib.parse.urlencode(data)
  675. data = data.encode('utf-8')
  676. req = urllib.request.Request(log_path, data)
  677. req.add_header('User-Agent', 'Magic Browser')
  678. resp = urllib.request.urlopen(req)
  679. respData = resp.read()
  680. print("p8_preview_photo ok")
  681. def tilt(msg):
  682. global Tilt_old
  683. Tilt_old = int(msg['Tilt']) + Tilt_old
  684. if (Tilt_old > 90):
  685. Tilt_old = 90
  686. Tilt(90)
  687. elif (Tilt_old < -25):
  688. Tilt_old = -25
  689. Tilt(-25)
  690. else:
  691. Tilt(Tilt_old)
  692. data = {'nr': nr, 'command': 'T', 'position': "R", 'time':"0", 'date': msg['date'],
  693. 'response': 'YES','tilt_angle': Tilt_old,'pan_angle':Pan_old,'zoom': Zoom_old}
  694. data = urllib.parse.urlencode(data)
  695. data = data.encode('utf-8')
  696. req = urllib.request.Request(log_path, data)
  697. req.add_header('User-Agent', 'Magic Browser')
  698. resp = urllib.request.urlopen(req)
  699. respData = resp.read()
  700. print("Tilt ok")
  701. data = {'nr': nr, 'tilt_angle':Tilt_old, 'pan_angle': Pan_old, 'zoom': Zoom_old}
  702. data = urllib.parse.urlencode(data)
  703. data = data.encode('utf-8')
  704. req = urllib.request.Request(log_angle_path, data)
  705. req.add_header('User-Agent', 'Magic Browser')
  706. resp = urllib.request.urlopen(req)
  707. respData = resp.read()
  708. print("T angle log")
  709. def pan(msg):
  710. global Pan_old
  711. Pan_old = int(msg['Pan']) + Pan_old
  712. if (Pan_old > 360):
  713. Pan_old = Pan_old - 360
  714. Pan(Pan_old)
  715. elif (Pan_old < 0):
  716. Pan_old = 360 + Pan_old
  717. Pan(Pan_old)
  718. else:
  719. Pan(Pan_old)
  720. 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}
  721. data = urllib.parse.urlencode(data)
  722. data = data.encode('utf-8')
  723. req = urllib.request.Request(log_path, data)
  724. req.add_header('User-Agent', 'Magic Browser')
  725. resp = urllib.request.urlopen(req)
  726. respData = resp.read()
  727. print("Pan ok")
  728. data = {'nr': nr, 'tilt_angle':Tilt_old, 'pan_angle': Pan_old, 'zoom': Zoom_old}
  729. data = urllib.parse.urlencode(data)
  730. data = data.encode('utf-8')
  731. req = urllib.request.Request(log_angle_path, data)
  732. req.add_header('User-Agent', 'Magic Browser')
  733. resp = urllib.request.urlopen(req)
  734. respData = resp.read()
  735. print("P angle log")
  736. def zoom(msg):
  737. global Zoom_old
  738. Zoom_old =int(msg['Zoom'])+ Zoom_old
  739. print(Zoom_old)
  740. if (Zoom_old > 5):
  741. Zoom_old = 5
  742. Zoom(0)
  743. elif (Zoom_old < 0):
  744. Zoom_old = 0
  745. Zoom(0)
  746. else:
  747. Zoom(int(msg['Zoom']))
  748. data = {'nr': nr, 'command': 'Z', 'position': "R", 'time':"0", 'date': msg['date'],
  749. 'response': 'YES','tilt_angle':Tilt_old, 'pan_angle':Pan_old,'zoom': Zoom_old}
  750. data = urllib.parse.urlencode(data)
  751. data = data.encode('utf-8')
  752. req = urllib.request.Request(log_path, data)
  753. req.add_header('User-Agent', 'Magic Browser')
  754. resp = urllib.request.urlopen(req)
  755. respData = resp.read()
  756. print("Zoom ok")
  757. data = {'nr': nr, 'tilt_angle':Tilt_old, 'pan_angle': Pan_old, 'zoom': Zoom_old}
  758. data = urllib.parse.urlencode(data)
  759. data = data.encode('utf-8')
  760. req = urllib.request.Request(log_angle_path, data)
  761. req.add_header('User-Agent', 'Magic Browser')
  762. resp = urllib.request.urlopen(req)
  763. respData = resp.read()
  764. print("Z angle log")
  765. def zoom0(msg):
  766. global Zoom_old
  767. Zoom(-(Zoom_old))
  768. Zoom_old = 0
  769. data = {'nr': nr, 'command': 'Z', 'position': "R", 'time':"0", 'date': msg['date'],
  770. 'response': 'YES','tilt_angle':Tilt_old, 'pan_angle':Pan_old,'zoom': -(Zoom_old)}
  771. data = urllib.parse.urlencode(data)
  772. data = data.encode('utf-8')
  773. req = urllib.request.Request(log_path, data)
  774. req.add_header('User-Agent', 'Magic Browser')
  775. resp = urllib.request.urlopen(req)
  776. respData = resp.read()
  777. print("Zoom ok")
  778. data = {'nr': nr, 'tilt_angle':Tilt_old, 'pan_angle': Pan_old, 'zoom': -(Zoom_old)}
  779. data = urllib.parse.urlencode(data)
  780. data = data.encode('utf-8')
  781. req = urllib.request.Request(log_angle_path, data)
  782. req.add_header('User-Agent', 'Magic Browser')
  783. resp = urllib.request.urlopen(req)
  784. respData = resp.read()
  785. print("Z angle log")
  786. def orpan(msg):
  787. global Pan_old
  788. Pan_old = int(msg['orPan'])
  789. if (Pan_old>360):
  790. Pan_old = Pan_old - 360
  791. Pan(Pan_old)
  792. elif(Pan_old<0):
  793. Pan_old = 360 + Pan_old
  794. Pan(Pan_old)
  795. else:
  796. Pan(Pan_old)
  797. def ortilt(msg):
  798. global Tilt_old
  799. Tilt_old = int(msg['orTilt'])
  800. if (Tilt_old > 90):
  801. Tilt_old = 90
  802. Tilt(90)
  803. elif (Tilt_old < -25):
  804. Tilt_old = -25
  805. Tilt(-25)
  806. else:
  807. Tilt(Tilt_old)
  808. def orzoom(msg):
  809. global Zoom_old
  810. Zoom_old = int(msg['orZoom']) - Zoom_old
  811. print(Zoom_old)
  812. Zoom(Zoom_old)
  813. Zoom_old = int(msg['orZoom'])
  814. ## device action.
  815. def take_preview_photo(msg):
  816. im = datetime.datetime.now().strftime('/home/pi/immediate/PV.jpg')
  817. save_photo = datetime.datetime.now().strftime(im)
  818. fileName = datetime.datetime.now().strftime(save_photo)
  819. req = requests.get(camera_image)
  820. file = open(fileName, 'wb')
  821. for chunk in req.iter_content(100000):
  822. file.write(chunk)
  823. file.close()
  824. dress = '/home/pi/immediate/'
  825. for root, dirs, files in os.walk(dress):
  826. print('files: {}'.format(len(files)))
  827. if len(files) >= 1:
  828. files.sort()
  829. for f in files:
  830. with open(os.path.join(root, f), "rb") as imageFile:
  831. str = base64.b64encode(imageFile.read())
  832. url = impv_path
  833. values = {'data': str, 'name': f}
  834. data = urllib.parse.urlencode(values)
  835. data = data.encode('utf-8')
  836. req = urllib.request.Request(url, data)
  837. req.add_header('User-Agent', 'Magic Browser')
  838. resp = urllib.request.urlopen(req)
  839. respData = resp.read()
  840. #print(respData)
  841. #os.remove(os.path.join(root, f))
  842. print("ok")
  843. data = {'nr': nr, 'command': 'S', 'position': msg['position'], 'time': msg['time'], 'date': msg['date'],
  844. 'response': 'YES', 'tilt_angle': 0, 'pan_angle': 0, 'zoom': 0}
  845. data = urllib.parse.urlencode(data)
  846. data = data.encode('utf-8')
  847. req = urllib.request.Request(log_path, data)
  848. req.add_header('User-Agent', 'Magic Browser')
  849. resp = urllib.request.urlopen(req)
  850. respData = resp.read()
  851. print("take_preview_photo ok")
  852. def ndvi(msg):
  853. req = requests.get(camera_ircut_high)
  854. time.sleep(2)
  855. fileName = datetime.datetime.now().strftime("/home/pi/ir_image1/"+msg['date']+"a.tif")
  856. req = requests.get(camera_image)
  857. file = open(fileName, 'wb')
  858. for chunk in req.iter_content(100000):
  859. file.write(chunk)
  860. file.close()
  861. req = requests.get(camera_ircut_low)
  862. time.sleep(2)
  863. fileName = datetime.datetime.now().strftime("/home/pi/n_image1/"+msg['date']+"b.tif")
  864. req = requests.get(camera_image)
  865. file = open(fileName, 'wb')
  866. for chunk in req.iter_content(100000):
  867. file.write(chunk)
  868. file.close()
  869. def main_n():
  870. dress = '/home/pi/ir_image1/'
  871. for root, dirs, files in os.walk(dress):
  872. print('files: {}'.format(len(files)))
  873. if len(files) >= 1:
  874. files.sort()
  875. for f in files:
  876. with open(os.path.join(root, f), "rb") as imageFile:
  877. str = base64.b64encode(imageFile.read())
  878. url = ndvi_path
  879. nr = '21'
  880. values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}
  881. data = urllib.parse.urlencode(values)
  882. data = data.encode('utf-8')
  883. req = urllib.request.Request(url, data)
  884. req.add_header('User-Agent', 'Magic Browser')
  885. resp = urllib.request.urlopen(req)
  886. respData = resp.read()
  887. print(os.path.join(root, f))
  888. # print(respData)
  889. os.remove(os.path.join(root, f))
  890. print("ok")
  891. def main_ir():
  892. dress = '/home/pi/n_image1/'
  893. for root, dirs, files in os.walk(dress):
  894. print('files: {}'.format(len(files)))
  895. if len(files) >= 1:
  896. files.sort()
  897. for f in files:
  898. with open(os.path.join(root, f), "rb") as imageFile:
  899. str = base64.b64encode(imageFile.read())
  900. url = ndvi_path
  901. nr = '21'
  902. values = {'c_na21': str, 'name_na21': f, 'nr21': nr}
  903. c = urllib.parse.urlencode(values)
  904. c = c.encode('utf-8')
  905. req = urllib.request.Request(url, c)
  906. req.add_header('User-Agent', 'Magic Browser')
  907. resp = urllib.request.urlopen(req)
  908. respData = resp.read()
  909. print(os.path.join(root, f))
  910. #print(respData)
  911. os.remove(os.path.join(root, f))
  912. print("ok")
  913. # ---------------------------------------------------------------------------------------
  914. main_n()
  915. main_ir()
  916. def Pan(pan):
  917. a = pan
  918. if a == 360:
  919. w = a - 1
  920. a1 = w * 100
  921. print(a1)
  922. k = '{0:x}'.format(a1)
  923. h = k.zfill(4)
  924. print(h)
  925. p3 = (h[0:2])
  926. p4 = (h[2:])
  927. str_10 = int(p3, 16)
  928. str_10_2 = int(p4, 16)
  929. sum_10 = (str_10 + str_10_2 + 76)
  930. str_16 = hex(sum_10)
  931. sum = (str_16[-2:])
  932. str_10_sum = int(sum, 16)
  933. SERIAL_PORT = '/dev/ttyS0'
  934. ser = serial.Serial(SERIAL_PORT, baudrate=9600, timeout=5)
  935. Pan = [0xFF, 0x01, 0x00, 0x4B, str_10, str_10_2, str_10_sum]
  936. ser.write(array.array('B', Pan).tostring())
  937. print('left360')
  938. elif 0 < a <= 359:
  939. a1 = a * 100
  940. print(a1)
  941. k = '{0:x}'.format(a1)
  942. h = k.zfill(4)
  943. print(h)
  944. p3 = (h[0:2])
  945. p4 = (h[2:])
  946. str_10 = int(p3, 16)
  947. str_10_2 = int(p4, 16)
  948. sum_10 = (str_10 + str_10_2 + 76)
  949. str_16 = hex(sum_10)
  950. sum = (str_16[-2:])
  951. str_10_sum = int(sum, 16)
  952. SERIAL_PORT = '/dev/ttyS0'
  953. ser = serial.Serial(SERIAL_PORT, baudrate=9600, timeout=5)
  954. Pan = [0xFF, 0x01, 0x00, 0x4B, str_10, str_10_2, str_10_sum]
  955. ser.write(array.array('B', Pan).tostring())
  956. print('left')
  957. elif a == 0:
  958. SERIAL_PORT = '/dev/ttyS0'
  959. ser = serial.Serial(SERIAL_PORT, baudrate=9600, timeout=5)
  960. Pan = [0xFF, 0x01, 0x00, 0x4B, 0x00, 0x00, 0x4C]
  961. ser.write(array.array('B', Pan).tostring())
  962. print('leftzore')
  963. def Tilt(tilt):
  964. a = tilt
  965. def up():
  966. q = int(360)
  967. a1 = (q - a) * 100
  968. print(a1)
  969. k = '{0:x}'.format(a1)
  970. h = k.zfill(4)
  971. print(h)
  972. p3 = (h[0:2])
  973. ##print(p3)
  974. p4 = (h[2:])
  975. ##print(p4)
  976. str_10 = int(p3, 16)
  977. str_10_2 = int(p4, 16)
  978. sum_10 = (str_10 + str_10_2 + 78)
  979. str_16 = hex(sum_10)
  980. ##print(str_16)
  981. sum = (str_16[-2:])
  982. str_10_sum = int(sum, 16)
  983. SERIAL_PORT = '/dev/ttyS0'
  984. ser = serial.Serial(SERIAL_PORT, baudrate=9600, timeout=5)
  985. Pan = [0xFF, 0x01, 0x00, 0x4D, str_10, str_10_2, str_10_sum]
  986. ser.write(array.array('B', Pan).tostring())
  987. print('up')
  988. def down():
  989. k = abs(a)
  990. a1 = k * 100
  991. print (a1)
  992. k = '{0:x}'.format(a1)
  993. h = k.zfill(4)
  994. print(h)
  995. p3 = (h[0:2])
  996. ##print(p3)
  997. p4 = (h[2:])
  998. ##print(p4)
  999. str_10 = int(p3, 16)
  1000. str_10_2 = int(p4, 16)
  1001. sum_10 = (str_10 + str_10_2 + 78)
  1002. str_16 = hex(sum_10)
  1003. ##print(str_16)
  1004. sum = (str_16[-2:])
  1005. str_10_sum = int(sum, 16)
  1006. SERIAL_PORT = '/dev/ttyS0'
  1007. ser = serial.Serial(SERIAL_PORT, baudrate=9600, timeout=5)
  1008. Pan = [0xFF, 0x01, 0x00, 0x4D, str_10, str_10_2, str_10_sum]
  1009. ser.write(array.array('B', Pan).tostring())
  1010. print('down')
  1011. if -1 < a <= 90:
  1012. up()
  1013. elif 0 > a >= -25:
  1014. down()
  1015. def Zoom(zoom):
  1016. def Zoom1():
  1017. headers = {'Content-Type': 'text/xml'}
  1018. data = '<?xml version="1.0" encoding="utf-8"?><request><ptzcmd><protocol>0</protocol><cmd>0</cmd><addr>1</addr></</request>'
  1019. url = camera_zoomin
  1020. r = requests.post(url, headers=headers, data=data)
  1021. print (r.content)
  1022. def Zoom_1():
  1023. headers = {'Content-Type': 'text/xml'}
  1024. data = '<?xml version="1.0" encoding="utf-8"?><request><ptzcmd><protocol>0</protocol><cmd>0</cmd><addr>1</addr></</request>'
  1025. url = camera_zoomout
  1026. r = requests.post(url, headers=headers, data=data)
  1027. print (r.content)
  1028. print ('you')
  1029. a = zoom
  1030. print(a)
  1031. if (a == 0):
  1032. print("NO")
  1033. elif (a>0):
  1034. for i in range(a):
  1035. time.sleep(1)
  1036. for j in range(10):
  1037. Zoom1()
  1038. else:
  1039. a=-1*a
  1040. for i in range(a):
  1041. time.sleep(1)
  1042. for j in range(10):
  1043. Zoom_1()
  1044. def ndvi1(msg):
  1045. server_log('NDVI1', 'start')
  1046. req = requests.get(camera_ircut_high)
  1047. time.sleep(2)
  1048. fileName = datetime.datetime.now().strftime("/home/pi/a1/2020-07-20_15.05a1.tif")
  1049. req = requests.get(camera_image)
  1050. file = open(fileName, 'wb')
  1051. for chunk in req.iter_content(100000):
  1052. file.write(chunk)
  1053. file.close()
  1054. path1 = '/home/pi/a1/2020-07-20_15.05a1.tif'
  1055. size1 = os.path.getsize(path1)
  1056. NDVI_log('NDVI1', size1)
  1057. req = requests.get(camera_ircut_low)
  1058. time.sleep(2)
  1059. fileName = datetime.datetime.now().strftime("/home/pi/b1/2020-07-20_15.05b1.tif")
  1060. req = requests.get(camera_image)
  1061. file = open(fileName, 'wb')
  1062. for chunk in req.iter_content(100000):
  1063. file.write(chunk)
  1064. file.close()
  1065. path2 = '/home/pi/b1/2020-07-20_15.05b1.tif'
  1066. size2 = os.path.getsize(path2)
  1067. NDVI_log('NDVI1', size2)
  1068. print("ok")
  1069. def main_n():
  1070. dress = '/home/pi/a1/'
  1071. for root, dirs, files in os.walk(dress):
  1072. print('files: {}'.format(len(files)))
  1073. if len(files) >= 1:
  1074. files.sort()
  1075. for f in files:
  1076. with open(os.path.join(root, f), "rb") as imageFile:
  1077. str = base64.b64encode(imageFile.read())
  1078. url = ndvi_path
  1079. nr = '1'
  1080. values = {'data_ira1': str, 'name_ira1': f, 'nr1': nr}
  1081. data = urllib.parse.urlencode(values)
  1082. data = data.encode('utf-8')
  1083. req = urllib.request.Request(url, data)
  1084. req.add_header('User-Agent', 'Magic Browser')
  1085. resp = urllib.request.urlopen(req)
  1086. respData = resp.read()
  1087. print(os.path.join(root, f))
  1088. #print(respData)
  1089. os.remove(os.path.join(root, f))
  1090. print("ok")
  1091. def main_ir():
  1092. dress = '/home/pi/b1/'
  1093. for root, dirs, files in os.walk(dress):
  1094. print('files: {}'.format(len(files)))
  1095. if len(files) >= 1:
  1096. files.sort()
  1097. for f in files:
  1098. with open(os.path.join(root, f), "rb") as imageFile:
  1099. str = base64.b64encode(imageFile.read())
  1100. url = ndvi_path
  1101. nr = '1'
  1102. values = {'c_na1': str, 'name_na1': f, 'nr1': nr}
  1103. c = urllib.parse.urlencode(values)
  1104. c = c.encode('utf-8')
  1105. req = urllib.request.Request(url, c)
  1106. req.add_header('User-Agent', 'Magic Browser')
  1107. resp = urllib.request.urlopen(req)
  1108. respData = resp.read()
  1109. print(os.path.join(root, f))
  1110. #print(respData)
  1111. os.remove(os.path.join(root, f))
  1112. print("ok")
  1113. # --------------------------------------------------------------------------------------
  1114. main_n()
  1115. main_ir()
  1116. print("NDVI 1 ok")
  1117. def ndvi2(msg):
  1118. server_log('NDVI2', 'start')
  1119. print(msg['date'])
  1120. req = requests.get(camera_ircut_high)
  1121. time.sleep(2)
  1122. fileName = datetime.datetime.now().strftime("/home/pi/a2/"+msg['date']+"a2.tif")
  1123. req = requests.get(camera_image)
  1124. file = open(fileName, 'wb')
  1125. for chunk in req.iter_content(100000):
  1126. file.write(chunk)
  1127. file.close()
  1128. req = requests.get(camera_ircut_low)
  1129. time.sleep(2)
  1130. fileName = datetime.datetime.now().strftime("/home/pi/b2/"+msg['date']+"b2.tif")
  1131. req = requests.get(camera_image)
  1132. file = open(fileName, 'wb')
  1133. for chunk in req.iter_content(100000):
  1134. file.write(chunk)
  1135. file.close()
  1136. def main_n():
  1137. dress = '/home/pi/a2/'
  1138. for root, dirs, files in os.walk(dress):
  1139. print('files: {}'.format(len(files)))
  1140. if len(files) >= 1:
  1141. files.sort()
  1142. for f in files:
  1143. with open(os.path.join(root, f), "rb") as imageFile:
  1144. str = base64.b64encode(imageFile.read())
  1145. url = ndvi_path
  1146. nr = '21'
  1147. values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}
  1148. data = urllib.parse.urlencode(values)
  1149. data = data.encode('utf-8')
  1150. req = urllib.request.Request(url, data)
  1151. req.add_header('User-Agent', 'Magic Browser')
  1152. resp = urllib.request.urlopen(req)
  1153. respData = resp.read()
  1154. print(os.path.join(root, f))
  1155. #print(respData)
  1156. os.remove(os.path.join(root, f))
  1157. print("ok")
  1158. def main_ir():
  1159. dress = '/home/pi/b2/'
  1160. for root, dirs, files in os.walk(dress):
  1161. print('files: {}'.format(len(files)))
  1162. if len(files) >= 1:
  1163. files.sort()
  1164. for f in files:
  1165. with open(os.path.join(root, f), "rb") as imageFile:
  1166. str = base64.b64encode(imageFile.read())
  1167. url = ndvi_path
  1168. nr = '21'
  1169. values = {'c_na21': str, 'name_na21': f, 'nr21': nr}
  1170. c = urllib.parse.urlencode(values)
  1171. c = c.encode('utf-8')
  1172. req = urllib.request.Request(url, c)
  1173. req.add_header('User-Agent', 'Magic Browser')
  1174. resp = urllib.request.urlopen(req)
  1175. respData = resp.read()
  1176. print(os.path.join(root, f))
  1177. #print(respData)
  1178. os.remove(os.path.join(root, f))
  1179. print("ok")
  1180. # ---------------------------------------------------------------------------------------
  1181. main_n()
  1182. main_ir()
  1183. data = {'nr': nr, 'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle':0, 'pan_angle': 0,'zoom': 0}
  1184. data = urllib.parse.urlencode(data)
  1185. data = data.encode('utf-8')
  1186. req = urllib.request.Request(log_path, data)
  1187. req.add_header('User-Agent', 'Magic Browser')
  1188. resp = urllib.request.urlopen(req)
  1189. respData = resp.read()
  1190. print("NDVI 2 ok")
  1191. def ndvi3(msg):
  1192. server_log('NDVI3', 'start')
  1193. print(msg['date'])
  1194. req = requests.get(camera_ircut_high)
  1195. time.sleep(2)
  1196. fileName = datetime.datetime.now().strftime("/home/pi/a3/"+msg['date']+"a3.tif")
  1197. req = requests.get(camera_image)
  1198. file = open(fileName, 'wb')
  1199. for chunk in req.iter_content(100000):
  1200. file.write(chunk)
  1201. file.close()
  1202. req = requests.get(camera_ircut_low)
  1203. time.sleep(2)
  1204. fileName = datetime.datetime.now().strftime("/home/pi/b3/"+msg['date']+"b3.tif")
  1205. req = requests.get(camera_image)
  1206. file = open(fileName, 'wb')
  1207. for chunk in req.iter_content(100000):
  1208. file.write(chunk)
  1209. file.close()
  1210. def main_n():
  1211. dress = '/home/pi/a3/'
  1212. for root, dirs, files in os.walk(dress):
  1213. print('files: {}'.format(len(files)))
  1214. if len(files) >= 1:
  1215. files.sort()
  1216. for f in files:
  1217. with open(os.path.join(root, f), "rb") as imageFile:
  1218. str = base64.b64encode(imageFile.read())
  1219. url = ndvi_path
  1220. nr = '21'
  1221. values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}
  1222. data = urllib.parse.urlencode(values)
  1223. data = data.encode('utf-8')
  1224. req = urllib.request.Request(url, data)
  1225. req.add_header('User-Agent', 'Magic Browser')
  1226. resp = urllib.request.urlopen(req)
  1227. respData = resp.read()
  1228. print(os.path.join(root, f))
  1229. # print(respData)
  1230. os.remove(os.path.join(root, f))
  1231. print("ok")
  1232. def main_ir():
  1233. dress = '/home/pi/b3/'
  1234. for root, dirs, files in os.walk(dress):
  1235. print('files: {}'.format(len(files)))
  1236. if len(files) >= 1:
  1237. files.sort()
  1238. for f in files:
  1239. with open(os.path.join(root, f), "rb") as imageFile:
  1240. str = base64.b64encode(imageFile.read())
  1241. url = ndvi_path
  1242. nr = '21'
  1243. values = {'c_na21': str, 'name_na21': f, 'nr21': nr}
  1244. c = urllib.parse.urlencode(values)
  1245. c = c.encode('utf-8')
  1246. req = urllib.request.Request(url, c)
  1247. req.add_header('User-Agent', 'Magic Browser')
  1248. resp = urllib.request.urlopen(req)
  1249. respData = resp.read()
  1250. print(os.path.join(root, f))
  1251. #print(respData)
  1252. os.remove(os.path.join(root, f))
  1253. print("ok")
  1254. # ---------------------------------------------------------------------------------------
  1255. main_n()
  1256. main_ir()
  1257. data = {'nr': nr, 'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle':0, 'pan_angle': 0,'zoom': 0}
  1258. data = urllib.parse.urlencode(data)
  1259. data = data.encode('utf-8')
  1260. req = urllib.request.Request(log_path, data)
  1261. req.add_header('User-Agent', 'Magic Browser')
  1262. resp = urllib.request.urlopen(req)
  1263. respData = resp.read()
  1264. print("NDVI 3 ok")
  1265. def ndvi4(msg):
  1266. server_log('NDVI4', 'start')
  1267. print(msg['date'])
  1268. req = requests.get(camera_ircut_high)
  1269. time.sleep(2)
  1270. fileName = datetime.datetime.now().strftime("/home/pi/a4/"+msg['date']+"a4.tif")
  1271. req = requests.get(camera_image)
  1272. file = open(fileName, 'wb')
  1273. for chunk in req.iter_content(100000):
  1274. file.write(chunk)
  1275. file.close()
  1276. req = requests.get(camera_ircut_low)
  1277. time.sleep(2)
  1278. fileName = datetime.datetime.now().strftime("/home/pi/b4/"+msg['date']+"b4.tif")
  1279. req = requests.get(camera_image)
  1280. file = open(fileName, 'wb')
  1281. for chunk in req.iter_content(100000):
  1282. file.write(chunk)
  1283. file.close()
  1284. def main_n():
  1285. dress = '/home/pi/a4/'
  1286. for root, dirs, files in os.walk(dress):
  1287. print('files: {}'.format(len(files)))
  1288. if len(files) >= 1:
  1289. files.sort()
  1290. for f in files:
  1291. with open(os.path.join(root, f), "rb") as imageFile:
  1292. str = base64.b64encode(imageFile.read())
  1293. url = ndvi_path
  1294. nr = '21'
  1295. values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}
  1296. data = urllib.parse.urlencode(values)
  1297. data = data.encode('utf-8')
  1298. req = urllib.request.Request(url, data)
  1299. req.add_header('User-Agent', 'Magic Browser')
  1300. resp = urllib.request.urlopen(req)
  1301. respData = resp.read()
  1302. print(os.path.join(root, f))
  1303. #print(respData)
  1304. os.remove(os.path.join(root, f))
  1305. print("ok")
  1306. def main_ir():
  1307. dress = '/home/pi/b4/'
  1308. for root, dirs, files in os.walk(dress):
  1309. print('files: {}'.format(len(files)))
  1310. if len(files) >= 1:
  1311. files.sort()
  1312. for f in files:
  1313. with open(os.path.join(root, f), "rb") as imageFile:
  1314. str = base64.b64encode(imageFile.read())
  1315. url = ndvi_path
  1316. nr = '21'
  1317. values = {'c_na21': str, 'name_na21': f, 'nr21': nr}
  1318. c = urllib.parse.urlencode(values)
  1319. c = c.encode('utf-8')
  1320. req = urllib.request.Request(url, c)
  1321. req.add_header('User-Agent', 'Magic Browser')
  1322. resp = urllib.request.urlopen(req)
  1323. respData = resp.read()
  1324. print(os.path.join(root, f))
  1325. #print(respData)
  1326. os.remove(os.path.join(root, f))
  1327. print("ok")
  1328. # ---------------------------------------------------------------------------------------
  1329. main_n()
  1330. main_ir()
  1331. data = {'nr': nr, 'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle':0, 'pan_angle': 0,'zoom': 0}
  1332. data = urllib.parse.urlencode(data)
  1333. data = data.encode('utf-8')
  1334. req = urllib.request.Request(log_path, data)
  1335. req.add_header('User-Agent', 'Magic Browser')
  1336. resp = urllib.request.urlopen(req)
  1337. respData = resp.read()
  1338. print("NDVI 4 ok")
  1339. def ndvi5(msg):
  1340. server_log('NDVI5', 'start')
  1341. print(msg['date'])
  1342. req = requests.get(camera_ircut_high)
  1343. time.sleep(2)
  1344. fileName = datetime.datetime.now().strftime("/home/pi/a5/"+msg['date']+"a5.tif")
  1345. req = requests.get(camera_image)
  1346. file = open(fileName, 'wb')
  1347. for chunk in req.iter_content(100000):
  1348. file.write(chunk)
  1349. file.close()
  1350. req = requests.get(camera_ircut_low)
  1351. time.sleep(2)
  1352. fileName = datetime.datetime.now().strftime("/home/pi/b5/"+msg['date']+"b5.tif")
  1353. req = requests.get(camera_image)
  1354. file = open(fileName, 'wb')
  1355. for chunk in req.iter_content(100000):
  1356. file.write(chunk)
  1357. file.close()
  1358. def main_n():
  1359. dress = '/home/pi/a5/'
  1360. for root, dirs, files in os.walk(dress):
  1361. print('files: {}'.format(len(files)))
  1362. if len(files) >= 1:
  1363. files.sort()
  1364. for f in files:
  1365. with open(os.path.join(root, f), "rb") as imageFile:
  1366. str = base64.b64encode(imageFile.read())
  1367. url = ndvi_path
  1368. nr = '21'
  1369. values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}
  1370. data = urllib.parse.urlencode(values)
  1371. data = data.encode('utf-8')
  1372. req = urllib.request.Request(url, data)
  1373. req.add_header('User-Agent', 'Magic Browser')
  1374. resp = urllib.request.urlopen(req)
  1375. respData = resp.read()
  1376. print(os.path.join(root, f))
  1377. # print(respData)
  1378. os.remove(os.path.join(root, f))
  1379. print("ok")
  1380. def main_ir():
  1381. dress = '/home/pi/b5/'
  1382. for root, dirs, files in os.walk(dress):
  1383. print('files: {}'.format(len(files)))
  1384. if len(files) >= 1:
  1385. files.sort()
  1386. for f in files:
  1387. with open(os.path.join(root, f), "rb") as imageFile:
  1388. str = base64.b64encode(imageFile.read())
  1389. url = ndvi_path
  1390. nr = '21'
  1391. values = {'c_na21': str, 'name_na21': f, 'nr21': nr}
  1392. c = urllib.parse.urlencode(values)
  1393. c = c.encode('utf-8')
  1394. req = urllib.request.Request(url, c)
  1395. req.add_header('User-Agent', 'Magic Browser')
  1396. resp = urllib.request.urlopen(req)
  1397. respData = resp.read()
  1398. print(os.path.join(root, f))
  1399. #print(respData)
  1400. os.remove(os.path.join(root, f))
  1401. print("ok")
  1402. # ---------------------------------------------------------------------------------------
  1403. main_n()
  1404. main_ir()
  1405. data = {'nr': nr, 'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle':0, 'pan_angle': 0,'zoom': 0}
  1406. data = urllib.parse.urlencode(data)
  1407. data = data.encode('utf-8')
  1408. req = urllib.request.Request(log_path, data)
  1409. req.add_header('User-Agent', 'Magic Browser')
  1410. resp = urllib.request.urlopen(req)
  1411. respData = resp.read()
  1412. print("NDVI 5 ok")
  1413. def ndvi6(msg):
  1414. server_log('NDVI6', 'start')
  1415. print(msg['date'])
  1416. req = requests.get(camera_ircut_high)
  1417. time.sleep(2)
  1418. fileName = datetime.datetime.now().strftime("/home/pi/a6/"+msg['date']+"a6.tif")
  1419. req = requests.get(camera_image)
  1420. file = open(fileName, 'wb')
  1421. for chunk in req.iter_content(100000):
  1422. file.write(chunk)
  1423. file.close()
  1424. req = requests.get(camera_ircut_low)
  1425. time.sleep(2)
  1426. fileName = datetime.datetime.now().strftime("/home/pi/b6/"+msg['date']+"b6.tif")
  1427. req = requests.get(camera_image)
  1428. file = open(fileName, 'wb')
  1429. for chunk in req.iter_content(100000):
  1430. file.write(chunk)
  1431. file.close()
  1432. def main_n():
  1433. dress = '/home/pi/a6/'
  1434. for root, dirs, files in os.walk(dress):
  1435. print('files: {}'.format(len(files)))
  1436. if len(files) >= 1:
  1437. files.sort()
  1438. for f in files:
  1439. with open(os.path.join(root, f), "rb") as imageFile:
  1440. str = base64.b64encode(imageFile.read())
  1441. url = ndvi_path
  1442. nr = '21'
  1443. values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}
  1444. data = urllib.parse.urlencode(values)
  1445. data = data.encode('utf-8')
  1446. req = urllib.request.Request(url, data)
  1447. req.add_header('User-Agent', 'Magic Browser')
  1448. resp = urllib.request.urlopen(req)
  1449. respData = resp.read()
  1450. print(os.path.join(root, f))
  1451. #print(respData)
  1452. os.remove(os.path.join(root, f))
  1453. print("ok")
  1454. def main_ir():
  1455. dress = '/home/pi/b6/'
  1456. for root, dirs, files in os.walk(dress):
  1457. print('files: {}'.format(len(files)))
  1458. if len(files) >= 1:
  1459. files.sort()
  1460. for f in files:
  1461. with open(os.path.join(root, f), "rb") as imageFile:
  1462. str = base64.b64encode(imageFile.read())
  1463. url = ndvi_path
  1464. nr = '21'
  1465. values = {'c_na21': str, 'name_na21': f, 'nr21': nr}
  1466. c = urllib.parse.urlencode(values)
  1467. c = c.encode('utf-8')
  1468. req = urllib.request.Request(url, c)
  1469. req.add_header('User-Agent', 'Magic Browser')
  1470. resp = urllib.request.urlopen(req)
  1471. respData = resp.read()
  1472. print(os.path.join(root, f))
  1473. #print(respData)
  1474. os.remove(os.path.join(root, f))
  1475. print("ok")
  1476. # ---------------------------------------------------------------------------------------
  1477. main_n()
  1478. main_ir()
  1479. data = {'nr': nr, 'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle':0, 'pan_angle': 0,'zoom': 0}
  1480. data = urllib.parse.urlencode(data)
  1481. data = data.encode('utf-8')
  1482. req = urllib.request.Request(log_path, data)
  1483. req.add_header('User-Agent', 'Magic Browser')
  1484. resp = urllib.request.urlopen(req)
  1485. respData = resp.read()
  1486. print("NDVI 6 ok")
  1487. def ndvi7(msg):
  1488. server_log('NDVI7', 'start')
  1489. print(msg['date'])
  1490. req = requests.get(camera_ircut_high)
  1491. time.sleep(2)
  1492. fileName = datetime.datetime.now().strftime("/home/pi/a7/"+msg['date']+"a7.tif")
  1493. req = requests.get(camera_image)
  1494. file = open(fileName, 'wb')
  1495. for chunk in req.iter_content(100000):
  1496. file.write(chunk)
  1497. file.close()
  1498. req = requests.get(camera_ircut_low)
  1499. time.sleep(2)
  1500. fileName = datetime.datetime.now().strftime("/home/pi/b7/"+msg['date']+"b7.tif")
  1501. req = requests.get(camera_image)
  1502. file = open(fileName, 'wb')
  1503. for chunk in req.iter_content(100000):
  1504. file.write(chunk)
  1505. file.close()
  1506. def main_n():
  1507. dress = '/home/pi/a7/'
  1508. for root, dirs, files in os.walk(dress):
  1509. print('files: {}'.format(len(files)))
  1510. if len(files) >= 1:
  1511. files.sort()
  1512. for f in files:
  1513. with open(os.path.join(root, f), "rb") as imageFile:
  1514. str = base64.b64encode(imageFile.read())
  1515. url = ndvi_path
  1516. nr = '21'
  1517. values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}
  1518. data = urllib.parse.urlencode(values)
  1519. data = data.encode('utf-8')
  1520. req = urllib.request.Request(url, data)
  1521. req.add_header('User-Agent', 'Magic Browser')
  1522. resp = urllib.request.urlopen(req)
  1523. respData = resp.read()
  1524. print(os.path.join(root, f))
  1525. #print(respData)
  1526. os.remove(os.path.join(root, f))
  1527. print("ok")
  1528. def main_ir():
  1529. dress = '/home/pi/b7/'
  1530. for root, dirs, files in os.walk(dress):
  1531. print('files: {}'.format(len(files)))
  1532. if len(files) >= 1:
  1533. files.sort()
  1534. for f in files:
  1535. with open(os.path.join(root, f), "rb") as imageFile:
  1536. str = base64.b64encode(imageFile.read())
  1537. url = ndvi_path
  1538. nr = '21'
  1539. values = {'c_na21': str, 'name_na21': f, 'nr21': nr}
  1540. c = urllib.parse.urlencode(values)
  1541. c = c.encode('utf-8')
  1542. req = urllib.request.Request(url, c)
  1543. req.add_header('User-Agent', 'Magic Browser')
  1544. resp = urllib.request.urlopen(req)
  1545. respData = resp.read()
  1546. print(os.path.join(root, f))
  1547. #print(respData)
  1548. os.remove(os.path.join(root, f))
  1549. print("ok")
  1550. # ---------------------------------------------------------------------------------------
  1551. main_n()
  1552. main_ir()
  1553. data = {'nr': nr, 'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle':0, 'pan_angle': 0,'zoom': 0}
  1554. data = urllib.parse.urlencode(data)
  1555. data = data.encode('utf-8')
  1556. req = urllib.request.Request(log_path, data)
  1557. req.add_header('User-Agent', 'Magic Browser')
  1558. resp = urllib.request.urlopen(req)
  1559. respData = resp.read()
  1560. print("NDVI 7 ok")
  1561. def ndvi8(msg):
  1562. #server_log('NDVI8', 'start')
  1563. print(msg['date'])
  1564. req = requests.get(camera_ircut_high)
  1565. time.sleep(2)
  1566. fileName = datetime.datetime.now().strftime("/home/pi/a8/"+msg['date']+"a8.tif")
  1567. req = requests.get(camera_image)
  1568. file = open(fileName, 'wb')
  1569. for chunk in req.iter_content(100000):
  1570. file.write(chunk)
  1571. file.close()
  1572. req = requests.get(camera_ircut_low)
  1573. time.sleep(2)
  1574. fileName = datetime.datetime.now().strftime("/home/pi/b8/"+msg['date']+"b8.tif")
  1575. req = requests.get(camera_image)
  1576. file = open(fileName, 'wb')
  1577. for chunk in req.iter_content(100000):
  1578. file.write(chunk)
  1579. file.close()
  1580. def main_n():
  1581. dress = '/home/pi/a8/'
  1582. for root, dirs, files in os.walk(dress):
  1583. print('files: {}'.format(len(files)))
  1584. if len(files) >= 1:
  1585. files.sort()
  1586. for f in files:
  1587. with open(os.path.join(root, f), "rb") as imageFile:
  1588. str = base64.b64encode(imageFile.read())
  1589. url = ndvi_path
  1590. nr = '21'
  1591. values = {'data_ira21': str, 'name_ira21': f, 'nr21': nr}
  1592. data = urllib.parse.urlencode(values)
  1593. data = data.encode('utf-8')
  1594. req = urllib.request.Request(url, data)
  1595. req.add_header('User-Agent', 'Magic Browser')
  1596. resp = urllib.request.urlopen(req)
  1597. respData = resp.read()
  1598. print(os.path.join(root, f))
  1599. #print(respData)
  1600. os.remove(os.path.join(root, f))
  1601. print("ok")
  1602. def main_ir():
  1603. dress = '/home/pi/b8/'
  1604. for root, dirs, files in os.walk(dress):
  1605. print('files: {}'.format(len(files)))
  1606. if len(files) >= 1:
  1607. files.sort()
  1608. for f in files:
  1609. with open(os.path.join(root, f), "rb") as imageFile:
  1610. str = base64.b64encode(imageFile.read())
  1611. url = ndvi_path
  1612. nr = '21'
  1613. values = {'c_na21': str, 'name_na21': f, 'nr21': nr}
  1614. c = urllib.parse.urlencode(values)
  1615. c = c.encode('utf-8')
  1616. req = urllib.request.Request(url, c)
  1617. req.add_header('User-Agent', 'Magic Browser')
  1618. resp = urllib.request.urlopen(req)
  1619. respData = resp.read()
  1620. print(os.path.join(root, f))
  1621. #print(respData)
  1622. os.remove(os.path.join(root, f))
  1623. print("ok")
  1624. # ---------------------------------------------------------------------------------------
  1625. main_n()
  1626. main_ir()
  1627. data = {'nr': nr, 'command':'C' ,'position':msg['position'],'time':msg['time'],'date':msg['date'],'response':'YES', 'tilt_angle':0, 'pan_angle': 0,'zoom': 0}
  1628. data = urllib.parse.urlencode(data)
  1629. data = data.encode('utf-8')
  1630. req = urllib.request.Request(log_path, data)
  1631. req.add_header('User-Agent', 'Magic Browser')
  1632. resp = urllib.request.urlopen(req)
  1633. respData = resp.read()
  1634. print("NDVI 8 ok")
  1635. ## Report the system information about this device to the server.
  1636. def system_info():
  1637. # get firmware version from the file
  1638. fw_version = open('/etc/fw_version').readline().strip()
  1639. localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1640. vpn_ip = get_ip_address('tun0')
  1641. # message to be sent in JSON format
  1642. payload = {
  1643. 'device_id': device_id,
  1644. 'localtime': localtime,
  1645. 'command': 'system_info',
  1646. 'project': project_name,
  1647. 'model': model_name,
  1648. 'fw_version': fw_version,
  1649. 'mqtt_sub_topic': mqtt_sub_topic,
  1650. 'mqtt_pub_topic': mqtt_pub_topic
  1651. }
  1652. if vpn_ip:
  1653. payload['vpn_connection'] = vpn_ip
  1654. else:
  1655. payload['vpn_connection'] = 'disconnected'
  1656. jsonobj = json.dumps(payload, sort_keys=True, indent=4)
  1657. mqtt_client.publish(mqtt_pub_topic, jsonobj, qos=2)
  1658. print('Sent:')
  1659. print(jsonobj)
  1660. ## Update system firmware.
  1661. # @param msg [in] The message from the server in JSON format.
  1662. def system_update(msg):
  1663. if (msg['filetype'] == 'img'):
  1664. # create a directory to store the firmware image
  1665. dirname = '/tmp/' + str(uuid.uuid1()) + '/'
  1666. filepath = dirname + 'update.img'
  1667. os.makedirs(dirname)
  1668. # start to download the firmware image
  1669. server_log('system_update', 'Downloading system firmware')
  1670. urllib.urlretrieve(msg['url'], filepath)
  1671. # check the SHA256 checksum of the firmware image
  1672. if (msg['sha256sum'] == get_sha256sum(filepath)):
  1673. server_log('system_update', 'Updating system firmware')
  1674. # start to upgrade the system firmware
  1675. os.system('sysupgrade -n ' + filepath)
  1676. else:
  1677. server_log('system_update', 'ERROR: SHA256 checksum is wrong')
  1678. shutil.rmtree(dirname, ignore_errors=True)
  1679. ## The callback function for connecting.
  1680. # @param client [in] The client instance for this callback.
  1681. # @param userdata [in] The private user data as set in Client() or user_data_set().
  1682. # @param flags [in] Response flags sent by the broker.
  1683. # @param rc [in] The connection result.
  1684. def on_connect(client, userdata, flags, rc):
  1685. # subscribe MQTT topic on connection
  1686. client.subscribe(mqtt_sub_topic, qos=2)
  1687. server_log('reboot | factory_reset', 'Boot completed')
  1688. logging.info('system running')
  1689. data = {'nr': nr, 'caveat': 'reboot'}
  1690. data = urllib.parse.urlencode(data)
  1691. data = data.encode('utf-8')
  1692. req = urllib.request.Request(reboot_path, data)
  1693. req.add_header('User-Agent', 'Magic Browser')
  1694. resp = urllib.request.urlopen(req)
  1695. respData = resp.read()
  1696. print("reboot check ok")
  1697. ## The callback function for processing messages from the server.
  1698. # @param client [in] The client instance for this callback.
  1699. # @param userdata [in] The private user data as set in Client() or user_data_set().
  1700. # @param msg [in] An instance of MQTT message.
  1701. def on_message(client, userdata, msg):
  1702. # print(msg.payload)
  1703. msg.payload = msg.payload.decode('utf-8')
  1704. jsonmsg = json.loads(msg.payload)
  1705. #print(jsonmsg)
  1706. print('Received:')
  1707. print(json.dumps(jsonmsg, sort_keys=True, indent=4, separators=(',', ':')))
  1708. # processing the command from the server
  1709. if (jsonmsg['command'] == 'a035'):
  1710. system_reboot()
  1711. elif (jsonmsg['command'] == 'a039'):
  1712. factory_reset()
  1713. elif (jsonmsg['command'] == 'system_update'):
  1714. system_update(jsonmsg)
  1715. elif (jsonmsg['command'] == 'a033'):
  1716. system_update_code()
  1717. elif (jsonmsg['command'] == 'vpn_connect'):
  1718. vpn_connect()
  1719. elif (jsonmsg['command'] == 'vpn_disconnect'):
  1720. vpn_disconnect()
  1721. elif (jsonmsg['node_id'] == "GTW009001001"):
  1722. if (jsonmsg['command'] == 'a000'):
  1723. call(jsonmsg)
  1724. elif (jsonmsg['command'] == 'a050'):
  1725. if (jsonmsg['position'] == '1'):
  1726. ndvi_1(jsonmsg)
  1727. elif (jsonmsg['position'] == '2'):
  1728. ndvi_2(jsonmsg)
  1729. elif (jsonmsg['position'] == '3'):
  1730. ndvi_3(jsonmsg)
  1731. elif (jsonmsg['position'] == '4'):
  1732. ndvi_4(jsonmsg)
  1733. elif (jsonmsg['position'] == '5'):
  1734. ndvi_5(jsonmsg)
  1735. elif (jsonmsg['position'] == '6'):
  1736. ndvi_6(jsonmsg)
  1737. elif (jsonmsg['position'] == '7'):
  1738. ndvi_7(jsonmsg)
  1739. elif (jsonmsg['position'] == '8'):
  1740. ndvi_8(jsonmsg)
  1741. elif (jsonmsg['command'] == 'a051'):
  1742. if (jsonmsg['position'] == '1'):
  1743. ndvi1(jsonmsg)
  1744. elif (jsonmsg['position'] == '2'):
  1745. ndvi2(jsonmsg)
  1746. elif (jsonmsg['position'] == '3'):
  1747. ndvi3(jsonmsg)
  1748. elif (jsonmsg['position'] == '4'):
  1749. ndvi4(jsonmsg)
  1750. elif (jsonmsg['position'] == '5'):
  1751. ndvi5(jsonmsg)
  1752. elif (jsonmsg['position'] == '6'):
  1753. ndvi6(jsonmsg)
  1754. elif (jsonmsg['position'] == '7'):
  1755. ndvi7(jsonmsg)
  1756. elif (jsonmsg['position'] == '8'):
  1757. ndvi8(jsonmsg)
  1758. elif (jsonmsg['position'] == '0'):
  1759. manual_ndvi(jsonmsg)
  1760. elif (jsonmsg['command'] == 'a014'):
  1761. tilt(jsonmsg)
  1762. elif (jsonmsg['command'] == 'a012'):
  1763. pan(jsonmsg)
  1764. elif (jsonmsg['command'] == 'a016'):
  1765. zoom(jsonmsg)
  1766. elif (jsonmsg['command'] == 'a052'):
  1767. zoom0(jsonmsg)
  1768. elif (jsonmsg['command'] == 'a018'):
  1769. if (jsonmsg['position'] == '1'):
  1770. p1_preview_photo(jsonmsg)
  1771. elif (jsonmsg['position'] == '2'):
  1772. p2_preview_photo(jsonmsg)
  1773. elif (jsonmsg['position'] == '3'):
  1774. p3_preview_photo(jsonmsg)
  1775. elif (jsonmsg['position'] == '4'):
  1776. p4_preview_photo(jsonmsg)
  1777. elif (jsonmsg['position'] == '5'):
  1778. p5_preview_photo(jsonmsg)
  1779. elif (jsonmsg['position'] == '6'):
  1780. p6_preview_photo(jsonmsg)
  1781. elif (jsonmsg['position'] == '7'):
  1782. p7_preview_photo(jsonmsg)
  1783. elif (jsonmsg['position'] == '8'):
  1784. p8_preview_photo(jsonmsg)
  1785. elif (jsonmsg['position'] == '0'):
  1786. take_preview_photo(jsonmsg)
  1787. else:
  1788. server_log(jsonmsg['command'], 'ERROR: Unknown command')
  1789. ## A thread used to subscribe to and wait for messages from the server.
  1790. def thread_job():
  1791. # create a MQTT client with a user name and password to subscribe to the messages
  1792. mqtt_thread_client = mqtt.Client()
  1793. mqtt_thread_client.on_connect = on_connect
  1794. mqtt_thread_client.on_message = on_message
  1795. mqtt_thread_client.username_pw_set(username='aisky-client', password='aiskyc')
  1796. mqtt_thread_client.connect(mqtt_server, mqtt_port, mqtt_alive)
  1797. mqtt_thread_client.loop_forever()
  1798. # create a MQTT client with a user name and password to publish messages
  1799. mqtt_client = mqtt.Client()
  1800. mqtt_client.username_pw_set(username='aisky-client', password='aiskyc')
  1801. mqtt_client.connect(mqtt_server, mqtt_port, mqtt_alive)
  1802. # create a thread to subscribe to and wait for messages from the server
  1803. mqtt_subscribe_thread = threading.Thread(target=thread_job)
  1804. mqtt_subscribe_thread.start()
  1805. mqtt_client.loop_forever()