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实时更新功能。

相关推荐
gobeyye9 分钟前
spring loC&DI 详解
java·spring·rpc
鱼跃鹰飞10 分钟前
Leecode热题100-295.数据流中的中位数
java·服务器·开发语言·前端·算法·leetcode·面试
我是浮夸13 分钟前
MyBatisPlus——学习笔记
java·spring boot·mybatis
小蜗笔记16 分钟前
在Python中实现多目标优化问题(7)模拟退火算法的调用
开发语言·python·模拟退火算法
TANGLONG22220 分钟前
【C语言】数据在内存中的存储(万字解析)
java·c语言·c++·python·考研·面试·蓝桥杯
杨荧22 分钟前
【JAVA开源】基于Vue和SpringBoot的水果购物网站
java·开发语言·vue.js·spring boot·spring cloud·开源
知识分享小能手35 分钟前
mysql学习教程,从入门到精通,SQL 修改表(ALTER TABLE 语句)(29)
大数据·开发语言·数据库·sql·学习·mysql·数据分析
魏大橙37 分钟前
Fastjson反序列化
开发语言·python
cyz14100140 分钟前
vue3+vite@4+ts+elementplus创建项目详解
开发语言·后端·rust
立黄昏粥可温1 小时前
Python 从入门到实战34(实例2:绘制蟒蛇)
开发语言·python