0%

配置时钟

启用i2c

sudo raspi-config

在interfacing options 中选择启用i2c

运行

sudo i2cdetect -l

查看所有的i2c总线

这里回显

i2c-1   i2c             bcm2835 I2C adapter                     I2C adapter

查看i2c上连接的设备

sudo i2cdetect -y 1

回显

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- 36 -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

这里的0x36为MAX17040G的地址; 0x68为DS1307Z+的地址

加载 i2c 模块

sudo modprobe i2c-dev

切换到超级管理 su

在i2cx 新增一个设备, 名字为ds1307, 设备地址为 0x68

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

查看我们的刚刚添加的硬件时钟的当前时间

hwclock -r

回显

2000-01-01 08:01:24.321813+0800

很显然这是刚添加的时钟, 还未同步正确的时间

将系统时间写入到硬件时钟

hwclock -w

这时我们再次读取硬件时钟的话

回显

2019-11-09 17:26:30.864206+0800

已经同步到系统时钟

同步硬件时钟到系统时钟

当我们的树莓派断电后, 再次启动后, 如果没有网络的话, 她是不能正确同步系统时间的, 但我们的 UPS 上有一颗时钟芯片, 并且一直有纽扣电池供电, 并不会丢失我们上面设置好的日期时间, 所以我们需要在树莓派启动时将硬件时钟的时间写入到树莓派的系统时间.

打开树莓派的启动文件rc.local

nano /etc/rc.local

在exit 0 前添加

modprobe i2c-dev
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
hwclock -r
hwclock -s

重启在无网络的情况下可以测试系统时钟是否被正确同步

读取电量

这里我们先简单跑一下板方提供的脚本


#!/usr/bin/env python
import struct
import smbus
import sys
import time


def readVoltage(bus):

"This function returns as float the voltage from the Raspi UPS Hat via the provided SMBus object"
address = 0x36
read = bus.read_word_data(address, 2)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
voltage = swapped * 1.25 /1000/16
return voltage


def readCapacity(bus):
"This function returns as a float the remaining capacity of the battery connected to the Raspi UPS Hat via the provided SMBus object"
address = 0x36
read = bus.read_word_data(address, 4)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
capacity = swapped/256
return capacity


bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)

while True:

print "++++++++++++++++++++"
print "Voltage:%5.2fV" % readVoltage(bus)

print "Battery:%5i%%" % readCapacity(bus)

if readCapacity(bus) == 100:

print "Battery FULL"

if readCapacity(bus) < 20:

print "Battery LOW"
print "++++++++++++++++++++"
time.sleep(2)

回显

++++++++++++++++++++
Voltage: 3.64V
Battery: 8%
Battery LOW
++++++++++++++++++++

参考文章

问题描述

富文本编辑器会提供插入图片或视频的功能,该功能一般情况时执行插入操作时上传相关文件,由服务器返回静态文件的url地址,前端通过插入img或是video标签,并将返回的url赋予src属性,这样就可以在前端看到我们插入的文件了,但是用户可能会插入新的文件或是删除已插入的文件,但是在前端我们只是删除img或是video标签,服务器上该资源的文件还是存在的,并没有被清除,事实上此时用户没有提交,我们也不能直接进行删除,毕竟用户还可能执行撤销操作,如果直接删除了服务器资源,就会导致文件资源404,找不到该资源,又要重新上传,用户体验太差。

解决思路

前端提供需要清除文件的地址数组

用户打开富文本编辑器,这可能是新建的或是对已有的内容进行编辑,我们可以初始化一个数组let uploads = [],如果是编辑操作,我们先通过querySelectorAll('[src]')得到文件相应的 dom 数组,后面进行遍历,将每一项的src值push到uploads数组里,这样我们就得到了以前上传并使用中的文件列表;在后续的上传操作中,每次上传我们都会将文件地址push到uploads数组里;当用户进行提交操作时,我们可以遍历出内容中所有的file并push到 let files = []数组中。

此时我们得到了两个数组,一个是 uploads 上传文件的数组,另一个是 files 内容中有效的文件数组,那取其差集,就是我们要清除的无用文件的数组 useless了,将该数组传到服务器,就可以清除相关的无用文件了。

这里有个小问题,如何取两个数组差集的问题,可以这样写:

let uploads = ['a', 'b', 'c', 'd', 'e', 'f']
let files = ['b', 'e']
let useless = []
for (let i=0; i<files.length; i++) {
let index = uploads.indexOf(files[i])
uploads.splice(index,1)
}
useless = uploads
console.log(`useless: ${useless}`)

这种实现由什么缺陷呢:整体上看,这种实现确实了我们的预期功能,并且后台也很简单,只需提供一个可以接收删除文件数组的接口,并执行删除就可以了;现实其实后台并没有这么简单,删除操作一般是危险的,需要我们警惕的,如果该实现上传到服务器的文件并没有进行数据库记录,只是放置在 static/uploads/ 的目录下,那就没有办法设置谁由权限删除某一个文件,也就是说只要提供删除数组,所有的可以操作编辑器的用户都可以进行删除操作,显然这是有安全问题的,所以对上传的文件一定要做数据库记录,并设置所有者,执行删除操作时也需要判断操作是否来自所有者。

既然上述存在的问题必须通过添加数据库记录来解决,那我们可以将上传的文件绑定到指定的内容id;对于新创建的内容,新上传的文件记录的内容id为null,当我们提交编辑器内容时,我们将files各记录的内容id设置为实际值;如果是编辑已有的内容,我们此时已有该内容的id,将该内容id下的所有文件记录的内容id设置为null,我们将files里的指定为该内容id,之后我们可以定期清理内容id为null的文件,即可清除无用的上传文件,这有点类似于js中内存垃圾清理实现方式之一的引用计数。

id article_id file_url
0 null http://domain.com/pic.jpg'
1 1 http://domain.com/pic.jpg'
2 1 http://domain.com/pic.jpg'
3 1 http://domain.com/pic.jpg'
4 1 http://domain.com/pic.jpg'
5 1 http://domain.com/pic.jpg'

安装

sudo apt-get install mongodb

错误处理

用以上命令安装完成后,执行 mongo 命令,并没有连接成功

MongoDB shell version: 2.4.14
connecting to: test
Wed Jul 17 22:40:55.765 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
exception: connect failed

查看安装打印的信息中,有这么一条 无法创建主目录"/var/lib/mongodb", 说明 mongodb 缺少权限

sudo service mongodb stop
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo service mongodb start

修改目录权限后再次执行 mongo 命令,显示连接成功

MongoDB shell version: 2.4.14
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
Wed Jul 17 22:50:05.612 [initandlisten]
Wed Jul 17 22:50:05.612 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Wed Jul 17 22:50:05.612 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Wed Jul 17 22:50:05.612 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Wed Jul 17 22:50:05.612 [initandlisten]
>