example netrexx code

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

example netrexx code

kenner

This pgm will find the temperature posted by a DS18B20 on a BBB. Of course it would need some scheduling and could be set up to read multiple sensors.

 

import java.text.SimpleDateFormat

trace methods

class ReadTemp

            properties static public

                        Debug_Level = 20

method main(arg=String[]) static public

    if Debug_Level > 20 then trace var P PinNum

    if Debug_Level > 10 then trace all

    if arg.length = 0 then do

        say '# Please specify directory of device output.'

        exit 1

    end

    argIn = arg[0]

    loop letterNum = 1 by 1 to arg.length - 1

        argIn = argIn || arg[letterNum]

    end

    say "arg = argIn ==>" argIn

-- we want:

    parse argIn SlaveDir

    say "parsed this:" SlaveDir

            ReadTemp(SlaveDir)

            return

-- this must be our constructor - one object for each GPIO

method ReadTemp(DirOfSlave) signals IOException, FileNotFoundException;

-- read the temperature: find the w1_slave

    if Debug_Level > 10 then trace all

                        spec = DirOfSlave

                        opts = 'BLS'

                        stem = ""

                        stem[0] = 0

                        tattrib = '----------'

                        nattrib = '----------'

                        stem = SysFileTree(spec, stem, opts, tattrib = '-----', nattrib = '------')

                        say "stem[0]" stem[0] --.tostring

            loop label dispstem myindex = 1 to stem[0]

                        Say "Contents of stem returned from SysFileTree:" stem[myindex]

                        parse stem[myindex] aDate aTime aNumber someAttrib anFName

                        say "Found the file:" anFName "\t\t" aDate "\t" aTime "\t" aNumber "\t" someAttrib

                        say "Temp in C and F:" TempDisplay(SysFileRead(anFName))

                        catch e4=FileNotFoundException

                                    say 'Exception caught:' e4 '\n  ' e4.getMessage()

                        catch e3=IOException

                                    say 'Exception caught:' e3 '\n  ' e3.getMessage()

    end dispstem

return

method SysFileRead(file) signals IOException,FileNotFoundException

    if Debug_Level > 10 then trace all

    f = File(file)

    in = BufferedReader(InputStreamReader(FileInputStream(f)))

    i = 0

    aStem = ''

    Loop forever

       line = in.readLine

       if line = null then leave

       i = i + 1

       aStem[i] = line

    End

    aStem[0] = i

    in.close

    return aStem

-- return temperature in F and C

-- @param stem  stem with contents of file 

method TempDisplay(stem)

            if Debug_Level > 10 then trace results

    loop i = 1 to stem[0]

       say stem[i]

       if stem[i].pos('YES') > 0 then iterate

               if stem[i].pos('t=') > 0 then do

                           temp_string = int stem[i].substr(stem[i].pos('t=')+2,5)

                           temp_c = temp_string / 1000.0

                           temp_f = temp_c * 9.0 / 5.0 + 32.0

               end

    end

    return temp_c temp_f

   

method SysFileTree(spec, stem, options = 'BLS', tattrib = '----------', nattrib = '----------')

            if Debug_Level > 10 then trace results

            if Debug_Level > 20 then trace var d f spec1 stem options1 tattrib1 nattrib

    stem = ''

    stem[0] = 0

    do

       d = spec.substr(1, spec.lastpos('/'))

       f = spec.substr(spec.lastpos('/') + 1)

       regex = f

       escapechar = char[] "\\+()^$.{}[]|"

       Loop i = 0 to escapechar.length - 1

          pos = regex.pos(escapechar[i], 1)

          Loop while pos > 0

     regex = regex.insert('\\', pos - 1)

     pos = regex.pos(escapechar[i], pos + 2)

          End

       End

       pos = regex.pos('--', 1)

       Loop while pos > 0

          regex = regex.insert('.', pos - 1)

          pos = regex.pos('--', pos + 2)

       End

       regex = regex.translate('.', '?')

 

       Recursive = 0

       if options.upper.pos('S') > 0 then do

          Recursive = 1

       end

 

       if options.upper.pos('I') > 0 then do

          regex = regex.insert('(?i)')

       end

 

       FileAndOrDir = 'B'

       if options.upper.pos('F') > 0 then do

          FileAndOrDir = 'F'

       end

       if options.upper.pos('D') > 0 then do

          FileAndOrDir = 'D'

       end

 

       if options.upper.pos('T') > 0 then do

          format = SimpleDateFormat("yy/MM/dd/HH/mm")

       end

       else do

          if options.upper.pos('L') > 0 then do

     format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

          end

          else do

     format = SimpleDateFormat()

          end

       end

 

       FilenameOnly = 0

       if options.upper.pos('O') > 0 then do

          FilenameOnly = 1

       end

 

       stem = FileTree(stem, d, regex, Recursive, FileAndOrDir, format, FilenameOnly, tattrib)

    catch IOException

       stem = ''

       stem[0] = 0

    end

    return stem

