├── .gitignore ├── HibernateMinRequired.userlibraries ├── README.md ├── pic ├── HointelligenceHome.jpg ├── smartHome.jpg └── smartHome1.jpg └── src ├── Dao ├── GreenHouseStatDao.java ├── GreenHouseStatDaoImple.java ├── HibernateUtil.java ├── UserDao.java └── UserDaoImple.java ├── entity ├── GreenHouseStat.java ├── User.hbm.xml └── User.java ├── hibernate.cfg.xml ├── serialException ├── ExceptionWriter.java ├── NoSuchPort.java ├── NotASerialPort.java ├── PortInUse.java ├── ReadDataFromSerialPortFailure.java ├── SendDataToSerialPortFailure.java ├── SerialPortInputStreamCloseFailure.java ├── SerialPortOutputStreamCloseFailure.java ├── SerialPortParameterFailure.java └── TooManyListeners.java └── view ├── Chart.java ├── SerialTool.java ├── loginView.java └── serialView.java /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | ### STS ### 3 | .apt_generated 4 | .classpath 5 | .factorypath 6 | .project 7 | .settings 8 | .springBeans 9 | .sts4-cache 10 | 11 | ### IntelliJ IDEA ### 12 | .idea 13 | *.iws 14 | *.iml 15 | *.ipr 16 | bin 17 | 18 | ### NetBeans ### 19 | /nbproject/private/ 20 | /nbbuild/ 21 | /dist/ 22 | /nbdist/ 23 | /.nb-gradle/ 24 | /build/ 25 | 26 | # Compiled class file 27 | *.class 28 | 29 | -------------------------------------------------------------------------------- /HibernateMinRequired.userlibraries: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HibernateSerial 2 | 3 | ## 一、系统概述 4 | 5 | ​ 本系统使用Zigbee协议栈对各传感器组网,传感器将数据定时传送给协调器,协调器通过串口将数据发送给Java编写上位机程序,上位机根据规则解析数据,并将数据保存到MySQL数据库,实现对室内的环境参数,如温湿度、光照强度、烟雾等的监测,并根据数值,控制室内空调(电机模拟)、灯光、蜂鸣器的工作,构建了智能家居系统的雏形。 6 | 7 | 8 | 9 | ## 二、系统使用说明 10 | 11 | ### 2.1、依赖包说明 12 | 13 | > 要能够构建过程需要以下依赖包: 14 | 15 | 1. 支持Java串口通信操作的jar包,RXTXcomm.jar ; 16 | 17 | 另外还需做如下配置: 18 | 19 | - 拷贝 rxtxSerial.dll 到 \jre\bin目录中; 20 | - 拷贝 rxtxParallel.dll 到 \jre\bin目录中; 21 | 22 | 2. JFreeChart图表绘制类库; 23 | 24 | 3. Hibernate的支持jar包。 25 | 26 | 27 | 28 | ### 2.2、传感器说明 29 | 30 | ​ 不同传感器的报文格式、解析方法、计算方法均有所差异,需要根据自己所拥有的传感器具体分析,修改代码,从而适应自己的设别。 31 | 32 | 33 | 34 | ## 三、系统功能说明 35 | 36 | ### 3.1、串口通信功能 37 | 38 | ​ 实现Java上位机程序与串口间的通信。 39 | 40 | ### 3.2、登录、注册功能 41 | 42 | ​ 通过登录、注册功能实现基本的安全控制、权限管理。 43 | 44 | ### 3.3、温湿度、光强、烟雾、室内人员监控 45 | 46 | ​ 温湿度传感器、光敏传感器、烟雾传感器、多普勒传感器通过Zigbee协议实现组网。各传感器定时将数据传递给协调器,协调器通过串口将数据发送给上位机,上位机解析并展示数据,并根据数据发送控制指令。 47 | 48 | ### 3.4、电机控制(模拟空调)功能 49 | 50 | ​ 根据设定的温度值,启动“空调”,实现室内温度保持在一个舒适的范围。 51 | 52 | ### 3.5、灯光亮度调节功能 53 | 54 | ​ 根据光强自动调节,保持室内亮度。 55 | 56 | ### 3.6、自动检测功能 57 | 58 | ​ 实现全自动操作,而不需要通过上位机程序中的按钮实现功能。 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /pic/HointelligenceHome.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/pic/HointelligenceHome.jpg -------------------------------------------------------------------------------- /pic/smartHome.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/pic/smartHome.jpg -------------------------------------------------------------------------------- /pic/smartHome1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/pic/smartHome1.jpg -------------------------------------------------------------------------------- /src/Dao/GreenHouseStatDao.java: -------------------------------------------------------------------------------- 1 | package Dao; 2 | 3 | import entity.GreenHouseStat; 4 | 5 | public interface GreenHouseStatDao { 6 | public abstract void GreenHouseStat_Add(GreenHouseStat ghs); 7 | } 8 | -------------------------------------------------------------------------------- /src/Dao/GreenHouseStatDaoImple.java: -------------------------------------------------------------------------------- 1 | package Dao; 2 | 3 | import org.hibernate.Session; 4 | 5 | import entity.GreenHouseStat; 6 | 7 | public class GreenHouseStatDaoImple implements GreenHouseStatDao { 8 | 9 | @Override 10 | public void GreenHouseStat_Add(GreenHouseStat ghs) { 11 | Session session = HibernateUtil.getCurrentSession(); 12 | session.save(ghs); 13 | session.beginTransaction().commit(); 14 | HibernateUtil.closeSession(session); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/Dao/HibernateUtil.java: -------------------------------------------------------------------------------- 1 | package Dao; 2 | 3 | import org.hibernate.Session; 4 | import org.hibernate.SessionFactory; 5 | import org.hibernate.cfg.Configuration; 6 | import org.hibernate.service.ServiceRegistry; 7 | import org.hibernate.service.ServiceRegistryBuilder; 8 | 9 | public class HibernateUtil { 10 | private static SessionFactory sessionFactory; 11 | static { 12 | Configuration configuration = new Configuration(); 13 | configuration.configure(); 14 | ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) 15 | .buildServiceRegistry(); 16 | sessionFactory = configuration.buildSessionFactory(serviceRegistry); 17 | } 18 | 19 | public static Session getCurrentSession() { 20 | Session session = sessionFactory.openSession(); 21 | return session; 22 | } 23 | 24 | public static void closeSession(Session session) { 25 | if (session != null) 26 | session.close(); 27 | } 28 | public static void closeSessionFactory(){ 29 | if (sessionFactory!=null) { 30 | sessionFactory.close(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Dao/UserDao.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/src/Dao/UserDao.java -------------------------------------------------------------------------------- /src/Dao/UserDaoImple.java: -------------------------------------------------------------------------------- 1 | package Dao; 2 | 3 | import org.hibernate.Session; 4 | 5 | import entity.User; 6 | 7 | public class UserDaoImple implements UserDao { 8 | 9 | @Override 10 | public boolean User_check(String username) { 11 | Session session = HibernateUtil.getCurrentSession(); 12 | User user = (User)session.get(User.class, username); 13 | HibernateUtil.closeSession(session); 14 | if (user!=null) { 15 | return true; 16 | } 17 | else 18 | return false; 19 | 20 | } 21 | 22 | @Override 23 | public void User_Login(User user) { 24 | Session session = HibernateUtil.getCurrentSession(); 25 | session.save(user); 26 | session.beginTransaction().commit(); 27 | HibernateUtil.closeSession(session); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/entity/GreenHouseStat.java: -------------------------------------------------------------------------------- 1 | package entity; 2 | 3 | import java.util.Date; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.Table; 11 | import javax.persistence.Temporal; 12 | import javax.persistence.TemporalType; 13 | 14 | @Entity 15 | @Table(name="GreenHouseStat") 16 | public class GreenHouseStat { 17 | @Column(name="GreenHouseStat_Gno") 18 | private long Gno; 19 | @Column(name="GreenHouseStat_Temper") 20 | private float temper; 21 | @Column(name="GreenHouseStat_Hum") 22 | private float hum; 23 | @Column(name="GreenHouseStat_Smoke") 24 | private String smoke; 25 | @Id 26 | @Temporal(TemporalType.TIMESTAMP) 27 | @Column(name="GreenHouseStat_Date") 28 | private Date date; 29 | 30 | public GreenHouseStat(){} 31 | 32 | public GreenHouseStat(long Gno, float temper, float hum, String smoke, Date date) { 33 | super(); 34 | this.Gno = Gno; 35 | this.temper = temper; 36 | this.hum = hum; 37 | this.smoke = smoke; 38 | this.date = date; 39 | } 40 | 41 | public long getGno() { 42 | return Gno; 43 | } 44 | 45 | public void setGno(long Gno) { 46 | this.Gno = Gno; 47 | } 48 | 49 | public float getTemper() { 50 | return temper; 51 | } 52 | 53 | public void setTemper(float temper) { 54 | this.temper = temper; 55 | } 56 | 57 | public float getHum() { 58 | return hum; 59 | } 60 | 61 | public void setHum(float hum) { 62 | this.hum = hum; 63 | } 64 | 65 | public String getSmoke() { 66 | return smoke; 67 | } 68 | 69 | public void setSmoke(String smoke) { 70 | this.smoke = smoke; 71 | } 72 | 73 | public Date getDate() { 74 | return date; 75 | } 76 | 77 | public void setDate(Date date) { 78 | this.date = date; 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/entity/User.hbm.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/entity/User.java: -------------------------------------------------------------------------------- 1 | package entity; 2 | 3 | public class User { 4 | private String username; 5 | private String password; 6 | public User(){} 7 | public User(String username, String password) { 8 | super(); 9 | this.username = username; 10 | this.password = password; 11 | } 12 | public String getUsername() { 13 | return username; 14 | } 15 | public void setUsername(String username) { 16 | this.username = username; 17 | } 18 | public String getPassword() { 19 | return password; 20 | } 21 | public void setPassword(String password) { 22 | this.password = password; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/hibernate.cfg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | com.mysql.jdbc.Driver 11 | jdbc:mysql://localhost:3306/hiberSerial 12 | yourUsername 13 | yourPasswd 14 | 15 | 16 | 1 17 | 18 | 19 | org.hibernate.dialect.MySQLDialect 20 | 21 | 22 | thread 23 | 24 | 25 | org.hibernate.cache.internal.NoCacheProvider 26 | 27 | 28 | true 29 | 30 | 31 | update 32 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/serialException/ExceptionWriter.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/src/serialException/ExceptionWriter.java -------------------------------------------------------------------------------- /src/serialException/NoSuchPort.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/src/serialException/NoSuchPort.java -------------------------------------------------------------------------------- /src/serialException/NotASerialPort.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/src/serialException/NotASerialPort.java -------------------------------------------------------------------------------- /src/serialException/PortInUse.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/src/serialException/PortInUse.java -------------------------------------------------------------------------------- /src/serialException/ReadDataFromSerialPortFailure.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/src/serialException/ReadDataFromSerialPortFailure.java -------------------------------------------------------------------------------- /src/serialException/SendDataToSerialPortFailure.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/src/serialException/SendDataToSerialPortFailure.java -------------------------------------------------------------------------------- /src/serialException/SerialPortInputStreamCloseFailure.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/src/serialException/SerialPortInputStreamCloseFailure.java -------------------------------------------------------------------------------- /src/serialException/SerialPortOutputStreamCloseFailure.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/src/serialException/SerialPortOutputStreamCloseFailure.java -------------------------------------------------------------------------------- /src/serialException/SerialPortParameterFailure.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/src/serialException/SerialPortParameterFailure.java -------------------------------------------------------------------------------- /src/serialException/TooManyListeners.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/src/serialException/TooManyListeners.java -------------------------------------------------------------------------------- /src/view/Chart.java: -------------------------------------------------------------------------------- 1 | package view; 2 | 3 | import java.awt.Color; 4 | import java.awt.Font; 5 | import java.io.IOException; 6 | 7 | import javax.swing.JFrame; 8 | import javax.swing.JPanel; 9 | import javax.swing.WindowConstants; 10 | 11 | import org.jfree.chart.ChartFactory; 12 | import org.jfree.chart.ChartPanel; 13 | import org.jfree.chart.JFreeChart; 14 | import org.jfree.chart.StandardChartTheme; 15 | import org.jfree.chart.axis.NumberAxis; 16 | import org.jfree.chart.axis.NumberTickUnit; 17 | import org.jfree.chart.plot.PlotOrientation; 18 | import org.jfree.chart.plot.XYPlot; 19 | import org.jfree.data.xy.XYDataset; 20 | import org.jfree.data.xy.XYSeries; 21 | import org.jfree.data.xy.XYSeriesCollection; 22 | import org.jfree.ui.RectangleInsets; 23 | 24 | 25 | public class Chart extends JFrame { 26 | public Chart() { 27 | } 28 | 29 | public static XYSeries xyTemseries = new XYSeries("tem"); 30 | public static XYSeries xyHumseries1 = new XYSeries("hum"); 31 | 32 | public static int hundroud = 0; 33 | public static int hundroud1 = 0; 34 | public static JFreeChart jfreechart = null; 35 | 36 | public JPanel getTemJFreeChart(){ 37 | 38 | jfreechart = ChartFactory.createXYLineChart( 39 | null, "time/s","Tem/Hum", (XYDataset) createDataset1(), 40 | PlotOrientation.VERTICAL, true, true, false); 41 | 42 | StandardChartTheme mChartTheme = new StandardChartTheme("CN"); 43 | mChartTheme.setLargeFont(new Font("黑体", Font.BOLD, 20)); 44 | mChartTheme.setExtraLargeFont(new Font("宋体", Font.PLAIN, 15)); 45 | mChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15)); 46 | // mChartTheme.setDomainGridlinePaint(Color.RED); 47 | ChartFactory.setChartTheme(mChartTheme); 48 | 49 | jfreechart.setBorderPaint(new Color(0,204,205)); 50 | jfreechart.setBorderVisible(true); 51 | 52 | XYPlot xyplot = (XYPlot) jfreechart.getPlot(); 53 | 54 | // Y轴 55 | NumberAxis numberaxis = (NumberAxis) xyplot.getRangeAxis(); 56 | numberaxis.setLowerBound(0); 57 | numberaxis.setUpperBound(70); 58 | numberaxis.setTickUnit(new NumberTickUnit(100d)); 59 | // 只显示整数值 60 | numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 61 | // numberaxis.setAutoRangeIncludesZero(true); 62 | numberaxis.setLowerMargin(0); // 数据轴下(左)边距 ­ 63 | numberaxis.setMinorTickMarksVisible(false);// 标记线是否显示 64 | numberaxis.setTickMarkInsideLength(0);// 外刻度线向内长度 65 | numberaxis.setTickMarkOutsideLength(0); 66 | 67 | // X轴的设计 68 | NumberAxis x = (NumberAxis) xyplot.getDomainAxis(); 69 | x.setAutoRange(true);// 自动设置数据轴数据范围 70 | // 自己设置横坐标的值 71 | x.setAutoTickUnitSelection(false); 72 | x.setTickUnit(new NumberTickUnit(60d)); 73 | // 设置最大的显示值和最小的显示值 74 | x.setLowerBound(0); 75 | x.setUpperBound(60); 76 | // 数据轴的数据标签:只显示整数标签 77 | x.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 78 | x.setAxisLineVisible(true);// X轴竖线是否显示 79 | x.setTickMarksVisible(true);// 标记线是否显示 80 | 81 | RectangleInsets offset = new RectangleInsets(0, 0, 0, 0); 82 | xyplot.setAxisOffset(offset);// 坐标轴到数据区的间距 83 | xyplot.setBackgroundAlpha(0.0f);// 去掉柱状图的背景色 84 | xyplot.setOutlinePaint(null);// 去掉边框 85 | 86 | // ChartPanel chartPanel = new ChartPanel(jfreechart); 87 | // chartPanel.restoreAutoDomainBounds();//重置X轴 88 | 89 | ChartPanel chartPanel = new ChartPanel(jfreechart, true); 90 | 91 | return chartPanel; 92 | } 93 | 94 | /** 95 | * 该方法是数据的设计 96 | * 97 | * @return 98 | */ 99 | public static XYDataset createDataset1() { 100 | XYSeriesCollection xyseriescollection = new XYSeriesCollection(); 101 | xyseriescollection.addSeries(xyTemseries); 102 | xyseriescollection.addSeries(xyHumseries1); 103 | return xyseriescollection; 104 | } 105 | 106 | /** 107 | * 画动态折线图,每秒显示一个温湿度的值,并于前一个点连成折线图 108 | * 每60s删除是所有点重画一次折线图的每个点。 109 | */ 110 | public static void dynamicRun() { 111 | int i = 0; 112 | while (true) { 113 | double factor = serialView.tem; 114 | hundroud = (int) factor; 115 | double factor1 = serialView.hum; 116 | hundroud1 = (int) factor1; 117 | 118 | jfreechart.setTitle("Trend map: "+"Tem="+hundroud+"degrees; "+"Hum="+hundroud1+"%"); 119 | jfreechart.getTitle().setFont(new Font("楷体", 0, 20));//设置标题字体 120 | 121 | xyTemseries.add(i, factor); 122 | xyHumseries1.add(i, factor1); 123 | 124 | //更新折线图的数值 125 | try { 126 | Thread.currentThread(); 127 | Thread.sleep(1000); 128 | } catch (InterruptedException e) { 129 | e.printStackTrace(); 130 | } 131 | i++; 132 | if (i == 60){ 133 | i=0; 134 | xyTemseries.delete(0, 59); 135 | xyHumseries1.delete(0, 59); 136 | continue; 137 | } 138 | } 139 | } 140 | 141 | /* 142 | * 创建折线图 143 | * */ 144 | public static void createChart() { 145 | Chart jz = new Chart();//新建Chart类为JFrame的子类 146 | /* 147 | * 新建窗体 148 | * */ 149 | JFrame frame0 = new JFrame();//新建JFrame窗体 150 | frame0.setSize(600, 400);//设置窗体尺寸大小 151 | frame0.getContentPane().add(jz.getTemJFreeChart());//将对象jz绘制的动态折线图添加到JFrame的面板上 152 | // frame0.getContentPane().add(jz.getTemJFreeChart(), BorderLayout.CENTER);//另外一种布局方式 153 | 154 | frame0.setVisible(true);//使得窗体对用户可见 155 | frame0.setLocationRelativeTo(null); // 窗口居于屏幕正中央 156 | frame0.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 157 | 158 | dynamicRun();//绘制动态折线图的函数 159 | 160 | } 161 | 162 | public static void main(String[] args) throws NumberFormatException, IOException { 163 | createChart();//绘制并显示动态折线图 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /src/view/SerialTool.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/src/view/SerialTool.java -------------------------------------------------------------------------------- /src/view/loginView.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/src/view/loginView.java -------------------------------------------------------------------------------- /src/view/serialView.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CUGX/HibernateSerial/8a79202947886a51ccbffe4c7009f6ec48122942/src/view/serialView.java --------------------------------------------------------------------------------