Skip to content

Commit

Permalink
Merge pull request #5 from kantimam/deploy
Browse files Browse the repository at this point in the history
Deploy
  • Loading branch information
kimamov authored Jan 20, 2021
2 parents 8936e1c + 4d171f2 commit 9452c1c
Show file tree
Hide file tree
Showing 17 changed files with 287 additions and 13 deletions.
2 changes: 1 addition & 1 deletion client/.eslintcache

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import LockCreate from './components/lock/LockCreate';
import BuildingList from './components/building/BuildingList';
import BuildingCreate from './components/building/BuildingCreate';
import Dashboard from './components/dashboard';
import ApartmentCreate from './components/apartment/ApartmentCreate';
import ApartmentList from './components/apartment/ApartmentList';


const isAdmin=permissions=>{
Expand All @@ -40,6 +42,7 @@ function App() {
<Admin dataProvider={restProvider(serverAdress)} dashboard={Dashboard} locale="en" authProvider={authProvider}>
{permissions=>[
<Resource icon={LocationCity} name="building" list={BuildingList} create={BuildingCreate}/>,
<Resource icon={LocationCity} name="apartment" list={ApartmentList} create={ApartmentCreate}/>,
<Resource icon={Lock} name="lock" list={LockList} create={LockCreate} options={{label: 'Locks'}}/>,
<Resource icon={Fingerprint} name="reader" list={ReaderList} show={ReaderShow} edit={ReaderEdit} options={{label: 'Controllers'}}/>,
<Resource icon={VpnKey} name="key" list={KeyList} create={KeyCreate} edit={KeyEdit} show={KeyShow}/>,
Expand Down
17 changes: 17 additions & 0 deletions client/src/components/apartment/ApartmentCreate.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// in posts.js
import * as React from "react";
import { Create, SimpleForm, TextInput, BooleanInput, DateTimeInput, ReferenceInput, SelectInput } from 'react-admin';





const ApartmentCreate = (props) => (
<Create title="Create a Building" {...props} >
<SimpleForm>
<TextInput source="name" />
</SimpleForm>
</Create>
);

export default ApartmentCreate
20 changes: 20 additions & 0 deletions client/src/components/apartment/ApartmentList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// in posts.js
import * as React from "react";
import { List, Datagrid, TextField, EditButton} from 'react-admin';

export const ApartmentList = (props) => (
<List
{...props}
>
<Datagrid rowClick="show">
<TextField source="name"/>
<EditButton/>
</Datagrid>
</List>
);





export default ApartmentList
11 changes: 11 additions & 0 deletions client/src/components/apartment/customViews/ApartmentOne.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'

const ApartmentOne = (props) => {
return (
<div>

</div>
)
}

export default ApartmentOne
11 changes: 11 additions & 0 deletions client/src/components/apartment/customViews/ApartmentThree.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'

const ApartmentOne = (props) => {
return (
<div>

</div>
)
}

export default ApartmentOne
11 changes: 11 additions & 0 deletions client/src/components/apartment/customViews/ApartmentTwo.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'

const ApartmentTwo = (props) => {
return (
<div>

</div>
)
}

export default ApartmentTwo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react'

const BuildingRosenstrasse = () => {
/* get data for building rosenstrasse */
return (
<div>

</div>
)
}

export default BuildingRosenstrasse
12 changes: 12 additions & 0 deletions client/src/components/building/customViews/BuildingView.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react'

const BuildingView = ({building, ...props}) => {

return (
<div>

</div>
)
}

export default BuildingView
4 changes: 3 additions & 1 deletion client/src/components/lock/LockCreate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ const LockCreate = (props) => (
<SimpleForm>
<TextInput source="name" />
<TextInput source="type" />

<ReferenceInput reference="reader" source="readerId" allowEmpty {...props}>
<SelectInput optionText="readerName" />
</ReferenceInput>
<ReferenceInput reference="building" source="buildingId" allowEmpty {...props}>
<SelectInput optionText="name" />
</ReferenceInput>
<ReferenceInput reference="apartment" source="apartmentId" allowEmpty {...props}>
<SelectInput optionText="name" />
</ReferenceInput>
</SimpleForm>
</Create>
);
Expand Down
3 changes: 3 additions & 0 deletions client/src/components/lock/LockList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export const LockList = (props) => (
<ReferenceField label="Building" reference="building" source="buildingId" link="show">
<TextField source="name"/>
</ReferenceField>
<ReferenceField label="Apartment" reference="apartment" source="apartmentId" link="show">
<TextField source="name"/>
</ReferenceField>
<ReferenceField label="Reader" reference="reader" source="readerId" link="show">
<TextField source="readerName"/>
</ReferenceField>
Expand Down
24 changes: 16 additions & 8 deletions client/src/components/reader/ReaderShow.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react'
import React, {useState} from 'react'
import { Toolbar, SaveButton, Create, SimpleForm, ReferenceInput, SelectInput, Datagrid, Show, SimpleShowLayout, TextField, DateField, ArrayField, BooleanField, ReferenceManyField, NumberField, useNotify, useRefresh, BooleanInput } from 'react-admin';
import { LockOpen} from '@material-ui/icons';
import ReaderShowActions from './ReaderShowActions'
import TextButtonField from '../customFields/TextButtonField';
import { Button } from '@material-ui/core';


const KeyEditToolbar = props => (
Expand All @@ -14,7 +15,7 @@ const KeyEditToolbar = props => (
const ShowPropsExtractor=({children, ...props})=>{
const notify=useNotify();
const refresh=useRefresh();
const {keys=[]}=props.record;
const [showDevice, setShow] = useState(false)


const openDoor=async(port)=>{
Expand All @@ -30,17 +31,19 @@ const ShowPropsExtractor=({children, ...props})=>{
}

const readerKeyRowStyle = (record, _index) => {
const keys=props?.record?.readerKeys;
if(!keys) return false;
return {
backgroundColor: record && keys && keys.find(key=>{
return (
key.uid === record.uid &&
key.name === record.name &&
key.key.uid === record.uid &&
key.key.name === record.name &&
key.acctype === record.acctype &&
key.acctype2 === record.acctype2 &&
key.acctype3 === record.acctype3 &&
key.acctype4 === record.acctype4 &&
key.acctype4 === record.acctype4 /* &&
key.acctype5 === record.acctype5 &&
key.acctype6 === record.acctype6) // add back valid until later
key.acctype6 === record.acctype6 */) // add back valid until later
}) ? '#efe' : 'white',
}
}
Expand Down Expand Up @@ -114,19 +117,24 @@ const ShowPropsExtractor=({children, ...props})=>{
</Datagrid>
</ArrayField>

<ReferenceManyField reference="devicekey" target="readerId" label="KEYS ON READER" {...props}>
<Button onClick={()=>setShow(!showDevice)} variant="contained" color={showDevice? "primary" : "secondary"}>
{`${ showDevice? "HIDE" : "SHOW" } DEVICE KEYS`}
</Button>

{showDevice && <ReferenceManyField reference="devicekey" target="readerId" label="KEYS ON READER" {...props}>
<Datagrid rowStyle={readerKeyRowStyle} {...props}>
<TextField source="name" />
<TextField source="uid" />
<DateField source="validUntil" showTime locales="de"/>
{/* <BooleanField label="one time" source="isOneTimeCode" /> */}
<NumberField source="acctype" />
<NumberField source="acctype2" />
<NumberField source="acctype3" />
<NumberField source="acctype4" />
<NumberField source="acctype5" />
<NumberField source="acctype6" />
</Datagrid>
</ReferenceManyField>
</ReferenceManyField>}


</SimpleShowLayout>
Expand Down
106 changes: 106 additions & 0 deletions src/controllers/apartmentController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { getRepository } from "typeorm"
import { Request, Response } from "express"
import { Reader } from "../entity/Reader"
import { client } from "../mqtt/connection";
import { Key } from "../entity/Key";
import getList from "../util/getList";
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";







export async function getApartments(req: Request, res: Response) {
getList(getRepository(Apartment), req, res)
}

export async function getApartment(req: Request, res: Response){
try {
const {id}=req.params;
const result = await getRepository(Apartment).findOne(id)

if(!result){
return res.status(404).send({message: `could not find building with the provided id ${id}`})
}
return res.send(result);
} catch (error) {
console.log(error)
res.status(500).send({error: "server error"})
}
}


export async function createApartment(req: Request, res: Response) {
try {
const result=await getRepository(Apartment).save(req.body);
res.send(result)
} catch (error) {
res.status(500).send({
error: error
})
}

}

export async function editBuilding(req: Request, res: Response) {
// function creates a connection between the reader and key inside the database and sends it over to the reader
try {
const readerId=req.params.id;
if(readerId === undefined){
throw "please provide a valid reader id"
}
const { body } = req;
if (!body.readerKeys || !Array.isArray(body.readerKeys)){
return res.status(400).send({error: "invalid request an array readerKeys is required"})
}

// there has to be a better option then deleting all the relationships before adding new but this should do for now
getRepository(ReaderKey)
.createQueryBuilder()
.delete()
.from(ReaderKey)
.where("readerId = :readerId", {readerId: readerId})
.execute();

// check if reader with that ip exists
const readerRepo=getRepository(Reader)
const readerResult = await readerRepo.findOne(readerId, {relations: ["readerKeys"]})

if (!readerResult) throw "no door found"

/* if(body.readerName){
readerResult.readerName=body.readerName;
} */
readerResult.acctypeName=body.acctypeName || "";
readerResult.acctype2Name=body.acctype2Name || "";
readerResult.acctype3Name=body.acctype3Name || "";
readerResult.acctype4Name=body.acctype4Name || "";
readerResult.acctype5Name=body.acctype5Name || "";
readerResult.acctype6Name=body.acctype6Name || "";

readerResult.readerKeys=body.readerKeys;

await readerRepo.save(readerResult)

return res.send(readerResult)

} catch (error) {
console.log(error)
res.status(500).send({
error: "failed to update keys on reader"
})
}
}







35 changes: 35 additions & 0 deletions src/entity/Apartment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Entity, Column, PrimaryGeneratedColumn, OneToMany, JoinColumn, ManyToOne } from "typeorm";
import { Building } from "./Building";
import { Lock } from "./Lock";


@Entity()
export class Apartment {
@PrimaryGeneratedColumn()
id: number;

@Column("varchar", { length: 100, nullable: false, default: "unbekannte Wohnung" })
name: string;

@Column("varchar", { nullable: true })
info: string;

@Column({ type: "int", nullable: true })
apartmentLockId: number;

@ManyToOne(()=> Lock)
@JoinColumn({name: "apartmentLockId"})
public apartmentLock: Lock;


@Column({ type: "int", nullable: true })
buildingId: number;

@ManyToOne(()=> Building, building=> building.apartments, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@JoinColumn({name: "buildingId"})
public building: Building;

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

}
7 changes: 4 additions & 3 deletions src/entity/Building.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from "typeorm";
import { AccessLog } from "./AccessLog";
import { Apartment } from "./Apartment";
import { Lock } from "./Lock";
import { NewKey } from "./NewKey";
import { ReaderKey } from "./ReaderKey";

@Entity()
export class Building {
Expand All @@ -18,6 +16,9 @@ export class Building {
@OneToMany(()=>Lock, lock=>lock.building, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
locks: Lock[];

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



}
Loading

0 comments on commit 9452c1c

Please sign in to comment.