def set_outputs(self, actions: Dict[int, ACTION]) -> None: """ Set state of multiple outputs at once >>> n.set_outputs({1: n.ACTION.ON, 2:n.ACTION.OFF}) """ # TODO verify if socket id's are in range if self._write_access: self._set_outputs(actions) else: raise AuthError("cannot write, without write access")
def _get(self) -> dict: try: response = requests.get(self._url, auth=requests.auth.HTTPBasicAuth( self._user, self._pass), verify=self._verify) except requests.exceptions.SSLError: raise AuthError("Invalid certificate") return self._parse_response(response)
def _post(self, body: dict) -> dict: try: response = requests.post( self._url, data=json.dumps(body), auth=requests.auth.HTTPBasicAuth(self._user, self._pass), verify=self._verify, ) except requests.exceptions.SSLError: raise AuthError("Invalid certificate") return self._parse_response(response)
def _parse_response(response: requests.Response) -> dict: """ Parse JSON response according to https://www.netio-products.com/files/NETIO-M2M-API-Protocol-JSON.pdf """ try: rj = response.json() except ValueError: raise CommunicationError("Response does not contain valid json") if response.status_code == 400: raise CommunicationError('Control command syntax error') if response.status_code == 401: raise AuthError('Invalid Username or Password') if response.status_code == 403: raise AuthError('Insufficient permissions to write') if not response.ok: raise CommunicationError("Communication with device failed") return rj
def __init__(self, url, auth_r=None, auth_rw=None, verify=None): self._url = url self._verify = verify # read-write can do read, so we don't need read-only permission if auth_rw: self._user = auth_rw[0] self._pass = auth_rw[1] self._write_access = True elif auth_r: self._user = auth_r[0] self._pass = auth_r[1] else: raise AuthError("No auth provided.") # request information about the Device r_json = self._get() self.NumOutputs = r_json["Agent"]["NumOutputs"] self.DeviceName = r_json["Agent"]["DeviceName"] self.SerialNumber = r_json["Agent"]["SerialNumber"]
def __init__(self, url, auth_r=None, auth_rw=None, verify=None, skip_init=False): self._url = url self._verify = verify # read-write can do read, so we don't need read-only permission if auth_rw: self._user = auth_rw[0] self._pass = auth_rw[1] self._write_access = True elif auth_r: self._user = auth_r[0] self._pass = auth_r[1] else: raise AuthError("No auth provided.") if not skip_init: self.init()
def set_output(self, id: int, action: ACTION = ACTION.NOCHANGE) -> None: if self._write_access: self._set_state(id, action) else: raise AuthError("cannot write, without write access")