2023-12-12 12:44:34 +01:00
|
|
|
import nagios
|
|
|
|
import socket_protocol
|
|
|
|
# from devdi.topic import topic_by_props
|
|
|
|
|
2023-12-20 07:26:39 +01:00
|
|
|
DID_FOLLOWS_SETPOINT = 'follow_setpoint'
|
2023-12-16 07:12:29 +01:00
|
|
|
DID_BATTERY_LEVEL = 'battery'
|
2023-12-16 08:16:45 +01:00
|
|
|
DID_HEARTBEAT = 'heartbeat'
|
2023-12-31 17:57:24 +01:00
|
|
|
DID_LINKQUALITY = 'linkquality'
|
2023-12-12 12:44:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
class server(socket_protocol.pure_json_protocol):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if 'devices' in kwargs:
|
|
|
|
self.__devices__ = kwargs.pop('devices')
|
|
|
|
socket_protocol.pure_json_protocol.__init__(self, *args, **kwargs)
|
|
|
|
#
|
|
|
|
if not self.__comm_inst__.IS_CLIENT:
|
2023-12-20 07:26:39 +01:00
|
|
|
self.register_callback(socket_protocol.SID_READ_REQUEST, DID_FOLLOWS_SETPOINT, self.device_status)
|
2023-12-16 07:12:29 +01:00
|
|
|
self.register_callback(socket_protocol.SID_READ_REQUEST, DID_BATTERY_LEVEL, self.device_status)
|
2023-12-31 17:57:24 +01:00
|
|
|
self.register_callback(socket_protocol.SID_READ_REQUEST, DID_LINKQUALITY, self.device_status)
|
2023-12-16 08:16:45 +01:00
|
|
|
self.register_callback(socket_protocol.SID_READ_REQUEST, DID_HEARTBEAT, self.device_status)
|
2023-12-12 12:44:34 +01:00
|
|
|
|
2023-12-16 07:12:29 +01:00
|
|
|
def device_status(self, msg):
|
2023-12-12 12:44:34 +01:00
|
|
|
if msg.get_status() == socket_protocol.STATUS_OKAY:
|
|
|
|
try:
|
|
|
|
dev = self.__devices__.get_str(**msg.get_data())
|
|
|
|
except:
|
2023-12-20 07:26:39 +01:00
|
|
|
return socket_protocol.STATUS_CALLBACK_ERROR, {"status": nagios.Nagios.UNKNOWN, "msg": "Device does not exist."}
|
2023-12-12 12:44:34 +01:00
|
|
|
if dev is None:
|
|
|
|
return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, {"status": nagios.Nagios.UNKNOWN, "msg": "Device does not exist."}
|
|
|
|
else:
|
|
|
|
return socket_protocol.STATUS_OKAY, dev.status(msg.get_data_id())
|
|
|
|
else:
|
|
|
|
return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, {"status": nagios.Nagios.UNKNOWN, "msg": "Socket protocol error."}
|