/*
 * Copyright 2016 Javier Garcia Alonso.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jutils.jhardware.info.graphicscard.unix;

import com.profesorfalken.jsensors.JSensors;
import com.profesorfalken.jsensors.model.components.Gpu;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jutils.jhardware.info.graphicscard.AbstractGraphicsCardInfo;
import org.jutils.jhardware.util.HardwareInfoUtils;

/**
 *
 * @author javier
 */
public class UnixGraphicsCardInfo extends AbstractGraphicsCardInfo {

    private static final String LSHW_COMMAND = "lshw";
    private static final String LSHW_PARAMS = "-C";

    private static String getGraphicsCardData() {
        return HardwareInfoUtils.executeCommand(LSHW_COMMAND, LSHW_PARAMS, "video");
    }

    @Override
    protected Map<String, String> parseInfo() {
        Map<String, String> graphicsCardDataMap = new HashMap<>();        

        String[] dataStringLines = getGraphicsCardData().split("\\r?\\n");

        int count = -1;
        for (final String dataLine : dataStringLines) {
            String line = dataLine.trim();
            if (line.startsWith("*-display")) {
                count++;
            } else if (line.startsWith("product:")) {
                graphicsCardDataMap.put("name_" + count, line.split(":", 2)[1]);
            } else if (line.startsWith("vendor:")) {
                graphicsCardDataMap.put("manufacturer_" + count, line.split(":", 2)[1]);
            } else if (line.startsWith("description:")) {
                graphicsCardDataMap.put("chip_type_" + count, line.split(":", 2)[1]);
            }
        }

        //Get Temperature and fan speed from jSensors
        List<Gpu> gpus = JSensors.get.components().gpus;
        for (int i = 0; i < count; i++) {
            if (gpus.size() > i) {
                Gpu gpu = gpus.get(i);
                if (gpu.sensors.temperatures != null && !gpu.sensors.temperatures.isEmpty()) {
                    graphicsCardDataMap.put("temperature_" + i,
                            String.valueOf(gpu.sensors.temperatures.get(0).value.intValue()));
                }
                if (gpu.sensors.fans != null && !gpu.sensors.fans.isEmpty()) {
                    graphicsCardDataMap.put("fan_" + i,
                            String.valueOf(gpu.sensors.fans.get(0).value.intValue()));
                }
            }
        }

        graphicsCardDataMap.put("numOfGraphicsCards", String.valueOf(count + 1));

        return graphicsCardDataMap;
    }

}

@@ -1437,17 +1437,151 @@
        %0A
-%0A
+        int count = -1;%0A        %0A        String graphicsCardData = getGraphicsCardData();%0A%0A        if (graphicsCardData != null) %7B%0A    
         
@@ -1660,32 +1660,24 @@
     
-int count = -1;%0A
+    %0A   
         
 for 
@@ -1668,24 +1668,25 @@
 %0A           
+ 
 for (final S
@@ -1729,24 +1729,28 @@
             
+    
 String line 
@@ -1776,24 +1776,28 @@
             
+   
+ 
 if (line.sta
@@ -1836,16 +1836,20 @@
         
+    
 count++;
@@ -1853,32 +1853,36 @@
 ++;%0A            
+   
+ 
 %7D else if (line.
@@ -1903,24 +1903,28 @@
 oduct:%22)) %7B%0A
+    
             
@@ -1984,32 +1984,36 @@
 it(%22:%22, 2)%5B1%5D);%0A
+    
             %7D el
@@ -2057,32 +2057,36 @@
                 
+    
 graphicsCardData
@@ -2134,32 +2134,36 @@
 it(%22:%22, 2)%5B1%5D);%0A
+    
             %7D el
@@ -2212,32 +2212,36 @@
                 
+    
 graphicsCardData
@@ -2302,34 +2302,40 @@
             
-%7D%0A
+    %7D%0A  
         
 %7D%0A%0A        /
@@ -2314,35 +2314,41 @@
     %7D%0A          
+  
 %7D%0A%0A
+    
         //Get Te
@@ -2393,16 +2393,20 @@
         
+    
 List%3CGpu
@@ -2450,24 +2450,28 @@
 us;%0A        
+    
 for (int i =
@@ -2500,24 +2500,28 @@
             
+ 
+   
 if (gpus.siz
@@ -2527,24 +2527,28 @@
 ze() %3E i) %7B%0A
+    
             
@@ -2582,32 +2582,36 @@
                 
+ 
+   
 if (gpu.sensors.
@@ -2685,32 +2685,36 @@
                 
+    
 graphicsCardData
@@ -2761,32 +2761,36 @@
                 
+ 
+   
 String.valueOf(g
@@ -2848,32 +2848,36 @@
                 
+    
 %7D%0A              
@@ -2870,32 +2870,36 @@
                 
+ 
+   
 if (gpu.sensors.
@@ -2957,32 +2957,36 @@
                 
+    
 graphicsCardData
@@ -3025,32 +3025,36 @@
                 
+  
+  
 String.valueOf(g
@@ -3088,32 +3088,54 @@
 e.intValue()));%0A
+                    %7D%0A
                 
@@ -3152,33 +3152,32 @@
     %7D%0A        %7D%0A
-%0A
         graphics

Fix test when lshw is not available