宿舍的上网环境是歌华有线,走的方式是很多年前在计算机知识科普书籍上看到的有线电视同轴电缆方式。 不知道是线路问题还是运营商赠送的 modem 问题,经常掉线,而且掉线后需要重启 modem 才能恢复。 这个过程需要手动操作,很麻烦。因为出问题的时候 modem 后台大部分时候也无法访问,所以通过 web 重启的选择也不可行。
所以我想到了用智能插座来控制 modem 的电源。这样就可以通过后台程序来自动重启 modem 了。但是 这里有个悖论问题是,modem 出问题的时候整个网络是不联通的,也就没办法通过互联网控制智能插座。 能不能通过局域网访问和控制智能插座呢?在网上一番搜寻找到了小米智能插座相关的一系列博客文章, 证明了这个方案的可行性。我主要参考了这篇小米智能插座监控设备耗电,并自动断电 感谢作者的分享,也简单记录一下我的实现过程。
我买的具体型号为小米智能插座3,没太研究明白到底有多少类似的款,应该大部分都是能满足要求的。 主要步骤如下:
在配置好网络添加进入米家APP后,可以在米家APP或者路由器后台获得智能插座的 IP 地址。
采用 Xiaomi-cloud-tokens-extractor 提供的方法获取,运行后可以获取局域网所有设备的 token 和型号等信息。 示例输出:
NAME: Mijia Smart Plug 3
ID: <id>
MAC: <mac>
IP: 192.168.1.1xx
TOKEN: <token>
MODEL: cuco.plug.v3
---------
NAME: Mi Control Hub
ID: <id>
MAC: <mac>
IP: 192.168.1.1xx
TOKEN: <token>
MODEL: lumi.gateway.v3
通过小米/米家产品库网站 https://home.miot-spec.com/ 获取设备的接口信息。
搜索 cuco.plug.v3 找到对应页面 https://home.miot-spec.com/spec/cuco.plug.v3
点击 查看属性和方法 可以看到接口信息,其中 Switch 是控制开关的接口,也是我们主要使用的。
# 获取当前状态
ret = s.raw_command('get_properties', [{'did':'Switch', 'siid':2, 'piid':1}])
print(ret)
# 设置状态:开机/关机
# value: True 关机,False 开机
ret = s.raw_command('set_properties', [{'did':'Switch', 'siid':2, 'piid':1,'value':False}])
print("set_switch_status: ",ret)
最终的监控脚本如下,可以配合 systemd 实现开机自启动。
注意:没有采用 ping 的方式监控 modem 的连通性,而是采用 requests 的方式监控 modem 的 web 服务是否正常。
import miio
import time
import requests
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
ip = '192.168.1.1xx'
token = ''
def check_net():
url = 'http://192.168.100.1' # 替换为要检测的网站URL
username = '' # 替换为网站的用户名
password = '' # 替换为网站的密码
session = requests.Session()
try:
response = session.get(url, auth=(username, password),timeout=1)
return(True)
except:
return(False)
def set_switch_status(value):
s = miio.device.Device(ip=ip, token=token)
ret = s.raw_command('set_properties', [{'did':'Switch', 'siid':2, 'piid':1,'value':value}])
logging.info("set_switch_status: " + str(ret))
logging.info('start monitoring network...')
err = 0
while True:
if not check_net():
err +=1
logging.error('err: ' + str(err))
else:
logging.debug('it works!')
err = 0
if err > 3:
logging.info('err: ' + str(err))
logging.critical('switching off...')
set_switch_status(False)
time.sleep(5)
logging.critical('switching on...')
set_switch_status(True)
time.sleep(120)
time.sleep(15)
Recently I have figured out how to handle link rot problem using Recoll. Of course you can use it for code searching, but we can do better.
Another method for searching code is using Github, if you don’t mind it’s online. Searching your own code is also easy with syntax user:<username>. However I want to be faster.
Years ago I tried Sourcegraph. I do not remember why I gave up. Maybe it’s because I was not familiar with Docker. Now I am using Docker everyday, actuaaly docker-compose, so I decided to give it another try. After finishing writing the previous sentence, I remembered that months ago sourcegraph cloud can host your own code, so I gave up self-hosting.
Now things is changing. Sourcegraph discontinued their cloud service. I have to host it myself, which is faster. Bad news is that you can only have 1 private repo now. https://news.ycombinator.com/item?id=34607118
We will be implementing changes to the Sourcegraph free tier on February 22, 2023 with the release of Sourcegraph 4.5. Details on the new free tier can be found here [1]. The changes are: The maximum number of private repositories that can be synced to each instance will be reduced from unlimited to 1. The free tier will no longer support SSO/SAML.
After more digging into hacker news, I found that you can still use sourcegraph to search your local code. https://news.ycombinator.com/item?id=36589033
In case someone is wondering how to search local repositories with sourcegraph, see https://docs.sourcegraph.com/admin/external_service/src_serv... and https://docs.sourcegraph.com/admin/deploy/docker-single-cont...
docker run --add-host=host.docker.internal:host-gateway --publish 7080:7080 --publish 127.0.0.1:3370:3370 --rm --volume ~/.sourcegraph/config:/etc/sourcegraph --volume ~/.sourcegraph/data:/var/opt/sourcegraph sourcegraph/server:5.1.2
docker run --rm=true --publish 3434:3434 --volume $PWD:/data/repos:ro sourcegraph/src-cli:latest serve-git /data/repos
So no more problems. Here is my docker-compose.yaml
version: "2.4" [5/3244]
services:
sourcegraph:
image: sourcegraph/server:5.2.0
restart: unless-stopped
container_name: sourcegraph
networks:
- traefik
ports:
- "7080:7080/tcp"
- "3370:3370/tcp"
environment:
- PUID=1000
- PGID=1000
volumes:
- /data/docker/sourcegraph/config:/etc/sourcegraph
- /data/docker/sourcegraph/data:/var/opt/sourcegraph
labels:
- "traefik.enable=true"
- "traefik.http.routers.code.entrypoints=http"
- "traefik.http.routers.code.rule=Host(`code.pm.lo`)"
- "traefik.http.services.code.loadbalancer.server.port=7080"
src-cli:
image: sourcegraph/src-cli:latest
restart: unless-stopped
container_name: src-cli
networks:
- traefik
ports:
- "3434:3434/tcp"
environment:
- PUID=1000
- PGID=1000
volumes:
- /data/Dropbox/Repos:/data/repos
command: serve-git /data/repos
labels:
- "traefik.enable=true"
- "traefik.http.routers.codesrc.entrypoints=http"
- "traefik.http.routers.codesrc.rule=Host(`codesrc.pm.lo`)"
- "traefik.http.services.codesrc.loadbalancer.server.port=3434"
networks:
traefik:
external: true
看完汉洋的伤痕椅子,
很多年前就知道信息不对称,看完文章居然回忆了好一会才又想到这个词。技术很重要,技术的扩散也很重要。
所以是不是别太执着当下的选择?毕竟有限的见识怎么可能让我们做出最完美的选择呢。
学习到一个新的佛教用语,悲智双运。
向 ChatGPT 请教,告知这是佛教中的一个重要概念,它包含了两个方面的修行:悲和智。
悲,或称为悲心,是指对众生的苦难和痛苦产生深切的同情心和关怀。它是一种无私的、无条件的爱和慈悲,不仅仅局限于个人的幸福,而是关心所有众生的福祉。通过培养悲心,佛教徒努力解除众生的痛苦,并帮助他们摆脱轮回的苦难。
智,或称为智慧,是指超越表面现象,深入理解事物真实本性的能力。它是通过反思和观察,超越迷惑和糊涂的认知,认识到事物的真实性质和宇宙的本质。智慧帮助人们认识到所有事物都是无常、无我和苦的,从而超越执着和執著,追求解脱和智慧的境界。
悲智双运强调慈悲和智慧的双重修行,认为这两方面的修行是相辅相成的。悲心促使佛教徒关心他人并帮助他们,而智慧帮助佛教徒认识到如何最有效地帮助众生。只有通过悲心和智慧的双重修行,佛教徒才能实现自己的解脱和成佛,并最大限度地利益他人。悲智双运是佛教修行的核心理念之一,也是追求智慧与慈悲的佛教徒的目标之一。
In English, the concept of “悲智双运” can be expressed as the “twin cultivation of compassion and wisdom” or the “practice of compassion and wisdom in tandem.”
Compassion refers to the deep sympathy and concern for the suffering of all beings, extending beyond personal happiness. It is an altruistic and unconditional love and compassion that seeks to alleviate the suffering of others and help them transcend the cycle of suffering.
Wisdom, on the other hand, involves going beyond surface appearances and gaining a profound understanding of the true nature of things. It is cultivated through reflection and observation, allowing one to see the impermanent, selfless, and unsatisfactory nature of all phenomena. Wisdom helps individuals transcend attachment and delusion, leading them towards liberation and the realization of the ultimate truth.
The twin cultivation of compassion and wisdom emphasizes the simultaneous development of these two aspects of spiritual practice. Compassion motivates individuals to care for others and provide assistance, while wisdom guides them in discerning the most effective ways to help sentient beings. By cultivating both compassion and wisdom, practitioners of Buddhism aim to attain personal liberation and enlightenment while benefiting others to the fullest extent. The twin cultivation of compassion and wisdom is a core principle in Buddhist practice and a goal for those seeking wisdom and compassion in Buddhism.