Skip to content

Commit

Permalink
final cleanup
Browse files Browse the repository at this point in the history
Co-authored-by: [email protected] <[email protected]>
Co-authored-by: Joshua <[email protected]>
  • Loading branch information
3 people committed Apr 17, 2021
1 parent 1ad1239 commit b9ac4a3
Show file tree
Hide file tree
Showing 16 changed files with 29 additions and 245 deletions.
8 changes: 2 additions & 6 deletions src/http/alarm/alarm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ export class AlarmService {
pattern: "plain",
},
});
} catch (e) {
console.log("eroorrr" + e);
}
} catch {}
});
};

Expand Down Expand Up @@ -159,7 +157,6 @@ export class AlarmService {

const invalids = await this.validateIds(data.ids);
if (invalids.length > 0) throw new InvalidIdException(invalids);

const conflicts: Conflict[] = this.getConflictingAlarms(
data.time,
data.days,
Expand Down Expand Up @@ -239,7 +236,6 @@ export class AlarmService {
) {
throw new NothingChangedException();
}
console.log(data.days);
if (data.days !== undefined && data.days.length === 0) {
throw new BadRequestException("days should not be empty");
}
Expand Down Expand Up @@ -345,7 +341,7 @@ export class AlarmService {

async getAlarmWithId(id: string): Promise<StandartResponse<Alarm>> {
return {
message: "Alarm with ID: " + id,
message: "Alarm with Id: " + id,
object: DatabaseAlarmService.alarmDocToAlarm(
await this.databaseServiceAlarm.getAlarmWithId(id),
),
Expand Down
1 change: 0 additions & 1 deletion src/http/esp/esp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export class EspService {
brightness: 255,
});
} catch (e) {
console.log(e);
return "Something went wrong!";
}
return id;
Expand Down
150 changes: 5 additions & 145 deletions src/http/lights/color/color.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ import { BadRequestException, Injectable } from "@nestjs/common";
import { isEqual, uniq } from "lodash";
import { CustomException } from "src/exceptions/custom-exception.exception";
import { DatabaseEspService } from "src/services/database/esp/database-esp.service";
import tinycolor from "tinycolor2";
import { NothingChangedException } from "../../../exceptions/nothing-changed.exception";
import { OffException } from "../../../exceptions/off.exception";
import { Light, PartialLight, StandartResponse } from "../../../interfaces";
import { EspDocument } from "../../../schemas/esp.schema";
import { TcpService } from "../../../services/tcp/tcp.service";
import { UtilsService } from "../../../services/utils/utils.service";
import { BlinkLedsDto } from "./dto/blink-leds.dto";
import { BlinkingLedsDto } from "./dto/blinking-leds.dto";
import { CustomData, CustomPatternDto } from "./dto/custom-pattern.dto";
import { FadingLedsDto } from "./dto/fading-leds.dto";
import { UpdateLedsDto } from "./dto/update-leds.dto";

@Injectable()
Expand All @@ -21,14 +18,13 @@ export class ColorService {
private databaseService: DatabaseEspService,
private utilsService: UtilsService,
private tcpService: TcpService,
) {}
) { }

async updateLeds(
id: string,
data: UpdateLedsDto,
): Promise<StandartResponse<Light>> {
data.colors = data.colors ?? [];

let errors = this.utilsService.isValidPattern(data);
if (errors?.length > 0) throw new BadRequestException(errors);

Expand Down Expand Up @@ -74,75 +70,17 @@ export class ColorService {
};
}

async fadeToColor(
id: string,
data: FadingLedsDto,
): Promise<StandartResponse<Light>> {
data.color = this.utilsService.makeValidHex(data.color);
data.delay = data.delay ?? 1000;
data.time = data.time ?? 5000;

const oldDoc = await this.databaseService.getEspWithId(id);

if (oldDoc.leds.pattern != "plain")
throw new BadRequestException("Pattern must be Plain!");
if (oldDoc.leds.colors[0] === data.color)
throw new NothingChangedException();

let resDoc: EspDocument;
if (data.delay < 1000 || data.time < 2000) {
this.tcpService.sendData(
this.utilsService.genJSONforEsp({
command: "fade",
data: { color: data.color },
}),
oldDoc.ip,
);

resDoc = await this.databaseService.updateEspWithId(id, {
leds: { colors: [data.color], pattern: "plain" },
});
} else {
resDoc = await this.databaseService.updateEspWithId(id, {
leds: {
colors: [data.color],
pattern: "fading",
},
});

let color: string = data.color;
let time: number = data.time;
let delay: number = data.delay;
this.utilsService
.fading(
resDoc.ip,
tinycolor(resDoc.leds.colors[0]),
tinycolor(color),
time,
delay,
)
.then(() => {
this.databaseService.updateEspWithId(oldDoc.uuid, {
leds: {
colors: [color],
pattern: oldDoc.leds.pattern,
},
});
});
return {
message: "Fading the color!",
object: DatabaseEspService.espDocToLight(resDoc),
};
}
}

async blinkColor(
id: string,
data: BlinkLedsDto,
): Promise<StandartResponse<Light>> {
data.color = this.utilsService.makeValidHex(data.color);
const doc: EspDocument = await this.databaseService.getEspWithId(id);



if (!doc.isOn) throw new OffException();

this.tcpService.sendData(
this.utilsService.genJSONforEsp({
command: "blink",
Expand All @@ -156,84 +94,6 @@ export class ColorService {
};
}

async blinkColorLoop(
id: string,
data: BlinkingLedsDto,
): Promise<StandartResponse<Light>> {
let length: number = data?.colors?.length ?? 0;

//make helper method
for (let i = 0; i < length; i++) {
data.colors[i] = this.utilsService.makeValidHex(data.colors[i]);
}

const oldDoc: EspDocument = await this.databaseService.getEspWithId(id);

if (oldDoc.leds.pattern != "plain")
throw new BadRequestException("Pattern must be Plain!");

const newDoc = await this.databaseService.updateEspWithId(id, {
leds: {
colors: data.colors ?? oldDoc.leds.colors,
pattern: "blinking",
},
});

if (data.delay < 100) data.delay = 100;
const delay = data.delay;
let runs: number = Math.ceil(data.time / delay);
let cIndex: number = 0;
let blinkColor: string = "#000000";

const runInterval = setInterval(() => {
if (runs <= 0) {
this.tcpService.sendData(
this.utilsService.genJSONforEsp({
command: "leds",
data: {
colors: this.utilsService.hexArrayToRgb(oldDoc.leds.colors),
pattern: "plain",
},
}),
oldDoc.ip,
);

this.databaseService.updateEspWithId(id, {
leds: {
colors: oldDoc.leds.colors,
pattern: oldDoc.leds.pattern,
},
});
clearInterval(runInterval);
return;
}

let prevColor: string = data.colors
? data.colors[cIndex] ?? oldDoc.leds.colors[0]
: oldDoc.leds.colors[0];
blinkColor = blinkColor == "#000000" ? prevColor : "#000000";

if (blinkColor == "#000000") {
cIndex = cIndex >= data?.colors?.length - 1 ? 0 : cIndex + 1;
}

this.tcpService.sendData(
this.utilsService.genJSONforEsp({
command: "leds",
data: { colors: blinkColor, pattern: "plain" },
}),
oldDoc.ip,
);

runs--;
}, delay);

return {
message: "Blinking colors!",
object: DatabaseEspService.espDocToLight(newDoc),
};
}

async applyCustom(
id: string,
data: CustomPatternDto,
Expand Down
3 changes: 2 additions & 1 deletion src/http/lights/color/dto/blink-leds.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import {
IsHexColor,
IsInt,
IsNotEmpty,
IsString,
IsString
} from "class-validator";
export class BlinkLedsDto {

@IsString()
@IsHexColor()
@IsNotEmpty()
Expand Down
26 changes: 0 additions & 26 deletions src/http/lights/color/dto/blinking-leds.dto.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/http/lights/control/control.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class ControlService {
const doc: EspDocument = await this.databaseService.deleteEspWithId(id);

return {
message: "Resetting...",
message: "Succesfully deleted light",
object: DatabaseEspService.espDocToLight(doc),
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/http/lights/general/general.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class GeneralService {

async get(id: string): Promise<StandartResponse<Light>> {
return {
message: "Light with Id " + id,
message: "Light with Id:" + id,
object: DatabaseEspService.espDocToLight(
await this.databaseService.getEspWithId(id),
),
Expand All @@ -35,7 +35,7 @@ export class GeneralService {
const doc: EspDocument = await this.databaseService.getEspWithId(id);
this.tcpService.sendData(data, doc.ip);
return {
message: "Passed data",
message: "Succesfully passed data!",
object: DatabaseEspService.espDocToLight(doc),
};
}
Expand Down
19 changes: 1 addition & 18 deletions src/http/lights/lights.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import {
Patch,
Post,
Put,
ValidationPipe,
ValidationPipe
} from "@nestjs/common";
import { Light, PartialLight, StandartResponse } from "../../interfaces";
import { ColorService } from "./color/color.service";
import { BlinkLedsDto } from "./color/dto/blink-leds.dto";
import { BlinkingLedsDto } from "./color/dto/blinking-leds.dto";
import { CustomPatternDto } from "./color/dto/custom-pattern.dto";
import { UpdateLedsDto } from "./color/dto/update-leds.dto";
import { ControlService } from "./control/control.service";
Expand Down Expand Up @@ -126,14 +125,6 @@ export class LightsController {
return this.colorService.updateLeds(id, data);
}

@Post(":light/fade")
async fadeToColor(
@Param("light") id: string,
@Body(new ValidationPipe()) data: { color: string; time: number },
): Promise<StandartResponse<Light>> {
return this.colorService.fadeToColor(id, data);
}

@Post("/:light/blink")
async blinkColor(
@Param("light") id: string,
Expand All @@ -142,14 +133,6 @@ export class LightsController {
return this.colorService.blinkColor(id, data);
}

@Post("/:light/blink/fancy")
async blinkColorLoop(
@Param("light") id: string,
@Body(new ValidationPipe()) data: BlinkingLedsDto,
): Promise<StandartResponse<Light>> {
return this.colorService.blinkColorLoop(id, data);
}

@Patch("/:light/custom")
async applyCustom(
@Param("light") id: string,
Expand Down
4 changes: 2 additions & 2 deletions src/http/lights/lights.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
MiddlewareConsumer,
Module,
NestModule,
RequestMethod,
RequestMethod
} from "@nestjs/common";
import { MongooseModule } from "@nestjs/mongoose";
import { LightValidationMiddleware } from "src/middlewares/light-validation.middleware";
Expand Down Expand Up @@ -35,6 +35,6 @@ export class LightsModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(LightValidationMiddleware)
.forRoutes({ path: "lights/:light/(.*)", method: RequestMethod.ALL });
.forRoutes({ path: "lights/:light(.*)", method: RequestMethod.ALL });
}
}
6 changes: 3 additions & 3 deletions src/http/lights/settings/settings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class SettingsService {
throw new NothingChangedException("No changes were made");
}
return {
message: "Succesfully updatet Light!",
message: "Succesfully updated Light!",
object: DatabaseEspService.espDocToLight(newDoc),
};
}
Expand Down Expand Up @@ -60,7 +60,7 @@ export class SettingsService {
});

return {
message: "Succesfully updated Lights brightness",
message: "Succesfully updated lights brightness!",
object: DatabaseEspService.espDocToLight(newDoc),
};
}
Expand All @@ -82,7 +82,7 @@ export class SettingsService {
count: count,
});
return {
message: "Succesfully updated LED count",
message: "Succesfully updated LED count!",
object: DatabaseEspService.espDocToLight(newDoc),
};
}
Expand Down
Loading

0 comments on commit b9ac4a3

Please sign in to comment.