whyclj
2019-06-24 66db8d445a53a8ed8410f7196f5c65de7a29bce7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package com.dev.btse.data;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import com.battmonitor.base.Com;
import com.battmonitor.sql.MysqlConnPool;
import com.battmonitor.sql.Sql_Mysql;
 
public class TmpSensor_SQL {
    
    
    public static void createRealDataBase(MysqlConnPool conn_pool) {
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());        
        try {
            sql.sqlMysqlExecute("CREATE DATABASE IF NOT EXISTS " + Sql_Mysql.DB_Sensor);                    //´´½¨´«¸ÐÆ÷ÐÅÏ¢±í
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public static void createHisDataBase(MysqlConnPool conn_pool) {
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());        
        try {
            sql.sqlMysqlExecute("CREATE DATABASE IF NOT EXISTS " + Sql_Mysql.DB_Sensor_History);            //´´½¨´«¸ÐÆ÷ÀúÊ·Êý¾Ý±í
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * ´´½¨´«¸ÐÆ÷ÐÅÏ¢±í
     * @param conn_pool
     */
    public static void createSensor_infTable(MysqlConnPool conn_pool) {
        String sql_str = "CREATE TABLE IF NOT EXISTS " + Sql_Mysql.SensorInf_Table + " (" + 
                "  `num` bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  `province` varchar(255) NOT NULL COMMENT 'Ê¡' DEFAULT ''," + 
                "  `city` varchar(255) NOT NULL COMMENT 'ÊÐ' DEFAULT ''," + 
                "  `county` varchar(255) NOT NULL COMMENT 'Çø/ÏØ' DEFAULT ''," + 
                "  `sensor_dev_id` int(11) NOT NULL DEFAULT '0' COMMENT 'É豸id'," + 
                "  `device_name` varchar(255) NOT NULL COMMENT 'É豸Ãû³Æ' DEFAULT ''," + 
                "  `note` varchar(255) NOT NULL DEFAULT ''," + 
                "  `airtmp_alarm` float NOT NULL DEFAULT '52.5'," + 
                "  `airhum_alarm` float NOT NULL DEFAULT '40.8'," + 
                "  `smoke_alarm` int(11) NOT NULL DEFAULT '8000'," + 
                "  PRIMARY KEY (`num`)," + 
                "  KEY `int_sensor_dev_id` (`sensor_dev_id`) USING BTREE" + 
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
        
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str);
            //Ìí¼Ó¿ÕÆøÎ¶ȸ澯ÁÐ
            ResultSet res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_sensor'"
                    + " AND table_name='tb_sensor_inf'"
                    + " AND column_name='airtmp_alarm'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.SensorInf_Table 
                                + " ADD COLUMN `airtmp_alarm` float NOT NULL DEFAULT '52.5';");
            }
            //Ìí¼Ó¿ÕÆøÊª¶È¸æ¾¯ÁÐ
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_sensor'"
                    + " AND table_name='tb_sensor_inf'"
                    + " AND column_name='airhum_alarm'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.SensorInf_Table 
                        + " ADD COLUMN `airhum_alarm` float NOT NULL DEFAULT '40.8';");
            }
            //Ìí¼ÓÑ̸и澯ÁÐ
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_sensor'"
                    + " AND table_name='tb_sensor_inf'"
                    + " AND column_name='smoke_alarm'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.SensorInf_Table 
                        + " ADD COLUMN `smoke_alarm` int(11) NOT NULL DEFAULT '8000';");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    /**
     * ´´½¨´«¸ÐÆ÷¶¨Î»ÐÅÏ¢±í
     * @param conn_pool
     */
    public static void createSensor_mapinfoTable(MysqlConnPool conn_pool) {
        String sql_str = "CREATE TABLE IF NOT EXISTS "+ Sql_Mysql.SensorMapinfo_Table +" (" + 
                "  `num` bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  `sensor_dev_id` int(11) NOT NULL DEFAULT ''," + 
                "  `longitude` double NOT NULL DEFAULT '0'," + 
                "  `latitude` double NOT NULL DEFAULT '0'," + 
                "  `address` varchar(255) NOT NULL DEFAULT ''," + 
                "  `note` varchar(255) NOT NULL DEFAULT ''," + 
                "  PRIMARY KEY (`num`)," + 
                "  UNIQUE KEY `unique_sensor_dev_id` (`sensor_dev_id`) USING BTREE" + 
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
 
    /**
     * ´´½¨´«¸ÐÆ÷É豸ÐÅÏ¢±í
     * @param conn_pool
     */
    public static void createSensor_StateTable(MysqlConnPool conn_pool) {
        String sql_str = "CREATE TABLE IF NOT EXISTS "+Sql_Mysql.SensorState_Table+" (" + 
                "  `num` bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  `sensor_dev_id` int(11) NOT NULL COMMENT 'É豸id'," + 
                "  `record_time` datetime NOT NULL COMMENT '¼Ç¼ʱ¼ä'," + 
                "  `airtmp` float NOT NULL COMMENT '¿ÕÆøÎ¶È'," + 
                "  `airhum` float NOT NULL COMMENT '¿ÕÆøÊª¶È'," + 
                "  `smoke` int(11) NOT NULL COMMENT 'Ñ̸Ð'," + 
                "  `water` int(11) NOT NULL," + 
                "  `lightintensity` int(11) NOT NULL," + 
                "  `CO2concentration` int(11) NOT NULL," + 
                "  `COconcentration` int(11) NOT NULL," + 
                "  `CH4concentration` int(11) NOT NULL," + 
                "  `O2concentration` int(11) NOT NULL," + 
                "  `dev_commcount` int(11) NOT NULL," + 
                "  `dev_errcommcount` int(11) NOT NULL," + 
                "  `note` varchar(255) NOT NULL DEFAULT ''," + 
                "  PRIMARY KEY (`num`)," + 
                "  UNIQUE KEY `index_sensor_dev_id` (`sensor_dev_id`) USING BTREE" + 
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    /**
     * ¸ù¾ÝÉ豸id²éѯÊÇ·ñ´æÔÚµ±Ç°É豸
     * @param conn_pool
     * @param sensor_dev_id
     * @return
     */
    public static TmpSensor_inf queryTmpSensorById(MysqlConnPool conn_pool,int sensor_dev_id) {
        TmpSensor_inf sensor = null;
        String sql_str = "SELECT * FROM db_sensor.tb_sensor_inf WHERE sensor_dev_id = "+sensor_dev_id;
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try {
            ResultSet res = sql.sqlMysqlQuery(sql_str);
            if(res.next()) {
                sensor = new TmpSensor_inf();
                sensor.setCity(res.getString("city"));
                sensor.setProvince(res.getString("province"));
                sensor.setCounty(res.getString("county"));
                sensor.setDevice_name(res.getString("device_name"));
                sensor.setSensor_dev_id(res.getInt("sensor_dev_id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
        return sensor;
    }
    
    /**
     *     ²åÈë»ò¸üÐÂÉ豸״̬
     * @param conn_pool
     * @param state
     */
    public static void insertOrUpdateTmpSensorState(MysqlConnPool conn_pool,TmpSensorState state) {
        String sql_str = Sql_Mysql.SensorState_Table +
                " SET sensor_dev_id = " + state.sensor_dev_id + "," + 
                " record_time = '" + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms) + "'," + 
                " airtmp = " + state.getAirtmp()+"," + 
                " airhum = " + state.getAirhum()+"," + 
                " smoke = " + state.getSmoke()+"," + 
                " water = " + state.getWater()+"," + 
                " lightintensity = " + state.getLightintensity()+"," + 
                " CO2concentration = " + state.getCO2concentration()+"," + 
                " COconcentration = " + state.getCOconcentration()+"," + 
                " CH4concentration = " + state.getCH4concentration()+"," + 
                " O2concentration = " + state.getO2concentration()+"," + 
                " dev_commcount = " + state.getDev_commcount()+"," + 
                " dev_errcommcount = " + state.getDev_errcommcount();
        String sql_str_replace = "REPLACE INTO " + sql_str;
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str_replace);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    /**
     *  ²éѯËùÓеĴ«¸ÐÆ÷É豸
     * @return
     */
    public static List<TmpSensor_inf> queryAllTmpSensor(MysqlConnPool pool){
        String sql_str = 
                        " SELECT * " + 
                        " FROM db_sensor.tb_sensor_inf " ;
        List<TmpSensor_inf> tmpinf = new ArrayList<>();
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res = sql.sqlMysqlQuery(sql_str);
        try {
            while(res.next()) {
                TmpSensor_inf tmp = new TmpSensor_inf();
                tmp.setCity(res.getString("city"));
                tmp.setCounty(res.getString("county"));
                tmp.setDevice_name(res.getString("device_name"));
                tmp.setProvince(res.getString("province"));
                tmp.setSensor_dev_id(res.getInt("sensor_dev_id"));
                
                tmpinf.add(tmp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
        return tmpinf;
    }
    
    //²éѯָ¶¨µÄ´«¸ÐÆ÷µÄ״̬
    public static TmpSensorState queryTmpSensorState(MysqlConnPool pool,int sensor_dev_id) {
        TmpSensorState state = null ;
        String sql_str = "SELECT * " + 
                " FROM " + Sql_Mysql.SensorState_Table + 
                " WHERE sensor_dev_id = " + sensor_dev_id;
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res = sql.sqlMysqlQuery(sql_str);
        try {
            if(res.next()) {
                state = new TmpSensorState();
                state.setSensor_dev_id(res.getInt("sensor_dev_id"));
                state.setAirhum(res.getFloat("airhum"));
                state.setAirtmp(res.getFloat("airtmp"));
                state.setSmoke(res.getInt("smoke"));
                state.setWater(res.getInt("water"));
                state.setLightintensity(res.getInt("lightintensity"));
                state.setCO2concentration(res.getInt("cO2concentration"));
                state.setCH4concentration(res.getInt("cH4concentration"));
                state.setCOconcentration(res.getInt("cOconcentration"));
                state.setO2concentration(res.getFloat("o2concentration"));
                state.setRecord_time(res.getTimestamp("record_time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
        return state;
    }
 
    /**
     * ´´½¨Ö¸¶¨µÄ´«¸ÐÆ÷ÀúÊ·Êý¾Ý±í
     * @param pool
     * @param sensor_dev_id
     */
    public static void createTmpSensorHistoryDataTable(MysqlConnPool pool, int sensor_dev_id) {
        String sql_str = "CREATE TABLE IF NOT EXISTS  "+Sql_Mysql.DB_Sensor_History+".`tb_sensor_hisdata_" + sensor_dev_id + "` (" + 
                "  `num` bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  `sensor_dev_id` int(11) NOT NULL DEFAULT '1' COMMENT 'É豸id'," + 
                "  `record_time` datetime NOT NULL DEFAULT '2000-01-01 00:00:00'," + 
                "  `airtmp` float NOT NULL DEFAULT '0' COMMENT '¿ÕÆøÎ¶È'," + 
                "  `airhum` float NOT NULL DEFAULT '0' COMMENT '¿ÕÆøÊª¶È'," + 
                "  `smoke` int(11) NOT NULL DEFAULT '100' COMMENT 'Ñ̸Ð'," + 
                "  `water` int(11) NOT NULL DEFAULT '0' COMMENT 'Ë®½þ'," + 
                "  `lightintensity` int(11) NOT NULL DEFAULT '0' COMMENT '¹âÕÕÇ¿¶È'," + 
                "  `CO2concentration` int(11) NOT NULL DEFAULT '0' COMMENT 'CO2Ũ¶È'," + 
                "  `COconcentration` int(11) NOT NULL DEFAULT '0'," + 
                "  `CH4concentration` int(11) NOT NULL DEFAULT '0'," + 
                "  `O2concentration` int(11) NOT NULL DEFAULT '0'," + 
                "  `note` varchar(255) DEFAULT NULL DEFAULT ''," + 
                "  PRIMARY KEY (`num`)," + 
                "  KEY `index_sensor_dev_id` (`sensor_dev_id`) USING BTREE" + 
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
        
    }
    
    /**
     * ±£´æÀúÊ·µÄ´«¸ÐÆ÷Êý¾Ý
     * @param pool
     * @param state
     */
    public static void saveSensorHistoryData(MysqlConnPool pool,TmpSensorState state) {
        String sql_str = "INSERT INTO "+Sql_Mysql.DB_Sensor_History +".`tb_sensor_hisdata_"+state.sensor_dev_id+"`"
                + "(sensor_dev_id,record_time,airtmp,airhum,smoke,water,lightintensity,CO2concentration,COconcentration,CH4concentration,O2concentration)"
                + " VALUES ("
                    +state.getSensor_dev_id()+", '"
                    +Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)+"', "
                    +state.getAirtmp()+","
                    +state.getAirhum()+", "
                    +state.getSmoke()+","
                    +state.getWater()+", "
                    +state.getLightintensity()+", "
                    +state.getCO2concentration()+", "
                    +state.getCOconcentration()+","
                    +state.getCH4concentration()+", "
                    +state.getO2concentration()+
                ");";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
}