Default Common Function Scrip

Discussion in 'C-Bus Automation Controllers' started by CBriggs, Mar 31, 2019.

  1. CBriggs

    CBriggs

    Joined:
    Aug 2, 2015
    Messages:
    13
    Likes Received:
    0
    Location:
    South Australia
    Hi all,
    I have managed to accidentally delete my default Common Function Scrip, and I don't remember what was in it. I remember something about sunset time calculations and a few other functions. Would anyone be able to cut and paste a copy of the default scrip so that I can put it back, or is there another way of restoring it? (I don't have it saved in backup)
    Thanks,
    Clayton
     
    CBriggs, Mar 31, 2019
    #1
  2. CBriggs

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,524
    Likes Received:
    173
    Location:
    Adelaide, Australia
    Code:
    -- user function library
    
    -- send an e-mail
    function mail(to, subject, message)
      -- make sure these settings are correct
      local settings = {
        -- "from" field, only e-mail must be specified here
        from = '[email protected]',
        -- smtp username
        user = '[email protected]',
        -- smtp password
        password = 'VWCW369LJ',
        -- smtp server
        server = 'smtp.telstra.com',
        -- smtp server port
        port = 465,
        -- enable ssl, required for gmail smtp
        secure = 'sslv23',
      }
    
      local smtp = require('socket.smtp')
    
      if type(to) ~= 'table' then
        to = { to }
      end
    
      for index, email in ipairs(to) do
        to[ index ] = '<' .. tostring(email) .. '>'
      end
    
      -- message headers and body
      settings.source = smtp.message({
        headers = {
          to = table.concat(to, ', '),
          subject = subject,
          ['Content-type'] = 'text/html; charset=utf-8',
        },
        body = message
      })
    
      -- fixup from field
      settings.from = '<' .. tostring(settings.from) .. '>'
      settings.rcpt = to
      alert('mail sent')
      return smtp.send(settings)
     
    end
    
    -- sunrise / sunset calculation
    function rscalc(latitude, longitude, when)
      local pi = math.pi
      local doublepi = pi * 2
      local rads = pi / 180.0
    
      local TZ = function(when)
        local ts = os.time(when)
        local utcdate, localdate = os.date('!*t', ts), os.date('*t', ts)
        localdate.isdst = false
    
        local diff = os.time(localdate) - os.time(utcdate)
        return math.floor(diff / 60) / 60
      end
    
      local range = function(x)
        local a = x / doublepi
        local b = doublepi * (a - math.floor(a))
        return b < 0 and (doublepi + b) or b
      end
    
      when = when or os.date('*t')
    
      local y2k = { year = 2000, month = 1, day = 1 }
      local y2kdays = os.time(when) - os.time(y2k)
      y2kdays = math.ceil(y2kdays / 86400)
    
      local meanlongitude = range(280.461 * rads + 0.9856474 * rads * y2kdays)
      local meananomaly = range(357.528 * rads + 0.9856003 * rads * y2kdays)
      local lambda = range(meanlongitude + 1.915 * rads * math.sin(meananomaly) + rads / 50 * math.sin(2 * meananomaly))
    
      local obliq = 23.439 * rads - y2kdays * rads / 2500000
    
      local alpha = math.atan2(math.cos(obliq) * math.sin(lambda), math.cos(lambda))
      local declination = math.asin(math.sin(obliq) * math.sin(lambda))
    
      local LL = meanlongitude - alpha
      if meanlongitude < pi then
        LL = LL + doublepi
      end
    
      local dfo = pi / 216.45
    
      if latitude < 0 then
        dfo = -dfo
      end
    
      local fo = math.min(math.tan(declination + dfo) * math.tan(latitude * rads), 1)
      local ha = 12 * math.asin(fo) / pi + 6
    
      local timezone = TZ(when)
      local equation = 12 + timezone + 24 * (1 - LL / doublepi) - longitude / 15
    
      local sunrise, sunset = equation - ha, equation + ha
    
      if sunrise > 24 then
        sunrise = sunrise - 24
      end
    
      if sunset > 24 then
        sunset = sunset - 24
      end
    
      return math.floor(sunrise * 60), math.ceil(sunset * 60)
    end
    
     
    Ashley, Mar 31, 2019
    #2
  3. CBriggs

    PMF

    Joined:
    Feb 6, 2019
    Messages:
    16
    Likes Received:
    2
    Thanks - had same problem!

    Is this running on latest firmware? I just tried cutting and pasting code above into common functions but got error message below when trying to save it...

    Screenshot.png
     
    Last edited: Mar 31, 2019
    PMF, Mar 31, 2019
    #3
  4. CBriggs

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,524
    Likes Received:
    173
    Location:
    Adelaide, Australia
    Not sure what happened there. The cut and paste left a bit out :) Try this lot.....

    And I forgot to delete my mail details. Oh, well, I'll have to go and change it now......


    Code:
    -- user function library
    
    -- send an e-mail
    function mail(to, subject, message)
      -- make sure these settings are correct
      local settings = {
        -- "from" field, only e-mail must be specified here
        from = 'xxxxx',
        -- smtp username
        user = 'xxxxxx',
        -- smtp password
        password = 'xxxxx',
        -- smtp server
        server = 'smtp.telstra.com',
        -- smtp server port
        port = 465,
        -- enable ssl, required for gmail smtp
        secure = 'sslv23',
      }
    
      local smtp = require('socket.smtp')
    
      if type(to) ~= 'table' then
        to = { to }
      end
    
      for index, email in ipairs(to) do
        to[ index ] = '<' .. tostring(email) .. '>'
      end
    
      -- message headers and body
      settings.source = smtp.message({
        headers = {
          to = table.concat(to, ', '),
          subject = subject,
          ['Content-type'] = 'text/html; charset=utf-8',
        },
        body = message
      })
    
      -- fixup from field
      settings.from = '<' .. tostring(settings.from) .. '>'
      settings.rcpt = to
      alert('mail sent')
      return smtp.send(settings)
     
    end
    
    -- sunrise / sunset calculation
    function rscalc(latitude, longitude, when)
      local pi = math.pi
      local doublepi = pi * 2
      local rads = pi / 180.0
    
      local TZ = function(when)
        local ts = os.time(when)
        local utcdate, localdate = os.date('!*t', ts), os.date('*t', ts)
        localdate.isdst = false
    
        local diff = os.time(localdate) - os.time(utcdate)
        return math.floor(diff / 60) / 60
      end
    
      local range = function(x)
        local a = x / doublepi
        local b = doublepi * (a - math.floor(a))
        return b < 0 and (doublepi + b) or b
      end
    
      when = when or os.date('*t')
    
      local y2k = { year = 2000, month = 1, day = 1 }
      local y2kdays = os.time(when) - os.time(y2k)
      y2kdays = math.ceil(y2kdays / 86400)
    
      local meanlongitude = range(280.461 * rads + 0.9856474 * rads * y2kdays)
      local meananomaly = range(357.528 * rads + 0.9856003 * rads * y2kdays)
      local lambda = range(meanlongitude + 1.915 * rads * math.sin(meananomaly) + rads / 50 * math.sin(2 * meananomaly))
    
      local obliq = 23.439 * rads - y2kdays * rads / 2500000
    
      local alpha = math.atan2(math.cos(obliq) * math.sin(lambda), math.cos(lambda))
      local declination = math.asin(math.sin(obliq) * math.sin(lambda))
    
      local LL = meanlongitude - alpha
      if meanlongitude < pi then
        LL = LL + doublepi
      end
    
      local dfo = pi / 216.45
    
      if latitude < 0 then
        dfo = -dfo
      end
    
      local fo = math.min(math.tan(declination + dfo) * math.tan(latitude * rads), 1)
      local ha = 12 * math.asin(fo) / pi + 6
    
      local timezone = TZ(when)
      local equation = 12 + timezone + 24 * (1 - LL / doublepi) - longitude / 15
    
      local sunrise, sunset = equation - ha, equation + ha
    
      if sunrise > 24 then
        sunrise = sunrise - 24
      end
    
      if sunset > 24 then
        sunset = sunset - 24
      end
    
      return math.floor(sunrise * 60), math.ceil(sunset * 60)
    end
    
     
    Ashley, Mar 31, 2019
    #4
  5. CBriggs

    PMF

    Joined:
    Feb 6, 2019
    Messages:
    16
    Likes Received:
    2
    Thanks - but bizarrely getting the same error message (copied below).

    It's this line it doesn't like:

    when = when or os.date('*t local y2k = { year = 2000, month = 1, day = 1 }

    If I comment it out it saves (but doesn't work!)

    Looking at the syntax I think its missing a comma (after the *t and a bracket at the end - but adding these doesn't;t help!!)

    Screenshot 2019-04-01 at 08.58.34.png
     
    PMF, Apr 1, 2019
    #5
  6. CBriggs

    CBriggs

    Joined:
    Aug 2, 2015
    Messages:
    13
    Likes Received:
    0
    Location:
    South Australia
    Thanks for posting your script Ashley, but unfortunately I have the same problem as PMF.
    Not sure what the issue is, but would love it if someone could check their script to compare.
    I'm not really sure what the scrip even achieves as I have some timers based on sunset time and they are still working and the time is still self adjusting even though my Common Functions script is empty.
     
    CBriggs, Apr 1, 2019
    #6
  7. CBriggs

    PMF

    Joined:
    Feb 6, 2019
    Messages:
    16
    Likes Received:
    2
    My timers work using sunrise and sunset - but I want to build sunset times into scripting and think this code is needed to realise the rscalc function...

    Again hoping someone has the 'clean' code somewhere!
     
    PMF, Apr 1, 2019
    #7
  8. CBriggs

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,524
    Likes Received:
    173
    Location:
    Adelaide, Australia
    No idea why it won't paste properly. The line should read:

    when = when or os.date('*t')
    local y2k = { year = 2000, month = 1, day = 1 }
     
    Ashley, Apr 1, 2019
    #8
  9. CBriggs

    CBriggs

    Joined:
    Aug 2, 2015
    Messages:
    13
    Likes Received:
    0
    Location:
    South Australia
    Thanks Ashley, but it still doesn't work. The string is not complete for the os.date function
     
    CBriggs, Apr 1, 2019
    #9
  10. CBriggs

    PMF

    Joined:
    Feb 6, 2019
    Messages:
    16
    Likes Received:
    2
    Found the same!

    If you look through the code there are obvious other corruptions - I've opened a private message with Ashley and shared my email address - hopefully I'll get the clean code and will let you know!
     
    PMF, Apr 1, 2019
    #10
  11. CBriggs

    Chrisnz

    Joined:
    Aug 23, 2007
    Messages:
    3
    Likes Received:
    0
    Location:
    New Zealand
    Hi Guys

    I have found the issue.

    Below is what the script should look like.

    when = when or os.date('*t')

    local y2k = { year = 2000, month = 1, day = 1 }
    local y2kdays = os.time(when) - os.time(y2k)
    y2kdays = math.ceil(y2kdays / 86400)

    It looks like you are missing a bracket.
     
    Chrisnz, Apr 1, 2019
    #11
  12. CBriggs

    PMF

    Joined:
    Feb 6, 2019
    Messages:
    16
    Likes Received:
    2
    Your right - interestingly when Ashley first posted the bracket wasn't missing! There's something really odd about the way this forum works as its corrupting entries.

    Even having corrected this there are other errors (see discussion above). I've asked Ashley to email code to me.

    Alternatively if Ashley (or someone else) could load the code into either a text or word file and attach to a post on here it might make it through uncorrupted!!
     
    PMF, Apr 1, 2019
    #12
  13. CBriggs

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,524
    Likes Received:
    173
    Location:
    Adelaide, Australia
  14. CBriggs

    PMF

    Joined:
    Feb 6, 2019
    Messages:
    16
    Likes Received:
    2
    PMF, Apr 1, 2019
    #14
    SMC likes this.
  15. CBriggs

    cbus-ed

    Joined:
    Feb 4, 2006
    Messages:
    11
    Likes Received:
    0
    Location:
    Melbourne
     
    cbus-ed, Apr 2, 2019
    #15
  16. CBriggs

    CBriggs

    Joined:
    Aug 2, 2015
    Messages:
    13
    Likes Received:
    0
    Location:
    South Australia
    Thanks Ashley, greatly appreciate the effort you put in to help us with this!
     
    CBriggs, Apr 2, 2019
    #16
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.