and here is the complete listing of the custom generator code:

# -*- coding: utf-8 -*-
import Image, ImageFont, ImageDraw, urllib, urllib2, time
import os, weewx.reportengine
#===============================================================================
#                    Class ImageGenerator
#===============================================================================

class CameraGenerator(weewx.reportengine.ReportGenerator):
    """Class for managing the cam image generator."""

    def run(self):
       # localpath  = 
os.path.join(self.config_dict['Station']['WEEWX_ROOT'],self.skin_dict['HTML_ROOT'])
        fontpath = self.skin_dict['Extras']['cam_lable_font_path']
        ptzfontsize = 
int(self.skin_dict['Extras']['ptz_cam_lable_font_size'])
        for ptzcam in self.skin_dict['Extras']['ptzcams'].keys():
            localpath = 
os.path.join(self.config_dict['Station']['WEEWX_ROOT'],self.skin_dict['HTML_ROOT'],ptzcam)
            camdef = self.skin_dict['Extras']['ptzcams'][ptzcam]
            preseturl=camdef['preseturl']
            imgurl = camdef['imgurl']
            for preset in camdef['presets'].keys():
                psdef = camdef['presets'][preset]
                psNum = int(psdef['number']) # API says add 256 to the 
preset number for the url
                urlNum = psNum + 256
                psCaption = psdef['caption']
                psSnap = int(psdef['snap'])
                moveurl = preseturl+str(urlNum) #the complete move to 
preset url
                tries = 0
                moved = False
                while tries < 3:
                    tries += 1
                    try:
                        mov = urllib.urlopen(moveurl) #tell the camera to 
move to the preset location
                        if mov.read().find('OK')>-1:
                            moved = True
                            mov.close() #close the link
                            break                
                    except:
                        print "Unexpected error moving camera:"
                    time.sleep(5.0)
                if not moved:
                    mov.close() #close the link
                    continue
                time.sleep(17.0) #wait for a few seconds for the camera to 
move
                if psSnap==1: #if a snapshot is called for on the 
preset                   
                    tries = 0
                    while tries < 3:
                        local = open(localpath+str(psNum) + '.jpg','wb') 
#open a file for the image
                        tries += 1
                        try:
                            local.write(urllib.urlopen(imgurl).read()) 
#download the snapshot image into the file
                            local.close()
                        except:
                            local.close()
                            os.remove(localpath+str(psNum) + '.jpg')
                            time.sleep(5.0)
                            continue
                        if os.path.getsize(localpath+str(psNum) + '.jpg')>0:
                            try:
                                outImage = 
Image.open(localpath+str(psNum)+'.jpg')
                                draw = ImageDraw.Draw(outImage)
                                
