Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2bcf8024 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

add Parsing stuff in DeviceHelper

parent 1b3682d6
Loading
Loading
Loading
Loading
+62 −1
Original line number Diff line number Diff line
@@ -15,7 +15,18 @@
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package ecorp.easy.installer.helpers;
import ecorp.easy.installer.models.steps.IStep;
import ecorp.easy.installer.models.Process;
import static ecorp.easy.installer.utils.ConfigParser.parseSteps;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
/**
 *
 * @author Andre Lam
@@ -23,6 +34,10 @@ import java.util.HashMap;
 * @author Ingo
 */
public class DeviceHelper {
    private final static Logger logger = LoggerFactory.getLogger(DeviceHelper.class);
    private final static String YAML_FOLDER_PATH = "/yaml/";

    
    private static final HashMap<String, String> map  = new HashMap<String, String>() {{
        put("hero2lte", "0001");
        put("herolte",  "0002");
@@ -39,10 +54,56 @@ public class DeviceHelper {

    /**
     * Return internal code for a given device
     * @param key the device code's name (example: Samsung galaxy S7 => herolte)
     * @param key the ADB device code's name (example: Samsung galaxy S7 => herolte)
     * @return can return null if no key matches
     */
    public static String getDeviceInternalcode(String key){
        return map.get(key);
    }
    
    /**
     * Load a yaml file 
     * @param filename the filename (don't forget extension)
     * @return a Map instance with yaml parsed content, or null
     * @throws IOException yaml file access give IO Error
     */
    private static HashMap loadYaml(String filename) throws IOException{
        logger.info("loadYaml("+YAML_FOLDER_PATH+filename+")");
        HashMap result = null;
        
        final URL url = DeviceHelper.class
                .getResource(YAML_FOLDER_PATH+filename);
        if (url == null)
            logger.debug("URL of yaml is null");
        else{
            try(InputStream is = url.openStream() ) {
                Yaml yaml = new Yaml();
                //load config file
                result = (HashMap) yaml.load(is);
            }
        }
        return result;
    }

    
    
    /**
     * Load FlashProcess for given Device
     * @param adbDevice
     * @return Process
     * @throws java.io.IOException  
     * @throws java.text.ParseException  
     */
    public static Process loadFlashProcess(String adbDevice) throws IOException, ParseException, NumberFormatException, NullPointerException{

        final Map yamlContent = loadYaml(adbDevice+"_flash.yml");
        final int stepsCount = (Integer) yamlContent.get("stepsCount");
        final Process flashingProcess = new Process(stepsCount);
        HashMap<String, IStep> steps = parseSteps( (HashMap) yamlContent.get("steps") );

        flashingProcess.setSteps(steps );
        
        return flashingProcess;
    }
    
}
+3 −3
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ import java.util.HashMap;
 */
public class Process {

    private final int stepCount; //Number of step to consider
    private final int stepsCount; //Number of step to consider
    private HashMap<String, IStep> steps; //Map of steps to follow

    /**
@@ -36,7 +36,7 @@ public class Process {
     * @param stepCount int number of step used for FlashProcess
     */
    public Process(int stepCount){
        this.stepCount = stepCount;
        this.stepsCount = stepCount;
        this.steps = new HashMap<>();
    }
    
@@ -45,7 +45,7 @@ public class Process {
     * @return 
     */
    public int getStepCount() {
        return stepCount;
        return stepsCount;
    }

    /**
+21 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -45,6 +46,26 @@ import org.slf4j.LoggerFactory;
public class ConfigParser {
    private final static Logger logger = LoggerFactory.getLogger(ConfigParser.class);
    
    
    
    
    /**
     * Parse steps from yaml (both for preparation and flashing)
     * @param yaml The data map load from yaml file
     * @return an HashMap of IStep
     * @throws ParseException 
     */
    public static HashMap<String, IStep> parseSteps(Map yaml)throws ParseException, NumberFormatException{
        logger.debug("parseSteps(yaml)");
        final HashMap<String, IStep> result = new HashMap<>();
        for(String key : (Set<String>) yaml.keySet() ){
            IStep step = ConfigParser.parseStep( (Map) yaml.get(key) );
            result.put(key,step);
        }
        return result;
    }

    
    /**
     * Create a single Step instance from a Map<String, String> containing yaml content
     * @param yaml