Mitä teit tänään tallissa?

Hyvän maun rajoissa, järjenkäyttö sallittu.

Valvoja: Moderators

Avatar
Polforea
Viestit: 12306
Liittynyt: 21 Kesä 2011, 01:17
Car(s): XF
159 Berlina
159 Berlina
348TB
350i DHC
4C 1:24 TBi
Paikkakunta: Viiva-k-fisu

Re: Mitä teit tänään tallissa?

Viesti Kirjoittaja Polforea »

Polforea kirjoitti:
25 Heinä 2026, 22:00
Olisi tuossa vähän vielä petrattavaa.
Petrasin ja uutta versiota pukkaa. Eli nyt on vähän parannettu koodia ja samalla taas opittu itekin jotain.

1) Tein prosessitarkastelun, mutta en sinänsä pid-tiedon -kautta vaan runfilen tiedostolukitukseen perustuen. Jos siis aiempi suoritus on kaatunut ja runfile poistamatta, voidaan jatkaa suoritusta
2) Koska cron ajaa skriptin kerran minuutissa, lisäsin kameraa etsivälle pingille 2 sekunnin odotusaikamääreen (oletuksen 3 sekuntia) ja laitoin sen toistamaan itseään 20 kertaa mikäli kameraa ei meinaa löytyä. Nyt jos kamera liittyy verkkoon esim hh:mm:30 -kohdalla, alkaa tiedonsiirto jo 2 sekunnnin päästä eikä vasta seuraavalla minuutilla
3) Kahteen edelliseen tukeutuen tein tulkinnan, että jos aiempi instanssi pyörii yhä, mutta kameraa ei verkosta löydy, on se aiempi instanssi jossain perätilassa (Wget tuntuu välillä niin tekevän) ja vanhan instanssin prosessin joutaa tappaa. Tähän viiden sekunnin viive siltä varalta, että yhteys on juuri katkennut ja vanha instanssi on juuri siirtämässä ehjäksi varmistettua tiedostoa turvaan
4) Ärsytti muuttaa montaa kohtaa samasta syystä, joten tein latauksesta funktion ja ajan sen useampaan kertaan eri tiedostotyyppejä vasten
5) Tiedostot ladataan nyt välihakemistoon ja vasta valmistuttuaan siirretään päivämääränsä mukaiseen hakemistoon. Tällä on kaksi etua - ensinnäkin jos lataus katkeaa yhtenä vuorokautena ja jatkuu seuraavana, voi jatkaa siitä mihin jäi ja toisekseen tiedostot jaotellaan päivähakemistoihin tallennusajan eikä latauspäivän perusteella.
6) Tuuppasin tiedoston kameralta poistavan curlin STDERR:n myös devnulliin, kun curl näköjään ulostaa statistiikat sitä kautta.

Lisäksi täppäsin kamerasta päälle tarjolla olevan 90 sekunnin viiveen parking modeen siirtymiselle, mikä antaa wifillekin näköjään saman 90s lisää elinaikaa. Lähetin myös Viofolle feature requestinä, että A) olisi kiva saada checksumit ettei tarttisi tuota tuplalatausta ja B) olisi kiva saada wifin sammutukseen viive, jos wifi on yhdistyneenä.


Nykyinen versio, olkaa hyviä:

Koodi: Valitse kaikki

#!/bin/bash
viofoip=192.168.10.20
viofodir=/home/eero/kuvat/jagcam
viofodate=`date +%Y-%m-%d`                                                                                                                                                      
loadingdir="incoming"                                                                                                                                                           
runfile=~/viofo.runfile                                                                                                                                                         
getprotected=true                                                                                                                                                               
getothers=false                                                                                                                                                                 
getparking=true                                                                                                                                                                 
getpictures=true                                                                                                                                                                
sumcounter=0                                                                                                                                                                    
connection=false                                                                                                                                                                
oldinstance=false                                                                                                                                                               
                                                                                                                                                                                
                                                                                                                                                                                
#Use file lock check to determine if there is old instance running                                                                                                              
exec 9>>"$runfile"                                                                                                                                                              
if ! flock -n 9                                                                                                                                                                 
then                                                                                                                                                                            
    oldinstance=true                                                                                                                                                            
#    exit 0                                                                                                                                                                     
else                                                                                                                                                                            
    echo $$ > $runfile                                                                                                                                                          
fi                                                                                                                                                                              
                                                                                                                                                                                
                                                                                                                                                                                
                                                                                                                                                                                
#Check if camera is or gets in the network and give up if not                                                                                                                   
while [[ "$connection" = "false" ]]                                                                                                                                             
do                                                                                                                                                                              
    sumcounter=$((sumcounter+1))                                                                                                                                                
    if ping -c 1 -w 2 $viofoip >/dev/null 2>&1                                                                                                                                  
    then                                                                                                                                                                        
        connection=true                                                                                                                                                         
    fi                                                                                                                                                                          
#    if ! ping -c 1 $viofoip >/dev/null 2>&1                                                                                                                                    
    if [[ $sumcounter -gt 20 ]] && [[ "$connection" == "false" ]]                                                                                                               
    then                                                                                                                                                                        
        if [[ "$oldinstance" == "true" ]] #If camera is not found from the network and old instance is running, old instance is determined to be jammed and will be killed      
        then                                                                                                                                                                    
            sleep 5s #Wait 5 seconds to ensure possible file comparing and moving gets finished                                                                                 
            kill  $(<"$runfile")                                                                                                                                                
        fi                                                                                                                                                                      
        exit 0                                                                                                                                                                  
    fi                                                                                                                                                                          
