当前位置: 首页 > news >正文

Java 家庭物联网

家庭物联网系统的代码和说明,包括用户认证、设备控制、数据监控、通知和警报、日志记录以及WebSocket实时更新功能。

### 项目结构

```plaintext
home-iot-system
├── backend
│   └── src
│       └── main
│           └── java
│               └── com
│                   └── example
│                       └── homeiot
│                           ├── config
│                           ├── controller
│                           ├── model
│                           ├── repository
│                           ├── service
│                           ├── websocket
│                           └── HomeIotApplication.java
├── frontend
│   ├── public
│   └── src
│       ├── components
│       ├── pages
│       ├── services
│       └── App.js
├── pom.xml
└── package.json
```

### 后端(Spring Boot)

#### `pom.xml`

```xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://www.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>home-iot-system</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
```

#### `HomeIotApplication.java`

```java
package com.example.homeiot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HomeIotApplication {
    public static void main(String[] args) {
        SpringApplication.run(HomeIotApplication.class, args);
    }
}
```

#### 用户认证和角色管理

##### `SecurityConfig.java`

```java
package com.example.homeiot.config;

import com.example.homeiot.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final UserService userService;

    public SecurityConfig(UserService userService) {
        this.userService = userService;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests()
                .antMatchers("/api/**").authenticated()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .anyRequest().permitAll()
                .and()
            .formLogin()
                .and()
            .httpBasic();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
```

##### `Role.java`

```java
package com.example.homeiot.model;

import javax.persistence.*;
import java.util.Set;

@Entity
public class Role {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;

    @ManyToMany(mappedBy = "roles")
    private Set<User> users;

    // getters and setters
}
```

##### `User.java`

```java
package com.example.homeiot.model;

import javax.persistence.*;
import java.util.Set;

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String username;
    private String password;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
      name = "user_role", 
      joinColumns = @JoinColumn(name = "user_id"), 
      inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles;

    // getters and setters
}
```

##### `UserRepository.java`

```java
package com.example.homeiot.repository;

import com.example.homeiot.model.User;
import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<User, Long> {
    User findByUsername(String username);
}
```

##### `RoleRepository.java`

```java
package com.example.homeiot.repository;

import com.example.homeiot.model.Role;
import org.springframework.data.repository.CrudRepository;

public interface RoleRepository extends CrudRepository<Role, Long> {
}
```

##### `UserService.java`

```java
package com.example.homeiot.service;

import com.example.homeiot.model.User;
import com.example.homeiot.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

@Service
public class UserService implements UserDetailsService {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    public User save(User user) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        return userRepository.save(user);
    }

    public User findByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("User not found");
        }
        return org.springframework.security.core.userdetails.User
                .withUsername(username)
                .password(user.getPassword())
                .authorities(user.getRoles().stream()
                        .map(role -> "ROLE_" + role.getName().toUpperCase())
                        .toArray(String[]::new))
                .build();
    }
}
```

#### 设备数据监控和日志记录

##### `Device.java`

```java
package com.example.homeiot.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;

@Entity
public class Device {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String status;
    private String data;
    private LocalDateTime lastUpdated;

    // getters and setters
}
```

##### `DeviceLog.java`

```java
package com.example.homeiot.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;

@Entity
public class DeviceLog {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private Long deviceId;
    private String status;
    private String data;
    private LocalDateTime timestamp;

    // getters and setters
}
```

##### `DeviceLogRepository.java`

```java
package com.example.homeiot.repository;

import com.example.homeiot.model.DeviceLog;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface DeviceLogRepository extends CrudRepository<DeviceLog, Long> {
    List<DeviceLog> findByDeviceId(Long deviceId);
}
```

##### `DeviceRepository.java`

```java
package com.example.homeiot.repository;

import com.example.homeiot.model.Device;
import org.springframework.data.repository.CrudRepository;

public interface DeviceRepository extends CrudRepository<Device, Long> {
}
```

##### `DeviceService.java`

