mirror of
https://github.com/thingsboard/thingsboard-gateway
synced 2025-10-26 22:31:42 +08:00
SNMP Connector improvements
This commit is contained in:
@@ -65,6 +65,11 @@ connectors:
|
||||
# configuration: rest.json
|
||||
#
|
||||
# -
|
||||
# name: SNMP Connector
|
||||
# type: snmp
|
||||
# configuration: snmp.json
|
||||
#
|
||||
# -
|
||||
# name: Custom Serial Connector
|
||||
# type: serial
|
||||
# configuration: custom_serial.json
|
||||
|
||||
@@ -27,6 +27,8 @@ except ImportError:
|
||||
TBUtility.install_package("puresnmp")
|
||||
import puresnmp
|
||||
|
||||
from puresnmp.exc import Timeout as SNMPTimeoutException
|
||||
|
||||
|
||||
class SNMPConnector(Connector, Thread):
|
||||
def __init__(self, gateway, config, connector_type):
|
||||
@@ -100,6 +102,7 @@ class SNMPConnector(Connector, Thread):
|
||||
"timeout": device.get("timeout", 6),
|
||||
"community": device["community"],
|
||||
}
|
||||
converted_data = {}
|
||||
for datatype in self.__datatypes:
|
||||
for datatype_config in device[datatype]:
|
||||
try:
|
||||
@@ -113,10 +116,15 @@ class SNMPConnector(Connector, Thread):
|
||||
if method not in self.__methods:
|
||||
log.error("Unknown method: %s, configuration is: %r", method, datatype_config)
|
||||
response = self.__process_methods(method, common_parameters, datatype_config)
|
||||
converted_data = device["uplink_converter"].convert((datatype, datatype_config), response)
|
||||
self.collect_statistic_and_send(self.get_name(), converted_data)
|
||||
converted_data.update(**device["uplink_converter"].convert((datatype, datatype_config), response))
|
||||
except SNMPTimeoutException:
|
||||
log.error("Timeout exception on connection to device \"%s\" with ip: \"%s\"", device["deviceName"], device["ip"])
|
||||
return
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
if isinstance(converted_data, dict) and (converted_data.get("attributes") or converted_data.get("telemetry")):
|
||||
self.collect_statistic_and_send(self.get_name(), converted_data)
|
||||
|
||||
|
||||
def __process_methods(self, method, common_parameters, datatype_config):
|
||||
response = None
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
from simplejson import dumps
|
||||
|
||||
from thingsboard_gateway.connectors.converter import Converter, log
|
||||
|
||||
@@ -37,6 +36,10 @@ class SNMPUplinkConverter(Converter):
|
||||
for item in data:
|
||||
res.update(**item)
|
||||
result[config[0]].append({config[1]["key"]: {str(k): str(v) for k, v in res.items()}})
|
||||
elif isinstance(data, str):
|
||||
result[config[0]].append({config[1]["key"]: data})
|
||||
elif isinstance(data, bytes):
|
||||
result[config[0]].append({config[1]["key"]: data.decode("UTF-8")})
|
||||
log.debug(result)
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
|
||||
Reference in New Issue
Block a user