@@ -10,14 +10,11 @@ import {gettext} from '$qui/base/i18n.js'
1010import Config from '$qui/config.js'
1111import * as Toast from '$qui/messages/toast.js'
1212import Debouncer from '$qui/utils/debouncer.js'
13- import { asap } from '$qui/utils/misc.js'
1413import * as ObjectUtils from '$qui/utils/object.js'
1514import * as PromiseUtils from '$qui/utils/promise.js'
16- import * as Window from '$qui/window.js'
1715
1816import * as AuthAPI from '$app/api/auth.js'
1917import * as APIConstants from '$app/api/constants.js'
20- import * as BaseAPI from '$app/api/base.js'
2118import * as DevicesAPI from '$app/api/devices.js'
2219import * as PortsAPI from '$app/api/ports.js'
2320import * as PrefsAPI from '$app/api/prefs.js'
@@ -27,20 +24,6 @@ import * as NotificationsAPI from '$app/api/notifications.js'
2724import { getGlobalProgressMessage } from '$app/common/common.js'
2825
2926
30- const DEVICE_POLL_INTERVAL = 5 /* Seconds */
31-
32- /* When actively polling a device, only update some selected attributes */
33- const DEVICE_POLLED_ATTRIBUTES = [
34- 'date' ,
35- 'uptime' ,
36- 'wifi_signal_strength' ,
37- 'temperature' ,
38- 'cpu_usage' ,
39- 'mem_usage' ,
40- 'storage_usage' ,
41- 'battery_level'
42- ]
43-
4427const STORAGE_KEY_PREFIX = 'cache'
4528const STORAGE_SET_DEBOUNCE_DELAY = 5000
4629const SAVE_PREFS_DEBOUNCE_DELAY = 1000
@@ -70,9 +53,6 @@ const savePrefsDebouncer = new Debouncer(() => {
7053/* Indicates that cache needs a reload asap */
7154let reloadNeeded = false
7255
73- /* The name of a device to be continuously polled */
74- let polledDeviceName = null
75-
7656/* Debouncers for cache setters */
7757const slaveDevicesSetLocalStorageCacheDebouncer = new Debouncer ( ( slaveDevices ) => {
7858 logger . debug ( 'saving slave devices to cache' )
@@ -546,18 +526,6 @@ export function updateFromEvent(event) {
546526 break
547527 }
548528
549- case 'slave-device-polling-update' : {
550- if ( event . params . name in slaveDevices ) {
551- Object . assign ( slaveDevices [ event . params . name ] . attrs , event . params . attrs )
552- slaveDevicesSetLocalStorageCacheDebouncer . call ( slaveDevices )
553- }
554- else {
555- logger . warn ( `received slave-device-polling-update event for unknown device "${ event . params . name } "` )
556- }
557-
558- break
559- }
560-
561529 case 'slave-device-add' : {
562530 if ( event . params . name in slaveDevices ) {
563531 logger . debug ( `received slave-device-add event for already existing device "${ event . params . name } "` )
@@ -579,10 +547,6 @@ export function updateFromEvent(event) {
579547 logger . warn ( `received slave-device-remove event for unknown device "${ event . params . name } "` )
580548 }
581549
582- if ( polledDeviceName === event . params . name ) {
583- polledDeviceName = null
584- }
585-
586550 break
587551 }
588552
@@ -649,14 +613,6 @@ export function updateFromEvent(event) {
649613
650614 break
651615 }
652-
653- case 'device-polling-update' : {
654- Object . assign ( mainDevice , event . params )
655- mainDeviceSetLocalStorageCacheDebouncer . call ( mainDevice )
656-
657- break
658- }
659-
660616 }
661617}
662618
@@ -900,96 +856,6 @@ export function reload(now = false) {
900856 }
901857}
902858
903- /**
904- * Return the name of the polled device.
905- * @alias qtoggle.cache.getPolledDeviceName
906- * @returns {?String }
907- */
908- export function getPolledDeviceName ( ) {
909- return polledDeviceName
910- }
911-
912- /**
913- * Set the name of the polled device. Passing `null` disables polling.
914- * @alias qtoggle.cache.setPolledDeviceName
915- * @param {?String } [deviceName]
916- */
917- export function setPolledDeviceName ( deviceName ) {
918- if ( deviceName == null ) {
919- logger . debug ( 'disabling device polling' )
920- }
921- else {
922- logger . debug ( `setting polled device name to "${ deviceName } "` )
923- }
924-
925- polledDeviceName = deviceName
926- }
927-
928- function pollDevice ( ) {
929- /* Choose between polling main device or a slave device */
930- let device = null
931- if ( polledDeviceName ) {
932- logger . debug ( `polling device "${ polledDeviceName } "` )
933- device = slaveDevices [ polledDeviceName ]
934- if ( ! device ) {
935- logger . debug ( 'skipping polling for unknown device' )
936- return
937- }
938-
939- if ( ! device . enabled ) {
940- logger . debug ( 'skipping polling for disabled device' )
941- return
942- }
943-
944- BaseAPI . setSlaveName ( polledDeviceName )
945- }
946- else {
947- logger . debug ( 'polling main device' )
948- }
949-
950- DevicesAPI . getDevice ( ) . then ( function ( attrs ) {
951-
952- asap ( function ( ) {
953- if ( device && polledDeviceName === device . name ) {
954- if ( ! ObjectUtils . deepEquals ( device . attrs , attrs ) ) {
955- let partialDevice = { name : device . name , attrs : { } }
956- DEVICE_POLLED_ATTRIBUTES . forEach ( function ( name ) {
957- if ( name in attrs ) {
958- partialDevice . attrs [ name ] = attrs [ name ]
959- }
960- } )
961- NotificationsAPI . fakeServerEvent ( 'slave-device-polling-update' , partialDevice )
962- }
963- }
964- else if ( polledDeviceName === '' ) {
965- if ( ! ObjectUtils . deepEquals ( mainDevice , attrs ) ) {
966- let partialAttrs = { }
967- DEVICE_POLLED_ATTRIBUTES . forEach ( function ( name ) {
968- if ( name in attrs ) {
969- partialAttrs [ name ] = attrs [ name ]
970- }
971- } )
972- NotificationsAPI . fakeServerEvent ( 'device-polling-update' , partialAttrs )
973- }
974- }
975- } )
976-
977- } ) . catch ( function ( e ) {
978-
979- if ( polledDeviceName == null ) {
980- logger . debug ( 'ignoring polling error after polling disabled' )
981- return
982- }
983- if ( ( e instanceof BaseAPI . APIError ) && ( e . code === 'no such device' ) ) {
984- logger . debug ( 'ignoring error while polling removed device' )
985- return
986- }
987-
988- logger . errorStack ( 'polling failed' , e )
989-
990- } )
991- }
992-
993859/**
994860 * Initialize the cache subsystem.
995861 * @alias qtoggle.cache.init
@@ -1006,26 +872,4 @@ export function init() {
1006872 }
1007873
1008874 } )
1009-
1010- /* Start a polling timer */
1011- setInterval ( function ( ) {
1012-
1013- /* Don't poll unless cache is ready */
1014- if ( ! whenCacheReady . isFulfilled ( ) ) {
1015- return
1016- }
1017-
1018- /* Don't poll unless window currently active */
1019- if ( ! Window . isActive ( ) ) {
1020- return
1021- }
1022-
1023- /* Don't poll if polling device disabled */
1024- if ( polledDeviceName == null ) {
1025- return
1026- }
1027-
1028- pollDevice ( )
1029-
1030- } , DEVICE_POLL_INTERVAL * 1000 )
1031875}
0 commit comments