Skip to content

Commit

Permalink
ref_state: implements commons package
Browse files Browse the repository at this point in the history
  • Loading branch information
javierajorge committed May 12, 2024
1 parent e686037 commit f9054bc
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 52 deletions.

This file was deleted.

This file was deleted.

92 changes: 92 additions & 0 deletions packages/shared-state-bat_links_info/tests/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"LiMe-430b0c": [
{
"dst_mac": "02:cc:4e:1f:73:aa",
"last_seen_msecs": 1560,
"tq": 255,
"src_mac": "02:cc:4e:43:0b:0c",
"iface": "wlan2-mesh_29"
},
{
"dst_mac": "02:ab:46:1f:73:aa",
"last_seen_msecs": 1000,
"tq": 255,
"src_mac": "02:ab:46:43:0b:0c",
"iface": "wlan1-mesh_29"
},
{
"dst_mac": "02:ab:46:dd:69:1c",
"last_seen_msecs": 1670,
"tq": 251,
"src_mac": "02:ab:46:43:0b:0c",
"iface": "wlan1-mesh_29"
}
],
"cheche": [
{
"dst_mac": "02:ab:46:dd:69:1c",
"last_seen_msecs": 20,
"tq": 255,
"src_mac": "02:ab:46:1f:73:aa",
"iface": "wlan1-mesh_29"
},
{
"dst_mac": "02:ab:46:43:0b:0c",
"last_seen_msecs": 1300,
"tq": 251,
"src_mac": "02:ab:46:1f:73:aa",
"iface": "wlan1-mesh_29"
},
{
"dst_mac": "02:cc:4e:43:0b:0c",
"last_seen_msecs": 900,
"tq": 242,
"src_mac": "02:cc:4e:1f:73:aa",
"iface": "wlan2-mesh_29"
},
{
"dst_mac": "02:58:47:dd:69:1c",
"last_seen_msecs": 1460,
"tq": 255,
"src_mac": "02:58:47:1f:73:aa",
"iface": "wlan0-mesh_29"
},
{
"dst_mac": "02:58:47:1f:73:f6",
"last_seen_msecs": 520,
"tq": 255,
"src_mac": "02:58:47:1f:73:aa",
"iface": "wlan0-mesh_29"
}
],
"konB-11s": [
{
"dst_mac": "02:58:47:1f:73:aa",
"last_seen_msecs": 1020,
"tq": 255,
"src_mac": "02:58:47:dd:69:1c",
"iface": "wlan0-mesh_29"
},
{
"dst_mac": "02:58:47:1f:73:f6",
"last_seen_msecs": 1010,
"tq": 255,
"src_mac": "02:58:47:dd:69:1c",
"iface": "wlan0-mesh_29"
},
{
"dst_mac": "02:ab:46:1f:73:aa",
"last_seen_msecs": 540,
"tq": 235,
"src_mac": "02:ab:46:dd:69:1c",
"iface": "wlan1-mesh_29"
},
{
"dst_mac": "02:ab:46:43:0b:0c",
"last_seen_msecs": 1140,
"tq": 255,
"src_mac": "02:ab:46:dd:69:1c",
"iface": "wlan1-mesh_29"
}
]
}
24 changes: 24 additions & 0 deletions packages/shared-state-ref_sate_commons/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# Copyright (C) 2023 Javier Jorge <[email protected]>
# Copyright (c) 2023 Instituto Nacional de Tecnología Industrial
# Copyright (C) 2023 Asociación Civil Altermundi <[email protected]>
# This is free software, licensed under the GNU Affero General Public License v3.
#

include ../../libremesh.mk

define Package/$(PKG_NAME)
SECTION:=lime
CATEGORY:=LibreMesh
TITLE:=Common components for refference satate datypes
MAINTAINER:= Javier <[email protected]>
DEPENDS:=+lua +luci-lib-jsonc +ubus-lime-utils \
+lime-system shared-state-async
PKGARCH:=all
endef

define Package/$(PKG_NAME)/description
Common components for refference satate datypes
endef

$(eval $(call BuildPackage,$(PKG_NAME)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/lua

--! LibreMesh
--! Copyright (C) 2024 Javier Jorge
--! Copyright (C) 2024 Asociación Civil Altermundi <[email protected]>
--! SPDX-License-Identifier: AGPL-3.0-only

local JSON = require("luci.jsonc")
local utils = require "lime.utils"

local ref_file_folder = "/etc/shared-state/ref_state/"

local input = JSON.parse(io.stdin:read("*all"))

local data_type = arg[0]:match(".*/([^/]+)/[^/]+$")

local function write_if_diff(data_type, input)
path = ref_file_folder .. data_type
local acutal = JSON.parse(utils.read_file(path))
if input[utils.hostname()] then
if not(utils.deepcompare(input[utils.hostname()],acutal or {})) then
utils.write_file(path,JSON.stringify(input[utils.hostname()]))
utils.unsafe_shell("logger -t shared-sate-ref-state "..data_type.." state changed")
return true
end
end
utils.unsafe_shell("logger -t shared-sate-ref-state "..data_type.." state did not change")
return false
end
write_if_diff(data_type,input)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
local utils = require 'lime.utils'
local test_utils = require 'tests.utils'
local JSON = require("luci.jsonc")
local uci = nil

--package.path = package.path .. ";packages/shared-state-bat_links_info/files/usr/share/shared-state/hooks/bat_links_info_ref/?;;"
--require ("shared-state-update_refstate")



describe('bat links ref state tests', function()

it('test obj store', function()
local function write_if_diff(data_type, input)
path = ref_file_folder .. data_type
local acutal = JSON.parse(utils.read_file(path))
if input[utils.hostname()] then
if not(utils.deepcompare(input[utils.hostname()],acutal or {})) then
utils.write_file(path,JSON.stringify(input[utils.hostname()]))
utils.unsafe_shell("logger -t shared-sate-ref-state "..data_type.." state changed")
return true
end
end
utils.unsafe_shell("logger -t shared-sate-ref-state "..data_type.." state did not change")
return false
end

local test_dir = test_utils.setup_test_dir()
local testfile = test_dir .. 'mydata'
assert.is.same({}, utils.read_obj_store(testfile))

local input=JSON.parse(utils.read_file("packages/shared-state-bat_links_info/tests/sample.json"))
assert.is_true (write_if_diff(testfile,input))
assert.is_false (write_if_diff(testfile,input))

end)

before_each('', function()
uci = test_utils.setup_test_uci()
stub(utils, "hostname", function () return "cheche" end)

end)

after_each('', function()
test_utils.teardown_test_uci(uci)
test_utils.teardown_test_dir()
end)

end)

0 comments on commit f9054bc

Please sign in to comment.