Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
6.8 kB
3
Indexable
Never
zad2:

SOUNDLEVELMONITOR :

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class SoundLevelMonitor {

    public static void main(String[] args) {
        while (true) {
            try {
                File soundFile = new File("soundfile");
                Scanner scanner = new Scanner(soundFile);
                int sum = 0;

                while (scanner.hasNextInt()) {
                    sum += scanner.nextInt();
                }

                scanner.close();
                int avg = sum / 10;

                String level;

                if (avg >= 40 && avg < 60) {
                    level = "LOW";
                } else if (avg >= 60 && avg < 80) {
                    level = "MEDIUM";
                } else {
                    level = "HIGH";
                }

                File noiseFile = new File("noisepollution");
                FileWriter writer = new FileWriter(noiseFile);
                writer.write(level);
                writer.flush();
                writer.close(); // Close the FileWriter

                Thread.sleep(3000);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

DOCKER :

FROM eclipse-temurin

WORKDIR /usr/src/myapp

COPY . .
RUN mkdir data
RUN javac SoundLevelMonitor.java

CMD ["java", "SoundLevelMonitor"]



SOUND SENSOR :

import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class SoundLevelSensor {
    public static void main(String[] args) {
        try {
            FileWriter writer = new FileWriter("soundlevel");
            Random random = new Random();

            while (true) {
                for (int i = 0; i < 10; i++) {
                    int soundLevel = random.nextInt(61) + 40; // Generating random number between 40 and 100
                    writer.write(soundLevel + " ");
                }
                writer.write("\n"); // New line after every 10 numbers
                writer.flush();
                Thread.sleep(20000); // Wait for 20 seconds
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

FROM eclipse-temurin

WORKDIR /usr/src/myapp

COPY . .
RUN mkdir data
RUN javac SoundLevelMonitor.java

CMD ["java", "SoundLevelMonitor"]


version: '3.0'

services:
  sound-level-sensor:
    build:
      context: ./Sound-sensor
      dockerfile: Dockerfile
    volumes:
      - ./Sound-sensor:/app/

  sound-level-monitor:
    build:
      context: ./Sound-monitor
      dockerfile: Dockerfile
    volumes:
      - ./Sound-monitor:/app/
      - ./Sound-sensor:/app/
    depends_on:
      - sound-level-sensor















=========================

ZAD3:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class SoundLevelMonitorr {

    public static void main(String[] args) {
        while (true) {
            try {
                File soundFile = new File("soundfile");
                Scanner scanner = new Scanner(soundFile);
                int sum = 0;

                while (scanner.hasNextInt()) {
                    sum += scanner.nextInt();
                }

                scanner.close();
                int avg = sum / 10;

                String level;

                // Читање на опкружувачките променливи
                int lowSoundLevel = Integer.parseInt(System.getenv("LOW_SOUNDLEVEL"));
                int mediumSoundLevel = Integer.parseInt(System.getenv("MEDIUM_SOUNDLEVEL"));
                int highSoundLevel = Integer.parseInt(System.getenv("HIGH_SOUNDLEVEL"));

                if (avg >= lowSoundLevel && avg < mediumSoundLevel) {
                    level = "LOW";
                } else if (avg >= mediumSoundLevel && avg < highSoundLevel) {
                    level = "MEDIUM";
                } else {
                    level = "HIGH";
                }

                File noiseFile = new File("noisepollution");
                FileWriter writer = new FileWriter(noiseFile);
                writer.write(level);
                writer.flush();
                writer.close(); // Close the FileWriter

                Thread.sleep(3000);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}


FROM eclipse-temurin

WORKDIR /usr/src/myapp

COPY . .
RUN mkdir data
RUN javac SoundLevelMonitorr.java
ENV LOW_SOUNDLEVEL 40
ENV MEDIUM_SOUNDLEVEL 60
ENV HIGH_SOUNDLEVEL 80
CMD ["java", "SoundLevelMonitorr"]


sensor :

import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class SoundLevelSensorr {
    public static void main(String[] args) {
        try {
            FileWriter writer = new FileWriter("soundlevel");
            Random random = new Random();

            while (true) {
                for (int i = 0; i < 10; i++) {
                    int soundLevel = random.nextInt(61) + 40; 
                    writer.write(soundLevel + " ");
                }
                writer.write("\n"); // New line after every 10 numbers
                writer.flush();
                Thread.sleep(20000); // Wait for 20 seconds
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

FROM eclipse-temurin

WORKDIR /usr/src/myapp

COPY . .
RUN mkdir data
RUN javac SoundLevelMonitor.java

CMD ["java", "SoundLevelMonitor"]

#NETWORK city-noisepollution-network


services:
  sound-level-sensor2:
    build:
      context: ./Sound-sensor2
      dockerfile: Dockerfile
    environment:
      - LOW_SOUNDLEVEL=40
      - MEDIUM_SOUNDLEVEL=60
      - HIGH_SOUNDLEVEL=80
    volumes:
      - ./Sound-sensor2:/app/
    networks:
      - city-noisepollution-network
    ports:
      - "80:80"

  sound-level-monitor2:
    build:
      context: ./Sound-monitor2
      dockerfile: Dockerfile
    environment:
      - LOW_SOUNDLEVEL=40
      - MEDIUM_SOUNDLEVEL=60
      - HIGH_SOUNDLEVEL=80
    volumes:
      - ./Sound-monitor2:/app/
      - ./Sound-sensor2:/app/
    depends_on:
      - sound-level-sensor2
    networks:
      - city-noisepollution-network
    ports:
      - "8080:8080"

networks:
  city-noisepollution-network:
    driver: bridge
Leave a Comment