Moving device count

smsoousa 3 years ago

Hello everybody! I'm trying to count how many devices are in motion through motionstate, however, the code normally reads other tables, but when I try to count this one in particular, it always shows as zero. Could someone give me a light? Traccar v.5.5
Thanks


const deviceMotionCount = (motionstate) => Object.values(devices).filter((d) => d.motionstate === motionstate).length;
.
.
.
<Typography variant="h5" value="motion"><font color="#fff" cursor="default">{`${deviceMotionCount('1')}`}</font></Typography>
Anton Tananaev 3 years ago

There's no motionstate property on the device. You should check the motion state on the position object instead.

smsoousa 3 years ago

The motionstate table, doesn't it store the motion situation? Ex: 1 for movement and 0 for stopped?

Anton Tananaev 3 years ago

It is stored in the table - yes, but there's no such field in the API.

smsoousa 3 years ago

So, for me it works perfectly, I just want to count how many devices are in '1' status. To count how many are on the move. However, it isn't working. It always shows 0 even with multiple devices having value 1 in the table. I'm doing it through the frontend, without using the api. It shows me like a little button.

Anton Tananaev 3 years ago

How do you get the data without using the API?

smsoousa 3 years ago

Forgive my English. I meant that I'm making the call through the frontend using the API

Anton Tananaev 3 years ago

Again, this information is not sent via the API.

smsoousa 3 years ago

I modified it and it looks like this:

const item = data[index];
const position = useSelector((state) => state.session.positions[item.id]);

const deviceMotion = (attributes) => Object.values(position).filter((d) => d.attributes === attributes).length;

<Typography variant="h5" value="offline"><font color="#fff" cursor="default">{`${deviceMotion('true')}`}</font></Typography>

But I get error:

TypeError: Cannot read properties of undefined (reading 'undefined')
smsoousa 3 years ago

I did a test, calling the 'category' table, and in it I can do the count, as well as other tables, only the 'motionstate' table that I can't count. Look:


const deviceCategoryCount = (category) => Object.values(devices).filter((d) => d.category === category).length;

<Typography variant="h5" value="offline"><font color="#fff" cursor="default">{`${deviceCategoryCount('car')}`}</font></Typography>

Description text
Description text