-- Private recursively searches a directory for files

-- @param stem          stem to fill

-- @param dirname       directory to start from

-- @param regex         regular expression of file to loop for

-- @param Recursive     descend recursively into directories

-- @param FileAndOrDir  FDB, File Directory or Both

-- @param format        SimpleDateFormat

-- @param FileNameOnly  format of returned stem, filename only or datetime size .HDR. filename

-- @param tattrib       target attributes mask to match .DHR. (-- any state, + set, - not set, . not applicable)

-- @return         stem

method FileTree(stem, dirname, regex, Recursive, FileAndOrDir, format = SimpleDateFormat, FilenameOnly, tattrib) private signals IOException

    If Debug_Level > 40 then trace results

    If Debug_Level > 40 then trace var stem dirname regex Recursive FileAndOrDir format FilenameOnly tattrib f addToStem

    dir = File(dirname)

 

    files = File[] dir.listFiles()

    if files <> null then do

       Loop label loopthrufiles i = 0 to files.length - 1

          f = Rexx files[i].toString()

--          say "f" f

          f = f.substr(f.lastpos('/') + 1)

--          say "f" f

          isDirectory = files[i].isDirectory()

          say "In FileTree, file in list: " f.toString() "\t\t regex" regex

--          if f.toString().matches(regex)  then do

          if f.pos(regex) > 0 then do                      

     addToStem = 0

     if FileAndOrDir == 'B' then do

        addToStem = 1

     end

     if FileAndOrDir == 'F' then do

        if \ isDirectory then do

           addToStem = 1

        end

     end

     if FileAndOrDir == 'D' then do

        if isDirectory then do

           addToStem = 1

        end

     end

     if addToStem then do

        if FileNameOnly then do

           stem[0] = stem[0] + 1

           stem[stem[0]] = files[i].toString()

           say "stem[0] stem[stem[0]]" stem[0] stem[stem[0]]

        end

        else do

           epoch = files[i].lastModified()

           cal = Calendar.getInstance()

           cal.setTimeInMillis(epoch)

 

           filedate = Rexx format.format(cal.gettime())

           if isDirectory then do

      filesize = '0'

           end

           else do

      filesize = Rexx files[i].length()

           end

 

           attrib = '.'

           if isDirectory then do

      attrib = attrib'D'

           end

           else do

      attrib = attrib'-'

           end

           if files[i].isHidden() then do

      attrib = attrib'H'

           end

           else do

      attrib = attrib'-'

           end

           if files[i].canWrite() then do

      attrib = attrib'-'

           end

           else do

      attrib = attrib'R'

           end

           attrib = attrib'.'

           if tattrib <> '----------' then do

      if tattrib.substr(2, 1) == '+' & attrib.substr(2, 1) <> 'D' then do

         addToStem = 0

      end

      else do

         if tattrib.substr(2, 1) == '-' & attrib.substr(2, 1) <> '-' then do

    addToStem = 0

         end

      end

      if tattrib.substr(3, 1) == '+' & attrib.substr(3, 1) <> 'H' then do

         addToStem = 0

      end

      else do

         if tattrib.substr(3, 1) == '-' & attrib.substr(3, 1) <> '-' then do

    addToStem = 0

         end

      end

      if tattrib.substr(4, 1) == '+' & attrib.substr(4, 1) <> 'R' then do

         addToStem = 0

      end

      else do

         if tattrib.substr(4, 1) == '-' & attrib.substr(4, 1) <> '-' then do

    addToStem = 0

         end

      end

           end

           if addToStem then do

      stem[0] = stem[0] + 1

      stem[stem[0]] = filedate.left(24)' 'filesize.right(14)' 'attrib' 'files[i].toString()

           end

        end

     end

          end

          if isDirectory & Recursive then do

     if files[i].getAbsolutePath == files[i].getCanonicalPath then do    -- don't descend into symlinks

        stem = FileTree(stem, Rexx files[i], regex, Recursive, FileAndOrDir, format, FilenameOnly, tattrib)

     end

          end

       End loopthrufiles

    end

    return stem


_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/