done                                                                                                                                                                            
                                                                                                                                                                                
                                                                                                                                                                                
#If we're still going strong, the camera is found from network - if there is still old instance running, let's exit this one                                                    
if [[ "$oldinstance" == "true" ]]                                                                                                                                               
then                                                                                                                                                                            
    exit 0                                                                                                                                                                      
fi                                                                                                                                                                              

function getviofofiles {                                                                                                                                                        
    cd $viofodir/$targetdir/$loadingdir                                                                                                                                         
    #Fetch the list of protected files                                                                                                                                          
    filet=`curl http://$viofoip/DCIM/$sourcedir 2>/dev/null|grep -i $filetype|cut -d \/ -f $dirdepth|cut -d \" -f 1|grep -v del`                                                
                                                                                                                                                                                
    #Fetch the protected files and delete them if successful                                                                                                                    
    for videofile in $filet                                                                                                                                                     
    do                                                                                                                                                                          
        sama=0                                                                                                                                                                  
        while [[ $sama < 3 ]]                                                                                                                                                   
        do                                                                                                                                                                      
            if [[ ! -f $videofile.comp ]]                                                                                                                                       
            then                                                                                                                                                                
                wget -c -t 3 -q  http://$viofoip/DCIM/$sourcedir/$videofile && mv $videofile $videofile.comp                                                                    
            fi                                                                                                                                                                  
            wget -c -t 3 -q http://$viofoip/DCIM/$sourcedir/$videofile #Fetch twice to cmopare the output files                                                                 
            if ! cmp $videofile.comp $videofile >/dev/null 2>&1                                                                                                                 
            then                                                                                                                                                                
                sama=$((sama=+1)) #Limit tries to 3                                                                                                                             
            else #If the files match, remove the compare-file and original from camera and move the ready file to corresponding date-directory                                  
                rm $videofile.comp                                                                                                                                              
                sama=4                                                                                                                                                          
                curl http://$viofoip/DCIM/$sourcedir/$videofile?del=1 >/dev/null 2>&1                                                                                           
                viofodate=`echo $videofile|cut -d \_ -f 1,2`                                                                                                                    
                if [[ ! -d "$viofodir/$targetdir/$viofodate" ]] #ensure corresponding date-directory exists                                                                     
                then                                                                                                                                                            
                        mkdir $viofodir/$targetdir/$viofodate                                                                                                                   
                fi                                                                                                                                                              
                                                                                                                                                                                
                mv $videofile $viofodir/$targetdir/$viofodate                                                                                                                   
            fi                                                                                                                                                                  
        done                                                                                                                                                                    
    done                                                                                                                                                                        
}                                                                                                                                                                               
                                                                                                                                                                                
#Fetch files marked as protected by driver if wanted                                                                                                                            
                                                                                                                                                                                
if [[ "$getprotected" = "true" ]]                                                                                                                                               
then                                                                                                                                                                            
                                                                                                                                                                                
    sourcedir="Movie/RO"        #Directory in camera, under DCIM                                                                                                                
    targetdir="protected"       #Type-specific target directory                                                                                                                 
    filetype="MP4"              #File extension                                                                                                                                 
    dirdepth=5                  #How deep is the directory structure on camera                                                                                                  
    getviofofiles               #Run the function                                                                                                                               
fi                                                                                                                                                                              
                                                                                                                                                                                
#Fetch parking video files if wanted                                                                                                                                            
if [[ "$getparking" = "true" ]]                                                                                                                                                 
then                                                                                                                                                                            
    sourcedir="Movie/Parking"                                                                                                                                                   
    targetdir="parking"                                                                                                                                                         
    filetype="MP4"                                                                                                                                                              
    dirdepth=5                                                                                                                                                                  
    getviofofiles                                                                                                                                                               
fi                                                                                                                                                                              
                                                                                                                                                                                
#Fetch other video files if wanted                                                                                                                                              
if [[ "$getothers" = "true" ]]                                                                                                                                                  
then                                                                                                                                                                            
    sourcedir="Movie"                                                                                                                                                           
    targetdir="others"                                                                                                                                                          
    filetype="MP4"                                                                                                                                                              
    dirdepth=4                                                                                                                                                                  
    getviofofiles                                                                                                                                                               
fi                                                                                                                                                                              
                                                                                                                                                                                
#Fetch still photos if wanted                                                                                                                                                   
if [[ "$getpictures" = "true" ]]                                                                                                                                                
then                                                                                                                                                                            
    sourcedir="Photo"                                                                                                                                                           
    targetdir="stills"                                                                                                                                                          
    filetype="JPG"                                                                                                                                                              
    dirdepth=4                                                                                                                                                                  
    getviofofiles                                                                                                                                                               
fi                                                                                                                                                                              
                                                                                                                                                                                
                                                                                                                                                                                
rm $runfile                                                                                                                                                                     
                                                                                                                                                                                
exit 0 
Tekemällä mitä haluaa oppii tekemään mitä haluaa.

Vastaa Viestiin