```java
package com.example.homeiot.service;

import com.example.homeiot.model.Device;
import com.example.homeiot.model.DeviceLog;
import com.example.homeiot.repository.DeviceLogRepository;
import com.example.homeiot.repository.DeviceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeviceService {
    @Autowired
    private DeviceRepository deviceRepository;

    @Autowired
    private DeviceLogRepository deviceLogRepository;

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    public List<Device> getAllDevices() {
        return (List<Device>) deviceRepository.findAll();
    }

    public Device addDevice(Device device) {
        device.setLastUpdated(LocalDateTime.now());
        return deviceRepository.save(device);
    }

    public Device updateDeviceStatus(Long id, String status) {
        Device device = deviceRepository.findById(id).orElseThrow(() -> new RuntimeException("Device not found"));
        device.setStatus(status);
        device.setLastUpdated(LocalDateTime.now());
        deviceRepository.save(device);

        DeviceLog log = new DeviceLog();
        log.setDeviceId(id);
        log.setStatus(status);
        log.setTimestamp(LocalDateTime.now());
        deviceLogRepository.save(log);

        messagingTemplate.convertAndSend("/topic/devices", device);

        return device;
    }

    public List<DeviceLog> getDeviceLogs(Long deviceId) {
        return deviceLogRepository.findByDeviceId(deviceId);
    }
}
```

##### `DeviceController.java`

```java
package com.example.homeiot.controller;

import com.example.homeiot.model.Device;
import com.example.homeiot.model.DeviceLog;
import com.example.homeiot.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/devices")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    @GetMapping
    public List<Device> getAllDevices() {
        return deviceService.getAllDevices();
    }

    @PostMapping
    public Device addDevice(@RequestBody Device device) {
        return deviceService.addDevice(device);
    }

    @PutMapping("/{id}/status")
    public Device updateDeviceStatus(@PathVariable Long id, @RequestParam String status) {
        return deviceService.updateDeviceStatus(id, status);
    }

    @GetMapping("/{id}/logs")
    public List<DeviceLog> getDeviceLogs(@PathVariable Long id) {
        return deviceService.getDeviceLogs(id);
    }

    @MessageMapping("/changeStatus")
    @SendTo("/topic/devices")
    public Device changeDeviceStatus(Device device) {
        return deviceService.updateDeviceStatus(device.getId(), device.getStatus());
    }
}
```

#### WebSocket 实时更新

##### `WebSocketConfig.java`

```java
package com.example.homeiot.websocket;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws").withSockJS();
    }
}
```

### 前端(React)

#### `package.json`

```json
{
  "name": "home-iot-frontend",
  "version": "1.0.0",
  "dependencies": {
    "axios": "^0.21.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.3",
    "sockjs-client": "^1.5.0",
    "@stomp/stompjs": "^6.1.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }
}
```

#### `App.js`

```jsx
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import SockJS from 'sockjs-client';
import { Stomp } from '@stomp/stompjs';

function App() {
  const [devices, setDevices] = useState([]);
  const [deviceName, setDeviceName] = useState('');
  const [client, setClient] = useState(null);

  useEffect(() => {
    fetchDevices();
    connectWebSocket();
  }, []);

  const fetchDevices = () => {
    axios.get('/api/devices')
      .then(response => setDevices(response.data))
      .catch(error => console.error('Error fetching devices:', error));
  };

  const addDevice = () => {
    axios.post('/api/devices', { name: deviceName, status: 'off' })
      .then(response => {
        setDevices([...devices, response.data]);
        setDeviceName('');
      })
      .catch(error => console.error('Error adding device:', error));
  };

  const updateDeviceStatus = (deviceId, status) => {
    axios.put(`/api/devices/${deviceId}/status`, null, { params: { status } })
      .then(response => {
        const updatedDevices = devices.map(device => device.id === deviceId ? response.data : device);
        setDevices(updatedDevices);
      })
      .catch(error => console.error('Error updating device status:', error));
  };

  const connectWebSocket = () => {
    const socket = new SockJS('/ws');
    const stompClient = Stomp.over(socket);
    stompClient.connect({}, frame => {
      console.log('Connected: ' + frame);
      stompClient.subscribe('/topic/devices', message => {
        const updatedDevice = JSON.parse(message.body);
        setDevices(prevDevices =>
          prevDevices.map(device => device.id === updatedDevice.id ? updatedDevice : device)
        );
      });
    });
    setClient(stompClient);
  };

  return (
    <div>
      <h1>Home IoT System</h1>
      <input
        type="text"
        value={deviceName}
        onChange={e => setDeviceName(e.target.value)}
        placeholder="Enter device name"
      />
      <button onClick={addDevice}>Add Device</button>
      <ul>
        {devices.map(device => (
          <li key={device.id}>
            {device.name} - {device.status}
            <button onClick={() => updateDeviceStatus(device.id, device.status === 'off' ? 'on' : 'off')}>
              Toggle Status
            </button>
          </li>
        ))}
      </ul>
    </div>
  );
}

export default App;
```

