def initNetworkDevice(): #get all devices list devicedbMgmt = DevicesMgt(None,dbfile) #deviceList = devicedbMgmt.show_devices() #Init Connect device deviceList = devicedbMgmt.get_deviceinfos() devicedbMgmt.record_all_deviceid() driverListFinal = [] for elem in deviceList: productType = elem.get('productType') version = elem.get('version') passwd = elem.get('passwd') passwd=DecodePassword(passwd) elem['passwd'] = passwd elem["id"] = str(elem["id"]) otherconnect = elem.get('subdevices') newotherconnect = [] if (otherconnect != None): for subelem in otherconnect: passwd=subelem.get('passwd') passwd=DecodePassword(passwd) newotherconnect.append({'id': subelem.get('id'), 'ip': subelem.get('ip'), 'port': subelem.get('port'), 'username':subelem.get('username'), 'passwd':passwd}) elem["subdevices"]= newotherconnect drivername = getDrivername(productType, version) elem['driver']= drivername driverListFinal.append(elem) network_devices._channelnum = g_channelnum network_devices._opsesn = g_checkesn network_devices.initConnectDevice(driverListFinal) network_devices.threadstart()
def subdevices_put(db, deviceid, sid): body= request.body ddict = body.read() try: deviceinfo = json.loads(ddict)['device'] ip = deviceinfo['ip'] port = deviceinfo['port'] username = deviceinfo['username'] passwd = deviceinfo['passwd'] if (ip == '' or username ==''or port==''): response.status = HTTP_SERVER_ERROR return dict(error='must have the following data (ip, port, username)') device_db = DevicesMgt(db) bret = device_db.update_subdevice( sid, ip, port, username, passwd) if (not bret): response.status = HTTP_SERVER_ERROR return dict(error='can not find the subdevice , detail: id=%s and ip=%s' %(sid, ip)) else: response.status = HTTP_RESPONSE_OK network_devices.modifySubDevice(deviceid, sid, deviceinfo) return dict(ok=True) except Exception as e: response.status = HTTP_SERVER_ERROR return dict(error='%s' % e)
def subdevices_post(db, deviceid): body= request.body ddict = body.read() try: deviceinfo = json.loads(ddict)['device'] ip = deviceinfo['ip'] port = deviceinfo.get('port') username = deviceinfo['username'] passwd = deviceinfo['passwd'] if (port is None ): port = 22 if (ip == '' or username ==''or port==''): response.status = HTTP_SERVER_ERROR return dict(error='must have the following data (ip, port, username)') device_db = DevicesMgt(db) subid = device_db.add_subdevices(deviceid, ip, port, username, passwd ) if subid == 0: response.status = HTTP_SERVER_ERROR return dict(error=('device id %s is not exist'%deviceid)) deviceinfo["id"] = subid network_devices.addSubDevice(deviceid, deviceinfo) return dict(id=subid) except Exception as e: response.status = HTTP_SERVER_ERROR return dict(error=e.message)
def subdevices_delete(db, deviceid, sid): try: devicedb = DevicesMgt(db) if (not devicedb.delete_subdevicebyid(sid)): response.status = HTTP_RESOURCE_EMPTY else: network_devices.deleteSubDevice(deviceid, sid) response.status = HTTP_RESPONSE_OK return dict(ok=True) except Exception, e: response.status = HTTP_SERVER_ERROR return dict(error=e.message)
def devices_config_put(db): body= request.body ddict = body.read() try: deviceinfo = json.loads(ddict)['device'] device_db = DevicesMgt(db) devicename = deviceinfo['devicename'] deviceid = str(deviceinfo['id']) ip = deviceinfo['ip'] username = deviceinfo['username'] passwd = deviceinfo['passwd'] productType = deviceinfo['productType'] version = deviceinfo['version'] port = deviceinfo.get('port') subdevices = deviceinfo.get('subdevices') if (ip == '' or username =='' or productType=='' or devicename==''): response.status = HTTP_SERVER_ERROR return dict(error='must have the following data (ip, devicename, username, productType)') if (subdevices is not None): for sudevice in subdevices: if (sudevice.get('id') == '' or sudevice.get('id') is None or sudevice.get('ip') == '' or sudevice.get('ip') is None or sudevice.get('username') =='' or sudevice.get('username') is None or sudevice.get('port')=='' or sudevice.get('port') is None): response.status = HTTP_SERVER_ERROR return dict(error='subdevices must have the following data (ip, port, username)') drivername = getDrivername(productType, version) if (drivername == None or drivername == ''): response.status = HTTP_SERVER_ERROR return dict(error='can not find the driver , detail: producttype=%s and version=%s' %(productType, version)) # update device ret = device_db.update_device(deviceid, devicename, ip, port, username, passwd, productType, version) if ret == False: response.status = HTTP_SERVER_ERROR return dict(error='id %s , device %s does not exist or devicename is repeat' % (deviceid, devicename)) response.status = HTTP_RESPONSE_OK deviceinfo['driver']= drivername deviceinfo['subdevices'] = None network_devices.modifyDevice(deviceinfo) # update sudevice if (subdevices is not None): for sudevice in subdevices: sid = sudevice.get('id') ip = sudevice.get('ip') username = sudevice.get('username') passwd = sudevice.get('passwd') port = sudevice.get('port') bret = device_db.update_subdevice( sid, ip, port, username, passwd) if (not bret): response.status = HTTP_SERVER_ERROR return dict(error='can not find the subdevice , detail: id=%s and ip=%s' %(sid, ip)) else: response.status = HTTP_RESPONSE_OK network_devices.modifySubDevice(deviceid, sid, sudevice) return dict(ok=True) except Exception as e: response.status = HTTP_SERVER_ERROR return dict(error='%s' % e)
def devices_config_post(db): body= request.body ddict = body.read() try: deviceinfo = json.loads(ddict)['device'] device_db = DevicesMgt(db) devicename = deviceinfo['devicename'] username = deviceinfo['username'] ip = deviceinfo['ip'] passwd = deviceinfo['passwd'] productType = deviceinfo['productType'] version = deviceinfo['version'] port = deviceinfo.get('port') deviceiid = deviceinfo.get('id') subdevices = deviceinfo.get('subdevices') if (deviceiid is not None and (not str(deviceiid).isdigit() or int(deviceiid) <= 0 or int(deviceiid) > sys.maxint )): response.status = HTTP_SERVER_ERROR return dict(error='The attribute ID is digital, greater than 0, and less than %s'% sys.maxint) if (ip == '' or username =='' or productType=='' or devicename==''): response.status = HTTP_SERVER_ERROR return dict(error='must have the following data (ip, devicename, username, productType)') drivername = getDrivername(productType, version) if (drivername == None or drivername == ''): response.status = HTTP_SERVER_ERROR return dict(error='can not find the driver , detail: producttype=%s and version=%s' %(productType, version)) #dectect if have sub devices if (subdevices is not None): for sudevice in subdevices: if (sudevice.get('ip') == '' or sudevice.get('ip') is None or sudevice.get('username') =='' or sudevice.get('username') is None or sudevice.get('port')=='' or sudevice.get('port') is None): response.status = HTTP_SERVER_ERROR return dict(error='subdevices must have the following data (ip, port, username)') if (port == None): port = '22' deviceinfo['port']=port #add devices deviceid = device_db.add_devices(devicename, ip, str(port), username, passwd, productType, version, deviceiid) if deviceid == 0: response.status = HTTP_SERVER_ERROR return dict(error='device %s or id already exists' % devicename) deviceinfo["id"] = deviceid deviceinfo['driver']= drivername deviceinfo['subdevices']= [] network_devices.addDevice(deviceinfo) #add subdevices if (subdevices is not None): for sudevice in subdevices: ip = sudevice.get('ip') username = sudevice.get('username') passwd = sudevice.get('passwd') port = sudevice.get('port') subid = device_db.add_subdevices(deviceid, ip, port, username, passwd ) subdeviceinfo = {} subdeviceinfo['ip'] = ip subdeviceinfo['port'] = port subdeviceinfo['username'] = username subdeviceinfo['passwd'] = passwd subdeviceinfo["id"] = subid network_devices.addSubDevice(str(deviceid), subdeviceinfo) return dict(id=deviceid) except Exception, e: response.status = HTTP_SERVER_ERROR return dict(error=e.message)