diff --git a/domain/src/utils/AnomalyTypeConverter.ts b/domain/src/utils/AnomalyTypeConverter.ts index 29d5051d1..3b10f7461 100644 --- a/domain/src/utils/AnomalyTypeConverter.ts +++ b/domain/src/utils/AnomalyTypeConverter.ts @@ -1,7 +1,7 @@ import { AnomalyType } from '../domain/anomaly/core/impl/enum/AnomalyType.js' export class AnomalyTypeConverter { - static convertToAnomalyType(type: String): AnomalyType { + static convertToAnomalyType(type: string): AnomalyType { switch (type.toUpperCase()) { case 'EXCEEDING': return AnomalyType.EXCEEDING @@ -12,7 +12,7 @@ export class AnomalyTypeConverter { } } - static convertToString(type: AnomalyType): String { + static convertToString(type: AnomalyType): string { switch (type) { case AnomalyType.EXCEEDING: return 'EXCEEDING' diff --git a/domain/src/utils/ContactTypeConverter.ts b/domain/src/utils/ContactTypeConverter.ts index e0138576e..c3d6fef49 100644 --- a/domain/src/utils/ContactTypeConverter.ts +++ b/domain/src/utils/ContactTypeConverter.ts @@ -1,7 +1,7 @@ import { ContactType } from '../domain/monitoring/core/impl/enum/ContactType.js' export class ContactTypeConverter { - static convertToContactType(type: String): ContactType { + static convertToContactType(type: string): ContactType { switch (type.toUpperCase()) { case 'SMS': return ContactType.SMS @@ -12,7 +12,7 @@ export class ContactTypeConverter { } } - static convertToString(type: ContactType): String { + static convertToString(type: ContactType): string { switch (type) { case ContactType.SMS: return 'SMS' diff --git a/domain/src/utils/MeasureConverter.ts b/domain/src/utils/MeasureConverter.ts index b64cb3814..4608837d7 100644 --- a/domain/src/utils/MeasureConverter.ts +++ b/domain/src/utils/MeasureConverter.ts @@ -1,7 +1,7 @@ import { Measure } from '../domain/device/core/impl/enum/Measure.js' export class MeasureConverter { - static convertToMeasure(measure: String): Measure { + static convertToMeasure(measure: string): Measure { switch (measure.toUpperCase()) { case 'TEMPERATURE': return Measure.TEMPERATURE @@ -14,7 +14,7 @@ export class MeasureConverter { } } - static convertToString(measure: Measure): String { + static convertToString(measure: Measure): string { switch (measure) { case Measure.TEMPERATURE: return 'TEMPERATURE' diff --git a/domain/src/utils/ObjectClassConverter.ts b/domain/src/utils/ObjectClassConverter.ts index 0f80a33e2..436c9dfed 100644 --- a/domain/src/utils/ObjectClassConverter.ts +++ b/domain/src/utils/ObjectClassConverter.ts @@ -1,7 +1,7 @@ import { ObjectClass } from '../domain/security-rule/core/impl/enum/ObjectClass.js' export class ObjectClassConverter { - static convertToObjectClass(type: String): ObjectClass { + static convertToObjectClass(type: string): ObjectClass { switch (type.toUpperCase()) { case 'ANIMAL': return ObjectClass.ANIMAL @@ -14,7 +14,7 @@ export class ObjectClassConverter { } } - static convertToString(type: ObjectClass): String { + static convertToString(type: ObjectClass): string { switch (type) { case ObjectClass.ANIMAL: return 'ANIMAL' diff --git a/frontend/src/components/devices/DeviceBadge.vue b/frontend/src/components/devices/DeviceBadge.vue index f8b114264..ade32d5d9 100644 --- a/frontend/src/components/devices/DeviceBadge.vue +++ b/frontend/src/components/devices/DeviceBadge.vue @@ -6,6 +6,8 @@ import { ref } from 'vue' import UpdateDevicePopup from '@/components/devices/UpdateDevicePopup.vue' import RequestHelper, { monitoringHost, monitoringPort } from '@/utils/RequestHelper' import { MeasureConverter } from 'domain/dist/utils' +import { popNegative, popPositive } from '@/scripts/Popups' +import { useQuasar } from 'quasar' defineProps<{ device: Device @@ -16,6 +18,7 @@ defineEmits<{ }>() const updatePopupVisible = ref(false) +const $q = useQuasar() const updateSensor = async (sensor: Sensor) => { await RequestHelper.put(`http://${monitoringHost}:${monitoringPort}/devices/sensors`, { @@ -27,10 +30,10 @@ const updateSensor = async (sensor: Sensor) => { }) }) .then(async (res: any) => { - alert('devo aggiornare i devices') - //TODO A CONFIRM POPUP + popPositive($q, 'Sensor updated successfully') }) .catch(error => { + popNegative($q, 'Error while updating sensor') console.log(error) }) } @@ -45,10 +48,10 @@ const updateCamera = async (camera: Camera) => { } }) .then(async (res: any) => { - alert('devo aggiornare i devices') - //TODO A CONFIRM POPUP + popPositive($q, 'Camera updated successfully') }) .catch(error => { + popNegative($q, 'Error while updating camera') console.log(error) }) } diff --git a/frontend/src/components/devices/UpdateDevicePopup.vue b/frontend/src/components/devices/UpdateDevicePopup.vue index 1fcf3b679..92919a264 100644 --- a/frontend/src/components/devices/UpdateDevicePopup.vue +++ b/frontend/src/components/devices/UpdateDevicePopup.vue @@ -36,7 +36,6 @@ const options = ref([ ]) const updateDevice = () => { - console.log(device) if (device.deviceId.type == DeviceType.SENSOR) { const updatedSensor: Sensor = deviceFactory.createSensor( deviceIdFactory.createSensorId(device.deviceId.code), diff --git a/frontend/src/components/security-rule/SecurityRuleBadge.vue b/frontend/src/components/security-rule/SecurityRuleBadge.vue index b6e9682aa..27fd56997 100644 --- a/frontend/src/components/security-rule/SecurityRuleBadge.vue +++ b/frontend/src/components/security-rule/SecurityRuleBadge.vue @@ -11,6 +11,8 @@ import UpdateSecurityRulePopup from './UpdateSecurityRulePopup.vue' import { ref } from 'vue' import { DeviceTypeConverter, MeasureConverter, ObjectClassConverter } from 'domain/dist/utils' import RequestHelper, { alarmHost, alarmPort } from '@/utils/RequestHelper' +import { popPositive, popNegative } from '@/scripts/Popups' +import { useQuasar } from 'quasar' defineProps<{ securityRule: SecurityRule @@ -21,24 +23,9 @@ defineEmits<{ }>() const updatePopupVisible = ref(false) +const $q = useQuasar() const updateExceedingRule = async (exceedingRule: ExceedingRule) => { - console.log(DeviceTypeConverter.convertToString(exceedingRule.deviceId.type)) - console.log(exceedingRule.deviceId.type) - console.log({ - id: exceedingRule.securityRuleId, - deviceId: { - type: DeviceTypeConverter.convertToString(exceedingRule.deviceId.type), - code: exceedingRule.deviceId.code - }, - description: exceedingRule.description, - minValue: exceedingRule.min, - maxValue: exceedingRule.max, - measure: MeasureConverter.convertToString(exceedingRule.measure), - from: exceedingRule.from.toISOString(), - to: exceedingRule.to.toISOString(), - contacts: exceedingRule.contactsToNotify - }) await RequestHelper.put(`http://${alarmHost}:${alarmPort}/security-rules/exceedings`, { id: exceedingRule.securityRuleId, deviceId: { @@ -54,10 +41,10 @@ const updateExceedingRule = async (exceedingRule: ExceedingRule) => { contacts: exceedingRule.contactsToNotify }) .then(async (res: any) => { - alert('devo aggiornare le exceeding rules') - //TODO A CONFIRM POPUP + popPositive($q, 'Exceeding rule updated successfully') }) .catch(error => { + popNegative($q, 'Error while updating exceeding rule') console.log(error) }) } @@ -76,10 +63,10 @@ const updateIntrusionRule = async (intrusionRule: IntrusionRule) => { contacts: intrusionRule.contactsToNotify }) .then(async (res: any) => { - alert('devo aggiornare le intrusion rules') - //TODO A CONFIRM POPUP + popPositive($q, 'Intrusion rule updated successfully') }) .catch(error => { + popNegative($q, 'Error while updating intrusion rule') console.log(error) }) } diff --git a/frontend/src/components/security-rule/UpdateSecurityRulePopup.vue b/frontend/src/components/security-rule/UpdateSecurityRulePopup.vue index bd9ceb984..33d2056c8 100644 --- a/frontend/src/components/security-rule/UpdateSecurityRulePopup.vue +++ b/frontend/src/components/security-rule/UpdateSecurityRulePopup.vue @@ -74,7 +74,6 @@ const measure: ref = ref(Measure.TEMPERATURE) const objectClass: ref = ref(ObjectClass.PERSON) const updateSecurityRule = () => { - console.log(securityRule) if (securityRule.deviceId.type == DeviceType.SENSOR) { const updatedExceedingRule: ExceedingRule = securityRuleFactory.createExceedingRule( (securityRule as ExceedingRule).min, diff --git a/frontend/src/scripts/Popups.ts b/frontend/src/scripts/Popups.ts new file mode 100644 index 000000000..1d751f135 --- /dev/null +++ b/frontend/src/scripts/Popups.ts @@ -0,0 +1,14 @@ +import { type QVueGlobals } from 'quasar' +export const popPositive = (q: QVueGlobals, message: string): void => { + q.notify({ + type: 'positive', + message: message + }) +} + +export const popNegative = (q: QVueGlobals, message: string): void => { + q.notify({ + type: 'negative', + message: message + }) +} diff --git a/frontend/src/scripts/presentation/device/ComposeDevice.ts b/frontend/src/scripts/presentation/device/ComposeDevice.ts new file mode 100644 index 000000000..041dba9b2 --- /dev/null +++ b/frontend/src/scripts/presentation/device/ComposeDevice.ts @@ -0,0 +1,37 @@ +import { type Camera, Measure, type Sensor } from 'domain/dist/domain/device/core' +import { MeasureConverter } from 'domain/dist/utils' +import { + type DeviceFactory, + DeviceFactoryImpl, + type DeviceIdFactory, + DeviceIdFactoryImpl, + type ResolutionFactory, + ResolutionFactoryImpl +} from 'domain/dist/domain/device/factories' + +const deviceIdFactory: DeviceIdFactory = new DeviceIdFactoryImpl() +const deviceFactory: DeviceFactory = new DeviceFactoryImpl() +const resolutionFactory: ResolutionFactory = new ResolutionFactoryImpl() + +export const composeSensor = (sensor: any): Sensor => { + return deviceFactory.createSensor( + deviceIdFactory.createSensorId(sensor._id.code), + sensor.ipAddress, + sensor.intervalMillis, + composeMeasure(sensor.measures) + ) +} + +export const composeCamera = (camera: any): Camera => { + return deviceFactory.createCamera( + deviceIdFactory.createCameraId(camera._id.code), + camera.ipAddress, + resolutionFactory.createResolution(camera.resolution.width, camera.resolution.height) + ) +} + +export function composeMeasure(measures: any): Measure[] { + return measures.map((measure: any) => { + return MeasureConverter.convertToMeasure(measure) + }) +} diff --git a/frontend/src/scripts/presentation/notification/ComposeNotification.ts b/frontend/src/scripts/presentation/notification/ComposeNotification.ts new file mode 100644 index 000000000..c43acb23c --- /dev/null +++ b/frontend/src/scripts/presentation/notification/ComposeNotification.ts @@ -0,0 +1,52 @@ +import RequestHelper, { alarmHost, alarmPort } from '@/utils/RequestHelper' +import { DeviceTypeConverter, MeasureConverter, ObjectClassConverter } from 'domain/dist/utils' +import { DeviceType } from 'domain/dist/domain/device/core' +import type { Exceeding, Intrusion } from 'domain/dist/domain/anomaly/core' +import type { NotificationFactory } from 'domain/dist/domain/alarm-system/factories' +import { NotificationFactoryImpl } from 'domain/dist/domain/alarm-system/factories' +import type { AnomalyFactory } from 'domain/dist/domain/anomaly/factories' +import { AnomalyFactoryImpl } from 'domain/dist/domain/anomaly/factories' +import type { DeviceIdFactory } from 'domain/dist/domain/device/factories' +import { DeviceIdFactoryImpl } from 'domain/dist/domain/device/factories' +import type { Notification } from 'domain/dist/domain/alarm-system/core' + +const notificationFactory: NotificationFactory = new NotificationFactoryImpl() +const anomalyFactory: AnomalyFactory = new AnomalyFactoryImpl() +const deviceIdFactory: DeviceIdFactory = new DeviceIdFactoryImpl() + +export const composeNotification = async (notification: any): Promise => { + const resAnomaly = await RequestHelper.get( + `http://${alarmHost}:${alarmPort}/anomalies/` + notification.anomalyId + ) + switch (DeviceTypeConverter.convertToDeviceType(resAnomaly.data.deviceId.type)) { + case DeviceType.CAMERA: + return notificationFactory.createIntrusionNotification( + notification._id, + composeIntrusion(resAnomaly.data) + ) + case DeviceType.SENSOR: + return notificationFactory.createExceedingNotification( + notification._id, + composeExceeding(resAnomaly.data) + ) + } +} + +function composeIntrusion(intrusion: any): Intrusion { + return anomalyFactory.createIntrusion( + intrusion._id, + deviceIdFactory.createCameraId(intrusion.deviceId.code), + new Date(intrusion.timestamp), + ObjectClassConverter.convertToObjectClass(intrusion.intrusionObject) + ) +} + +function composeExceeding(exceeding: any): Exceeding { + return anomalyFactory.createExceeding( + exceeding._id, + deviceIdFactory.createSensorId(exceeding.deviceId.code), + new Date(exceeding.timestamp), + MeasureConverter.convertToMeasure(exceeding.measure), + exceeding.value + ) +} diff --git a/frontend/src/scripts/presentation/security-rule/ComposeSecurityRule.ts b/frontend/src/scripts/presentation/security-rule/ComposeSecurityRule.ts new file mode 100644 index 000000000..d50746864 --- /dev/null +++ b/frontend/src/scripts/presentation/security-rule/ComposeSecurityRule.ts @@ -0,0 +1,47 @@ +import type { ExceedingRule, IntrusionRule } from 'domain/dist/domain/security-rule/core' +import { ContactTypeConverter, MeasureConverter, ObjectClassConverter } from 'domain/dist/utils' +import type { Contact } from 'domain/dist/domain/monitoring/core' +import { type SecurityRuleFactory, SecurityRuleFactoryImpl } from 'domain/dist/domain/security-rule/factories' +import { type DeviceIdFactory, DeviceIdFactoryImpl } from 'domain/dist/domain/device/factories' +import { type ContactFactory, ContactFactoryImpl } from 'domain/dist/domain/monitoring/factories' + +const securityRuleFactory: SecurityRuleFactory = new SecurityRuleFactoryImpl() +const deviceIdFactory: DeviceIdFactory = new DeviceIdFactoryImpl() +const contactFactory: ContactFactory = new ContactFactoryImpl() + +export function composeExceedingSecurityRule(exceedingRule: any): ExceedingRule { + return securityRuleFactory.createExceedingRule( + exceedingRule.minValue, + exceedingRule.maxValue, + MeasureConverter.convertToMeasure(exceedingRule.measure), + exceedingRule._id, + deviceIdFactory.createSensorId(exceedingRule.deviceId.code), + exceedingRule.creatorId, + composeContacts(exceedingRule.contacts), + exceedingRule.description, + new Date(exceedingRule.from), + new Date(exceedingRule.to) + ) +} + +export function composeIntrusionSecurityRule(intrusionRule: any): IntrusionRule { + return securityRuleFactory.createIntrusionRule( + ObjectClassConverter.convertToObjectClass(intrusionRule.objectClass), + intrusionRule._id, + deviceIdFactory.createCameraId(intrusionRule.deviceId.code), + intrusionRule.creatorId, + composeContacts(intrusionRule.contacts), + intrusionRule.description, + new Date(intrusionRule.from), + new Date(intrusionRule.to) + ) +} + +function composeContacts(contacts: any): Contact[] { + return contacts.map((contact: any) => { + return contactFactory.createContact( + contact.value, + ContactTypeConverter.convertToContactType(contact.type) + ) + }) +} diff --git a/frontend/src/views/DevicesView.vue b/frontend/src/views/DevicesView.vue index c2ccb93de..3aa351b6b 100644 --- a/frontend/src/views/DevicesView.vue +++ b/frontend/src/views/DevicesView.vue @@ -1,22 +1,19 @@