font=ImageFont.truetype(fontpath,ptzfontsize)
                                timestr = time.strftime('%a, %m/%d/%y at 
%I:%M %p %Z',time.localtime(time.time()))
                                draw.rectangle([0,0,1280,30],(0,0,0,10))
                                draw.text((5,5),psCaption +' updated on 
'+timestr,font=font)
                                outImage.save(localpath+str(psNum)+'.jpg')
                                break
                            except:
                                print 'error with camera image and PIL'
                                os.remove(localpath+str(psNum) + '.jpg')
                                time.sleep(5.0)
                            continue
                        else:
                            if tries == 3:
                                os.remove(localpath+str(psNum) + '.jpg')
                                break
                            time.sleep(5.0)
                
                
        chanurl = self.skin_dict['Extras']['fixedcams']['chanurl']
        imgurl = self.skin_dict['Extras']['fixedcams']['imgurl']
        localpath  = 
os.path.join(self.config_dict['Station']['WEEWX_ROOT'],self.skin_dict['HTML_ROOT'],'cam')
        for fixedcam in 
self.skin_dict['Extras']['fixedcams']['cameras'].keys():
            camdef = 
self.skin_dict['Extras']['fixedcams']['cameras'][fixedcam]
            port=int(camdef['port'])
            urlchan = chanurl+str(port)
            tries = 0
            while tries <= 3:
                tries += 1
                try:
                    chan = urllib.urlopen(urlchan)
                    break
                except IOError as (errno, strerror):
                    print "I/O error({0}): {1}".format(errno, strerror)
                except ValueError:
                    print "Could not convert data to an integer."
                except:
                    print "Unexpected error:", sys.exc_info()[0]
                    raise
            chan.close()
            time.sleep(1.0)           
            local = open(localpath+str(port+1)+'.jpg','wb')
            tries=0
            while tries <= 3:
                tries += 1
                try:
                    local.write(urllib.urlopen(imgurl).read())
                    break
                except IOError as (errno, strerror):
                    print "I/O error({0}): {1}".format(errno, strerror)
                except ValueError:
                    print "Could not convert data to an integer."
                except:
                    print "Unexpected error:", sys.exc_info()[0]
                    raise
            local.close()
            outImage = Image.open(localpath+str(port+1)+'.jpg')
            draw = ImageDraw.Draw(outImage)
            font=ImageFont.truetype(fontpath,15)
            timestr = time.strftime('%a, %m/%d/%y at %I:%M %p 
%Z',time.localtime(time.time()))
            draw.rectangle([0,0,640,20],(0,0,0,10))
            draw.text((5,5),"Cam " + str(port+1)+' updated on 
'+timestr,font=font)
            outImage.save(localpath+str(port+1)+'.jpg')



On Saturday, September 23, 2017 at 10:04:46 AM UTC-7, Joe Percival wrote:
>
> First, an explanation...
> I'm adding a report that downloads images from webcams, manipulates them, 
> and copies them to a local file.  An FTP report then sends them to a 
> specific location on my remote web server.
> The skin for this custom report includes a configuration file but does not 
> use a template file.  The skin configuration file contains a set of 
> parameters used by the custom generator to find, manipulate, and store the 
> images locally.
> The custom generator is a highly modified (like completely) version of the 
> original ImageGenerator.
>
> Here is what I have now and what I understand needs to change...
>
> In the old weewx.conf:
> [Reports]
>     [[CameraSnapshots]]
>         skin=snapshots
>
> In the new weewx.conf:
> [StdReport]
>      [[CameraSnapshots]]
>           skin=snapshots
>
> In the old skins/snapshots/skin.conf:
> <snipped out all sorts of parameter goodies used by the generator to find 
> and manipulate images>
> [Generators]
>     generator_list = weewx.cameragenerator.CameraGenerator
>     #weewx.filegenerator.FileGenerator, 
> weewx.imagegenerator.ImageGenerator, weewx.reportengine.CopyGenerator
>
> In the new skins/snapshots/skin.conf:
> <snipped out all the same parameter goodies which will probably not need 
> to change... we'll see>
> [Generators]
>     generator_list = user.cameragenerator.CameraGenerator
>     #weewx.filegenerator.FileGenerator, 
> weewx.imagegenerator.ImageGenerator, weewx.reportengine.CopyGenerator
>
> in the old bin/weewx/cameragenerator.py:
> import Image, ImageFont, ImageDraw, urllib, urllib2, time
> import os, weewx.reportengine
>
> #===============================================================================
> #                    Class CameraGenerator
>
> #===============================================================================
>
> class CameraGenerator(weewx.reportengine.ReportGenerator):
>     """Class for managing the cam image generator."""
>
> In the new user/cameragenerator.py:
> import Image, ImageFont, ImageDraw, urllib, urllib2, time
> import os, weewx.reportengine
>
> #===============================================================================
> #                    Class CameraGenerator
>
> #===============================================================================
>
> class CameraGenerator(weewx.reportengine.ReportGenerator):
>     """Class for managing the cam image generator."""
>
> Is that the right place to start?
> Thanks,
> joe
>
> On Friday, September 22, 2017 at 5:31:18 PM UTC-7, Tom Keffer wrote:
>>
>> It should go in bin/user.
>>
>> -tk
>>
>> On Fri, Sep 22, 2017 at 5:26 PM, Joe Percival <[email protected]> 
>> wrote:
>>
>>> Actually the most important first question I have is regarding the 
>>> custom generator location.... bin/weewx or do I need to move it to bin/user?
>>>
>>> --
>>> You received this message because you are subscribed to the Google 
>>> Groups "weewx-user" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to [email protected].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to