public TaskRead(Map map1, Map m_max, Map m_min, DBCollection coll, ModbusSlaveSet slave) { try { //// 此处只要点击开始就能保存数据 /// 方案:利用数据源发送单个布尔型变量(1,0)交替,链接成功,先判断是否链接成功,然后在保存数据 synchronized (slave) { Map map = new HashMap(); Set set1 = map1.entrySet(); Iterator it1 = set1.iterator(); while (it1.hasNext()) { Map.Entry<String, Map<String, String>> entry = (Map.Entry<String, Map<String, String>>) it1.next(); Map<String, String> map2 = entry.getValue(); for (Iterator it2 = map2.entrySet().iterator(); it2.hasNext(); ) { Map.Entry<String, String> entry2 = (Map.Entry<String, String>) it2.next(); String name = entry2.getKey().toString(); String paramAddr = entry2.getValue().toString(); int fun = (int) (Double.parseDouble(paramAddr)); if (paramAddr.substring(0, 1).equals("4")) { Short d4 = slave.getProcessImage(1).getInputRegister(fun % 10000); double dmax = (Double) m_max.get(name); double dmin = (Double) m_min.get(name); double dValue = 0; if (d4 >= 0) { dValue = dmax * d4 / 32000; } else { dValue = dmin * d4 / (-32000); } map.put(name, dValue); } if (paramAddr.substring(0, 1).equals("3")) { Short d3 = slave.getProcessImage(1).getHoldingRegister(fun % 10000); double dmax = (Double) m_max.get(name); double dmin = (Double) m_min.get(name); double dValue = 0; if (d3 >= 0) { dValue = dmax * d3 / 32000; } else { dValue = dmin * d3 / (-32000); } map.put(name, dValue); } if (paramAddr.substring(0, 1).equals("2")) { map.put(name, slave.getProcessImage(2).getInput(fun % 10000)); } if (paramAddr.substring(0, 1).equals("1")) { Boolean a = slave.getProcessImage(2).getCoil(fun % 10000 - 1); map.put(name, a); } } } Calendar calendar = Calendar.getInstance(); Date dd = calendar.getTime(); BasicDBObject doc = new BasicDBObject(); doc.put("_id", dd); Set set = map.entrySet(); Iterator it = set.iterator(); while (it.hasNext()) { Map.Entry<String, String> entry1 = (Map.Entry<String, String>) it.next(); doc.put(entry1.getKey(), entry1.getValue()); } coll.insert(doc); } } catch (Exception ex) { } }
public static void main(String[] args) { try { meet myobj = new meet(); // load jsiconfig.txt ArrayList<String> myconfiglist = new ArrayList<String>(); myconfiglist = myobj.loadArray("jsiconfig.txt"); // The text uri // "mongodb://*****:*****@ds023288.mongolab.com:23288/sample"; String textUri = myconfiglist.get(0); // Create MongoClientURI object from which you get MongoClient obj MongoClientURI uri = new MongoClientURI(textUri); // Connect to that uri MongoClient m = new MongoClient(uri); // get the database named sample String DBname = myconfiglist.get(1); DB d = m.getDB(DBname); // get the collection mycollection in sample String collectionName = myconfiglist.get(2); DBCollection collection = d.getCollection(collectionName); // System.out.println("Config: "+textUri+":"+DBname+":"+collectionName); // twitter4j // Twitter twitter = new TwitterFactory().getInstance(); Twitter twitter = new TwitterFactory().getSingleton(); User user = twitter.verifyCredentials(); // Twitter collection of latest tweets into the home account - defaulted to latest 20 tweets// ////////////////////////////////////////////////////////////// ArrayList<String> mylatesttweetslist = new ArrayList<String>(); // get list of tweets from a user or the next tweet listed try { long actid = 0; // twitter.createFriendship(actid); // The factory instance is re-useable and thread safe. // Twitter twitter = TwitterFactory.getSingleton(); List<Status> statuses = twitter.getHomeTimeline(); System.out.println("Showing home timeline."); for (Status status : statuses) { // System.out.println(status.getUser().getName() + ":" +status.getText()); // Addes timeline to an array String mytweets = status.getUser().getName() + ":" + status.getText(); mylatesttweetslist.add(mytweets); } ; } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to get timeline: " + te.getMessage()); System.exit(-1); } // MongoDB Insert Below // ////////////////////////// // System Date Date sd = new Date(); String sysdate = sd.toString(); // Toggle the below to display and insert the transactions as required boolean showtrans = true; boolean inserttrans = true; // checkArray - loads args to a text string to allow the contains function String checkString = ""; for (int ck = 0; ck < args.length; ck++) { checkString = checkString + args[ck]; } ; // display transactions flag on runnning jsimongo eg: java jsimongo -d will NOT display // transactions // insert transactions flag on runnning jsimongo eg: java jsimongo -i will NOT insert // transactions if (args.length > 0) { if (checkString.contains("-d")) showtrans = false; if (checkString.contains("-i")) inserttrans = false; } ; int x = 0; for (String atweet : mylatesttweetslist) { x++; // Display tweets to console if (showtrans == true) { System.out.println("tweet : " + atweet); System.out.println("Created_DateTime : " + sysdate); // was sysdate } ; // Insert JSON into MongoDB if (inserttrans == true) { BasicDBObject b = new BasicDBObject(); System.out.println("tweet : " + atweet); System.out.println("Created_DateTime : " + sysdate); // was sysdate // Insert the JSON object into the chosen collection collection.insert(b); } ; Thread.sleep(1); } ; System.out.println("End, the number of tweets inserted at this time was: " + x); } catch (Exception e) { e.printStackTrace(); } }