@@ -26,14 +26,10 @@ local dbg = require "dbg"
2626local nixio = require " nixio"
2727nixio .fs = require " nixio.fs"
2828local uci = require " luci.model.uci" .cursor ()
29- local luci = require " luci"
30- luci .json = require " luci.json"
31- local httpclient = require " luci.httpclient"
3229local ubus = require " ubus"
3330local ub = assert (ubus .connect (), " unable to connect to ubus" )
3431
3532local HW_CHECK_OVERRIDE = (arg [1 ] == " -f" )
36- local SKIP_SERVER_SYNC = (arg [1 ] == " -s" )
3733local O_RDWR_CREAT = nixio .open_flags (' rdwr' , ' creat' )
3834local MAX_TRIES = 5
3935
@@ -48,7 +44,6 @@ local MAX_ANALOG_SENSORS = tonumber(flukso.main.max_analog_sensors)
4844local ANALOG_ENABLE = (MAX_ANALOG_SENSORS == 3 ) and 1 or 0
4945local RESET_COUNTERS = (flukso .main .reset_counters == " 1" )
5046local LED_MODE = tonumber (flukso .main .led_mode or 256 )
51- local WAN_ENABLED = (flukso .daemon .enable_wan_branch == " 1" )
5247local LAN_ENABLED = (flukso .daemon .enable_lan_branch == " 1" )
5348
5449local function last_prov_sensor ()
@@ -82,18 +77,6 @@ local API_PATH = "/www/sensor/"
8277local CGI_SCRIPT = " /usr/bin/restful"
8378local AVAHI_PATH = " /etc/avahi/services/flukso.service"
8479
85- -- WAN settings
86- local WAN_BASE_URL = flukso .daemon .wan_base_url .. " sensor/"
87- local WAN_KEY = " 0123456789abcdef0123456789abcdef"
88- uci :foreach (" system" , " system" , function (x ) WAN_KEY = x .key end ) -- quirky but it works
89-
90- -- https header helpers
91- local FLUKSO_VERSION = " 000"
92- uci :foreach (" system" , " system" , function (x ) FLUKSO_VERSION = x .version end )
93-
94- local USER_AGENT = " Fluksometer v" .. FLUKSO_VERSION
95- local CACERT = flukso .daemon .cacert
96-
9780-- map exit codes to strings
9881local EXIT_STRING = {
9982 [- 1 ] = " no synchronisation" ,
@@ -104,7 +87,6 @@ local EXIT_STRING = {
10487 [4 ] = " sensor board hardware compatibility check failed" ,
10588 [5 ] = " analog sensor numbering error" ,
10689 [6 ] = " port numbering error" ,
107- [7 ] = " synchronisation with Flukso server failed"
10890}
10991
11092
@@ -413,99 +395,6 @@ local function create_avahi_config()
413395 end
414396end
415397
416- --- POST each sensor's parameters to the /sensor/xyz endpoint
417- -- @return none
418- local function phone_home ()
419- local function json_config (i ) -- type(i) --> "string"
420- local config = {}
421-
422- config [" class" ] = flukso [i ][" class" ]
423- config [" type" ] = flukso [i ][" type" ]
424- config [" function" ] = flukso [i ][" function" ]
425- config [" voltage" ] = tonumber (flukso [i ][" voltage" ])
426- config [" current" ] = tonumber (flukso [i ][" current" ])
427- config [" constant" ] = tonumber (flukso [i ][" constant" ])
428- config [" enable" ] = tonumber (flukso [i ][" enable" ])
429-
430- if config [" class" ] == " analog" then
431- local phase = tonumber (flukso .main .phase )
432-
433- if phase == 1 or
434- phase == 3 and i == " 1" then
435- config [" phase" ] = phase
436- end
437- end
438-
439- return luci .json .encode { config = config }
440- end
441-
442- local headers = {}
443- headers [" Content-Type" ] = " application/json"
444- headers [" X-Version" ] = " 1.0"
445- headers [" User-Agent" ] = USER_AGENT
446-
447- local options = {}
448- options .sndtimeo = 5
449- options .rcvtimeo = 5
450-
451- options .tls_context_set_verify = " peer"
452- options .cacert = CACERT
453- options .method = " POST"
454- options .headers = headers
455-
456- local http_persist = httpclient .create_persistent ()
457-
458- local err = false
459-
460- for i = 1 , LAST_PROV_SENSOR do
461- if flukso [tostring (i )] ~= nil and flukso [tostring (i )].id then
462- local sensor_id = flukso [tostring (i )].id
463-
464- if i ~= LAST_PROV_SENSOR then
465- options .headers [" Connection" ] = " keep-alive"
466- else
467- options .headers [" Connection" ] = " close"
468- end
469-
470- options .body = json_config (tostring (i ))
471- options .headers [" Content-Length" ] = tostring (# options .body )
472-
473- local hash = nixio .crypto .hmac (" sha1" , WAN_KEY )
474- hash :update (options .body )
475- options .headers [" X-Digest" ] = hash :final ()
476-
477- local url = WAN_BASE_URL .. sensor_id
478- local response , code , call_info = http_persist (url , options )
479-
480- local level
481-
482- if code == 200 or code == 204 then
483- level = " info"
484- else
485- level = " err"
486- err = true
487- end
488-
489- nixio .syslog (level , string.format (" %s %s: %s" , options .method , url , code ))
490-
491- -- if available, send additional error info to the syslog
492- if type (call_info ) == " string" then
493- nixio .syslog (" err" , call_info )
494- elseif type (call_info ) == " table" then
495- local auth_error = call_info .headers [" WWW-Authenticate" ]
496-
497- if auth_error then
498- nixio .syslog (" err" , string.format (" WWW-Authenticate: %s" , auth_error ))
499- end
500- end
501- end
502- end
503-
504- if err then
505- exit (7 )
506- end
507- end
508-
509398-- open the connection to the syslog deamon, specifying our identity
510399nixio .openlog (" fsync" , " pid" )
511400
542431-- notify other flukso daemons of a config change
543432ub :send (" flukso.sighup" , {})
544433
545- -- sync config with the server
546- if WAN_ENABLED and not SKIP_SERVER_SYNC then
547- phone_home ()
548- end
549-
550434print (arg [0 ] .. " completed successfully. Bye!" )
551435exit (0 )
0 commit comments