001package kamera;
002
003import processing.core.*; 
004
005import java.net.*;
006import java.util.Enumeration;
007import java.util.Collections;
008
009import java.util.Properties;
010import java.util.Set;
011import java.util.Map;
012
013//import org.apache.http.conn.util.InetAddressUtils;
014
015//Um TIMEOFFSET.txt vom Downloadbereich zu holen. Achtung: Read/Wrizte-Permission auf Filesystem nötig.
016
017/**
018*  <h4>Info.java</h4>
019*
020*  Copyright (C) 2015 Guido Kramann<br/>
021*  kramann@fh-brandenburg.de<br/>
022*  http://www.kramann.info<br/>
023*  <br/>
024*  
025*  Stellt Informationen über den Rechner zur Verfügung.             <br/>
026*  U.a. das aktuelle Betriebssystem (Linux, Windows, Android, ...)  <br/>
027*  Kann dazu genutzt werden, zwischen Plattform abhängigem Code     <br/>
028*  automatisch umzuschalten.                                        <br/>
029*  <br/>
030*  ACHTUNG: Die Methoden isAndroid() und isLinux() müssen noch ausführlich getestet werden. <br/>
031*  Es kann sein, dass die Art der Festlegung (Betriebssystem-Version enthält das Wort "generic") nicht immer funktioniert.<br/>
032*/
033public class Info
034{
035    public static long TIMEOFFSETMILLIS = 0;
036    
037    private String[] hex = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
038
039    private String user_name;
040    private String home_verzeichnis;
041    private String betriebssystem;
042    private String betriebssystem_architektur;
043    private String betriebssystem_version;
044    private String zeilenumbruch;
045    private String ip = null;
046    private String mac = null;
047
048    public String getUserName() { return user_name; }
049    public String getHomeVerzeichnis() { return home_verzeichnis; }
050    public String getBetriebssystem() { return betriebssystem; }
051    public String getBetriebssystemArchitektur() { return betriebssystem_architektur; }
052    public String getBetriebssystemVersion() { return betriebssystem_version; }
053    public String getZeilenumbruch() { return zeilenumbruch; }
054    public String getIP() { return ip; }
055    public String getMAC() { return mac; }
056
057    
058    public static long currentTimeMillis()
059    {
060        return System.currentTimeMillis() + TIMEOFFSETMILLIS;
061    }
062    
063    /**
064    *   Liefert Array mit zwei Spalten:<br/>
065    *   Bezeichner | Wert
066    */
067    public String[][] getInfo()
068    {
069        return new String[][] {
070                                  {"UserName",user_name},
071                                  {"HomeVerzeichnis",home_verzeichnis},
072                                  {"Betriebssystem",betriebssystem},
073                                  {"BetriebssystemArchitektur",betriebssystem_architektur},
074                                  {"BetriebssystemVersion",betriebssystem_version},
075                                  {"Zeilenumbruch",zeilenumbruch},
076                                  {"IP",ip},
077                                  {"MAC",mac}
078                              };
079    }
080
081    public void printInfo()
082    {
083        String[][] inf = getInfo();
084        for(int i=0;i<inf.length;i++)
085        {
086            System.out.println(i+".: "+inf[i][0]+": "+inf[i][1]);
087        }
088    }
089
090    /**
091    *   Ausgabe aller Info-Daten auf dem Screen von Processing. <br/>
092    *   Aufrufbeispiel aus einem Processing-Scetch heraus: <br/>
093    *   Info info = new Info();<br/>
094    *   info.showInfo(this,50,50);<br/>
095    */
096    public void showInfo(PApplet pap, int xoffset, int yoffset)
097    {
098        String[][] inf = getInfo();
099        for(int i=0;i<inf.length;i++)
100        {
101            pap.text(i+".: "+inf[i][0]+": "+inf[i][1],xoffset,yoffset+30*i);
102        }
103    }
104
105    public Info()
106    {
107        Properties prop = System.getProperties();
108        Set<String> keys = prop.stringPropertyNames();
109        Object[] keyso = keys.toArray();
110        //for(int i=0;i<keyso.length;i++)
111        //    println(keyso[i]);
112        betriebssystem     = prop.getProperty("os.name");
113        betriebssystem_architektur = prop.getProperty("os.arch");
114        betriebssystem_version = prop.getProperty("os.version");
115        home_verzeichnis   = prop.getProperty("user.home"); 
116        user_name          = prop.getProperty("user.name"); 
117        zeilenumbruch      = prop.getProperty("line.separator");   
118
119        //bestimmeIPundMAC();
120
121        ip = getLocalIpv4Address();
122
123        System.out.println("ip="+ip);
124        System.out.println("mac="+mac);
125        
126    }   
127
128    public boolean isAndroid()
129    {
130        if(betriebssystem.equals("Linux") && !betriebssystem_version.contains("generic"))
131            return true;
132        else
133            return false;
134    }
135    public boolean isLinux()
136    {
137        if(betriebssystem.equals("Linux") && betriebssystem_version.contains("generic"))
138            return true;
139        else
140            return false;
141    }
142
143
144    /**
145    *   Android-Code IP-Adresse holen
146    */
147    public String getLocalIpv4Address() 
148        {
149        try {
150            for (Enumeration<NetworkInterface> networkInterfaceEnum = NetworkInterface
151                    .getNetworkInterfaces(); networkInterfaceEnum.hasMoreElements();) 
152                        {
153                NetworkInterface networkInterface = networkInterfaceEnum.nextElement();
154                for (Enumeration<InetAddress> ipAddressEnum = networkInterface
155                        .getInetAddresses(); ipAddressEnum.hasMoreElements();) 
156                                {
157                    InetAddress inetAddress = (InetAddress) ipAddressEnum
158                            .nextElement();
159                    //---Prüfen, ob es sich um eine Loopback-Adresse handelt,
160                    // dass es IPv4 ist---
161                                        String iptest = ""+inetAddress;
162                    if (!inetAddress.isLoopbackAddress()
163                                             //würde nur unter Android laufen:
164                                             //&& InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())
165                                             //ersatzweise:
166                                             && !iptest.contains(":")
167                                           ) 
168                                        {
169                                                //gleich MAC-Adresse mit bestimmen:
170                                                byte[] ha = networkInterface.getHardwareAddress();
171                                                mac="";
172                                                for(int k=0;k<ha.length;k++)
173                                                {
174                                                    int x = ha[k];
175                                                    if(x<0)
176                                                        x=256+x;
177                                                    mac+=hex[x/16];
178                                                    mac+=hex[x%16];
179                                                    if(k<ha.length-1)
180                                                        mac+=":";
181                                                } 
182
183
184                        return inetAddress.getHostAddress();
185                    }
186                }
187            }
188        } catch (SocketException ex) {
189//          Log.e("getLocalIpv4Address", ex.toString());
190        }
191        return null;
192    }
193
194    /**
195     * Hilfsmethode, um zu bestimmen, welches Device "die kelinste IP" hat.
196     * Liefert bei 172.14.0.83 die 83<br/>
197     * Liefert bei 127.0.0.1 die 1<br/>
198     * usw.
199     */
200    public int getNiedrigstenIPteil(String ipa)
201    {
202         if(ipa!=null && ipa.length()>=7)
203         {
204             int end = ipa.length()-1;
205             int anzahl=0;
206             while(ipa.charAt(end)!='.' && end>0 )
207             {
208                 end--;
209                 anzahl++;
210             }    
211             String part = ipa.substring(end+1,end+1+anzahl);    
212             return (int)Double.parseDouble(part);
213         }
214         else
215         {
216             return Integer.MAX_VALUE;
217         }
218    }
219    
220    /**
221    *   Linux-Code IP- und MAC-Adresse  bestimmen
222    */
223    public void bestimmeIPundMAC()
224    {
225        try 
226        {
227            ip = "";
228            Enumeration<NetworkInterface> ints = NetworkInterface.getNetworkInterfaces();
229            for(NetworkInterface ni : Collections.list(ints))
230            {
231                 byte[] ha = ni.getHardwareAddress();
232                 Enumeration<InetAddress> add = ni.getInetAddresses();
233                 for(InetAddress ia : Collections.list(add))
234                 {
235                     String ip_neu = ""+ia.getHostAddress();
236                     System.out.println("alternative ip: "+ip_neu);
237                     if(!ip_neu.contains(":") && ip_neu.length()>ip.length())
238                     {
239                         ip = ip_neu;
240                         mac="";
241                         for(int k=0;k<ha.length;k++)
242                         {
243                             int x = ha[k];
244                             if(x<0)
245                                 x=256+x;
246                             mac+=hex[x/16];
247                             mac+=hex[x%16];
248                             if(k<ha.length-1)
249                                 mac+=":";
250                         } 
251                     }
252                     System.out.println(""+ia.getHostAddress());
253                 }
254            }
255
256//            if(mac==null)
257//                ip = getLocalIpv4Address();
258        } 
259        catch (Exception e) 
260        {
261
262        }      
263    }
264}