def testTypeConversionsChar(self): ''' Test the conversion of all possible IAS data types ''' msg = JsonMsg("MPoint-IDC", 'X', IASType.CHAR,operationalMode=OperationalMode.CLOSING) jStr = msg.dumps() print(jStr) fromJString = JsonMsg.parse(jStr) self.assertEqual(fromJString.mPointID, "MPoint-IDC") self.assertEqual(fromJString.value, 'X') self.assertEqual(fromJString.valueType,IASType.CHAR) self.assertEqual(fromJString.operationalMode, OperationalMode.CLOSING)
def testTypeConversionsAlarm(self): ''' Test the conversion of all possible IAS data types ''' msg = JsonMsg("MPoint-IDC", Alarm.SET_MEDIUM, IASType.ALARM,operationalMode=OperationalMode.SHUTTEDDOWN) jStr = msg.dumps() print(jStr) fromJString = JsonMsg.parse(jStr) self.assertEqual(fromJString.mPointID, "MPoint-IDC") self.assertEqual(fromJString.value, Alarm.SET_MEDIUM) self.assertEqual(fromJString.valueType,IASType.ALARM) self.assertEqual(fromJString.operationalMode, OperationalMode.SHUTTEDDOWN)
def testTypeConversionsString(self): ''' Test the conversion of all possible IAS data types ''' msg = JsonMsg("MPoint-IDS", "A Test String", IASType.STRING,operationalMode=OperationalMode.OPERATIONAL) jStr = msg.dumps() print(jStr) fromJString = JsonMsg.parse(jStr) self.assertEqual(fromJString.mPointID, "MPoint-IDS") self.assertEqual(fromJString.value, "A Test String") self.assertEqual(fromJString.valueType,IASType.STRING) self.assertEqual(fromJString.operationalMode, OperationalMode.OPERATIONAL)
def testTypeConversionsBool(self): ''' Test the conversion of all possible IAS data types ''' msg = JsonMsg("MPoint-IDB", True, IASType.BOOLEAN,operationalMode=OperationalMode.INITIALIZATION) jStr = msg.dumps() print(jStr) fromJString = JsonMsg.parse(jStr) self.assertEqual(fromJString.mPointID, "MPoint-IDB") self.assertEqual(fromJString.value, True) self.assertEqual(fromJString.valueType,IASType.BOOLEAN) self.assertEqual(fromJString.operationalMode, OperationalMode.INITIALIZATION)
def testTypeConversionsArrayDoubles(self): msg = JsonMsg("MPoint-ARRAY-DOUBLES", [0, 0.123, -987.321, -1], IASType.ARRAYOFDOUBLES, operationalMode=OperationalMode.OPERATIONAL) jStr = msg.dumps() print(jStr) fromJString = JsonMsg.parse(jStr) self.assertEqual(fromJString.mPointID, "MPoint-ARRAY-DOUBLES") self.assertEqual(fromJString.value, [0, 0.123, -987.321, -1]) self.assertEqual(fromJString.valueType, IASType.ARRAYOFDOUBLES) self.assertEqual(fromJString.operationalMode, OperationalMode.OPERATIONAL)
def testTypeConversionsArrayLongs(self): msg = JsonMsg("MPoint-ARRAY-LONGS", [1, 2, 3, 4, 5, -1], IASType.ARRAYOFLONGS, operationalMode=OperationalMode.OPERATIONAL) jStr = msg.dumps() print(jStr) fromJString = JsonMsg.parse(jStr) self.assertEqual(fromJString.mPointID, "MPoint-ARRAY-LONGS") self.assertEqual(fromJString.value, [1, 2, 3, 4, 5, -1]) self.assertEqual(fromJString.valueType, IASType.ARRAYOFLONGS) self.assertEqual(fromJString.operationalMode, OperationalMode.OPERATIONAL)
def testTypeConversionsLong(self): ''' Test the conversion of all possible IAS data types ''' tStamp = datetime.datetime.utcnow() msg = JsonMsg("MPoint-IDL", 97, IASType.LONG,tStamp,OperationalMode.DEGRADED) jStr = msg.dumps() print(jStr) fromJString = JsonMsg.parse(jStr) self.assertEqual(fromJString.mPointID, "MPoint-IDL") self.assertEqual(fromJString.value, 97) self.assertEqual(fromJString.valueType,IASType.LONG) self.assertEqual(fromJString.operationalMode, OperationalMode.DEGRADED)
def testTypeConversionsDouble(self): ''' Test the conversion of all possible IAS data types ''' tStamp = datetime.datetime.utcnow() msg = JsonMsg("MPoint-ID", 2.3, IASType.DOUBLE,tStamp,OperationalMode.CLOSING) jStr = msg.dumps() print(jStr) fromJString = JsonMsg.parse(jStr) self.assertEqual(fromJString.mPointID, "MPoint-ID") self.assertEqual(fromJString.value, 2.3) self.assertEqual(fromJString.valueType,IASType.DOUBLE) self.assertEqual(fromJString.operationalMode, OperationalMode.CLOSING)
def submit(self, mPointID, value, valueType, timestamp=datetime.utcnow(), operationalMode=None): ''' Submit a monitor point or alarm with the give ID to the java plugin. The monitor point is added to the dictionary and will be sent later @param mPointID: the not None nor empty ID of the monitor point @param value: the value of the monitor point @param valueType: (IasTye)the IasType of the monitor point @param timestamp: (datetime) the timestamp when the value has been red from the monitored system @param operationalMode (OperationalMode) the optional operational mode @see: JsonMsg.IAS_SUPPORTED_TYPES ''' if not mPointID: raise ValueError("The ID can't be None neither empty") if timestamp is None: raise ValueError("The timestamp can't be None") if value is None: raise ValueError("The value can't be None") if valueType is None: raise ValueError("The type can't be None") if self._shuttedDown: return msg = JsonMsg(mPointID, value, valueType, timestamp, operationalMode) self._lock.acquire() self._MPointsToSend[msg.mPointID] = msg self._lock.release() logging.debug( "Monitor point %s of type %s submitted with value %s and mode %s (%d values in queue)", msg.mPointID, msg.valueType, msg.value, msg.operationalMode, len(self._MPointsToSend))
def testSentValue(self): ''' Test that the plugin effectively sent what has been submitted ''' self.plugin.start() self.plugin.submit("MPoint-ID", 2.3, IASType.DOUBLE) self.plugin.submit("MPoint-IDOpMode", 5, IASType.INT, operationalMode=OperationalMode.MAINTENANCE) self.plugin.submit("MPoint-Alarm", Alarm.SET_CRITICAL, IASType.ALARM, operationalMode=OperationalMode.DEGRADED) time.sleep(2 * UdpPlugin.SENDING_TIME_INTERVAL) self.assertEqual(len(self.receiver.msgReceived), 3) dict = {} for jmsg in self.receiver.msgReceived: msg = JsonMsg.parse(jmsg) dict[msg.mPointID] = msg m = dict["MPoint-ID"] self.assertEqual(m.mPointID, "MPoint-ID") self.assertEqual(m.value, 2.3) self.assertEqual(m.valueType, IASType.DOUBLE) self.assertIsNone(m.operationalMode) m = dict["MPoint-IDOpMode"] self.assertEqual(m.mPointID, "MPoint-IDOpMode") self.assertEqual(m.value, 5) self.assertEqual(m.valueType, IASType.INT) self.assertEqual(m.operationalMode, OperationalMode.MAINTENANCE) m = dict["MPoint-Alarm"] self.assertEqual(m.mPointID, "MPoint-Alarm") self.assertEqual(m.value, Alarm.SET_CRITICAL) self.assertEqual(m.valueType, IASType.ALARM) self.assertEqual(m.operationalMode, OperationalMode.DEGRADED)