Skip to content

Commit

Permalink
dashboard created
Browse files Browse the repository at this point in the history
  • Loading branch information
kimamov committed Jan 27, 2021
1 parent 967c2f1 commit d1006f6
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 64 deletions.
2 changes: 1 addition & 1 deletion client/.eslintcache

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import { Box } from '@material-ui/core'
import { Box, Typography } from '@material-ui/core'
import React from 'react'
import LockCell from '../../../dashboard/LockCell'

const ApartmentOne = (props) => {
/* */
const LockOrNull=({lock})=>lock? <LockCell lock={lock}/> : null;

const ApartmentOne = ({apartment={}, props}) => {
// sort locks by their slot on the reader
const sortedLocks=apartment.locks && apartment.locks.length
? apartment.locks.sort((lockA, lockB)=>lockA.slot-lockB.slot)
: []
console.log(sortedLocks)

return (
<Box display="flex" margin={2}>
<Box id="links" display="flex" flexDirection="column">
<LockCell/>
<LockCell/>
</Box>
<LockCell/>
<Box id="rechts" display="flex" justifyContent="stretch" flexDirection="column">
<LockCell/>
<LockCell/>
<LockCell/>
<LockCell/>
<Box marginX="auto">
<Typography align="center" variant="h6">{apartment.name}</Typography>
<Box display="flex" margin={2}>
<Box id="links" display="flex" flexDirection="column">
<LockCell lock={sortedLocks[1]}/>
<LockCell lock={sortedLocks[2]}/>
</Box>
<LockCell lock={apartment.apartmentLock }/>
<Box id="rechts" display="flex" justifyContent="stretch" flexDirection="column">
<LockCell lock={sortedLocks[3]}/>
<LockCell lock={sortedLocks[4]}/>
<LockCell lock={sortedLocks[5]}/>
<LockCell lock={sortedLocks[6]}/>
</Box>
</Box>
</Box>

)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,26 @@ const BuildingRosenstrasse = (props) => {
console.log(building)
return (
<Box display="inline-block">
<Typography>
<Typography variant="h5">
Gebäude Rosenstraße
</Typography>
<Box position="relative" /* bgcolor="blue" */ display="flex" flexDirection="column">
<LockCell/>
<Box
display="flex"
justifyContent="space-between"
border="2px solid grey"
borderRadius={4}
/* paddingTop={4} */
> {/* Apartments */}
<ApartmentOne />
<ApartmentOne />
<ApartmentOne />
{building && building.apartments?
<Box position="relative" /* bgcolor="blue" */ display="flex" flexDirection="column">
<LockCell/>
<Box
display="flex"
flexWrap="wrap"
justifyContent="center"
border="2px solid grey"
borderRadius={4}
>
{building?.apartments.map(apartment=><ApartmentOne apartment={apartment}/>)}{/* Apartments */}
</Box>
</Box>
</Box>
:
<Typography>building could not be found</Typography>
}

</Box>
)
}
Expand Down
48 changes: 42 additions & 6 deletions client/src/components/dashboard/LockCell.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React from 'react'
import Box from '@material-ui/core/Box'
import { styled } from '@material-ui/core';
import {Add} from '@material-ui/icons';
import {useRedirect, useNotify} from 'react-admin'


const Cell=styled(Box)({
margin: "1px",
Expand All @@ -10,13 +13,16 @@ const Cell=styled(Box)({
border: "2px solid rgba(100,100,100,0.4)",
borderRadius: "4px",
overflow: "hidden",
display: "inline-flex",
alignItems: "center",
justifyContent: "center",
'&:hover': {
transform: "scale(1.04)"
}
})

const LockCell = ({
lock={},
lock,
style={},
colors={
open: "green",
Expand All @@ -25,20 +31,50 @@ const LockCell = ({
free: "grey"
},
onClick,
...props}
) => {
const lockNumber=props.lockNumber || 0;
...props
}) => {

const notify=useNotify();

const openLock=async()=>{
try {
notify(`started opening lock ${lock.name}`, "info")
const res=await fetch(`${"https://localhost:5000/api"}/opendoor/${lock.readerId}?port=${lock.slot}`)
//const json=await res.json();
notify(`successfully opened lock ${lock.name}`, "info")
} catch (error) {
notify(`could not open lock ${lock.name}`, "warning")
console.log(error)
}
}

if(!lock) return <EmptyLockCell/>
return (
<Cell
padding={2}
bgcolor={lock.open? colors.open : colors.closed}
flex={1}
style={style}
onClick={onClick}
onClick={openLock}
>
{lockNumber}
{lock.number || "no number"}
</Cell>
)
}

const EmptyLockCell=()=>{
const redirect=useRedirect();

return (
<Cell
padding={2}
bgcolor={"lightgrey"}
flex={1}
onClick={()=>redirect(`/lock/create`)}
>
<Add/>
</Cell>
)
}

export default LockCell
12 changes: 9 additions & 3 deletions client/src/components/lock/LockCreate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ const LockCreate = (props) => {
return(
<Create title="Create a Lock" {...props} >
<SimpleForm>
<TextInput source="name" />
<TextInput source="type" />
<NumberInput source="slot" min={1} max={6}/>
<TextInput source="name" required/>
<SelectInput source="type" choices={[
{ id: 'Tresorschloss', name: 'Tresorschloss' },
{ id: 'Wohnungsschloss', name: 'Wohnungsschloss' },
{ id: 'Gebäudeschloss', name: 'Gebäudeschloss' },
]} required/>
<NumberInput label="Slot (1-6)" source="slot" min={1} max={6} required/>

<ReferenceInput reference="building" source="buildingId" allowEmpty {...props}>
<SelectInput optionText="name" />
</ReferenceInput>
<FormDataConsumer>
{({ formData, ...rest }) => (
formData.type!=="Gebäudeschloss" && // if the lock is of type Gebäudeschloss dont show this
<ReferenceInput
reference="apartment"
source="apartmentId"
Expand All @@ -37,6 +42,7 @@ const LockCreate = (props) => {
<FormDataConsumer>
{({ formData, ...rest }) => (
<ReferenceInput
label={"connected to Controller"}
reference="reader"
source="readerId"
allowEmpty
Expand Down
4 changes: 2 additions & 2 deletions client/src/components/reader/ReaderShow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const ShowPropsExtractor=({children, ...props})=>{
</Datagrid>
</ArrayField>

<TextButtonField onClick={()=>openDoor(1)} label="Relay 1 (acctype)" variant="contained" source="acctypeName">
{/* <TextButtonField onClick={()=>openDoor(1)} label="Relay 1 (acctype)" variant="contained" source="acctypeName">
<LockOpen/>
</TextButtonField>
<TextButtonField onClick={()=>openDoor(2)} label="Relay 2 (acctype2)" variant="contained" source="acctype2Name">
Expand All @@ -87,7 +87,7 @@ const ShowPropsExtractor=({children, ...props})=>{
</TextButtonField>
<TextButtonField onClick={()=>openDoor(6)} label="Relay 6 (acctype6)" variant="contained" source="acctype4Name">
<LockOpen/>
</TextButtonField>
</TextButtonField> */}

{/* TODO MAKE THIS GRID HAVING IT STACKED ON DESKTOP GETS MESSY */}
<Create title=" " resource={props.resource} onSuccess={onSuccess}>
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/buildingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function getBuildings(req: Request, res: Response) {
export async function getBuilding(req: Request, res: Response){
try {
const {id}=req.params;
const result = await getRepository(Building).findOne(id, {relations: ["apartments", "apartments.locks"]})
const result = await getRepository(Building).findOne(id, {relations: ["apartments", "apartments.locks", "apartments.apartmentLock", "buildingLock"]})

if(!result){
return res.status(404).send({message: `could not find building with the provided id ${id}`})
Expand Down
27 changes: 9 additions & 18 deletions src/controllers/lockController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dateToUnix from "../util/dateToUnix";
import { ReaderKey } from "../entity/ReaderKey";
import { Lock } from "../entity/Lock";
import { Building } from "../entity/Building";
import { Apartment } from "../entity/Apartment";



Expand Down Expand Up @@ -37,27 +38,17 @@ export async function getLock(req: Request, res: Response){

export async function createLock(req: Request, res: Response) {
try {
/*
const {building_id, reader_id, ...lockRest}=req.body;
const lock=getRepository(Lock).create(lockRest);

const repo=await getRepository(Lock);
const lock=repo.create(req.body as Lock);

if(building_id){
const building=await getRepository(Building).findOne(building_id);
if(building){
lock.building=building;
}
if(lock.type==="Wohnungsschloss" && lock.apartmentId){
lock.apartmentLock=await getRepository(Apartment).findOne(lock.apartmentId)
lock.apartmentId=null;
}else if(lock.type==="Wohnungsschloss" && lock.buildingId){
lock.buildingLock=await getRepository(Building).findOne(lock.buildingId)
}
if(reader_id){
const reader=await getRepository(Reader).findOne(reader_id);
if(reader){
lock.reader=reader;
}
} */

const result=await getRepository(Lock).save(req.body);
const result=await repo.save(lock);
res.send(result)
} catch (error) {
res.status(500).send({
Expand Down
4 changes: 2 additions & 2 deletions src/entity/Apartment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Apartment {
@Column({ type: "int", nullable: true })
apartmentLockId: number;

@ManyToOne(()=> Lock)
@ManyToOne(()=> Lock, lock=>lock.apartmentLock, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@JoinColumn({name: "apartmentLockId"})
public apartmentLock: Lock;

Expand All @@ -32,7 +32,7 @@ export class Apartment {
@JoinColumn({name: "buildingId"})
public building: Building;

@OneToMany(()=>Lock, lock=>lock.building, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@OneToMany(()=>Lock, lock=>lock.apartment, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
locks: Lock[];

@OneToMany(()=>Reader, reader=>reader.apartment, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
Expand Down
14 changes: 11 additions & 3 deletions src/entity/Lock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, OneToMany, ManyToMany, JoinColumn, JoinTable } from "typeorm";
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, OneToMany, ManyToMany, JoinColumn, JoinTable, OneToOne } from "typeorm";
import { Apartment } from "./Apartment";
import { Building } from "./Building";
import { Key } from "./Key";
Expand Down Expand Up @@ -42,6 +42,14 @@ export class Lock {
@JoinColumn({name: "apartmentId"})
public apartment: Apartment;



@OneToOne(()=> Apartment, apartment=> apartment.apartmentLock, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
public apartmentLock: Apartment;

@OneToOne(()=> Building, building=> building.buildingLock, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
public buildingLock: Building;


@Column({ type: "int", nullable: true })
readerId: number;
Expand All @@ -51,13 +59,13 @@ export class Lock {
public reader: Reader;


@ManyToMany(type => Lock, lock => lock.previousLocks)
/* @ManyToMany(type => Lock, lock => lock.previousLocks)
@JoinTable()
public nextLocks: Lock[];
@ManyToMany(type=> Lock, lock => lock.nextLocks)
@JoinTable()
public previousLocks: Lock[];
public previousLocks: Lock[]; */

@OneToMany(type=> Key, key => key.lock, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
public keys: Key[];
Expand Down
7 changes: 7 additions & 0 deletions src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ router.get("/apartment/:id", getApartment)
router.post("/apartment"/* , checkAuth */, createApartment)


/* router.get("/openlock/:id", (req, res)=>{
console.log(req.params);
res.send({
message: `successfully opened lock`
})
}) */

router.post("/lock"/* , checkAuth */, createLock)

router.get("/lock"/* , checkAuth */, getLocks)
Expand Down

0 comments on commit d1006f6

Please sign in to comment.