2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 12:49:34 +00:00

CULflash separated from updatefhem to a new module

git-svn-id: https://svn.fhem.de/fhem/trunk@1624 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
mfr69bs 2012-06-17 13:55:00 +00:00
parent 5a1f00ab48
commit ed0d76f444
4 changed files with 150 additions and 63 deletions

View File

@ -44,6 +44,7 @@
new global attribute <exclude_from_update> added (M. Fischer)
- feature: optional telnet password added / telnet port is optional
- feature: holiday returns all matches, not only the first.
- change: CULflash separated from updatefhem to a new module (M. Fischer)
- 2011-12-31 (5.2)
- bugfix: applying smallscreen attributes to firefox/opera

146
fhem/FHEM/99_CULflash.pm Normal file
View File

@ -0,0 +1,146 @@
##############################################
# $Id: $
# modified by M. Fischer
package main;
use strict;
use warnings;
use IO::Socket;
sub CommandCULflash($$);
sub GetHttpFile($$@);
sub SplitNewFiletimes($);
my $server = "fhem.de:80";
my $sdir = "/fhemupdate2";
my $ftime = "filetimes.txt";
my $dfu = "dfu-programmer";
#####################################
sub
CULflash_Initialize($$)
{
my %chash = ( Fn=>"CommandCULflash",
Hlp=>"<cul> <type>,flash the CUL from the nightly SVN" );
$cmds{CULflash} = \%chash;
}
#####################################
sub
CommandCULflash($$)
{
my ($cl, $param) = @_;
my $modpath = (-d "update" ? "update" : $attr{global}{modpath});
my $moddir = "$modpath/FHEM";
my %ctypes = (
CUL_V2 => "at90usb162",
CUL_V2_HM => "at90usb162",
CUL_V3 => "atmega32u4",
CUL_V4 => "atmega32u2",
);
my @a = split("[ \t]+", $param);
return "Usage: CULflash <Fhem-CUL-Device> <CUL-type>, ".
"where <CUL-type> is one of ". join(" ", sort keys %ctypes)
if(!(int(@a) == 2 &&
($a[0] eq "none" || ($defs{$a[0]} && $defs{$a[0]}{TYPE} eq "CUL")) &&
$ctypes{$a[1]}));
my $cul = $a[0];
my $target = $a[1];
################################
# First get the index file to prove the file size
my $filetimes = GetHttpFile($server, "$sdir/$ftime");
return "Can't get $ftime from $server" if(!$filetimes);
# split filetime and filesize
my ($ret, $filetime, $filesize) = SplitNewFiletimes($filetimes);
return $ret if($ret);
################################
# Now get the firmware file:
my $content = GetHttpFile($server, "$sdir/FHEM/$target.hex");
return "File size for $target.hex does not correspond to filetimes.txt entry"
if(length($content) ne $filesize->{"FHEM/$target.hex"});
my $localfile = "$moddir/$target.hex";
open(FH,">$localfile") || return "Can't write $localfile";
print FH $content;
close(FH);
my $cmd = "($dfu MCU erase && $dfu MCU flash TARGET && $dfu MCU start) 2>&1";
my $mcu = $ctypes{$target};
$cmd =~ s/MCU/$mcu/g;
$cmd =~ s/TARGET/$localfile/g;
if($cul ne "none") {
CUL_SimpleWrite($defs{$cul}, "B01");
sleep(4); # B01 needs 2 seconds for the reset
}
Log 1, "CULflash $cmd";
my $result = `$cmd`;
Log 1, "CULflash $result";
return $result;
}
sub
GetHttpFile($$@)
{
my ($host, $filename, $timeout) = @_;
$timeout = 2.0 if(!defined($timeout));
$filename =~ s/%/%25/g;
my $conn = IO::Socket::INET->new(PeerAddr => $host);
if(!$conn) {
Log 1, "CULflash Can't connect to $host\n";
undef $conn;
return undef;
}
$host =~ s/:.*//;
my $req = "GET $filename HTTP/1.0\r\nHost: $host\r\n\r\n\r\n";
syswrite $conn, $req;
shutdown $conn, 1; # stopped writing data
my ($buf, $ret) = ("", "");
$conn->timeout($timeout);
for(;;) {
my ($rout, $rin) = ('', '');
vec($rin, $conn->fileno(), 1) = 1;
my $nfound = select($rout=$rin, undef, undef, $timeout);
if($nfound <= 0) {
Log 1, "CULflash GetHttpFile: Select timeout/error: $!";
undef $conn;
return undef;
}
my $len = sysread($conn,$buf,65536);
last if(!defined($len) || $len <= 0);
$ret .= $buf;
}
$ret=~ s/(.*?)\r\n\r\n//s; # Not greedy: switch off the header.
Log 4, "CULflash Got http://$host$filename, length: ".length($ret);
undef $conn;
return $ret;
}
sub
SplitNewFiletimes($)
{
my $filetimes = shift;
my $ret;
my (%filetime, %filesize) = ();
foreach my $l (split("[\r\n]", $filetimes)) {
chomp($l);
$ret = "Corrupted filetimes.txt file"
if($l !~ m/^20\d\d-\d\d-\d\d_\d\d:\d\d:\d\d /);
last if($ret);
my ($ts, $fs, $file) = split(" ", $l, 3);
$filetime{$file} = $ts;
$filesize{$file} = $fs;
}
return ($ret, \%filetime, \%filesize);
}
# vim: ts=2:et
1;

