def _processDeviceList(self, deviceList): ''' ''' ip_addr = "" device_name = "" resultCode, resultMessage = True, "" for host in deviceList: device = DeviceModel.find_by_name( host) if DeviceModel.find_by_name( host) is not None else DeviceModel.find_by_ipAddress(host) if not device: if self.validIP(host): try: device_name = socket.getfqdn(host) except: continue ip_addr = host else: device_name = host try: ip_addr = socket.gethostbyname(host) except: continue #except: ip_addr = host device = DeviceModel(name=device_name, ipAddress=ip_addr, deviceClass=1) try: device.save_to_db(commit=False) except: resultCode, resultMessage = False, "An error occurred inserting the device." self._hosts.append(device) return resultCode, resultMessage
def post(self, name): if DeviceModel.find_by_name(name): return { 'message': f'Bad Request: A device with name {name} already exists. ' f'No two duplicate devices can be created.' }, 400 device = DeviceModel(name) device.save_to_db() return device.json(), 201
def put(self, name): data = Device.parser.parse_args() device = DeviceModel.find_by_name(name) if device is None: device = DeviceModel(name) device.save_to_db() return device.json(), 201 else: device.state = data['state'] device.save_to_db() return device.json(), 201
def post(self): data = Device.arguments_device.parse_args() if DeviceModel.find_by_mac(data['mac']): return {"message": "The device {} already exists."}, 400 device = DeviceModel(data['mac'], "ON") try: device.save() except: return { "message": 'An internal error ocurred trying to create a new device.' }, 500 return device.json()
def _processDeviceList(self, deviceList): ''' ''' resultCode, resultMessage = True, "" for device_name in deviceList: device = DeviceModel.find_by_name(device_name) if not device: device = DeviceModel(device_name, deviceClass=1) try: device.save_to_db(commit=True) except: resultCode, resultMessage = False, "An error occurred inserting the device." self._hosts.append(device) return resultCode, resultMessage
def buildCommand(self): for deviceName in self.job.parameters["hostsConfiguration"]: device = DeviceModel.find_by_name(deviceName) if not device: device = DeviceModel(deviceName, deviceClass=1) try: device.save_to_db(commit=False) except: resultCode, resultMessage = False, "An error occurred inserting the device." command = Environment().from_string( self.job.parameters["remoteCommand"]).render( self.job.parameters["hostsConfiguration"][deviceName]) task = TaskModel(self.job.id, device.id) task.parameters["command"] = command task.save_to_db(commit=False) return command
def put(self, devId): data = Device.parser.parse_args() device = DeviceModel.find_by_name(devId) if device is None: device = DeviceModel(devId, data['power'], data['voltage'], data['current']) else: device.power = data['power'] device.voltage = data['voltage'] device.current = data['current'] device.save_to_db() return device.json()
def read_configurations(): for file in os.listdir(conf_dir): if file.endswith(".lircd.conf"): f = open(os.path.join(conf_dir, file), 'r') device = DeviceModel(name = file[:-11]) stop = False start = False for row in f: if re.search("end codes", row): stop = True if start and not stop: key = KeyModel(re.findall("KEY_\w+", row)[0]) device.insert_key(key) if re.search("begin codes", row): start = True f.close() devices.append(device)
def put(self): data = Device.arguments_device.parse_args() device_find = DeviceModel.find_by_mac(data['mac']) if device_find: device_find.update(data['status']) device_find.save() return device_find.json(), 200 device = DeviceModel(**data) try: device.save() except: return { 'message': 'An internal error ocurred trying to save device' }, 500 return device, 201
def post(self): parser = reqparse.RequestParser() parser.add_argument('device_id', type=str, help='Every device must have unique device id', required=True, trim=True) parser.add_argument('name', type=str, help='Device name is missing', trim=True, required=True) args = parser.parse_args() if DeviceModel.find_by_device_id(args['device_id']): return {"message": "Device name must be unique"}, 400 device = DeviceModel(args['name'], args['device_id']) device.save_to_db() return device.json(), 200
def post(self, devId): if DeviceModel.find_by_name(devId): return { 'message': "A Device with devId '{}' already exists.".format(devId) }, 400 data = Device.parser.parse_args() device = DeviceModel(devId, data['power'], data['voltage'], data['current']) try: device.save_to_db() except: return { "message": "An error occurred inserting the device data." }, 500 return device.json(), 201
def post(self, name=None): data = json.loads(request.data) print(data) cipher_suite = Fernet(KEY) if data.get('login'): data['login'] = cipher_suite.encrypt( data.get('login').encode('ascii')).decode('ascii') if data.get('password'): data['password'] = cipher_suite.encrypt( data.get('password').encode('ascii')).decode('ascii') name = data.get("name", name) if DeviceModel.find_by_name(name): return { 'error': "A device with name : {}, already exists.".format(name) }, 400 device = DeviceModel(**data) #try: device.save_to_db() #except: #return {"error": "An error occurred creating the device."}, 500 return device.json(), 201