【毕设_11】终端完成&数据库设计

一、终端代码

      单片机程序已全部完成,本着开源的思想,虽然还未答辩,毕竟也不是什么重要的代码程序。下载链接

pp.PNG

二、数据库设计

      目前已完成获取终端信息状态,并保存到数据库中。
      下面是数据表设计

| 数据表 |||||
| -------- |
| 表名 | redata ||||
| 功能说明 | 保存智能开关插座状态信息 ||||
| 数据库用户| 智能开关插座管理人员 ||||
| 主键| id ||||
| 其他字段| intensity, heat, light, state, opentime, closetime||||
| 索引字段| ||||
| 名称| 数据类型 | 长度 | 主键 | 注释 |
| id | int | 2 | true| 主键 |
| intensity | varchar | 6 | false| 光照强度 |
| heat| int | 2 | false | 热感 1 有人 0 无人 |
| light| int | 2 | false | 亮度 1 亮 0 暗 |
| state| int | 2 | false | 开关状态 1 开 0 关 |
| opentime| time | 0 | false | 定时打开时间 |
| closetime| time| 0 | false | 定时关闭时间 |

      保存数据代码程序已完成。下载链接

 1public class stmstart {
 2
 3	public static final String IP_ADDR = "192.168.2.1";//服务器地址 
 4
 5	public static final int PORT = 8000;//服务器端口号  
 6
 7	public static Socket s=null;	
 8
 9	public static RecvThread	rth=null;	
10	public static void main(String[] args) {
11		// TODO Auto-generated method stub
12		try {
13			s = new Socket(IP_ADDR, PORT);
14			rth = new RecvThread();
15			rth.start();	
16			while (true);
17		} catch (UnknownHostException e) {
18			// TODO Auto-generated catch block
19			e.printStackTrace();
20		} catch (IOException e) {
21			// TODO Auto-generated catch block
22			e.printStackTrace();
23		} finally {
24			rth.interrupt();
25			try {
26				s.close();
27			} catch (IOException e) {
28				// TODO Auto-generated catch block
29				e.printStackTrace();
30			}
31		}
32	}
33	
34	public static class RecvThread extends Thread{
35		@Override
36		public void run() {
37			// TODO Auto-generated method stub
38			while(true){
39				try {	
40					InputStream in=s.getInputStream();
41				    byte[] data=new byte[1024];
42				    int len=in.read(data);
43				    String res = new String(data,0,len,"GBK");
44				    String intensity = res.substring(3, 8);
45				    int heat = Integer.parseInt(res.substring(8, 9));
46				    int light = Integer.parseInt(res.substring(9, 10));
47				    int state =  Integer.parseInt(res.substring(10, 11));
48				    String opentime = res.substring(11, 13)+":"+res.substring(13, 15)+":"+res.substring(15, 17);
49				    String closetime = res.substring(17, 19)+":"+res.substring(19, 21)+":"+res.substring(21, 23);
50				    update(intensity,heat,light,state, opentime,closetime);			    			    
51				} 
52				catch (Exception e) {
53					
54				}
55			}
56		}
57	}
58	public static String byteArrayToStr(byte[] byteArray) {
59		if (byteArray == null) {
60			return null;
61		}
62		String str = null;
63		try {
64			str = new String(byteArray,"GBK");
65		} catch (UnsupportedEncodingException e) {
66			// TODO Auto-generated catch block
67			e.printStackTrace();
68		}
69		return str;
70	}
71	public static  void update(String intensity,int heat,int light,int state,String opentime,String closetime){
72		String sql="update redata set intensity=?,heat=?,light=?,state=?,opentime=?,closetime=? where id=1";
73		Connection conn = DBUtil.open();
74		try {
75			PreparedStatement pstmt = conn.prepareStatement(sql);
76			pstmt.setString(1, intensity);
77			pstmt.setInt(2, heat);
78			pstmt.setInt(3, light);
79			pstmt.setInt(4, state);
80			pstmt.setString(5, opentime);
81			pstmt.setString(6, closetime);
82			pstmt.executeUpdate();
83		} catch (SQLException e) {
84			e.printStackTrace();
85		}finally{
86			DBUtil.close(conn);
87		}
88	}
89}
90

三、B/S 控制端

      UI初步已完成,待完善。

四、Server 端

      响应式控制终端未实现。

今天你努力了吗
    评论
    0 评论
avatar

取消