191 lines
6.0 KiB
Python
191 lines
6.0 KiB
Python
from uuid import uuid4
|
|
import uuid
|
|
from flask import jsonify
|
|
import json
|
|
from extensions.rt_service import rt_service as rts
|
|
|
|
# from vrpmdvmonitoringschema import VRPMDV_MonitoringSchema
|
|
# from vrpmdvmonitoring import VRPMDV_Monitoring
|
|
|
|
from vrpmdvmonitoringschema import VRPMDV_MonitoringSchema
|
|
from vrpmdvmonitoring import VRPMDV_Monitoring
|
|
|
|
class VRPMDV_Data:
|
|
|
|
def __init__(self):
|
|
#self.loaded = False
|
|
self.mons:list = []
|
|
self.loaded = self.loadFile()
|
|
self.rtservice = rts.RT_Service()
|
|
|
|
def loadFile(self):
|
|
try:
|
|
with open('./mons.json', 'r') as f:
|
|
fmons = f.read()
|
|
|
|
if not fmons:
|
|
# return no Item
|
|
return True
|
|
else:
|
|
# data = json.loads(fmons)
|
|
schema = VRPMDV_MonitoringSchema(many=True)
|
|
#res = schema.loads(fmons) # data
|
|
self.mons = schema.loads(fmons) # .append(res)
|
|
return True
|
|
except:
|
|
#nothing todo ML we should create a file later the database
|
|
print("file not found")
|
|
|
|
def saveFile(self):
|
|
try:
|
|
with open('./mons.json', 'w') as f:
|
|
schema = VRPMDV_MonitoringSchema()
|
|
result = schema.dumps(self.mons, many=True)
|
|
f.write(result)
|
|
return True
|
|
except:
|
|
#nothing todo
|
|
print("could not write file")
|
|
|
|
def findMon(mon):
|
|
return mon
|
|
|
|
def getMonitorings(self):
|
|
# 1st time read file
|
|
# if not self.loaded :
|
|
# self.loaded = self.loadFile()
|
|
|
|
schema = VRPMDV_MonitoringSchema()
|
|
result = schema.dumps(self.mons, many=True)
|
|
return result
|
|
|
|
def getMonitoring(self, vrpmid):
|
|
try:
|
|
matched_obj = next(x for x in self.mons if str(x.id) == vrpmid)
|
|
except:
|
|
return "no Item found"
|
|
|
|
# we find it and we return it
|
|
schema = VRPMDV_MonitoringSchema()
|
|
return schema.dumps(matched_obj)
|
|
|
|
def setMonitoring(self, id, request):
|
|
try:
|
|
matched_obj = next(x for x in self.mons if str(x.id) == id)
|
|
except:
|
|
return "no Item found"
|
|
|
|
if 'name' in request : matched_obj.name = request['name']
|
|
if 'samplerate' in request : matched_obj.samplerate = request['samplerate']
|
|
if 'sampleperiod' in request : matched_obj.sampleperiod = request['sampleperiod']
|
|
if 'downtime' in request : matched_obj.downtime = request['downtime']
|
|
if 'status' in request : matched_obj.setStatus(request['status'])
|
|
self.saveFile()
|
|
|
|
# we find it and we return it
|
|
schema = VRPMDV_MonitoringSchema()
|
|
return schema.dumps(matched_obj)
|
|
|
|
|
|
# try:
|
|
# matched_obj = next(x for x in self.mons if str(x.id) == vrpmid)
|
|
# matched_obj.name = request.name
|
|
# matched_obj.samplerate = request.samplerate
|
|
# matched_obj.sampleperiod = request.sampleperiod
|
|
# matched_obj.downtime = request.downtime
|
|
# self.saveFile()
|
|
|
|
# except:
|
|
# return "no Item found"
|
|
|
|
|
|
# # we find it and we return it
|
|
# schema = VRPMDV_MonitoringSchema()
|
|
# return schema.dumps(matched_obj)
|
|
|
|
|
|
|
|
def createMonitoring(self, request):
|
|
#mon = VRPMDV_Monitoring(request["name"], request["samplerate"], request["sampleperiod"], request["downtime"], request["owner"])
|
|
id = uuid.uuid4()
|
|
mon = VRPMDV_Monitoring(id, request.name, request.samplerate, request.sampleperiod, request.downtime, request.status, request.owner)
|
|
iscreated = mon.createMonitoring()
|
|
#iscreated = self.rtservice.createMonitoring(str(id))
|
|
|
|
# if not self.loaded :
|
|
# self.loaded = self.loadFile()
|
|
# create monitoring
|
|
self.mons.append(mon)
|
|
#save to file
|
|
self.saveFile()
|
|
#create result Object in json
|
|
schema = VRPMDV_MonitoringSchema()
|
|
return schema.dumps(mon)
|
|
|
|
def deleteMonitoring(self, vrpmid):
|
|
# if not self.loaded :
|
|
# self.loaded = self.loadFile()
|
|
|
|
# find monitoring with uuid
|
|
#result = filter(lambda mon: str(mon.uuid) == vrpmid["uuid"], self.mons)
|
|
try:
|
|
matched_obj = next(x for x in self.mons if str(x.id) == vrpmid)
|
|
except:
|
|
return "no Item found"
|
|
|
|
|
|
# we find it, delete on realtime side and now remove from list
|
|
matched_obj.deleteMonitoring()
|
|
self.mons.remove(matched_obj)
|
|
#save the list
|
|
self.saveFile()
|
|
#make result
|
|
schema = VRPMDV_MonitoringSchema()
|
|
return schema.dump(matched_obj)
|
|
|
|
|
|
def startMonitoring(self, vrpmid):
|
|
# if not self.loaded :
|
|
# self.loaded = self.loadFile()
|
|
|
|
#call the start API of the c++ driver
|
|
try:
|
|
matched_obj = next(x for x in self.mons if str(x.id) == vrpmid)
|
|
except:
|
|
return "no Item found"
|
|
|
|
if matched_obj.startMonitoring() :
|
|
#TODO ML return the state
|
|
return "started"
|
|
|
|
return "created"
|
|
|
|
def stopMonitoring(self, vrpmid):
|
|
# if not self.loaded :
|
|
# self.loaded = self.loadFile()
|
|
|
|
#call the start API of the c++ driver
|
|
try:
|
|
matched_obj = next(x for x in self.mons if str(x.id) == vrpmid)
|
|
except:
|
|
return "no Item found"
|
|
|
|
matched_obj.startMonitoring()
|
|
return matched_obj.monstate.name
|
|
|
|
|
|
def setStatus(self, id, vrpmStatus):
|
|
if 'status' in vrpmStatus :
|
|
matched_obj = self.findMonitoring(id)
|
|
#call the start API of the c++ driver
|
|
matched_obj.setStatus(vrpmStatus['status'])
|
|
return matched_obj.status
|
|
|
|
return ""
|
|
|
|
def findMonitoring(self, id):
|
|
try:
|
|
return next(x for x in self.mons if str(x.id) == id)
|
|
except:
|
|
return "no Item found"
|
|
|