Blame view
220222_final/GW/python/gat.py
5.25 KB
35833671e
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Do not execute directly. Please execute gateway_http_uplink.py Created on Mon Oct 19 2021 @author: ALKADRI Mohammad """ from gateway_http_downlink import Downlink from Endpoint import Endpoint import time from datetime import datetime import os import json import base64 import numpy as np import threading import socket import struct sema=threading.Semaphore(value=0) sema2=threading.Semaphore(value=0) APIKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlfa2V5X2lkIjoiYzA2ZGY1MDMtNjE5Zi00ZjI2LTgxNzEtYTU0OTRmMWJmYmRmIiwiYXVkIjoiYXMiLCJpc3MiOiJhcyIsIm5iZiI6MTYyMjY1MzMzMSwic3ViIjoiYXBpX2tleSJ9.23eLyvgd5zheP9hDM0acCAl9ojhQjLTZAU77IKqhvQY' server = 'localhost:8080' #just defining an instance to be used downlink = Downlink(server, APIKey) directory = '/home/pi/python/' def WriteFile(body, event): filename = directory + "log/" + \ time.strftime("%Y_%m_%d_%H-%M-%S") + '_' + event # in case of unexisting folder 'log' if not os.path.exists(directory + 'log'): os.mkdir(directory + 'log') # in case of already existing file : if os.path.exists(filename + ".json"): i = 2 while(os.path.exists(filename + '_' + str(i) + ".json")): i += 1 path = filename + '_' + str(i) + ".json" else: path = filename + ".json" f = open(path, "a") f.write(body.decode("utf-8")) f.close() print("file written : %s" % path) def step0(endpoint): downlink.FlushQueue(endpoint.EUI) downlink.Lorasend(endpoint.EUI, b'\x46') print("State: The data sent by Gateway") #sema2.release() #threading.Thread(target=thread_server, args=()).start() def step1(endpoint): global TT #global c #threading.Thread(target=thread_Tgw, args=()).start() sema.acquire(1) #if TT == 5555: # c=1 #threading.Thread(target=thread_Tgw, args=()).start() #print ("T : %s" %TT) print ("T : %s" % struct.pack('dd',TT[0],TT[1]))# TT.to_bytes(8, 'big')) downlink.FlushQueue(endpoint.EUI) # send all data downlink.Lorasend(endpoint.EUI, struct.pack('dd',TT[0],TT[1]))#TT.to_bytes(8, 'big')) def reset(endpoint): downlink.FlushQueue(endpoint.EUI) downlink.Lorasend(endpoint.EUI, b'\x55') #initialisation packet print("enpoint reset : %s" % endpoint.EUI) def postcompute(event, body, Tn): global data print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]) json_ = json.loads(body) endpoint = Endpoint() endpoint.EUIfromjson(json_) print("request from : EUI : %s" % endpoint.EUI) print("name : %s" % json_['deviceName']) # write json in a file WriteFile(body, event) print("event : %s " % event) print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]) #treat all cases if event == 'up': #b64 to bytes data = base64.b64decode(json_["data"]) #bytes to integer #integer = int.from_bytes(data, 'big') print("data received from Endpoint : ") print("base 64 : %s" % json_["data"]) print("bytes : %s" % data) #print("int : %s" % integer) print() print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]) data = int.from_bytes(data, 'big') print("data : %s" % data) if data == 0x5374617274 : # 0x5374617274 : Start sema2.release() global c c=1 if data == 0x54696D6573: # 0x54696D6573 : Times threading.Thread(target=thread_Tgw, args=()).start() #step0(endpoint) elif event == 'txack': print("data : %s" % data) if data == 0x5374617274 : # 0x5374617274 : Start step0(endpoint) elif data == 0x54696D6573: # 0x54696D6573 : Times step1(endpoint) else: print("error : uncovered State") reset(endpoint) elif event == 'error': print('error : ' + json_["error"]) else: #step1(endpoint) print("possible error : uncovered event") print(body) reset(endpoint) print() def thread_Tgw(): global c global TT TT = [5555,5555] while(c): #global TT sock_T=socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock_T.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) sock_T.connect(('127.0.0.1',2222)) TT=sock_T.recv(64) print(TT) #TT=int.from_bytes(TT, 'little') TT=struct.unpack('dd',TT) #sema.release() c=0 sock_T.close() sema.release() def thread_server(): a=1 while(1): #global TT sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) sock.bind(('127.0.0.1',4242)) sock.listen(1) conn,addr=sock.accept() with conn: data='q' sema2.acquire(1) conn.send(data.encode('ascii')) a=0 sock.close() threading.Thread(target=thread_server, args=()).start() c=1 print("Chirpstack server for downlink : %s" % downlink.server) |