Jump to content
C4 Forums | Control4

DriverWorks - Is It Possible To Have Lua File Alive When Restart Or Reboot Controller(hc-250)?


Recommended Posts

In my sample app stored other device variable values and will update those values for every one hour in xml file as storage medium created through driverworks file i/o operation and retrieved using API-SendDataToUI to Flash side for Display and Calculation.

In which facing some defects :
When i restart or reboot (hc-250)controller the xml file getting delete.

Please any one suggest instead of using xml file as storage medium, shall i use MySql database in my local to overcome the above defects.
If it possible means please suggest whether i should connect database using lua in driverside itself or by using php because i have to update variable values for every 1 hour.

Please any one suggest the better way to resolve the defects which i am facing.

Link to comment
Share on other sites


The files created by using DriverWorks file operations (C4:FileWrite, etc.) are not deleted on reboot of the controller.

 

I think you may have some other issues in your driver code that are causing you to truncate the file if this is what you're seeing.

 

File operations are the best way for your driver to save larger amounts of data.

 

RyanE

Link to comment
Share on other sites

Following are my lua code in <script > section in which retrieving other device variable and saving it in driver using own variable.Later sending these variable in 

File write option to save those values in Data.xml file and read values for every 1 hour using timer.Finally sending those read data to FlashUI using API - C4:SendDataToUI 

 

Likewise only am accessing the exist/create file in Controller.So can you please take a look and suggest whether its a correct or i need to follow some other procedure to resolve the above defect. 

 

<script>
      <![CDATA[
   --Initialize variable
    READING = ""
         C4:AddVariable("READING", 0, "NUMBER") 
 
        --Retrieving light energy items 
         function RegisterVariable(idDevice,idVariable)
          --Register LIstener for light items
          C4:RegisterVariableListener(95,10000)
          C4:RegisterVariableListener(95,10001)
          C4:RegisterVariableListener(95,10002)
          C4:RegisterVariableListener(95,10003)
          C4:RegisterVariableListener(95,10004)
          C4:RegisterVariableListener(95,10005)
 
          --UnRegister LIstener for light items
          C4:UnregisterVariableListener(95,10000)
          C4:UnregisterVariableListener(95,10001)
          C4:UnregisterVariableListener(95,10002)
          C4:UnregisterVariableListener(95,10003)
          C4:UnregisterVariableListener(95,10004)
          C4:UnregisterVariableListener(95,10005)
         end
         RegisterVariable()     
        function OnWatchedVariableChanged(idDevice, idVariable, strValue)
              if(idVariable == 10000)then
                   onMins = strValue
                   print(onMins)
              elseif(idVariable == 10001)then
                   offMins = strValue    
                   print(offMins)
              elseif(idVariable == 10002)then
                   minsOnToday = strValue
                   print(minsOnToday)
              elseif(idVariable == 10003)then
                   currentPower = strValue
                   print(currentPower)
              elseif(idVariable == 10004)then
                   energyUsed = strValue
                   print(energyUsed)
              elseif(idVariable == 10005)then
                   energyUsedToday = strValue
                   print(energyUsedToday)
              end
        end
        --Storing the light energy information items in xml file
        function FileData()
             file = C4:FileOpen("Data.xml")
             if (file == -1) then
                  print("failed to open")
             else    
                  data = "["..tostring(os.date("%x,%X")).."]<minutesON>"..tostring(onMins).."</minutesON><minutesOFF>"..tostring(offMins).."</minutesOFF><minutesOnToday>"..tostring(minsOnToday).."</minutesOnToday><currentPower>"..tostring(currentPower).."</currentPower><energyUsed>"..tostring(energyUsed).."</energyUsed><energyUsedToday>"..tostring(energyUsedToday).."</energyUsedToday>"
                        datawritten = C4:FileWrite(file, string.len(data), data)
                        filesize = C4:FileGetSize(file)
                        valid = C4:FileIsValid(file)
                        C4:FileSetPos(file, 0)
                        filedata = C4:FileRead(file, 175000)
                        print(filedata)
                        C4:FileClose(file)
             end
        end
        FileData()
        --Setting Timer for Update
        function startDebugTimer()
          if (g_DebugTimer) then
              g_DebugTimer = C4:KillTimer(g_DebugTimer)
          end
              g_DebugTimer = C4:AddTimer(1, "HOURS")
        end
        startDebugTimer()
        function OnTimerExpired(idTimer)
          if (idTimer == g_DebugTimer) then
                --print("helloworld!")    
                RegisterVariable()     
                FileData()
                startDebugTimer()
          end
              C4:KillTimer(idTimer)
        end
        --Sending the light energy items to FlashUI
        function ExecuteCommand(strCommand, tParams)                
          if(strCommand == "TEST") then
             C4:SendDataToUI("<data>".. filedata .."</data>")
             C4:SetVariable("READING", 523190)
          end
        end
        ExecuteCommand()
 
      ]]>
    </script>
Link to comment
Share on other sites

After getting suggestion from your side I tried File API itself for creation of file and save data for it ,now those files are alive even after restart of Controller with data.

Thanks for your reply!.

Now i need some modification in driver code part.
Following are the method used to retrieve other device variable values:

--Retrieve the items
function RegisterVariable(idDevice,idVariable)
--Register LIstener for items
C4:RegisterVariableListener(95,10000)
C4:RegisterVariableListener(95,10001)
C4:RegisterVariableListener(95,10002)
C4:RegisterVariableListener(95,10003)
C4:RegisterVariableListener(95,10004)
C4:RegisterVariableListener(95,10005)