View File

@ -7,7 +7,6 @@ use warnings;
use IO::Socket;
sub CommandUpdatefhem($$);
sub CommandCULflash($$);
sub GetHttpFile($$@);
sub ParseChanges($);
sub ReadOldFiletimes($);
@ -17,8 +16,6 @@ sub FileList($);
my $server = "fhem.de:80";
my $sdir = "/fhemupdate2";
my $ftime = "filetimes.txt";
my $dfu = "dfu-programmer";
#####################################
sub
@ -28,9 +25,6 @@ updatefhem_Initialize($$)
Hlp=>",update fhem from the nightly SVN" );
$cmds{updatefhem} = \%fhash;
my %chash = ( Fn=>"CommandCULflash",
Hlp=>"<cul> <type>,flash the CUL from the nightly SVN" );
$cmds{CULflash} = \%chash;
}
#####################################
@ -316,63 +310,6 @@ CommandUpdatefhem($$)
return $ret;
}
sub
CommandCULflash($$)
{
my ($cl, $param) = @_;
my $modpath = (-d "update" ? "update" : $attr{global}{modpath});
my $moddir = "$modpath/FHEM";
my %ctypes = (
CUL_V2 => "at90usb162",
CUL_V2_HM => "at90usb162",
CUL_V3 => "atmega32u4",
CUL_V4 => "atmega32u2",
);
my @a = split("[ \t]+", $param);
return "Usage: CULflash <Fhem-CUL-Device> <CUL-type>, ".
"where <CUL-type> is one of ". join(" ", sort keys %ctypes)
if(!(int(@a) == 2 &&
($a[0] eq "none" || ($defs{$a[0]} && $defs{$a[0]}{TYPE} eq "CUL")) &&
$ctypes{$a[1]}));
my $cul = $a[0];
my $target = $a[1];
################################
# First get the index file to prove the file size
my $filetimes = GetHttpFile($server, "$sdir/$ftime");
return "Can't get $ftime from $server" if(!$filetimes);
# split filetime and filesize
my ($ret, $filetime, $filesize) = SplitNewFiletimes($filetimes);
return $ret if($ret);
################################
# Now get the firmware file:
my $content = GetHttpFile($server, "$sdir/FHEM/$target.hex");
return "File size for $target.hex does not correspond to filetimes.txt entry"
if(length($content) ne $filesize->{"FHEM/$target.hex"});
my $localfile = "$moddir/$target.hex";
open(FH,">$localfile") || return "Can't write $localfile";
print FH $content;
close(FH);
my $cmd = "($dfu MCU erase && $dfu MCU flash TARGET && $dfu MCU start) 2>&1";
my $mcu = $ctypes{$target};
$cmd =~ s/MCU/$mcu/g;
$cmd =~ s/TARGET/$localfile/g;
if($cul ne "none") {
CUL_SimpleWrite($defs{$cul}, "B01");
sleep(4); # B01 needs 2 seconds for the reset
}
Log 1, "updatefhem $cmd";
my $result = `$cmd`;
Log 1, "updatefhem $result";
return $result;
}
sub
GetHttpFile($$@)
{

View File

@ -513,3 +513,6 @@
- Sun Jun 03 2012 (M. Fischer)
- Added new global attribute <exclude_from_update>
- Added new parameter <changed> to updatefhem
- Sun Jun 17 2012 (M. Fischer)
- CULflash routine from updatefhem removed. CULflash is a standalone module from now.