驱动和参考文档
- 文档
Java Language Center http://docs.mongodb.org/ecosystem/drivers/java/
- 驱动和开发库
Mongo Java Driver:已经放到Maven库中了,http://central.maven.org/maven2/org/mongodb/mongo-java-driver/
import com.mongodb.DB; import com.mongodb.MongoClient;
MongoClient mongo = new MongoClient(host, port); DB db = mongo.getDB("lmiot");
package com.iotlabs.lmiot.device.imp; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import com.iotlabs.lmiot.device.Device; import com.iotlabs.lmiot.device.DeviceRegistry; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; public class DeviceServiceImpl extends Device implements DeviceRegistry { private static final Logger logger = Logger.getLogger(DeviceServiceImpl.class.getName()); private DeviceStatusService status = null; private DBCollection collection; private DB db; public void setMongoDb(DB db) { this.db = db; this.collection = db.getCollection("devices"); } public void setDeviceStatusService(DeviceStatusService status) { this.status = status; } @Override public Device register(String deviceId, String name, String description) throws Exception { logger.log(Level.INFO, "注册终端{0}", new Object[]{deviceId}); BasicDBObject doc = new BasicDBObject(); doc.put("device_id", deviceId); doc.put("name", name); doc.put("description", description); doc.put("registration_date", new Date()); doc.put("active", true); Device device = this.find(deviceId); if(device != null) { throw new Exception("注册设备失败,此设备已经被注册!"); } else { this.collection.insert(doc); device = this.find(deviceId); } return device; } @Override public Device find(String deviceId) { DeviceServiceImpl device = null; BasicDBObject q = new BasicDBObject(); q.put("device_id", deviceId); BasicDBObject fields = null; BasicDBObject orderby = null; DBObject one = this.collection.findOne(q, fields, orderby); if(one != null) { device = new DeviceServiceImpl(); device.setMongoDb(this.db); device.setId((String) one.get("device_id")); device.setName((String) one.get("name")); device.setDescription((String) one.get("description")); device.setRegistrationDate((Date) one.get("registration_date")); device.online = status.isOnline(deviceId); device.active = (Boolean) one.get("active"); } return device; } @Override public Device[] find() { List<DeviceServiceImpl> devices = new ArrayList<DeviceServiceImpl>(); DBCursor curs = this.collection.find(); while(curs.hasNext()) { DBObject one = curs.next(); if(one != null) { DeviceServiceImpl device = new DeviceServiceImpl(); device.setMongoDb(this.db); device.setId((String) one.get("device_id")); device.setName((String) one.get("name")); device.setDescription((String) one.get("description")); device.setRegistrationDate((Date) one.get("registration_date")); device.online = status.isOnline(device.getId()); device.active = (Boolean) one.get("active"); devices.add(device); } } Device[] deviceArray = new Device[]{}; return devices.toArray(deviceArray); } // for device @Override public void active() { this.active = true; BasicDBObject q = new BasicDBObject(); q.put("device_id", this.getId()); BasicDBObject u = new BasicDBObject(); u.put("active", this.active); this.collection.update(q, new BasicDBObject("$set", u), true, false); } @Override public void inactive() { this.active = false; BasicDBObject q = new BasicDBObject(); q.put("device_id", this.getId()); BasicDBObject u = new BasicDBObject(); u.put("active", this.active); this.collection.update(q, new BasicDBObject("$set", u), true, false); } @Override public void online() { this.online = true; status.online(this.getId()); } @Override public void offline() { this.online = false; status.offline(this.getId()); } }