Skip to content

Commit

Permalink
feat: add notification, rules and devices popups
Browse files Browse the repository at this point in the history
  • Loading branch information
paga16-hash committed Jan 31, 2024
1 parent 5a4f83d commit b26f647
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 193 deletions.
4 changes: 2 additions & 2 deletions domain/src/utils/AnomalyTypeConverter.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,7 +12,7 @@ export class AnomalyTypeConverter {
}
}

static convertToString(type: AnomalyType): String {
static convertToString(type: AnomalyType): string {
switch (type) {
case AnomalyType.EXCEEDING:
return 'EXCEEDING'
Expand Down
4 changes: 2 additions & 2 deletions domain/src/utils/ContactTypeConverter.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,7 +12,7 @@ export class ContactTypeConverter {
}
}

static convertToString(type: ContactType): String {
static convertToString(type: ContactType): string {
switch (type) {
case ContactType.SMS:
return 'SMS'
Expand Down
4 changes: 2 additions & 2 deletions domain/src/utils/MeasureConverter.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,7 +14,7 @@ export class MeasureConverter {
}
}

static convertToString(measure: Measure): String {
static convertToString(measure: Measure): string {
switch (measure) {
case Measure.TEMPERATURE:
return 'TEMPERATURE'
Expand Down
4 changes: 2 additions & 2 deletions domain/src/utils/ObjectClassConverter.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,7 +14,7 @@ export class ObjectClassConverter {
}
}

static convertToString(type: ObjectClass): String {
static convertToString(type: ObjectClass): string {
switch (type) {
case ObjectClass.ANIMAL:
return 'ANIMAL'
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/scripts/Popups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

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
})
}
35 changes: 35 additions & 0 deletions frontend/src/scripts/presentation/device/ComposeDevice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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)
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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";

const notificationFactory: NotificationFactory = new NotificationFactoryImpl()
const anomalyFactory: AnomalyFactory = new AnomalyFactoryImpl()
const deviceIdFactory: DeviceIdFactory = new DeviceIdFactoryImpl()

export async function composeNotification(notification: any) {
await RequestHelper.get(`http://${alarmHost}:${alarmPort}/anomalies/` + notification.anomalyId)
.then((anomaly: any) => {
switch (DeviceTypeConverter.convertToDeviceType(anomaly.data.deviceId.type)) {
case DeviceType.CAMERA:
return notificationFactory.createIntrusionNotification(notification._id, composeIntrusion(anomaly.data))
case DeviceType.SENSOR:
return notificationFactory.createExceedingNotification(notification._id, composeExceeding(anomaly.data))
}
})
.catch(error => {
console.log(error)
})
}

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
)
}
Original file line number Diff line number Diff line change
@@ -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)
)
})
}
45 changes: 12 additions & 33 deletions frontend/src/views/DevicesView.vue
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
<script lang="ts"></script>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import DeviceBadge from '@/components/devices/DeviceBadge.vue'
import type { DeviceFactory, DeviceIdFactory, ResolutionFactory } from '@domain/device/factories'
import { DeviceFactoryImpl, DeviceIdFactoryImpl, ResolutionFactoryImpl } from '@domain/device/factories'
import type { Camera, Device, Sensor } from '@domain/device/core'
import { Measure } from '@domain/device/core'
import NewDevicePopup from '@/components/devices/NewDevicePopup.vue'
import { popNegative, popPositive } from '@/scripts/Popups.js'
import RequestHelper, { monitoringHost, monitoringPort } from '@/utils/RequestHelper'
import { DeviceTypeConverter, MeasureConverter } from '@utils/index'
const deviceIdFactory: DeviceIdFactory = new DeviceIdFactoryImpl()
const deviceFactory: DeviceFactory = new DeviceFactoryImpl()
const resolutionFactory: ResolutionFactory = new ResolutionFactoryImpl()
import { useQuasar } from 'quasar'
import { composeCamera, composeSensor } from '@/scripts/presentation/device/ComposeDevice'
const sensors: ref<Sensor[]> = ref([])
const cameras: ref<Camera[]> = ref([])
const $q = useQuasar()
const getSensors = async () => {
await RequestHelper.get(`http://${monitoringHost}:${monitoringPort}/devices/sensors`)
Expand All @@ -43,29 +40,6 @@ const getCameras = async () => {
})
}
const composeSensor = (sensor: any): Sensor => {
return deviceFactory.createSensor(
deviceIdFactory.createSensorId(sensor._id.code),
sensor.ipAddress,
sensor.intervalMillis,
composeMeasure(sensor.measures)
)
}
const composeCamera = (camera: any): Camera => {
return deviceFactory.createCamera(
deviceIdFactory.createCameraId(camera._id.code),
camera.ipAddress,
resolutionFactory.createResolution(camera.resolution.width, camera.resolution.height)
)
}
function composeMeasure(measures: any): Measure[] {
return measures.map((measure: any) => {
return MeasureConverter.convertToMeasure(measure)
})
}
const insertSensor = async (sensor: Sensor) => {
await RequestHelper.post(`http://${monitoringHost}:${monitoringPort}/devices/sensors`, {
code: sensor.deviceId.code,
Expand All @@ -76,10 +50,11 @@ const insertSensor = async (sensor: Sensor) => {
})
})
.then(async (res: any) => {
//TODO A CONFIRM POPUP
popPositive($q, 'Sensor added successfully')
await getSensors()
})
.catch(error => {
popNegative($q, 'Error while deleting sensor')
console.log(error)
})
}
Expand All @@ -94,10 +69,12 @@ const insertCamera = async (camera: Camera) => {
}
})
.then(async (res: any) => {
//TODO A CONFIRM POPUP
await getSensors()
await getCameras()
popPositive($q, 'Camera added successfully')
})
.catch(error => {
popNegative($q, 'Error while deleting camera')
console.log(error)
})
}
Expand All @@ -108,10 +85,12 @@ const deleteDevice = async (device: Device) => {
device.deviceId.code
)
.then(async (res: any) => {
//TODO A CONFIRM POPUP
await getSensors()
await getCameras()
popPositive($q, 'Device deleted successfully')
})
.catch(error => {
popNegative($q, 'Error while deleting device')
console.log(error)
})
}
Expand Down
17 changes: 1 addition & 16 deletions frontend/src/views/HomeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { AnomalyTypeConverter, DeviceTypeConverter, MeasureConverter } from 'dom
import { AnomalyType } from 'domain/dist/domain/anomaly/core'
import { useTopicsStore } from '@/stores/topics'
import { type AxiosResponse, HttpStatusCode } from 'axios'
import { composeSensor } from "@/scripts/presentation/device/ComposeDevice";
const topicsStore = useTopicsStore()
Expand All @@ -36,22 +37,6 @@ RequestHelper.get(`http://${monitoringHost}:${monitoringPort}/devices/sensors`).
const environmentDataFactory = new EnvironmentDataFactoryImpl()
const composeSensor = (sensor: any): Sensor => {
console.log(composeMeasure(sensor.measures))
return deviceFactory.createSensor(
deviceIdFactory.createSensorId(sensor._id.code),
sensor.ipAddress,
sensor.intervalMillis,
composeMeasure(sensor.measures)
)
}
function composeMeasure(measures: any): Measure[] {
return measures.map((measure: any) => {
return MeasureConverter.convertToMeasure(measure)
})
}
onBeforeMount(() => {
console.log(
'resume' +
Expand Down
Loading

0 comments on commit b26f647

Please sign in to comment.