系统具备了用户认证、角色管理、设备数据监控、日志记录、通知和警报、以及WebSocket实时更新功能。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 正则表达式练习
  • Hive排序字段解析
  • 提升用户体验之requestAnimationFrame实现前端动画
  • 嵌入式通信协议全解析:SPI、I²C、UART详解(附带面试题)
  • 使用gitlab的CI/CD实现logseq笔记自动发布为单页应用
  • 算法-位图与底层运算逻辑
  • k8s-第十节-Ingress
  • WPF UI 界面布局 魔术棒 文字笔记识别 技能提升 布局功能扩展与自定义 继承Panel的对象,测量与排列 系列七
  • leetcode判断二分图
  • 机器学习——岭回归
  • 基于Pinia的WebSocket管理与优化实践(实现心跳重连机制,异步发送)
  • C# 用户权限界面的测试内容
  • (数据大屏)(Hadoop)基于SSM框架的学院校友管理系统的设计与实现+文档
  • 1119 胖达与盆盆奶
  • 深入Django(八)
  • 【许晓笛】 EOS 智能合约案例解析(3)
  • 2019.2.20 c++ 知识梳理
  • css的样式优先级
  • DOM的那些事
  • iOS | NSProxy
  • Java程序员幽默爆笑锦集
  • node和express搭建代理服务器(源码)
  • Node项目之评分系统(二)- 数据库设计
  • SpringCloud集成分布式事务LCN (一)
  • VUE es6技巧写法(持续更新中~~~)
  • 闭包--闭包之tab栏切换(四)
  • 关于字符编码你应该知道的事情
  • 目录与文件属性:编写ls
  • 数组的操作
  • 王永庆:技术创新改变教育未来
  • 小程序开发之路(一)
  • 硬币翻转问题,区间操作
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件
  • 与 ConTeXt MkIV 官方文档的接驳
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • [Shell 脚本] 备份网站文件至OSS服务(纯shell脚本无sdk) ...
  • Unity3D - 异步加载游戏场景与异步加载游戏资源进度条 ...
  • # windows 运行框输入mrt提示错误:Windows 找不到文件‘mrt‘。请确定文件名是否正确后,再试一次
  • #java学习笔记(面向对象)----(未完结)
  • #经典论文 异质山坡的物理模型 2 有效导水率
  • #知识分享#笔记#学习方法
  • $.ajax()参数及用法
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (WSI分类)WSI分类文献小综述 2024
  • (八十八)VFL语言初步 - 实现布局
  • (附源码)spring boot儿童教育管理系统 毕业设计 281442
  • (附源码)ssm考试题库管理系统 毕业设计 069043
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (七)glDrawArry绘制
  • (四)鸿鹄云架构一服务注册中心
  • (万字长文)Spring的核心知识尽揽其中
  • (一一四)第九章编程练习
  • ***监测系统的构建(chkrootkit )
  • .bashrc在哪里,alias妙用
  • .htaccess配置重写url引擎