Device Manager

  • 0 Odpowiedzi
  • 2170 Wyświetleń

0 użytkowników i 1 Gość przegląda ten wątek.

*

Offline marecki_0luk1

  • Administratorzy
  • ***** 430
  • 26
  • Nazwa i wersja ID: HC3 5.070.42 / HC2 4.600
Device Manager
« dnia: Maj 08, 2015, 10:55:22 pm »
Zapewne nie jestem jedynym użytkownikiem, który co chwilę dodaje/rekonfiguruje/usuwa urządzenia co wiąże się z nowymi Node ID.
Zrobiłem więc urządzenie wirtualne, które przechowuje mi ID, dla poszczególnych świateł i rolet w pomieszczeniach.

Za całość odpowiadają dwa przyciski. Pierwszy tworzy nam listę, która jest zapisywana w zmiennej. Załączam postać ogólną - możecie zmieniać jak chcecie ;)
-- Zmienne
lvs_DeviceList = "0:1:H";
--[[ Tutaj proponuję więcej zmiennych per pokój ]]--
lvs_DSeparator = ":";
lvs_LSeparator = ",";
lvs_DeviceActive = "true";

-- Rolety
lvs_roller_type = "R";
lva_roller_type = {"com.fibaro.FGRM222", "com.fibaro.FGRM221"};

-- Światła
lvs_light_type = "L";
lvs_light_attribute = "light";
lva_light_type = {"com.fibaro.binarySwitch"};


HC2 = Net.FHttp("127.0.0.1", 11111);
response ,status, errorCode = HC2:GET("/api/devices/");

if (tonumber(status) == 200) then
jsonTable = json.decode(response);
  for key,value in pairs(jsonTable) do   
    for r=1, #lva_roller_type do
      if jsonTable[key].type == lva_roller_type[r] and tostring(jsonTable[key].enabled) == lvs_DeviceActive then
      lvs_DeviceList = lvs_DeviceList .. lvs_LSeparator .. jsonTable[key].roomID  ..
        lvs_DSeparator .. jsonTable[key].id ..
        lvs_DSeparator .. lvs_roller_type;
        end;
      end;
      for l=1, #lva_light_type do
      if jsonTable[key].type == lva_light_type[l] and tostring(jsonTable[key].enabled) == lvs_DeviceActive  then
      for i=1, #jsonTable[key].interfaces do
          if jsonTable[key].interfaces[i] == lvs_light_attribute then
              lvs_DeviceList = lvs_DeviceList .. lvs_LSeparator ..  jsonTable[key].roomID  ..
        lvs_DSeparator .. jsonTable[key].id ..
        lvs_DSeparator .. lvs_light_type;
          end;
            end;
        end;
      end;
    end;
end;

fibaro:debug(lvs_DeviceList);
fibaro:setGlobal("gv_DeviceList", lvs_DeviceList);

Tutaj core skryptu, dzięki któremu mamy znowu wartości w postaci table i możemy zrobić loopa na włącz, wyłącz, otwórz, zamknij, cokolwiek.
function split(ips_array, ips_divider)
local lv_last_position,opa_array = 0,{};
 
  -- if divider is null then default
  if (ips_divider == '') then
  ips_divider = ',';
  end;
     
  for lv_a, lv_b in function() return string.find(ips_array,ips_divider,lv_last_position,true) end do
table.insert(opa_array,string.sub(ips_array,lv_last_position,lv_a-1))
    lv_last_position = lv_a + 1
  end;
   
  -- last insertion
table.insert(opa_array,string.sub(ips_array,lv_last_position))
  return opa_array;
end;


local lva_DeviceList;
local lva_RoomDeviceList;
local lvs_GlobalValue;
lvs_GlobalValue = tostring(fibaro:getGlobal("gv_DeviceList"));
fibaro:debug(lvs_GlobalValue);
lva_DeviceList = split(lvs_GlobalValue, ',');
for i=1, #lva_DeviceList do
  fibaro:debug(tostring(lva_DeviceList[i]));
  lva_RoomDeviceList = split(tostring(lva_DeviceList[i]), ":");
  for j=1, #lva_RoomDeviceList do
    if j == 1 then
    fibaro:debug(i .. ":" .. fibaro:getRoomName(tonumber(lva_RoomDeviceList[j])));     
    end;
  end;
end;