Beispiel #1
0
 async def _init_device(self) -> None:
     """Check if the device sends counters."""
     oids = tuple(self._iterate_oids(OIDS.values()))
     try:
         request_args = [
             self._snmp_engine,
             hlapi.CommunityData("public", mpModel=0),
             hlapi.UdpTransportTarget((self._host, self._port),
                                      timeout=2,
                                      retries=10),
             hlapi.ContextData(),
         ]
     except PySnmpError as err:
         raise ConnectionError(err) from err
     errindication, errstatus, errindex, _ = await hlapi.getCmd(
         *request_args, *oids)
     if errindication:
         raise SnmpError(errindication)
     if errstatus:
         oids = tuple(self._iterate_oids(OIDS_WITHOUT_COUNTERS.values()))
         errindication, errstatus, errindex, _ = await hlapi.getCmd(
             *request_args, *oids)
         if errindication:
             raise SnmpError(errindication)
         if errstatus:
             raise SnmpError(f"{errstatus}, {errindex}")
         _LOGGER.debug("The printer %s doesn't send 'counters'", self._host)
         self._counters = False
         self._oids = oids
     self._need_init = False
Beispiel #2
0
    async def _get_data(self):
        """Retreive data from printer."""
        raw_data = {}
        snmp_engine = hlapi.SnmpEngine()

        try:
            request_args = [
                snmp_engine,
                hlapi.CommunityData("public", mpModel=0),
                hlapi.UdpTransportTarget((self._host, self._port),
                                         timeout=2,
                                         retries=10),
                hlapi.ContextData(),
            ]
            errindication, errstatus, errindex, restable = await hlapi.getCmd(
                *request_args, *self._oids)
            # unconfigure SNMP engine
            lcd.unconfigure(snmp_engine, None)
        except PySnmpError as error:
            self.data = {}
            raise ConnectionError(error)
        if errindication:
            self.data = {}
            raise SnmpError(errindication)
        if errstatus:
            self.data = {}
            raise SnmpError(f"{errstatus}, {errindex}")
        for resrow in restable:
            if str(resrow[0]) in OIDS_HEX:
                # asOctet gives bytes data b'\x00\x01\x04\x00\x00\x03\xf6\xff'
                temp = resrow[-1].asOctets()
                # convert to string without checksum FF at the end, gives 000104000003f6
                temp = "".join(["%.2x" % x for x in temp])[0:-2]
                # split to 14 digits words in list, gives ['000104000003f6']
                temp = [
                    temp[ind:ind + 2 * self._split]
                    for ind in range(0, len(temp), 2 * self._split)
                ]
                # map sensors names to OIDs
                raw_data[str(resrow[0])] = temp
            else:
                raw_data[str(resrow[0])] = str(resrow[-1])
        return raw_data
Beispiel #3
0
    async def _get_data(self):
        """Retreive data from printer."""
        raw_data = {}

        if not self._snmp_engine:
            self._snmp_engine = hlapi.SnmpEngine()

        try:
            request_args = [
                self._snmp_engine,
                hlapi.CommunityData("public", mpModel=0),
                hlapi.UdpTransportTarget(
                    (self._host, self._port), timeout=2, retries=10
                ),
                hlapi.ContextData(),
            ]
            errindication, errstatus, errindex, restable = await hlapi.getCmd(
                *request_args, *self._oids
            )
        except PySnmpError as err:
            self.data = {}
            raise ConnectionError(err) from err
        if errindication:
            self.data = {}
            raise SnmpError(errindication)
        if errstatus:
            self.data = {}
            raise SnmpError(f"{errstatus}, {errindex}")
        for resrow in restable:
            if str(resrow[0]) in OIDS_HEX:
                # asOctet gives bytes data b'c\x01\x04\x00\x00\x00\x01\x11\x01\x04\x00\
                # x00\x05,A\x01\x04\x00\x00"\xc41\x01\x04\x00\x00\x00\x01o\x01\x04\x00\
                # x00\x19\x00\x81\x01\x04\x00\x00\x00F\x86\x01\x04\x00\x00\x00\n\xff'
                temp = resrow[-1].asOctets()
                # convert to string without checksum FF at the end, gives
                # '630104000000011101040000052c410104000022c4310104000000016f01040000190
                #  0810104000000468601040000000a'
                temp = "".join(["%.2x" % x for x in temp])[0:-2]
                # split to 14 digits words in list, gives ['63010400000001',
                # '1101040000052c', '410104000022c4', '31010400000001',
                # '6f010400001900', '81010400000046', '8601040000000a']
                temp = [temp[ind : ind + 14] for ind in range(0, len(temp), 14)]
                # map sensors names to OIDs
                raw_data[str(resrow[0])] = temp
            else:
                raw_data[str(resrow[0])] = str(resrow[-1])
        # for legacy printers
        for resrow in restable:
            if str(resrow[0]) == OIDS[ATTR_MAINTENANCE]:
                # asOctet gives bytes data
                temp = resrow[-1].asOctets()
                # convert to string without checksum FF at the end, gives
                # 'a101020414a201020c14a301020614a401020b14'
                temp = "".join(["%.2x" % x for x in temp])[0:-2]
                if self._legacy_printer(temp):
                    self._legacy = True
                    # split to 10 digits words in list, gives ['a101020414',
                    # 'a201020c14', 'a301020614', 'a401020b14']
                    temp = [temp[ind : ind + 10] for ind in range(0, len(temp), 10)]
                    # map sensors names to OIDs
                    raw_data[str(resrow[0])] = temp
                    break
        return raw_data