aisky-mqttd 64 KB

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