def GetImageDetails(self, imageName): query = {"imageName": imageName} s = requests.Session() servicesArray = ServiceRegistry.getServices("Data") details = [] for service in servicesArray: try: r = s.get(service["ServiceAddress"] + "/info", params=query) r.raise_for_status() details.append(json.loads(r.text)) break except requests.exceptions.RequestException: continue servicesArray = ServiceRegistry.getServices("Analytics") for service in servicesArray: try: r = requests.get(service["ServiceAddress"] + "/imageSearch", params=query) r.raise_for_status() details.append(json.loads(r.text)) except requests.exceptions.RequestException: continue return json.dumps(details).encode()
async def newImage(self, image, imageName, coordinateN, coordinateE): #analyse image analysedData = self.analyser.analyseImage(image) print(analysedData) if (analysedData == None): return analysedData["imageName"] = imageName #save results self.storage.insertAnalyticData(analysedData) #notify others await self.communicator.sendMessage(analysedData) if "Eating" not in analysedData or analysedData["Eating"] == False: serviceList = ServiceRegistry.getServices("Command") parametars = { "coordinateN": coordinateN, "coordinateE": coordinateE, "listOfParamtears": "Give food" } for service in serviceList: try: r = requests.post(service["ServiceAddress"] + "/command", params=parametars) return except requests.exceptions.RequestException: continue
def dataSearch(self, coordinateN, coordinateE, startTime, endTime): servicesArray = ServiceRegistry.getServices("Data") s = requests.Session() query = {} print(coordinateE == "") print(coordinateN) if coordinateE != "" and float(coordinateE) > 0: query["coordinateE"] = float(coordinateE) if coordinateN != "" and float(coordinateN) > 0: query["coordinateN"] = float(coordinateE) if startTime != "": query["startTime"] = startTime if endTime != "": query["endTime"] = endTime for service in servicesArray: try: r = s.get(service["ServiceAddress"] + "/data", params=query, stream=True) r.raise_for_status() for line in r.iter_content(1024): yield line except requests.exceptions.RequestException: continue
def GetImages(self): servicesArray = ServiceRegistry.getServices("Data") s = requests.Session() for service in servicesArray: try: r = s.get(service["ServiceAddress"] + "/data", stream=True) r.raise_for_status() for line in r.iter_content(1024): yield line except requests.exceptions.RequestException: continue
def index(self, imageName): #Get data centaras servicesArray = ServiceRegistry.getServices("Data") s = requests.Session() for service in servicesArray: response = s.get(service["ServiceAddress"]+"/image/"+imageName,) if response.status_code >= 200 and response.status_code < 300: cherrypy.response.headers["Content-Type"] = 'image/jpeg' return response.content raise cherrypy.HTTPError(404, "Your image could not be found in any active service")
def reloadSensors(self): serviceArray = ServiceRegistry.getServices("Sensors") print(serviceArray) for sensor in self.sensors: sensor.Updated = False for service in serviceArray: if service["ServiceName"] != "Sensors": continue if service["ServerName"] in self.sensors: self.sensors[service["ServerName"]].serviceAddress = service[ "ServiceAddress"] self.sensors[service["ServerName"]].Updated = True else: N = 0 E = 0 try: rs = requests.get(service["ServiceAddress"]) except requests.exceptions.RequestException as e: # This is the correct syntax print(e) continue if rs.status_code >= 200 and rs.status_code < 300: print(rs.text) serviceStatus = json.loads(rs.text) N = serviceStatus["CoordinateN"] E = serviceStatus["CoordinateE"] newsensor = SensorData(service["ServerName"], service["ServiceAddress"], N, E) self.sensors[service["ServerName"]] = newsensor self.parent.addSensor(service["ServerName"]) for key in self.sensors: if self.sensors[key].Updated == False: self.parent.removeSensor(self.sensors[key].ServerName) self.sensors.pop(key)
def informationSearch(self, animalName, feeding, notfeeding): servicesArray = ServiceRegistry.getServices("Analytics") s = requests.Session() query = {} if animalName in list(NotificationRegistry.NotificationRegistry. Instance().animalSubscription.keys()): query['animalName'] = animalName if feeding != notfeeding: query["feeding"] = feeding for service in servicesArray: try: r = s.get(service["ServiceAddress"] + "/informationSearch", params=query, stream=True) r.raise_for_status() for line in r.iter_content(1024): yield line except requests.exceptions.RequestException: continue
def getServices(self, serviceName): ServiceRegistry.getServices(serviceName)
args["port"] = int(args["port"]) #connection loop logic = Logic() loop = asyncio.get_event_loop() sensorThread = threading.Thread(target=asyncoThreading, args=( loop, logic, args, )) sensorThread.start() #Consul ServiceRegistry.registry("Analytics", args["name"], port=args["port"], serviceRegistryAddress=args['serviceRegistryAddress']) #chery pie api dbGateway = DBapi.ImageAPI(logic) informationGateway = ImageInformationApi.InfomrationSearchAPI(logic) cherrypy.config.update({ 'server.socket_port': args["port"], 'server.socket_host': '0.0.0.0' }) conf = { '/': { 'request.dispatch': cherrypy.dispatch.MethodDispatcher(), 'tools.response_headers.on': True,
ag.add_argument('-n', "--name", required=False, default="Gateway", help="Name of the sensor") ag.add_argument('-d', "--domain", required=False, default="127.0.0.1:8080", help="Domain of web application") args = vars(ag.parse_args()) if __name__ == "__main__": ServiceRegistry.registry( "Gateway", args["name"], port=args["port"], serviceRegistryAddress=args['serviceRegistryAddress']) #Communciator start communicationLoop = asyncio.get_event_loop() commThread = threading.Thread(target=communicationThread, args=(communicationLoop, args)) commThread.start() #Cherry configuration WebSocketPlugin(cherrypy.engine).subscribe() cherrypy.tools.websocket = WebSocketTool() cherrypy.config.update({ 'server.socket_host': '0.0.0.0', 'tools.sessions.on':
args["NorthCoordiante"] = float(args["NorthCoordiante"]) args["EastCoordinate"] = float(args["EastCoordinate"]) args["skipFirstNFrames"] = int(args["skipFirstNFrames"]) #Main program logic = Logic(args) loop = asyncio.get_event_loop() sensorThread = threading.Thread(target=asyncoThreading, args=( loop, logic, )) sensorThread.start() ServiceRegistry.registry("Sensors", args["name"], port=args["port"], serviceRegistryAddress=args['serviceRegistryAddress']) API = APIGateway.Gateway(logic) cherrypy.config.update({'server.socket_port': args["port"]}) cherrypy.config.update({'server.socket_host': '0.0.0.0'}) conf = { '/': { 'request.dispatch': cherrypy.dispatch.MethodDispatcher(), 'tools.response_headers.on': True, 'tools.response_headers.headers': [('Content-Type', 'text/plain')], } } cherrypy.quickstart(API, '/', conf)
'--name', default="Command1", required=False, help='Name of service') ap.add_argument('-r', '--ServiceRegistry', default="http://127.0.0.1:8761/", required=False, help='Address of service registry') args = vars(ap.parse_args()) args["port"] = int(args["port"]) cherrypy.config.update({'server.socket_port': args["port"]}) cherrypy.config.update({'server.socket_host': '0.0.0.0'}) ServiceRegistry.registry("Command", args["name"], args["port"], args["ServiceRegistry"]) CommandContainerAPI = CommandContainer.CommandContainer() conf = { '/': { 'request.dispatch': cherrypy.dispatch.MethodDispatcher(), 'tools.response_headers.on': True, 'tools.response_headers.headers': [('Content-Type', 'text/plain')] } } command = Command.Command() command.address = "command" command.listOfParametars = ["command"] command.addressOfActuator = "/"