Wednesday, July 29, 2015

Selenium login code

package com.scm.test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class RegistrationTest {
   
public static void main(String[] args) {
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
//  Wait For Page To Load

// Navigate to URL
driver.get("http://192.168.1.1/");
// Maximize the window.
driver.manage().window().maximize();
// Enter UserName
driver.findElement(By.id("username")).sendKeys("admin");
// Enter Password
driver.findElement(By.id("password")).sendKeys("password");
// Wait For Page To Load
// Click on 'Sign In' button
driver.findElement(By.id("loginBtn")).click();
//Click on Compose Mail.
driver.findElement(By.xpath("//div[@class='z0']/div")).click();

//Close the browser.
driver.close();
}
}

Tuesday, June 23, 2015

TCL programs

AF: Pacific Gas and Electric aka PG&E has installed smart gas and electric meters throughout northern california which collect hourly and daily usage data. However they provide no easy way to access this data besides through their website. If you are like me and like to own your data then you can use this to download and save the comma delimited (CSV) usage information.
 package require http
 package require tdom
 package require tls
 package require vfs::zip

 http::register https 443 [list ::tls::socket]

 set tz "America/Los_Angeles"
 set datadir /usr/backup/power
 set user USER
 set pass PASSWORD

 if {$argc > 0} {
    set date [clock scan [lindex $argv 0]]
 } else {
    set date [clock seconds]
 }
 set month [clock format $date -format "%Y-%m"]

 proc cookies {t} {
    set cookies [list]

    foreach {k v} [http::meta $t] {
        if {[string tolower $k] eq "set-cookie"} {
            set v [split $v \;]
            set v [lindex $v 0]
            if {[lindex [split $v =] 1] == ""} continue
            lappend cookies $v
        }
    }
    return [list Cookie [join $cookies "; "]]
 }

 proc get_usage {month user pass} {
    set t [http::geturl "https://www.pge.com/eum/login" -query [http::formatQuery USER $user PASSWORD $pass pass_placeholder Password TARGET https://www.pge.com/myenergyweb/appmanager/pge/customer]]
 
    set cookies [cookies $t]
    http::cleanup $t

    # all this should need is EUMSessionID and SMSESSION
    set t [http::geturl "https://www.pge.com/affwebservices/public/saml2sso?SPID=sso.opower.com&RelayState=https%3A%2F%2Fpge.opower.com%2Fei%2Fapp%2FmyEnergyUse" -headers $cookies]
    set body [http::data $t]
    #parray $t
    http::cleanup $t

    set query [list]
    set html [dom parse -html $body]
    set root [$html documentElement]
    set action [[$root getElementsByTagName form] getAttribute action]
    foreach input [$root getElementsByTagName input] {
        if {[$input getAttribute type] != "hidden"} continue
        lappend query [$input getAttribute name] [$input getAttribute value]
    }
    
    
    set t [http::geturl $action -query [http::formatQuery {*}$query]]
    set body [http::data $t]
    http::cleanup $t

    set query [list]
    set html [dom parse -html $body]
    set root [$html documentElement]
    set action [[$root getElementsByTagName form] getAttribute action]
    foreach input [$root getElementsByTagName input] {
        if {[$input getAttribute type] != "hidden"} continue
        lappend query [$input getAttribute name] [$input getAttribute value]
    }
    
    set t [http::geturl $action -query [http::formatQuery {*}$query]]
    set cookies [cookies $t]
    http::cleanup $t

    set t [http::geturl "https://pge.opower.com/ei/app/myEnergyUse" -headers $cookies]
    #<a data-trigger-dialog="href" href="/ei/app/modules/customer/610138/bill_periods/export-dialog">Export your data</a>
    regexp {modules/customer/(\d+)/bill_periods} [http::data $t] -> customer
    http::cleanup $t

    set t [http::geturl "https://pge.opower.com/ei/app/modules/customer/$customer/energy/download?billing=false&bill=$month" -headers $cookies]

    if {[http::ncode $t] != 200} {
        return
    }

    set zip "/tmp/usage-$month.zip"
    set fh [open $zip w+]
    fconfigure $fh -translation binary
    puts -nonewline $fh [http::data $t]
    close $fh
    http::cleanup $t
    return $zip
 }

 proc write_csv {zip} {
    global datadir tz
    set mnt_file [vfs::zip::Mount $zip $zip]
    
    set in [open "$zip/DailyNaturalGasUsage.csv"]
    set csv [read $in]
    close $in
    #TYPE,DATE,USAGE,UNITS,NOTES
    #Natural gas usage,2011-10-27,1.03,therms,

    array set data {}
    foreach line [split $csv \n] {
        set line [split $line ,]
        if {[lindex $line 0] != "Natural gas usage"} continue
        set start [clock scan [lindex $line 1] -timezone $tz]
        set day [clock format $start -format "%Y-%m-%d"]
        lappend data($day) "$start,[lindex $line 2]"
    }
    foreach {day vals} [array get data] {
        set fn "$datadir/gas-$day.csv"
        if {[file exists $fn] && [file size $fn] != 0} continue
        set fh [open $fn w+]
        puts "writing $fn"
        puts $fh [join $vals \n]
        close $fh
    }


    set in [open "$zip/DailyElectricUsage.csv"]
    set csv [read $in]
    close $in
    #TYPE,DATE,START TIME,END TIME,USAGE,UNITS,NOTES
    #Electric usage,2011-09-28,00:00,00:59,0.16,kWh,
    
    unset -nocomplain data
    array set data {}
    foreach line [split $csv \n] {
        set line [split $line ,]
        if {[lindex $line 0] != "Electric usage"} continue
        set start [clock scan "[lindex $line 1] [lindex $line 2]" -timezone $tz]
        set day [clock format $start -format "%Y-%m-%d"]
        lappend data($day) "$start,[lindex $line 4]"
    }
    foreach {day vals} [array get data] {
        set fn "$datadir/electric-$day.csv"
        if {[file exists $fn] && [file size $fn] != 0} continue
        set fh [open $fn w+]
        puts "writing $fn"
        puts $fh [join $vals \n]
        close $fh
    }
    
    vfs::unmount $zip
 }

 set zip [get_usage $month $user $pass]
 if {$zip == ""} exit 1
 write_csv $zip
 file delete $zip

Friday, February 13, 2015

WMM Parameters





WMM Parameters for AP,STA in 11b,11a,11g,11n mode


CWmin
CWMax
AIFSNx
TXOP(ms)
AC_BK
15
1023
7
0
AC_BE
15
1023(63 for AP)
3
0
AC_VI
7
15
2(1 for AP)
3.008(6.016 for 11b)


AC_VO
3
7
2(1 for AP)
1.504(3.264 for 11b)



Wednesday, February 11, 2015

802.11 basics

DIFS

The IEEE 802.11 family of standards describe the DCF protocol, which controls access to the physical medium. A station must sense the status of the wireless medium before transmitting. If it finds that the medium is continuously idle for DCF Interframe Space (DIFS) duration, it is then permitted to transmit a frame. If the channel is found busy during the DIFS interval, the station should defer its transmission.
DIFS duration can be calculated by the following method.


DIFS = SIFS + (2 * Slot time)

Standard Slot time (µs) DIFS (µs) SIFS (µs)
IEEE 802.11b 20 50 10
IEEE 802.11a 9 34 16
IEEE 802.11g 9 or 20 28 or 50 10
IEEE 802.11n (2.4 GHz) 9 or 20 28 or 50 10
IEEE 802.11n (5 GHz) 9 34 16
IEEE 802.11ac (5 GHz) 9 34 16

IEEE 802.11g is backward compatible with IEEE 802.11b. When these devices are associated with same AP all the timing parameters are changed to 802.11b.


Short Interframe Space (SIFS), is the amount of time in micro seconds required for a wireless interface to process a received frame and to respond with a response frame. It is the difference in time between the first symbol of the response frame in the air and the last symbol of the received frame in the air. A SIFS time consists of the delay in receiver RF, PLCP delay and the MAC processing delay, which depends on the physical layer used. In IEEE 802.11 networks, SIFS is the interframe spacing prior to transmission of an acknowledgment, a Clear To Send (CTS) frame, a block ack frame that is an immediate response to either a block ack request frame or an A-MPDU, the second or subsequent MPDU of a fragment burst, a station responding to any polling a by point coordination function and during contention free periods of point coordination function.

Channels

2.4 GHz (802.11b/g/n)


Graphical representation of 2.4 GHz band channels overlapping

Graphical representation of Wireless LAN channels in 2.4 GHz band
There are 14 channels designated in the 2.4 GHz range spaced 5 MHz apart (with the exception of a 12 MHz spacing before channel 14).
Note that for 802.11g/n it is not possible to guarantee orthogonal frequency-division multiplexing (OFDM) operation thus affecting the number of possible non-overlapping channels depending on radio operation.



Channel Frequency
(MHz)
United States & Canada Europe Switzerland Japan Singapore China Israel Korea Turkey Australia South Africa Brazil Taiwan New Zealand
40/20 MHz 40/20 MHz Unknown MHz
40/20 MHz 10 MHz 40/20 MHz 40/20 MHz 20 MHz 20 MHz 40/20 MHz 40/20 MHz 40/20 MHz 40/20 MHz 40/20 MHz 40/20 MHz
183 4915 No No No No Yes No No No No No No No No No No
184 4920 No No No Yes Yes No No No No No No No No No No
185 4925 No No No No Yes No No No No No No No No No No
187 4935 No No No No Yes No No No No No No No No No No
188 4940 No No No Yes Yes No No No No No No No No No No
189 4945 No No No No Yes No No No No No No No No No No
192 4960 No No No Yes No No No No No No No No No No No
196 4980 No No No Yes No No No No No No No No No No No
7 5035 No No No No Yes No No No No No No No No No No
8 5040 No No No No Yes No No No No No No No No No No
9 5045 No No No No Yes No No No No No No No No No No
11 5055 No No No No Yes No No No No No No No No No No
12 5060 No No No No No No No No No No No No No No No
16 5080 No No No No No No No No No No No No No No No
34 5170 No No Indoors Client only
[clarification needed]
No Yes No Yes Yes Indoors No Indoors Indoors No Indoors
36 5180 Indoors Yes Indoors Indoors No Yes Yes Yes Yes Indoors Yes Indoors Indoors No Indoors
38 5190 No No Indoors Client only No Yes Yes Yes Yes Indoors No Indoors Indoors No Indoors
40 5200 Indoors Yes Indoors Indoors No Yes Yes Yes Yes Indoors Yes Indoors Indoors No Indoors
42 5210 No No Indoors Client only No Yes Yes Yes Yes Indoors No Indoors Indoors No Indoors
44 5220 Indoors Yes Indoors Indoors No Yes Yes Yes Yes Indoors Yes Indoors Indoors No Indoors
46 5230 No No Indoors Client only No Yes Yes Yes Yes Indoors No Indoors Indoors No Indoors
48 5240 Indoors Yes Indoors Indoors No Yes Yes Yes Yes Indoors Yes Indoors Indoors No Indoors
52 5260 DFS Indoors/DFS/TPC Indoors/DFS/TPC (otherwise limited to 100mW instead of 200mW) Indoors/DFS/TPC No Yes DFS/TPC Yes Yes Indoors DFS/TPC Indoors Indoors No DFS/TPC
56 5280 DFS Indoors/DFS/TPC Indoors/DFS/TPC (otherwise limited to 100mW instead of 200mW) Indoors/DFS/TPC No Yes DFS/TPC Yes Yes Indoors DFS/TPC Indoors Indoors Yes DFS/TPC
60 5300 DFS Indoors/DFS/TPC Indoors/DFS/TPC (otherwise limited to 100mW instead of 200mW) Indoors/DFS/TPC No Yes DFS/TPC Yes Yes Indoors DFS/TPC Indoors Indoors Yes DFS/TPC
64 5320 DFS Indoors/DFS/TPC Indoors/DFS/TPC (otherwise limited to 100mW instead of 200mW) Indoors/DFS/TPC No Yes DFS/TPC Yes Yes Indoors DFS/TPC Indoors Indoors Yes DFS/TPC
100 5500 DFS DFS/TPC DFS/TPC (otherwise limited to 500mW instead of 1W) DFS/TPC No No No No Yes DFS/TPC DFS/TPC Yes DFS Yes DFS/TPC
104 5520 DFS DFS/TPC DFS/TPC (otherwise limited to 500mW instead of 1W) DFS/TPC No No No No Yes DFS/TPC DFS/TPC Yes DFS Yes DFS/TPC
108 5540 DFS DFS/TPC DFS/TPC (otherwise limited to 500mW instead of 1W) DFS/TPC No No No No Yes DFS/TPC DFS/TPC Yes DFS Yes DFS/TPC
112 5560 DFS DFS/TPC DFS/TPC (otherwise limited to 500mW instead of 1W) DFS/TPC No No No No Yes DFS/TPC DFS/TPC Yes DFS Yes DFS/TPC
116 5580 DFS DFS/TPC DFS/TPC (otherwise limited to 500mW instead of 1W) DFS/TPC No No No No Yes DFS/TPC DFS/TPC Yes DFS Yes DFS/TPC
120 5600 No DFS/TPC DFS/TPC (otherwise limited to 500mW instead of 1W) DFS/TPC No No No No Yes DFS/TPC No Yes DFS Yes DFS/TPC
124 5620 No DFS/TPC DFS/TPC (otherwise limited to 500mW instead of 1W) DFS/TPC No No No No Yes DFS/TPC No Yes DFS Yes DFS/TPC
128 5640 No DFS/TPC DFS/TPC (otherwise limited to 500mW instead of 1W) DFS/TPC No No No No Yes DFS/TPC No Yes DFS Yes DFS/TPC
132 5660 DFS DFS/TPC DFS/TPC (otherwise limited to 500mW instead of 1W) DFS/TPC No No No No No DFS/TPC DFS/TPC Yes DFS Yes DFS/TPC
136 5680 DFS DFS/TPC DFS/TPC (otherwise limited to 500mW instead of 1W) DFS/TPC No No No No No DFS/TPC DFS/TPC Yes DFS Yes DFS/TPC
140 5700 DFS DFS/TPC DFS/TPC (otherwise limited to 500mW instead of 1W) DFS/TPC No No No No No DFS/TPC DFS/TPC Yes DFS Yes DFS/TPC
149 5745 Yes in study, SRD (25 mW) No No No Yes Yes No Yes No Yes No Yes Yes Yes
153 5765 Yes in study, SRD (25 mW) No No No Yes Yes No Yes No Yes No Yes Yes Yes
157 5785 Yes in study, SRD (25 mW) No No No Yes Yes No Yes No Yes No Yes Yes Yes
161 5805 Yes in study, SRD (25 mW) No No No Yes Yes No Yes No Yes No Yes Yes Yes
165 5825 Yes in study, SRD (25 mW) No No No Yes Yes No Yes No Yes No Yes Yes Yes

Wi-Fi (802.11) PHY Data Rates

Devices supporting the latest 802.11ac (draft) standard are now being certified by the Wi-Fi Alliance. 802.11ac promises various improvements over 802.11n. Starting from the first IEEE 802.11 standard in 1997 to the latest 11ac standard, there have been improvements in various aspects of 802.11 networks - speed of the network being one of the major improvements. This article attempts to explain the PHY data rates of 11b, 11a/g, 11n and 11ac.


Modulation


The above diagram shows the modulation techniques used in different 802.11 PHYsical layers. 802.11 systems support two (Ignoring the optional and obsolete types) types of modulation - DSSS/CCK and OFDM. This article is not going to cover the details of DSSS/CCK (a good intro can be found here) and OFDM. The below figure gives an overview of both the techniques.




Data Rate

DSSS/CCK (11b Date Rates)
DSSS/CCK data symbol is formed by spreading and modulating the data bits. The data rate depends on : 
  • Chips per second (CSec) = 11,000,000
  • Chips per symbol (CSym) = 11 (DSSS) or 8 (DSSS with CCK)
  • Bits per symbol (NBits) = 1 or 2 (DSSS), 4 or 8 (DSSS with CCK)
Data Rate = (CSec/CSym)*NBits

Chips per second Chips per symbol Bits per symbol Data Rate (Mbps)
11,000,000 11 1 1
11,000,000 11 2 2
11,000,000 8 4 5.5
11,000,000 8 8 11
11b Data Rates 

OFDM

DSSS/CCK uses the entire available bandwidth as one single channel. OFDM divides the channel into multiple(overlapping) sub-channels. The data rate depends on:
  •  Symbol duration (in other words symbols per second) (SDur)
    • Symbol duration depends on "Guard Interval" between symbols.
  •  Bits per symbol (NBits)
  •  Coding rate (CRate)
    • Some bits are used for error correction and do not carry data 
  • Number of sub-channels (NChan)
    • Depends on channel width(20/40/80/160)
    • 11n/11ac have more sub-channels in the same bandwidth compared to 11a/11g
Data Rate = (1/SDur)*(NBits*CRate)*NChan

11a/11g Rates

Modulation NBits CRate NChan SDur (micro sec) Data Rate (Mbps)
BPSK 1 1/2 48 4 6
BPSK 1 3/4 48 4 9
QPSK 2 1/2 48 4 12
QPSK 2 3/4 48 4 18
16-QAM 4 1/2 48 4 24
16-QAM 4 3/4 48 4 36
64-QAM 6 2/3 48 4 48
64-QAM 6 3/4 48 4 54
11a/11g Data Rates
Note: The above table shows date rates for 20MHz channel width. 5,10 MHz channel widths are not shown.

11n/11ac Data Rates

11n and 11ac data rate improvements are due to:
  • The number of sub-channels in 11n and 11ac is more than 11a and 11g. 
  • Higher coding rate (5/6)
  • 11ac also uses 256-QAM which further increases the data rate.
  • Wider Channels (40,80,160)
  • Multiple Spatial Streams (MIMO) (Nss
Modulation NBits CRate NChan Data Rate (Mbps)
SDur=4us
Data Rate (Mbps)
SDur=3.6us
BPSK 1 1/2 52 6.5 7.2
QPSK 2 1/2 52 13 14.4
QPSK 2 3/4 52 19.5 21.7
16-QAM 4 1/2 52 26 28.9
16-QAM 4 3/4 52 39 43.3
64-QAM 6 2/3 52 52 57.8
64-QAM 6 3/4 52 58.5 65
64-QAM 6 5/6 52 65 72.2
256-QAM (11ac) 8 3/4 52 78 86.7
256-QAM (11ac) 8 5/6 52 86.7 96.3
11n and 11ac Data Rates (20 MHz, 1 SS)
The entries highlighted are 11ac only rates and the rest are common to 11n and 11ac.
Updated on 20/08/2014: Note: The last entry,i.e, MCS9 (256 QAM, 5/6 rate) is not allowed in 11ac 20 MHz channel.
The maximum data rate is achieved with MIMO and maximum channel width.


Channel Width NBits CRate NChan Data Rate (Mbps)
SDur=3.6us (Nss=1)
11n Max Rate
(Nss=4)
20 Mhz 6 5/6 52 72.2 288.8
40 Mhz 6 5/6 108 150 600
11n Max Data Rates

Channel Width NBits CRate NChan Data Rate (Mbps)
SDur=3.6us (Nss=1)
11ac Max Rate
(Nss=8)
20 Mhz 8 5/6 52 96.3 577.8
(Nss=6)
40 Mhz 8 5/6 108 200 1600
80 Mhz 8 5/6 234 433.3 3466.4
160 Mhz 8 5/6 468 866.7 6933.6
11ac Max Data Rates 
Updated on 20/08/2014: Max valid spatial streams for 11ac 20 MHz channels corrected to 6.

Summary

The improvement in data rate from 11b to 11ac is due to various factors: OFDM, Wider Channels, QAM, Higher coding rate and MIMO.  The figures below depicts the improvement in data rate due to each factor(excluding MIMO).