def _ReadGroupValues(ReadGroup): if ReadGroup.groupID == None or ReadGroup.Client.accessToken == None: return _url = RestConstant.READ_GROUP_URI + ReadGroup.groupID _status_code, _hearders, _text = ReadGroup.Client._Http.syncHttpAPI( httpMethod=RestConstant.GET, function_uri=_url, payload=None) if _status_code == 200: _response = json.loads(_text) if _response.get('id') == ReadGroup.groupID: ReadGroup.valves = _response.get('variables') Result = dict() for _item in ReadGroup.valves: Result[_item['path']] = _item['value'] return Result elif _status_code == 404: # groupID不正确 ReadGroup.groupID = None _response = json.loads(_text) _reason = _response.get('error').get('details')[0].get('reason') logging.error(_reason) raise RESTException(_reason) elif _status_code == 401: # 令牌不正确loads ReadGroup.Client.accessToken = None _reason = _hearders.get('WWW-Authenticate') logging.error(_reason) raise RESTException( _reason) # Bearer realm="pxcapi", error="invalid_token" elif _status_code == 400: # 指令不正确 _response = json.loads(_text) _reason = _response.get('error').get('details')[0].get('reason') logging.error(_reason) raise RESTException(_reason) elif _status_code == 410: # sessionID不正确 logging.error('invalidSessionID') raise RESTException('invalidSessionID')
def _ReportGroups(self): if self.Client.accessToken == None: return _status_code, _hearders, _text = self.Client._Http.syncHttpAPI( httpMethod=RestConstant.GET, function_uri=RestConstant.REPORT_GROUP_URI, payload=None) if _status_code == 200: _response = json.loads(_text).get('groups') self.Client.groupReportResult = _response return _response elif _status_code == 401: # 令牌不正确loads self.accessToken = None _reason = _hearders.get('WWW-Authenticate') logging.error(_reason) raise RESTException( _reason) # Bearer realm="pxcapi", error="invalid_token" elif _status_code == 400: # 指令不正确 _response = json.loads(_text) _reason = _response.get('error').get('details')[0].get('reason') logging.error(_reason) raise RESTException(_reason) elif _status_code == 410: # sessionID不正确 logging.error('invalidSessionID') raise RESTException('invalidSessionID')
def _ReadVariables(self, variableNames, pathPrefix=RestConstant.PATHPREFIX): if self.Client.accessToken == None: return _data = { 'pathPrefix': pathPrefix, 'paths': ','.join(variableNames) } _status_code, _hearders, _text = self.Client._Http.syncHttpAPI(httpMethod=RestConstant.GET, function_uri=RestConstant.READ_VARIABLES_GET_URI, payload=None, Params=_data) if _status_code == 200: _response = json.loads(_text) tmp = _response.get('variables') _ResultDict = dict() for _item in tmp: _varFullName = _item.get('path') c = _varFullName.index('/') _ResultDict[_varFullName[c + 1:]] = _item.get('value') return _ResultDict elif _status_code == 401: # 令牌不正确 self.Client.accessToken = None _reason = _hearders.get('WWW-Authenticate') raise RESTException(_reason) # Bearer realm="pxcapi", error="invalid_token" elif _status_code == 410: # sessionID不正确 logging.error('invalidSessionID') raise RESTException('invalidSessionID')
def _RegisterGroup(self, variableNames, pathPrefix=RestConstant.PATHPREFIX): if self.Client.accessToken == None: return _payload = {'pathPrefix': pathPrefix, 'paths': variableNames} _status_code, _hearders, _text = self.Client._Http.syncHttpAPI( httpMethod=RestConstant.POST, function_uri=RestConstant.REGISTER_GROUP_URI, payload=json.dumps(_payload)) if _status_code == 201: # Creat success _response = json.loads(_text) if _response.get('variables'): # return ReadGroup(groupID=_response.get('id'), vars=_response.get('variables'), clientInfo=clientInfo) logging.info('Group ID : {}'.format(_response.get('id'))) return _response.get('id'), _response.get('variables') elif _status_code == 404: # 变量名不正确 _response = json.loads(_text) _reason = _response.get('error').get('details')[0].get('reason') logging.error(str(_reason)) raise RESTException(_reason) elif _status_code == 401: # 令牌不正确 self.accessToken = None _reason = _hearders.get('WWW-Authenticate') logging.error(str(_reason)) raise RESTException( _reason) # Bearer realm="pxcapi", error="invalid_token" elif _status_code == 410: # sessionID不正确 logging.error('invalidSessionID') raise RESTException('invalidSessionID')
def _maintainSessionID(self): logging.debug('Maintain SessionID ...') _uri = RestConstant.MAINTAIN_SESSION_URI + self.sessionID for i in range(3): _status_code, _hearders, _text = self.client._Http.syncHttpAPI( httpMethod=RestConstant.POST, function_uri=_uri, payload=None) if _status_code == 200: return if _status_code == 410: # ID失效 logging.error('invalidSessionID') raise RESTException('invalidSessionID') _r = json.loads(_text) logging.error('Can not maintain session ID' + str(_r)) raise RESTException('Can not maintain session ID' + str(_r))
def _Login(self): logging.debug('logging ...') for i in range(3): Authentication.__getAuthToken(self) _payload = { 'code': self.authToken, 'grant_type': 'authorization_code', 'username': self.client.PLCnUserName, 'password': self.client.PLCnPasswd } _status_code, _hearders, _text = self.client._Http.syncHttpAPI( httpMethod=RestConstant.POST, function_uri=RestConstant.ACCESS_TOCKEN_URI, payload=json.dumps(_payload)) if _status_code == 200: _response = json.loads(_text) if _response['state'] == self.authToken: self.client.accessToken = _response['access_token'] return elif _status_code == 401: _response = json.loads(_text) _reason = _response['error']['details'][0]['reason'] if _reason == 'wrongPassword': logging.error('Wrong password') raise RESTException('Wrong password') elif _status_code == 410: self.client._Session._createSessionID()
def asyncStop(self): if self._asynThread: self._asynThread.disbleThread = True self._asynThread.join() self._asynThread = None self.Client._Http.NewSession() else: logging.error('No asyncTask instance , Have you start ?') raise RESTException('No asyncTask instance')
def _getApiInfo(self): logging.debug('trying to get api information ...') for i in range(3): _status_code, _hearders, _text = self.client.loop.run_until_complete( self.client._Http.asyncHttpAPI(httpMethod=RestConstant.GET, function_uri=RestConstant.SERVICE_DESCRIPTION_URI, payload=None)) if _status_code == 200: _response = json.loads(_text) if 'apiVersion' in _response and 'version' in _response: self.apiversion.append(_response['apiVersion']) self.apiversion.append(_response['version']) logging.info('Get PLCnext API version : {}'.format(self.apiversion[1])) return else: logging.error('Can not recognize API') raise RESTException('Can not recognize API') # 其他状态字以后再议 logging.error('Status return code is unexcepted') raise RESTException('Status return code is unexcepted')
def _SendOUT(self, variables, pathPrefix=RestConstant.PATHPREFIX): _data = {'pathPrefix': pathPrefix, 'variables': variables} _status_code, _hearders, _text = self.Client._Http.syncHttpAPI( httpMethod=RestConstant.PUT, function_uri=RestConstant.WRITE_VARIABLES_PUT_URI, payload=json.dumps(_data)) if _status_code == 200: _response = json.loads(_text) return _response.get('variables') elif _status_code == 401: # 令牌不正确 self.Client.accessToken = None _reason = _hearders.get('WWW-Authenticate') raise RESTException( _reason) # Bearer realm="pxcapi", error="invalid_token" elif _status_code == 410: # sessionID不正确 logging.error('invalidSessionID') raise RESTException('invalidSessionID')
def asyncStart( self, poolSize=100, ): self.Client._Http.mount(pool_connections=5, pool_maxsize=poolSize + 20) if self._asynThread == None: self._asynThread = AsyncRead(self, 2, 'AsyncRead_1', poolSize) if 'ESM_DATA.ESM_INFOS[1].TICK_COUNT' in self._varName_BACKUP: if self._asynThread.isAlive() == False: self._asynThread.start() else: logging.error( "Must have 'ESM_DATA.ESM_INFOS[1].TICK_COUNT' in this group !" ) raise RESTException( "Must have 'ESM_DATA.ESM_INFOS[1].TICK_COUNT' in this group !" )
def _createSessionID(self): logging.debug('trying to creat session id ...') for i in range(3): _randint = random.randint(100, 999) _payload = 'stationID=' + str(_randint) _status_code, _hearders, _text =self.client.loop.run_until_complete (self.client._Http.asyncHttpAPI(httpMethod=RestConstant.POST, function_uri=RestConstant.CREATE_SESSION_URI, payload=_payload)) if _status_code == 201: _response = json.loads(_text) if 'sessionID' in _response: self.sessionID = _response['sessionID'] logging.info('Get session ID : {}'.format(self.sessionID)) return if _status_code == 409: # 冲突 pass logging.error('Can not creat session ID') raise RESTException('Can not creat session ID')