--UnRegister LIstener for items
C4:UnregisterVariableListener(95,10000)
C4:UnregisterVariableListener(95,10001)
C4:UnregisterVariableListener(95,10002)
C4:UnregisterVariableListener(95,10003)
C4:UnregisterVariableListener(95,10004)
C4:UnregisterVariableListener(95,10005)
end
RegisterVariable()
function OnWatchedVariableChanged(idDevice, idVariable, strValue)
if(idVariable == 10000)then
onMins = strValue
print(onMins)
elseif(idVariable == 10001)then
offMins = strValue
print(offMins)
elseif(idVariable == 10002)then
minsOnToday = strValue
print(minsOnToday)
elseif(idVariable == 10003)then
currentPower = strValue
print(currentPower)
elseif(idVariable == 10004)then
energyUsed = strValue
print(energyUsed)
elseif(idVariable == 10005)then
energyUsedToday = strValue
print(energyUsedToday)
end
end

Here deviceID and VariableID's are given manually by identifying from Programming section in Composer.
Is there any other method available to retrieve other device variable values dynamically because if my custom driver file used in other System/Controller means it must dynamically identify its Particular device(ex:light.c4i) for retrieving its variableID and its value also for saving it in file.

Using this C4:GetDevicesByC4iName("light.c4i") API i can able to identify only that light device model with ID in Project among number of devices which contains,but not able to retrieve its variableID dynamically.

So can you please suggest how could i able to identify those particular device variableID and its values dynamically instead of using C4:RegisterVariableListener API.

Link to comment
Share on other sites

Variable IDs are pretty consistent per device type, so anything that's using light.c4i should have the same variable IDs as any other device that's using light.c4i as a proxy.

 

There's no native DriverWorks way to determine Variables for a particular device.

 

RyanE

Link to comment
Share on other sites

Thanks for your Suggestion!

 

Using this C4:GetDevicesByC4iName("light.c4i") API i can able to identify only that light device model with ID in Project among number of devices which contains.

 

Getting Lua  output like this:

Devices ID's in Room:
575    
95      
579    
577    
 
Now among these ID's only one light device ID is connected whose id is 95 and remaining devices are simply added in my project but not connected.
Is there any method or API available to identify(dynamically) the only connected light device ID alone  with director (i mean which the one is plugged-in).
Because for that connected device ID alone I need to retrieve its Variable value using its VariableID to save it in File
Like this:
 
function RegisterVariable(idDevice,idVariable)                         
          idDevice = deviceid -- ID which is received using above API
          print("register")
          --Register LIstener for light items
          C4:RegisterVariableListener(idDevice,10000)
          C4:RegisterVariableListener(idDevice,10001)
         
          --UnRegister LIstener for light items
          C4:UnregisterVariableListener(idDevice,10000)
          C4:UnregisterVariableListener(idDevice,10001)         
end
RegisterVariable()     
function OnWatchedVariableChanged(idDevice, idVariable, strValue)
              if(idVariable == 10000)then
                   onMins = strValue
                   print(onMins)
              elseif(idVariable == 10001)then
                   offMins = strValue    
                   print(offMins)
              end              
end
 
 
So can you please suggest how could i able to identify the only connected deviceID alone among number of same device type in project.

 

Link to comment
Share on other sites

There's no way to determine if a device in a project is connected to a 'live' device, since that light proxy, for example, could be connected to a Control4 device, or a Lutron device, or some sleepy-node end device that never goes online unless you send something to it.

 

RyanE

Link to comment
Share on other sites

Thanks for your reply!

 

I tried using C4:listGetSelectedDevice () API to identify but its also not working.

 

So can you please suggest whether is it possible to connect same device(light) type for many times in a Room or only once 'live' in a Room.

Link to comment
Share on other sites

  • 2 weeks later...
Using DriverworksSDK created a XML file and using Timer the data written to the file for every one hour by C4:FileWrite() with date and time element.

To read the whole data from file used following API:

C4:FileSetPos(file, 0) and C4:FileRead(file, filesize) .

 

XML File contain data like this:

Receiving data from Driver 570--<data>[Device 95 : 07/01/14,10:00:18]<minutesON>1780</minutesON>

Receiving data from Driver 570--<data>[Device 95 : 07/01/14,11:00:18]<minutesON>1840</minutesON>

Receiving data from Driver 570--<data>[Device 582 : 07/01/14,11:00:18]<minutesON>0</minutesON>

Receiving data from Driver 570--<data>[Device 95 : 07/01/14,12:00:18]<minutesON>1870</minutesON>

Receiving data from Driver 570--<data>[Device 582 : 07/01/14,12:00:18]<minutesON>60</minutesON>

 

Now i need to read only the latest item(07/01/14,12:00:18)values and send to FlashUI for some calculations.

 

Following are my full code for IO Operation:

function FileData()

             file = C4:FileOpen("Data.xml")

             if (file == -1) then

                  print("failed to open")

             else    

                  data = "["..tostring(os.date("%x,%X")).."]<minutesON>"..tostring(onMins).."</minutesON><minutesOFF>"..tostring(offMins).."</minutesOFF><minutesOnToday>"..tostring(minsOnToday).."</minutesOnToday><currentPower>"..tostring(currentPower).."</currentPower><energyUsed>"..tostring(energyUsed).."</energyUsed><energyUsedToday>"..tostring(energyUsedToday).."</energyUsedToday>"

                        datawritten = C4:FileWrite(file, string.len(data), data)

                        filesize = C4:FileGetSize(file)

                        valid = C4:FileIsValid(file)

                        C4:FileSetPos(file, 0) -- Instead of having 0 i have to start from latest written position either by time&date or position?

                        filedata = C4:FileRead(file, filesize)

                        print(filedata)

                        C4:FileClose(file)

             end

        end

        FileData()

 


So can you please suggest whether using date and time or position in My_Xml File, could i able to retrieve latest written item alone.

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.