1 /*
2  * This file is part of Arduino.
3  *
4  * Arduino is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * As a special exception, you may use this file as part of a free software
19  * library without restriction.  Specifically, if other files instantiate
20  * templates or use macros or inline functions from this file, or you compile
21  * this file and link it with other files to produce an executable, this
22  * file does not by itself cause the resulting executable to be covered by
23  * the GNU General Public License.  This exception does not however
24  * invalidate any other reasons why the executable file might be covered by
25  * the GNU General Public License.
26  *
27  * Copyright 2013 Arduino LLC (http://www.arduino.cc/)
28  */
29 
30 package cc.arduino.packages.discoverers;
31 
32 import cc.arduino.packages.BoardPort;
33 import cc.arduino.packages.Discovery;
34 import cc.arduino.packages.discoverers.serial.SerialBoardsLister;
35 
36 import java.util.LinkedList;
37 import java.util.List;
38 import java.util.Timer;
39 
40 public class SerialDiscovery implements Discovery {
41 
42   private Timer serialBoardsListerTimer;
43   private final List<BoardPort> serialBoardPorts;
44   private SerialBoardsLister serialBoardsLister = new SerialBoardsLister(this);
45 
SerialDiscovery()46   public SerialDiscovery() {
47     this.serialBoardPorts = new LinkedList<>();
48   }
49 
50   @Override
listDiscoveredBoards()51   public List<BoardPort> listDiscoveredBoards() {
52     return getSerialBoardPorts(false);
53   }
54 
55   @Override
listDiscoveredBoards(boolean complete)56   public List<BoardPort> listDiscoveredBoards(boolean complete) {
57     return getSerialBoardPorts(complete);
58   }
59 
getSerialBoardPorts(boolean complete)60   private List<BoardPort> getSerialBoardPorts(boolean complete) {
61       if (complete) {
62         return new LinkedList<>(serialBoardPorts);
63       }
64       List<BoardPort> onlineBoardPorts = new LinkedList<>();
65       for (BoardPort port : serialBoardPorts) {
66         if (port.isOnline() == true) {
67           onlineBoardPorts.add(port);
68         }
69       }
70       return onlineBoardPorts;
71   }
72 
setSerialBoardPorts(List<BoardPort> newSerialBoardPorts)73   public void setSerialBoardPorts(List<BoardPort> newSerialBoardPorts) {
74       serialBoardPorts.clear();
75       serialBoardPorts.addAll(newSerialBoardPorts);
76   }
77 
forceRefresh()78   public void forceRefresh() {
79     serialBoardsLister.retriggerDiscovery(false);
80   }
81 
setUploadInProgress(boolean param)82   public void setUploadInProgress(boolean param) {
83     serialBoardsLister.uploadInProgress = param;
84   }
85 
pausePolling(boolean param)86   public void pausePolling(boolean param) { serialBoardsLister.pausePolling = param;}
87 
88   @Override
start()89   public void start() {
90     this.serialBoardsListerTimer = new Timer(SerialBoardsLister.class.getName());
91     serialBoardsLister.start(serialBoardsListerTimer);
92   }
93 
94   @Override
stop()95   public void stop() {
96     this.serialBoardsListerTimer.purge();
97   }
98 }
99