-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi_sensor.py
32 lines (27 loc) · 1.24 KB
/
wifi_sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import asyncio
from typing import Any, ClassVar, Dict, Mapping, Optional
from viam.components.sensor import Sensor
from viam.proto.app.robot import ComponentConfig
from viam.proto.common import ResourceName
from viam.resource.base import ResourceBase
from viam.resource.types import Model, ModelFamily
class MySensor(Sensor):
# Subclass the Viam Sensor component and implement the required functions
MODEL: ClassVar[Model] = Model(ModelFamily("viam", "wifi_sensor"), "linux")
@classmethod
def new(cls, config: ComponentConfig, dependencies: Mapping[ResourceName, ResourceBase]):
sensor = cls(config.name)
return sensor
async def get_readings(self, extra: Optional[Dict[str, Any]] = None, **kwargs) -> Mapping[str, Any]:
with open("/proc/net/wireless") as wifi_stats:
content = wifi_stats.readlines()
quality = content[2]
quality = quality.split(" ")
clean_quality = [x for x in quality if x != '']
return {"wifi-link": clean_quality[2], "wifi-level": clean_quality[3], "wifi-noise": clean_quality[4]}
async def main():
wifi=MySensor(name="wifi")
signal = await wifi.get_readings()
print(signal)
if __name__ == '__main__':
asyncio.run(main())